mirror of
https://github.com/danog/dns.git
synced 2024-11-26 20:14:51 +01:00
commit
2728ca6a37
@ -121,4 +121,90 @@ class AddrTest extends \PHPUnit_Framework_TestCase
|
||||
"At least one of the name lookups did not resolve."
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check that caches do actually cache results.
|
||||
*/
|
||||
function testCachingOfResults() {
|
||||
|
||||
$memoryCache = new MemoryCache();
|
||||
|
||||
$namesFirstRun = [
|
||||
'google.com',
|
||||
'github.com',
|
||||
'google.com',
|
||||
'github.com',
|
||||
];
|
||||
|
||||
$namesSecondRun = [
|
||||
'google.com',
|
||||
'github.com',
|
||||
];
|
||||
|
||||
$setCount = count(array_unique(array_merge($namesFirstRun, $namesSecondRun)));
|
||||
$getCount = count($namesFirstRun) + count($namesSecondRun);
|
||||
|
||||
$mockedCache = \Mockery::mock($memoryCache);
|
||||
|
||||
/** @var $mockedCache \Mockery\Mock */
|
||||
|
||||
$mockedCache->shouldReceive('store')->times($setCount)->passthru();
|
||||
$mockedCache->shouldReceive('get')->times($getCount)->passthru();
|
||||
|
||||
$mockedCache->makePartial();
|
||||
|
||||
$reactor = (new ReactorFactory)->select();
|
||||
$resolver = (new ResolverFactory)->createResolver(
|
||||
$reactor,
|
||||
null, //$serverAddr = null,
|
||||
null, //$serverPort = null,
|
||||
null, //$requestTimeout = null,
|
||||
$mockedCache,
|
||||
null //$hostsFilePath = null
|
||||
);
|
||||
|
||||
$results = [];
|
||||
|
||||
//Lookup the first set of names
|
||||
foreach ($namesFirstRun as $name) {
|
||||
$resolver->resolve($name, function($addr) use($name, $resolver, &$results) {
|
||||
$results[] = [$name, $addr];
|
||||
});
|
||||
}
|
||||
|
||||
$reactor->run();
|
||||
|
||||
$this->assertCount(
|
||||
count($namesFirstRun),
|
||||
$results,
|
||||
"At least one of the name lookups did not resolve in the first run."
|
||||
);
|
||||
|
||||
$results = [];
|
||||
|
||||
//Lookup a second set of names
|
||||
foreach ($namesSecondRun as $name) {
|
||||
$resolver->resolve($name, function($addr) use($name, $resolver, &$results) {
|
||||
$results[] = [$name, $addr];
|
||||
});
|
||||
}
|
||||
|
||||
$reactor->run();
|
||||
|
||||
foreach ($results as $nameAndAddr) {
|
||||
list($name, $addr) = $nameAndAddr;
|
||||
$validIP = filter_var($addr, FILTER_VALIDATE_IP);
|
||||
$this->assertNotFalse(
|
||||
$validIP,
|
||||
"Server name $name did not resolve to a valid IP address"
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertCount(
|
||||
count($namesSecondRun),
|
||||
$results,
|
||||
"At least one of the name lookups did not resolve in the second run."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -171,8 +171,5 @@ class CacheTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
//check that garbage collection collects.
|
||||
$memoryCache->store($key, $value, 0);
|
||||
$memoryCache->collectGarbage();
|
||||
|
||||
//TODO - check that the memoryCache contains a single item
|
||||
}
|
||||
}
|
||||
|
95
test/AddrTest/HostsFileTest.php
Normal file
95
test/AddrTest/HostsFileTest.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
use Addr\HostsFile;
|
||||
use Addr\NameValidator;
|
||||
use Addr\AddressModes;
|
||||
|
||||
/**
|
||||
* Class HostsFileTest
|
||||
* @group hosts
|
||||
*/
|
||||
class HostsFileTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
function testBasicIPV4() {
|
||||
$tests = [
|
||||
['192.168.1.1', 'host1.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
||||
['192.168.1.1', 'host2.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
||||
|
||||
//Check that ipv4 is returned when prefer_inet6 is set
|
||||
['192.168.1.1', 'host1.example.com', AddressModes::PREFER_INET6, AddressModes::INET4_ADDR],
|
||||
|
||||
//Check non-existant domains return null
|
||||
[null, 'host4.example.com', AddressModes::INET4_ADDR, null],
|
||||
];
|
||||
|
||||
$this->runHostsFileTests($tests, __DIR__.'/../fixtures/ipv4Hosts.txt');
|
||||
}
|
||||
|
||||
|
||||
function testBasicIPV6() {
|
||||
$tests = [
|
||||
//Examples taken from http://en.wikipedia.org/wiki/IPv6_address
|
||||
['2001:db8::2:1', 'host1.example.com', AddressModes::INET6_ADDR, AddressModes::INET6_ADDR],
|
||||
['2001:db8:0:1:1:1:1:1', 'host2.example.com', AddressModes::INET6_ADDR, AddressModes::INET6_ADDR],
|
||||
['2001:db8::1:0:0:1', 'host3.example.com', AddressModes::INET6_ADDR, AddressModes::INET6_ADDR],
|
||||
|
||||
//Check that prefer inet6 does indeed return an inet6 address
|
||||
['2001:db8::2:1', 'host1.example.com', AddressModes::PREFER_INET6, AddressModes::INET6_ADDR],
|
||||
|
||||
//Check request for ipv4 returns null
|
||||
[null, 'host1.example.com', AddressModes::INET4_ADDR, null],
|
||||
];
|
||||
|
||||
$this->runHostsFileTests($tests, __DIR__.'/../fixtures/ipv6Hosts.txt');
|
||||
}
|
||||
|
||||
function testMixedIPVersions() {
|
||||
$tests = [
|
||||
//Examples taken from http://en.wikipedia.org/wiki/IPv6_address
|
||||
['2001:db8::2:1', 'host1.example.com', AddressModes::INET6_ADDR, AddressModes::INET6_ADDR],
|
||||
['2001:db8:0:1:1:1:1:1', 'host2.example.com', AddressModes::INET6_ADDR, AddressModes::INET6_ADDR],
|
||||
['2001:db8::1:0:0:1', 'host3.example.com', AddressModes::INET6_ADDR, AddressModes::INET6_ADDR],
|
||||
['192.168.1.1', 'host1.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
||||
['192.168.1.1', 'host2.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
||||
|
||||
//Check that the prefer inet6 works
|
||||
['2001:db8::2:1', 'host1.example.com', AddressModes::PREFER_INET6, AddressModes::INET6_ADDR],
|
||||
|
||||
//Check that a host that is only listed as ipv6 does not return a result for ipv4
|
||||
[null, 'host1.example.com', AddressModes::INET4_ADDR, null],
|
||||
|
||||
];
|
||||
|
||||
$this->runHostsFileTests($tests, __DIR__.'/../fixtures/mixedVersionHosts.txt');
|
||||
}
|
||||
|
||||
|
||||
|
||||
function runHostsFileTests($tests, $hostsFile) {
|
||||
$nameValidator = new NameValidator;
|
||||
$hostsFile = new HostsFile($nameValidator, $hostsFile);
|
||||
|
||||
foreach ($tests as $test) {
|
||||
list($expectedResult, $hostname, $inputAddrMode, $expectedAddrMode) = $test;
|
||||
|
||||
$result = $hostsFile->resolve($hostname, $inputAddrMode);
|
||||
|
||||
if ($expectedResult === null) {
|
||||
$this->assertNull($result);
|
||||
}
|
||||
else {
|
||||
list($resolvedAddr, $resolvedMode) = $result;
|
||||
$this->assertEquals(
|
||||
$expectedAddrMode,
|
||||
$resolvedMode
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$expectedResult,
|
||||
$resolvedAddr,
|
||||
"Failed to resolve $hostname from $expectedResult. Expected `$expectedResult` but got `$resolvedAddr`."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
2
test/fixtures/ipv4Hosts.txt
vendored
Normal file
2
test/fixtures/ipv4Hosts.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
192.168.1.1 host1.example.com
|
||||
192.168.1.2 host2.example.com
|
3
test/fixtures/ipv6Hosts.txt
vendored
Normal file
3
test/fixtures/ipv6Hosts.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
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
|
13
test/fixtures/mixedVersionHosts.txt
vendored
Normal file
13
test/fixtures/mixedVersionHosts.txt
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
192.168.1.1 host1.example.com
|
||||
192.168.1.2 host2.example.com
|
||||
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
|
||||
|
||||
#next line is empty for coverage
|
||||
|
||||
//empty_line_for_coverage
|
||||
|
||||
|
||||
#next line has invalid address for coverage
|
||||
1.2.300 doesntexist.com
|
Loading…
Reference in New Issue
Block a user