Skip to content

Commit

Permalink
#54 Fix null terminated string decoding (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeevdv authored Aug 4, 2021
1 parent 93621c3 commit fabd439
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function encoder(int $protocolVersion): callable
function decode(string $binaryString): Either
{
$packedFormat = \sprintf(
'C%s/N%s/A*%s',
'C%s/N%s/a*%s',
PROTOCOL_ACCESSOR_VERSION,
PROTOCOL_ACCESSOR_SCHEMA_ID,
PROTOCOL_ACCESSOR_AVRO
Expand Down
20 changes: 20 additions & 0 deletions test/ProtocolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

class ProtocolTest extends AbstractFunctionalTestCase
{
private const NULL_TERMINATED_HEX = '000000270f303000000000';
private const NULL_TERMINATED_AVRO_HEX = '303000000000';

/**
* @test
*/
Expand Down Expand Up @@ -149,4 +152,21 @@ public function validate_returns_nothing_for_invalid_unpacked(): void

$this->assertInstanceOf(Nothing::class, validate(WIRE_FORMAT_PROTOCOL_VERSION, $decoded));
}

/**
* @test
*/
public function null_terminated_values_unpacked_correctly(): void
{
$decoded = decode(\hex2bin(self::NULL_TERMINATED_HEX));

$this->assertInstanceOf(Right::class, $decoded);

$unpacked = $decoded->extract();

$this->assertIsArray($unpacked);
$this->assertSame(WIRE_FORMAT_PROTOCOL_VERSION, $unpacked[PROTOCOL_ACCESSOR_VERSION]);
$this->assertSame(self::SCHEMA_ID, $unpacked[PROTOCOL_ACCESSOR_SCHEMA_ID]);
$this->assertSame(self::NULL_TERMINATED_AVRO_HEX, \bin2hex($unpacked[PROTOCOL_ACCESSOR_AVRO]));
}
}

0 comments on commit fabd439

Please sign in to comment.