1
0
mirror of https://github.com/danog/dns.git synced 2025-01-23 05:51:11 +01:00

Added resolver tests.

This commit is contained in:
Danack 2014-07-22 18:31:44 +01:00
parent 06317638c6
commit 155d33475b
3 changed files with 212 additions and 1 deletions

View File

@ -81,7 +81,8 @@ class HostsFileTest extends \PHPUnit_Framework_TestCase {
list($resolvedAddr, $resolvedMode) = $result; list($resolvedAddr, $resolvedMode) = $result;
$this->assertEquals( $this->assertEquals(
$expectedAddrMode, $expectedAddrMode,
$resolvedMode $resolvedMode,
"Incorrect address type received was expecting $expectedAddrMode but received $resolvedMode."
); );
$this->assertEquals( $this->assertEquals(

View 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);
}
}

8
test/fixtures/resolverTest.txt vendored Normal file
View 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