diff --git a/test/AddrTest/AddrTest.php b/test/AddrTest/AddrTest.php index 4cc8b78..35ce5af 100644 --- a/test/AddrTest/AddrTest.php +++ b/test/AddrTest/AddrTest.php @@ -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." + ); + } } diff --git a/test/AddrTest/CacheTest.php b/test/AddrTest/CacheTest.php index d4c02a5..65f5fc5 100644 --- a/test/AddrTest/CacheTest.php +++ b/test/AddrTest/CacheTest.php @@ -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 } } diff --git a/test/AddrTest/HostsFileTest.php b/test/AddrTest/HostsFileTest.php new file mode 100644 index 0000000..276838e --- /dev/null +++ b/test/AddrTest/HostsFileTest.php @@ -0,0 +1,95 @@ +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`." + ); + } + } + } +} diff --git a/test/fixtures/ipv4Hosts.txt b/test/fixtures/ipv4Hosts.txt new file mode 100644 index 0000000..fa1f64d --- /dev/null +++ b/test/fixtures/ipv4Hosts.txt @@ -0,0 +1,2 @@ +192.168.1.1 host1.example.com +192.168.1.2 host2.example.com \ No newline at end of file diff --git a/test/fixtures/ipv6Hosts.txt b/test/fixtures/ipv6Hosts.txt new file mode 100644 index 0000000..0e6c82c --- /dev/null +++ b/test/fixtures/ipv6Hosts.txt @@ -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 diff --git a/test/fixtures/mixedVersionHosts.txt b/test/fixtures/mixedVersionHosts.txt new file mode 100644 index 0000000..60a6cf6 --- /dev/null +++ b/test/fixtures/mixedVersionHosts.txt @@ -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 \ No newline at end of file