shouldReceive('decode')->withAnyArgs()->andThrow("Exception", "Testing bad packet"); $responseInterpreter = new ResponseInterpreter($decoder); $result = $responseInterpreter->decode("SomePacket"); $this->assertNull($result); } public function testInvalidMessage() { $message = \Mockery::mock('LibDNS\Messages\Message'); $message->shouldReceive('getType')->once()->andReturn(\LibDNS\Messages\MessageTypes::QUERY); $decoder = \Mockery::mock('LibDNS\Decoder\Decoder'); $decoder->shouldReceive('decode')->once()->andReturn($message); $responseInterpreter = new ResponseInterpreter($decoder); $result = $responseInterpreter->decode("SomePacket"); $this->assertNull($result); } public function testInvalidResponseCode() { $message = \Mockery::mock('LibDNS\Messages\Message'); $message->shouldReceive('getType')->once()->andReturn(\LibDNS\Messages\MessageTypes::RESPONSE); $message->shouldReceive('getResponseCode')->once()->andReturn(42); $decoder = \Mockery::mock('LibDNS\Decoder\Decoder'); $decoder->shouldReceive('decode')->once()->andReturn($message); $responseInterpreter = new ResponseInterpreter($decoder); $result = $responseInterpreter->decode("SomePacket"); $this->assertNull($result); } /** * @group CNAME */ public function testNewsBBC() { $testPacket = $this->getPacketString(self::$bbcNews); $decoder = (new DecoderFactory)->create(); $responseInterpreter = new ResponseInterpreter($decoder); $decoded = $responseInterpreter->decode($testPacket); list($id, $response) = $decoded; //Check the IPV4 result $interpreted = $responseInterpreter->interpret($response, AddressModes::INET4_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals(AddressModes::INET4_ADDR, $type); //@TODO - this should be multiple - 212.58.246.82 and 212.58.246.83 $this->assertSame("212.58.246.82", $addr); $this->assertSame(174, $ttl); $interpreted = $responseInterpreter->interpret($response, AddressModes::INET6_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals("newswww.bbc.net.uk", $addr); $this->assertEquals(AddressModes::CNAME, $type); $this->assertNull($ttl); } public function createResponseInterpreter() { $decoder = (new DecoderFactory)->create(); $responseInterpreter = new ResponseInterpreter($decoder); return $responseInterpreter; } public function testNoResults() { $responseInterpreter = $this->createResponseInterpreter(); $packet = $this->getPacketString(self::$standardQueryResponse); $decoded = $responseInterpreter->decode($packet); list($id, $message) = $decoded; $interpreted = $responseInterpreter->interpret($message, AddressModes::INET4_ADDR); $this->assertNull($interpreted); $interpreted = $responseInterpreter->interpret($message, AddressModes::INET6_ADDR); $this->assertNull($interpreted); } public function testNoSuchName() { $responseInterpreter = $this->createResponseInterpreter(); $packet = $this->getPacketString(self::$standardQueryResponseNoSuchName); $decoded = $responseInterpreter->decode($packet); list($id, $message) = $decoded; $interpreted = $responseInterpreter->interpret($message, AddressModes::INET4_ADDR); $this->assertNull($interpreted, "Response with 'no such name' was not interpreted to null."); } public function testMixed() { $responseInterpreter = $this->createResponseInterpreter(); $packet = $this->getPacketString(self::$standardQueryResponseMixed); $decoded = $responseInterpreter->decode($packet); list($id, $message) = $decoded; //Get the IPv4 part $interpreted = $responseInterpreter->interpret($message, AddressModes::INET4_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals(AddressModes::INET4_ADDR, $type); $this->assertEquals('204.152.184.88', $addr); $this->assertEquals(600, $ttl); //Get the IPv6 part $interpreted = $responseInterpreter->interpret($message, AddressModes::INET6_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals(AddressModes::INET6_ADDR, $type); $this->assertEquals('2001:4f8:0:2::d', $addr); $this->assertEquals(600, $ttl); } public function testIPv4() { $responseInterpreter = $this->createResponseInterpreter(); $packet = $this->getPacketString(self::$standardQueryResponseA); $decoded = $responseInterpreter->decode($packet); list($id, $message) = $decoded; //Get the IPv4 part $interpreted = $responseInterpreter->interpret($message, AddressModes::INET4_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals(AddressModes::INET4_ADDR, $type); $this->assertEquals('204.152.190.12', $addr); $this->assertEquals(82159, $ttl); //Get the IPv6 part $interpreted = $responseInterpreter->interpret($message, AddressModes::INET6_ADDR); $this->assertNull($interpreted); } public function testIPv6() { $responseInterpreter = $this->createResponseInterpreter(); $packet = $this->getPacketString(self::$standardQueryResponseIPV6); $decoded = $responseInterpreter->decode($packet); list($id, $message) = $decoded; //Get the IPv4 part $interpreted = $responseInterpreter->interpret($message, AddressModes::INET4_ADDR); $this->assertNull($interpreted); //Get the IPv6 part $interpreted = $responseInterpreter->interpret($message, AddressModes::INET6_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals(AddressModes::INET6_ADDR, $type); $this->assertEquals('2001:4f8:4:7:2e0:81ff:fe52:9a6b', $addr); $this->assertEquals(86340, $ttl); } public function testCnameResponse() { $responseInterpreter = $this->createResponseInterpreter(); $packet = $this->getPacketString(self::$standardQueryResponseCNAME); $decoded = $responseInterpreter->decode($packet); list($id, $message) = $decoded; //Try to get an IPv4 answer - but actually get a CNAME $interpreted = $responseInterpreter->interpret($message, AddressModes::INET4_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals(AddressModes::CNAME, $type); $this->assertEquals('www.l.google.com', $addr); //Try to get an IPv6 answer - but actually get a CNAME $interpreted = $responseInterpreter->interpret($message, AddressModes::INET6_ADDR); list($type, $addr, $ttl) = $interpreted; $this->assertEquals(AddressModes::CNAME, $type); $this->assertEquals('www.l.google.com', $addr); } }