From fabd439f31b0d0fbd0f9bb70c13ad04efd16cd83 Mon Sep 17 00:00:00 2001 From: Dmitry Alekseev Date: Wed, 4 Aug 2021 12:02:47 +0300 Subject: [PATCH] #54 Fix null terminated string decoding (#56) --- src/Protocol.php | 2 +- test/ProtocolTest.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Protocol.php b/src/Protocol.php index 0ceb18a..25f23e7 100644 --- a/src/Protocol.php +++ b/src/Protocol.php @@ -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 diff --git a/test/ProtocolTest.php b/test/ProtocolTest.php index 90b41f9..eb483f4 100644 --- a/test/ProtocolTest.php +++ b/test/ProtocolTest.php @@ -23,6 +23,9 @@ class ProtocolTest extends AbstractFunctionalTestCase { + private const NULL_TERMINATED_HEX = '000000270f303000000000'; + private const NULL_TERMINATED_AVRO_HEX = '303000000000'; + /** * @test */ @@ -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])); + } }