2014-07-19 15:37:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AddrTest;
|
|
|
|
|
2014-07-22 17:30:34 +02:00
|
|
|
use Addr\AddressModes;
|
|
|
|
use Addr\ResolverFactory;
|
|
|
|
use Alert\ReactorFactory;
|
2014-07-19 15:37:48 +02:00
|
|
|
use Addr\ResponseInterpreter;
|
2014-07-22 17:30:34 +02:00
|
|
|
use LibDNS\Decoder\DecoderFactory;
|
2014-07-19 15:37:48 +02:00
|
|
|
|
2014-07-21 18:48:36 +02:00
|
|
|
class ResponseInterpreterTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testCatchesExceptionAndReturnsNull()
|
|
|
|
{
|
2014-07-19 15:37:48 +02:00
|
|
|
$decoder = \Mockery::mock('LibDNS\Decoder\Decoder');
|
|
|
|
$decoder->shouldReceive('decode')->withAnyArgs()->andThrow("Exception", "Testing bad packet");
|
|
|
|
$responseInterpreter = new ResponseInterpreter($decoder);
|
|
|
|
$result = $responseInterpreter->decode("SomePacket");
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
2014-07-21 18:48:36 +02:00
|
|
|
public function testInvalidMessage()
|
|
|
|
{
|
2014-07-19 15:37:48 +02:00
|
|
|
$message = \Mockery::mock('LibDNS\Messages\Message');
|
|
|
|
$message->shouldReceive('getType')->once()->andReturn(\LibDNS\Messages\MessageTypes::QUERY);
|
2014-07-21 18:48:36 +02:00
|
|
|
|
2014-07-19 15:37:48 +02:00
|
|
|
$decoder = \Mockery::mock('LibDNS\Decoder\Decoder');
|
|
|
|
$decoder->shouldReceive('decode')->once()->andReturn($message);
|
|
|
|
|
|
|
|
$responseInterpreter = new ResponseInterpreter($decoder);
|
|
|
|
$result = $responseInterpreter->decode("SomePacket");
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
2014-07-21 18:48:36 +02:00
|
|
|
public function testInvalidResponseCode()
|
|
|
|
{
|
2014-07-19 15:37:48 +02:00
|
|
|
$message = \Mockery::mock('LibDNS\Messages\Message');
|
|
|
|
$message->shouldReceive('getType')->once()->andReturn(\LibDNS\Messages\MessageTypes::RESPONSE);
|
|
|
|
$message->shouldReceive('getResponseCode')->once()->andReturn(42);
|
2014-07-21 18:48:36 +02:00
|
|
|
|
2014-07-19 15:37:48 +02:00
|
|
|
$decoder = \Mockery::mock('LibDNS\Decoder\Decoder');
|
|
|
|
$decoder->shouldReceive('decode')->once()->andReturn($message);
|
|
|
|
|
|
|
|
$responseInterpreter = new ResponseInterpreter($decoder);
|
|
|
|
$result = $responseInterpreter->decode("SomePacket");
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
2014-07-22 17:30:34 +02:00
|
|
|
|
|
|
|
// Test string below was generated with
|
|
|
|
//
|
|
|
|
// function getSaneString($result) {
|
|
|
|
// $resultInHex = unpack('H*', $result);
|
|
|
|
// $resultInHex = $resultInHex[1];
|
|
|
|
//
|
|
|
|
// $charsAsHex = str_split($resultInHex, 2);
|
|
|
|
//
|
|
|
|
// $output = '';
|
|
|
|
//
|
|
|
|
// foreach ($charsAsHex as $charAsHex) {
|
|
|
|
// $decimal = hexdec($charAsHex);
|
|
|
|
// if ($decimal >= 32 && $decimal <= 126) {
|
|
|
|
// $output .= chr($decimal);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// $output .= '\x'.$charAsHex;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return $output;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group CNAME
|
|
|
|
*/
|
|
|
|
public function testCNAME() {
|
|
|
|
|
|
|
|
$testPacket = "\x00\x00\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x04news\x03bbc\x02co\x02uk\x00\x00\x01\x00\x01\xc0\x0c\x00\x05\x00\x01\x00\x00\x01W\x00\x12\x07newswww\x03bbc\x03net\xc0\x18\xc0,\x00\x01\x00\x01\x00\x00\x00\xd2\x00\x04\xd4:\xf6P\xc0,\x00\x01\x00\x01\x00\x00\x00\xd2\x00\x04\xd4:\xf6Q";
|
|
|
|
|
|
|
|
$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);
|
|
|
|
$this->assertSame("212.58.246.80", $addr);
|
|
|
|
$this->assertSame(210, $ttl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$interpreted = $responseInterpreter->interpret($response, AddressModes::CNAME);
|
|
|
|
list($type, $addr, $ttl) = $interpreted;
|
|
|
|
$this->assertEquals(AddressModes::INET4_ADDR, $type);
|
|
|
|
$this->assertSame("newswww.bbc.net.uk", $addr);
|
|
|
|
$this->assertNull($ttl);
|
|
|
|
|
|
|
|
$interpreted = $responseInterpreter->interpret($response, AddressModes::INET6_ADDR);
|
|
|
|
list($type, $addr, $ttl) = $interpreted;
|
|
|
|
//This looks borked - it's returning the CNAME but as the asserts above are going to fail
|
|
|
|
// this won't be reached.
|
|
|
|
//$type = 8 aka CNAME
|
|
|
|
//$addr = "newswww.bbc.net.uk" aka CNAME
|
|
|
|
//$ttl = null
|
|
|
|
}
|
|
|
|
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|