Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New datastream protocol should be strictly compatible to legacy one #15

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Io/DatastreamProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Clue\QDataStream\Reader;
use Clue\QDataStream\QVariant;
use Clue\QDataStream\Types;
use InvalidArgumentException;

class DatastreamProtocol extends Protocol
{
Expand All @@ -16,6 +17,10 @@ public function isLegacy()

public function writeVariantList(array $list)
{
if (isset($list[0]) && !is_integer($list[0])) {
throw new InvalidArgumentException('List MUST start with an integer value in order to distinguish from map encoding');
}

$writer = new Writer(null, $this->types, $this->userTypeWriter);

// datastream protocol just uses list contents
Expand Down Expand Up @@ -52,8 +57,10 @@ public function readVariant($packet)
}

if ($value[0] === self::REQUEST_INITDATA) {
// make sure InitData is in line with legacy protocol wire format
// first 3 elements are unchanged, everything else should be a map
return array_slice($value, 0, 3) + $this->listToMap(array_slice($value, 3));
// https://github.com/quassel/quassel/blob/master/src/common/protocols/datastream/datastreampeer.cpp#L383
return array_slice($value, 0, 3) + array(3 => $this->listToMap(array_slice($value, 3)));
}

return $value;
Expand Down
19 changes: 19 additions & 0 deletions tests/Io/DatastreamProtocolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,23 @@ public function testIsNotLegacy()
{
$this->assertFalse($this->protocol->isLegacy());
}

/**
* @expectedException InvalidArgumentException
*/
public function testCanNotTransportListStartingWithString()
{
$this->protocol->writeVariantList(array('does', 'not', 'work'));
}

public function testInitDataWireFormatWillBeRepresentedLikeLegacyProtocol()
{
// the message as it is sent over the wire
$message = array(Protocol::REQUEST_INITDATA, 'Network', '1', 'k1', 'v1', 'k2', 'v2');

// the actual message interpretation (in line with legacy protocol wire format)
$expected = array(Protocol::REQUEST_INITDATA, 'Network', '1', array('k1' => 'v1', 'k2' => 'v2'));

$this->assertEquals($expected, $this->protocol->readVariant($this->protocol->writeVariantList($message)));
}
}