mirror of
https://github.com/danog/dns.git
synced 2024-11-26 20:14:51 +01:00
Merge pull request #11 from Danack/addResolverTest
Add resolver and ResponseInterpreter tests.
This commit is contained in:
commit
671cffe242
202
test/AddrTest/ResolverTest.php
Normal file
202
test/AddrTest/ResolverTest.php
Normal file
@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
|
||||
namespace AddrTest;
|
||||
|
||||
use Addr\AddressModes;
|
||||
use Addr\Cache;
|
||||
use Alert\ReactorFactory;
|
||||
use Addr\Client;
|
||||
use Addr\NameValidator;
|
||||
use LibDNS\Decoder\DecoderFactory;
|
||||
use LibDNS\Encoder\EncoderFactory;
|
||||
use LibDNS\Messages\MessageFactory;
|
||||
use LibDNS\Records\QuestionFactory;
|
||||
use Addr\RequestBuilder;
|
||||
use Addr\ResponseInterpreter;
|
||||
use Addr\HostsFile;
|
||||
use Addr\Resolver;
|
||||
use Addr\ResolutionErrors;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @group resolver
|
||||
*/
|
||||
class ResolverTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
|
||||
/**
|
||||
* @return Resolver
|
||||
*/
|
||||
function createResolver($reactor)
|
||||
{
|
||||
$nameValidator = new NameValidator;
|
||||
$cache = new Cache\MemoryCache;
|
||||
|
||||
|
||||
$serverAddr = null;
|
||||
$serverPort = null;
|
||||
$requestTimeout = null;
|
||||
$hostsFilePath = null;
|
||||
|
||||
$client = new Client(
|
||||
$reactor,
|
||||
new RequestBuilder(
|
||||
new MessageFactory,
|
||||
new QuestionFactory,
|
||||
(new EncoderFactory)->create()
|
||||
),
|
||||
new ResponseInterpreter(
|
||||
(new DecoderFactory)->create()
|
||||
),
|
||||
$cache, $serverAddr, $serverPort, $requestTimeout
|
||||
);
|
||||
|
||||
$hostsFile = new HostsFile($nameValidator, __DIR__.'/../fixtures/resolverTest.txt');
|
||||
$resolver = new Resolver($reactor, $nameValidator, $client, $hostsFile);
|
||||
|
||||
return $resolver;
|
||||
}
|
||||
|
||||
function testInvalidName()
|
||||
{
|
||||
$reactor = (new ReactorFactory)->select();
|
||||
$resolver = $this->createResolver($reactor);
|
||||
|
||||
$callbackCount = 0;
|
||||
$resultAddr1 = null;
|
||||
$resultType1 = null;
|
||||
|
||||
$callback1 = function ($addr, $type) use (&$callbackCount, &$resultAddr1, &$resultType1) {
|
||||
$callbackCount++;
|
||||
$resultAddr1 = $addr;
|
||||
$resultType1 = $type;
|
||||
};
|
||||
|
||||
$alphabet = "abcdefghijklmnopqrstuvwxyz";
|
||||
$tooLongName = $alphabet.$alphabet; //52
|
||||
$tooLongName = $tooLongName.$tooLongName; //104
|
||||
$tooLongName = $tooLongName.$tooLongName; //208
|
||||
$tooLongName = $tooLongName.$alphabet; //234
|
||||
$tooLongName = $tooLongName.$alphabet; //260
|
||||
|
||||
$resolver->resolve($tooLongName, $callback1, AddressModes::PREFER_INET6);
|
||||
|
||||
$reactor->run();
|
||||
|
||||
$this->assertEquals(1, $callbackCount);
|
||||
$this->assertNull($resultAddr1);
|
||||
$this->assertSame(ResolutionErrors::ERR_INVALID_NAME, $resultType1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @group internet
|
||||
*/
|
||||
function testUnknownName()
|
||||
{
|
||||
$reactor = (new ReactorFactory)->select();
|
||||
$resolver = $this->createResolver($reactor);
|
||||
|
||||
$callbackCount = 0;
|
||||
$resultAddr1 = null;
|
||||
$resultType1 = null;
|
||||
|
||||
$callback1 = function ($addr, $type) use (&$callbackCount, &$resultAddr1, &$resultType1) {
|
||||
$callbackCount++;
|
||||
$resultAddr1 = $addr;
|
||||
$resultType1 = $type;
|
||||
};
|
||||
|
||||
$resolver->resolve("doesntexist", $callback1, AddressModes::PREFER_INET6);
|
||||
$reactor->run();
|
||||
|
||||
$this->assertEquals(1, $callbackCount);
|
||||
$this->assertNull($resultAddr1);
|
||||
$this->assertSame(ResolutionErrors::ERR_NO_RECORD, $resultType1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check that getting localhost name resolves correctly.
|
||||
*/
|
||||
function testLocalHost()
|
||||
{
|
||||
$reactor = (new ReactorFactory)->select();
|
||||
$resolver = $this->createResolver($reactor);
|
||||
|
||||
$callbackCount = 0;
|
||||
$resultAddr1 = null;
|
||||
$resultType1 = null;
|
||||
|
||||
$resultAddr2 = null;
|
||||
$resultType2 = null;
|
||||
|
||||
$callback1 = function ($addr, $type) use (&$callbackCount, &$resultAddr1, &$resultType1) {
|
||||
$callbackCount++;
|
||||
$resultAddr1 = $addr;
|
||||
$resultType1 = $type;
|
||||
};
|
||||
|
||||
$callback2 = function ($addr, $type) use (&$callbackCount, &$resultAddr2, &$resultType2) {
|
||||
$callbackCount++;
|
||||
$resultAddr2 = $addr;
|
||||
$resultType2 = $type;
|
||||
};
|
||||
|
||||
$resolver->resolve("localhost", $callback1, AddressModes::INET4_ADDR);
|
||||
$resolver->resolve("localhost", $callback2, AddressModes::PREFER_INET6);
|
||||
|
||||
$reactor->run();
|
||||
|
||||
$this->assertEquals(2, $callbackCount);
|
||||
$this->assertSame('127.0.0.1', $resultAddr1);
|
||||
$this->assertSame(AddressModes::INET4_ADDR, $resultType1, "Wrong result type - should be INET4_ADDR but got $resultType1");
|
||||
|
||||
$this->assertSame('::1', $resultAddr2);
|
||||
$this->assertSame(AddressModes::INET6_ADDR, $resultType2, "Wrong result type - should be INET6_ADDR but got $resultType2");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Basic tests
|
||||
*/
|
||||
function testBasic()
|
||||
{
|
||||
$reactor = (new ReactorFactory)->select();
|
||||
$resolver = $this->createResolver($reactor);
|
||||
|
||||
$callbackCount = 0;
|
||||
$resultAddr1 = null;
|
||||
$resultType1 = null;
|
||||
|
||||
$resultAddr2 = null;
|
||||
$resultType2 = null;
|
||||
|
||||
$callback1 = function ($addr, $type) use (&$callbackCount, &$resultAddr1, &$resultType1) {
|
||||
$callbackCount++;
|
||||
$resultAddr1 = $addr;
|
||||
$resultType1 = $type;
|
||||
};
|
||||
|
||||
$callback2 = function ($addr, $type) use (&$callbackCount, &$resultAddr2, &$resultType2) {
|
||||
$callbackCount++;
|
||||
$resultAddr2 = $addr;
|
||||
$resultType2 = $type;
|
||||
};
|
||||
|
||||
$resolver->resolve("host1.example.com", $callback1, AddressModes::INET4_ADDR);
|
||||
$resolver->resolve("resolvertest", $callback2, AddressModes::INET4_ADDR);
|
||||
$reactor->run();
|
||||
|
||||
$this->assertEquals(2, $callbackCount);
|
||||
$this->assertSame('192.168.1.1', $resultAddr1);
|
||||
$this->assertSame(AddressModes::INET4_ADDR, $resultType1);
|
||||
|
||||
$this->assertSame('192.168.1.3', $resultAddr2);
|
||||
$this->assertSame(AddressModes::INET4_ADDR, $resultType1);
|
||||
}
|
||||
}
|
@ -2,7 +2,11 @@
|
||||
|
||||
namespace AddrTest;
|
||||
|
||||
use Addr\AddressModes;
|
||||
use Addr\ResolverFactory;
|
||||
use Alert\ReactorFactory;
|
||||
use Addr\ResponseInterpreter;
|
||||
use LibDNS\Decoder\DecoderFactory;
|
||||
|
||||
class ResponseInterpreterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -41,4 +45,61 @@ class ResponseInterpreterTest extends \PHPUnit_Framework_TestCase
|
||||
$result = $responseInterpreter->decode("SomePacket");
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
// 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::INET6_ADDR);
|
||||
$this->markTestSkipped("I am unsure what the correct response should be.");
|
||||
//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
|
||||
}
|
||||
|
||||
}
|
||||
|
8
test/fixtures/resolverTest.txt
vendored
Normal file
8
test/fixtures/resolverTest.txt
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
192.168.1.1 host1.example.com
|
||||
192.168.1.2 host2.example.com
|
||||
192.168.1.3 resolvertest
|
||||
2001:db8::2:1 host1.example.com
|
||||
2001:db8:0:1:1:1:1:1 host2.example.com
|
||||
2001:db8::1:0:0:1 host3.example.com
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user