diff --git a/test/AddrTest/HostsFileTest.php b/test/AddrTest/HostsFileTest.php index 276838e..3092f7d 100644 --- a/test/AddrTest/HostsFileTest.php +++ b/test/AddrTest/HostsFileTest.php @@ -81,7 +81,8 @@ class HostsFileTest extends \PHPUnit_Framework_TestCase { list($resolvedAddr, $resolvedMode) = $result; $this->assertEquals( $expectedAddrMode, - $resolvedMode + $resolvedMode, + "Incorrect address type received was expecting $expectedAddrMode but received $resolvedMode." ); $this->assertEquals( diff --git a/test/AddrTest/ResolverTest.php b/test/AddrTest/ResolverTest.php new file mode 100644 index 0000000..76b136c --- /dev/null +++ b/test/AddrTest/ResolverTest.php @@ -0,0 +1,202 @@ +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); + } +} \ No newline at end of file diff --git a/test/fixtures/resolverTest.txt b/test/fixtures/resolverTest.txt new file mode 100644 index 0000000..19d4270 --- /dev/null +++ b/test/fixtures/resolverTest.txt @@ -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 + +