Skip to content

Commit fabd439

Browse files
authored
#54 Fix null terminated string decoding (#56)
1 parent 93621c3 commit fabd439

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Protocol.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function encoder(int $protocolVersion): callable
5858
function decode(string $binaryString): Either
5959
{
6060
$packedFormat = \sprintf(
61-
'C%s/N%s/A*%s',
61+
'C%s/N%s/a*%s',
6262
PROTOCOL_ACCESSOR_VERSION,
6363
PROTOCOL_ACCESSOR_SCHEMA_ID,
6464
PROTOCOL_ACCESSOR_AVRO

test/ProtocolTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
class ProtocolTest extends AbstractFunctionalTestCase
2525
{
26+
private const NULL_TERMINATED_HEX = '000000270f303000000000';
27+
private const NULL_TERMINATED_AVRO_HEX = '303000000000';
28+
2629
/**
2730
* @test
2831
*/
@@ -149,4 +152,21 @@ public function validate_returns_nothing_for_invalid_unpacked(): void
149152

150153
$this->assertInstanceOf(Nothing::class, validate(WIRE_FORMAT_PROTOCOL_VERSION, $decoded));
151154
}
155+
156+
/**
157+
* @test
158+
*/
159+
public function null_terminated_values_unpacked_correctly(): void
160+
{
161+
$decoded = decode(\hex2bin(self::NULL_TERMINATED_HEX));
162+
163+
$this->assertInstanceOf(Right::class, $decoded);
164+
165+
$unpacked = $decoded->extract();
166+
167+
$this->assertIsArray($unpacked);
168+
$this->assertSame(WIRE_FORMAT_PROTOCOL_VERSION, $unpacked[PROTOCOL_ACCESSOR_VERSION]);
169+
$this->assertSame(self::SCHEMA_ID, $unpacked[PROTOCOL_ACCESSOR_SCHEMA_ID]);
170+
$this->assertSame(self::NULL_TERMINATED_AVRO_HEX, \bin2hex($unpacked[PROTOCOL_ACCESSOR_AVRO]));
171+
}
152172
}

0 commit comments

Comments
 (0)