diff --git a/lib/Rdata/OPT.php b/lib/Rdata/OPT.php index fdb5ef2..c04b5ac 100755 --- a/lib/Rdata/OPT.php +++ b/lib/Rdata/OPT.php @@ -98,7 +98,7 @@ public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null) $rdLength = $rdLength ?? strlen($rdata); $endOffset = $offset + $rdLength; - do { + while ($offset < $endOffset) { $integers = @unpack('ncode/nlength', $rdata, $offset); if (false === $integers) { throw new DecodeException(static::TYPE, $rdata); @@ -112,6 +112,6 @@ public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null) } $option->fromWire($rdata, $offset, $integers['length']); $this->options[] = $option; - } while ($offset < $endOffset); + } } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 44e6c4f..8c544bf 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -267,6 +267,16 @@ public function testWire8(): void $this->assertEquals($expectation, $msg->toWire()); } + public function testWire9(): void + { + $expectation = $this->getWireTestData(9); + $msg = Message::fromWire($this->getWireTestData(9)); + $additionals = $msg->getAdditionals(); + $this->assertCount(1, $additionals); + $this->assertInstanceOf(OPT::class, $additionals[0]->getRdata()); + $this->assertEquals($expectation, $msg->toWire()); + } + public function testWire10(): void { $this->expectException(UnexpectedValueException::class); diff --git a/tests/Rdata/OptTest.php b/tests/Rdata/OptTest.php index aae4ce0..8817b0f 100644 --- a/tests/Rdata/OptTest.php +++ b/tests/Rdata/OptTest.php @@ -59,6 +59,15 @@ public function testFromWire2(): void $opt->fromWire($wire); } + public function testFromWire3(): void + { + $wire = ''; + + $opt = new OPT(); + $opt->fromWire($wire); + $this->assertCount(0, $opt->getOptions()); + } + public function testToWire(): void { $options = []; diff --git a/tests/Resources/wire/wire_test.data9 b/tests/Resources/wire/wire_test.data9 new file mode 100644 index 0000000..cd12ca4 --- /dev/null +++ b/tests/Resources/wire/wire_test.data9 @@ -0,0 +1,13 @@ +# HEADER SECTION +30 99 # ID +01 20 # FLAGS +00 01 # QDCOUNT=1 +00 00 # ANCOUNT=0 +00 00 # NSCOUNT=0 +00 01 # ARCOUNT=1 + +#QUESTION SECTION +05 67 6f 6f 67 65 03 63 6f 6d 00 00 01 00 01 # question section: google.com IN A + +#ADDITIONAL RECORDS +00 00 29 04 d0 00 00 00 00 00 00