2014-07-21 23:56:48 +02:00
|
|
|
<?php
|
|
|
|
|
2014-09-24 19:31:27 +02:00
|
|
|
namespace Amp\Dns\Test;
|
2014-09-23 22:47:55 +02:00
|
|
|
|
|
|
|
use Amp\Dns\HostsFile;
|
|
|
|
use Amp\Dns\NameValidator;
|
|
|
|
use Amp\Dns\AddressModes;
|
2014-07-21 23:56:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class HostsFileTest
|
|
|
|
* @group hosts
|
|
|
|
*/
|
2014-09-23 22:47:55 +02:00
|
|
|
class HostsFileTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
public function testBasicIPV4() {
|
2014-07-21 23:56:48 +02:00
|
|
|
$tests = [
|
|
|
|
['192.168.1.1', 'host1.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
2014-07-22 04:31:07 +02:00
|
|
|
['192.168.1.2', 'host2.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
|
|
|
|
|
|
|
//Check that ipv4 is returned when both v4 and v6 are is set
|
|
|
|
['192.168.1.1', 'host1.example.com', AddressModes::INET4_ADDR | AddressModes::INET6_ADDR, AddressModes::INET4_ADDR],
|
|
|
|
['192.168.1.2', 'host2.example.com', AddressModes::INET4_ADDR | AddressModes::INET6_ADDR, AddressModes::INET4_ADDR],
|
|
|
|
|
|
|
|
//Check that ipv4 is returned when ANY_* is set
|
|
|
|
['192.168.1.1', 'host1.example.com', AddressModes::ANY_PREFER_INET4, AddressModes::INET4_ADDR],
|
|
|
|
['192.168.1.2', 'host2.example.com', AddressModes::ANY_PREFER_INET4, AddressModes::INET4_ADDR],
|
|
|
|
['192.168.1.1', 'host1.example.com', AddressModes::ANY_PREFER_INET6, AddressModes::INET4_ADDR],
|
|
|
|
['192.168.1.2', 'host2.example.com', AddressModes::ANY_PREFER_INET6, AddressModes::INET4_ADDR],
|
2014-07-21 23:56:48 +02:00
|
|
|
|
2014-07-22 04:31:07 +02:00
|
|
|
//Check request for ipv6 returns null
|
|
|
|
[null, 'host1.example.com', AddressModes::INET6_ADDR, null],
|
2014-07-21 23:56:48 +02:00
|
|
|
|
|
|
|
//Check non-existant domains return null
|
|
|
|
[null, 'host4.example.com', AddressModes::INET4_ADDR, null],
|
|
|
|
];
|
|
|
|
|
2014-09-23 22:47:55 +02:00
|
|
|
$this->runHostsFileTests($tests, __DIR__ . '/fixtures/ipv4Hosts.txt');
|
2014-07-22 04:31:07 +02:00
|
|
|
}
|
2014-07-21 23:56:48 +02:00
|
|
|
|
2014-09-23 22:47:55 +02:00
|
|
|
public function testBasicIPV6() {
|
2014-07-21 23:56:48 +02:00
|
|
|
$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],
|
|
|
|
|
2014-07-22 04:31:07 +02:00
|
|
|
//Check that ipv6 is returned when both v4 and v6 are is set
|
|
|
|
['2001:db8::2:1', 'host1.example.com', AddressModes::INET6_ADDR | AddressModes::INET4_ADDR, AddressModes::INET6_ADDR],
|
|
|
|
['2001:db8:0:1:1:1:1:1', 'host2.example.com', AddressModes::INET6_ADDR | AddressModes::INET4_ADDR, AddressModes::INET6_ADDR],
|
|
|
|
['2001:db8::1:0:0:1', 'host3.example.com', AddressModes::INET6_ADDR | AddressModes::INET4_ADDR, AddressModes::INET6_ADDR],
|
|
|
|
|
|
|
|
//Check that ipv6 is returned when ANY_* is set
|
|
|
|
['2001:db8::2:1', 'host1.example.com', AddressModes::ANY_PREFER_INET4, AddressModes::INET6_ADDR],
|
|
|
|
['2001:db8:0:1:1:1:1:1', 'host2.example.com', AddressModes::ANY_PREFER_INET4, AddressModes::INET6_ADDR],
|
|
|
|
['2001:db8::1:0:0:1', 'host3.example.com', AddressModes::ANY_PREFER_INET4, AddressModes::INET6_ADDR],
|
|
|
|
['2001:db8::2:1', 'host1.example.com', AddressModes::ANY_PREFER_INET6, AddressModes::INET6_ADDR],
|
|
|
|
['2001:db8:0:1:1:1:1:1', 'host2.example.com', AddressModes::ANY_PREFER_INET6, AddressModes::INET6_ADDR],
|
|
|
|
['2001:db8::1:0:0:1', 'host3.example.com', AddressModes::ANY_PREFER_INET6, AddressModes::INET6_ADDR],
|
|
|
|
|
2014-07-21 23:56:48 +02:00
|
|
|
//Check request for ipv4 returns null
|
|
|
|
[null, 'host1.example.com', AddressModes::INET4_ADDR, null],
|
2014-07-22 04:31:07 +02:00
|
|
|
|
|
|
|
//Check non-existant domains return null
|
|
|
|
[null, 'host4.example.com', AddressModes::INET6_ADDR, null],
|
2014-07-21 23:56:48 +02:00
|
|
|
];
|
|
|
|
|
2014-09-23 22:47:55 +02:00
|
|
|
$this->runHostsFileTests($tests, __DIR__ . '/fixtures/ipv6Hosts.txt');
|
2014-07-21 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:47:55 +02:00
|
|
|
public function testMixedIPVersions() {
|
2014-07-21 23:56:48 +02:00
|
|
|
$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],
|
2014-07-22 04:31:07 +02:00
|
|
|
['192.168.1.2', 'host2.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
|
|
|
['192.168.1.4', 'host4.example.com', AddressModes::INET4_ADDR, AddressModes::INET4_ADDR],
|
|
|
|
|
|
|
|
//Check that v4 is returned by default
|
|
|
|
['192.168.1.1', 'host1.example.com', AddressModes::INET4_ADDR | AddressModes::INET6_ADDR, AddressModes::INET4_ADDR],
|
|
|
|
|
2014-07-21 23:56:48 +02:00
|
|
|
//Check that the prefer inet6 works
|
2014-07-22 04:31:07 +02:00
|
|
|
['2001:db8::2:1', 'host1.example.com', AddressModes::INET4_ADDR | AddressModes::INET6_ADDR | AddressModes::PREFER_INET6, AddressModes::INET6_ADDR],
|
|
|
|
|
|
|
|
//Check that the ANY_* works
|
|
|
|
['192.168.1.1', 'host1.example.com', AddressModes::ANY_PREFER_INET4, AddressModes::INET4_ADDR],
|
|
|
|
['2001:db8::2:1', 'host1.example.com', AddressModes::ANY_PREFER_INET6, AddressModes::INET6_ADDR],
|
|
|
|
|
|
|
|
//Check that a host that is only listed as ipv4 does not return a result for ipv6
|
|
|
|
[null, 'host4.example.com', AddressModes::INET6_ADDR, null],
|
|
|
|
|
2014-07-21 23:56:48 +02:00
|
|
|
//Check that a host that is only listed as ipv6 does not return a result for ipv4
|
2014-07-22 04:31:07 +02:00
|
|
|
[null, 'host3.example.com', AddressModes::INET4_ADDR, null],
|
2014-07-21 23:56:48 +02:00
|
|
|
];
|
|
|
|
|
2014-09-23 22:47:55 +02:00
|
|
|
$this->runHostsFileTests($tests, __DIR__ . '/fixtures/mixedVersionHosts.txt');
|
2014-07-21 23:56:48 +02:00
|
|
|
}
|
2014-07-22 04:31:07 +02:00
|
|
|
|
2014-09-23 22:47:55 +02:00
|
|
|
public function runHostsFileTests($tests, $hostsFile) {
|
2014-07-21 23:56:48 +02:00
|
|
|
$nameValidator = new NameValidator;
|
|
|
|
$hostsFile = new HostsFile($nameValidator, $hostsFile);
|
|
|
|
|
2014-07-22 04:31:07 +02:00
|
|
|
foreach ($tests as $i => $test) {
|
2014-07-21 23:56:48 +02:00
|
|
|
list($expectedResult, $hostname, $inputAddrMode, $expectedAddrMode) = $test;
|
|
|
|
|
|
|
|
$result = $hostsFile->resolve($hostname, $inputAddrMode);
|
|
|
|
|
|
|
|
if ($expectedResult === null) {
|
|
|
|
$this->assertNull($result);
|
2014-07-22 04:31:07 +02:00
|
|
|
} else {
|
2014-07-21 23:56:48 +02:00
|
|
|
list($resolvedAddr, $resolvedMode) = $result;
|
2014-07-22 04:31:07 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expectedAddrMode,
|
|
|
|
$resolvedMode,
|
|
|
|
"Failed to resolve $hostname to $expectedResult. " .
|
|
|
|
"Expected `" . var_export($expectedAddrMode, true) . "` " .
|
|
|
|
"but got `" . var_export($resolvedAddr, true) . "` " .
|
|
|
|
" when running test #" . $i
|
|
|
|
);
|
|
|
|
|
2014-07-21 23:56:48 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
$expectedResult,
|
|
|
|
$resolvedAddr,
|
2014-07-22 04:31:07 +02:00
|
|
|
"Failed to resolve $hostname to $expectedResult. " .
|
|
|
|
"Expected `$expectedResult` but got `$resolvedAddr` " .
|
|
|
|
" when running test #" . $i
|
2014-07-21 23:56:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|