From 06317638c6035d256a99667693692515ee279165 Mon Sep 17 00:00:00 2001 From: Danack Date: Tue, 22 Jul 2014 16:30:34 +0100 Subject: [PATCH 1/3] Added hard-coded packet response to allow testing of the decoding. --- test/AddrTest/ResponseInterpreterTest.php | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/test/AddrTest/ResponseInterpreterTest.php b/test/AddrTest/ResponseInterpreterTest.php index fe05c7d..63ea228 100644 --- a/test/AddrTest/ResponseInterpreterTest.php +++ b/test/AddrTest/ResponseInterpreterTest.php @@ -2,7 +2,11 @@ namespace AddrTest; +use Addr\AddressModes; +use Addr\ResolverFactory; +use Alert\ReactorFactory; use Addr\ResponseInterpreter; +use LibDNS\Decoder\DecoderFactory; class ResponseInterpreterTest extends \PHPUnit_Framework_TestCase { @@ -41,4 +45,67 @@ class ResponseInterpreterTest extends \PHPUnit_Framework_TestCase $result = $responseInterpreter->decode("SomePacket"); $this->assertNull($result); } + +// Test string below was generated with +// +// function getSaneString($result) { +// $resultInHex = unpack('H*', $result); +// $resultInHex = $resultInHex[1]; +// +// $charsAsHex = str_split($resultInHex, 2); +// +// $output = ''; +// +// foreach ($charsAsHex as $charAsHex) { +// $decimal = hexdec($charAsHex); +// if ($decimal >= 32 && $decimal <= 126) { +// $output .= chr($decimal); +// } +// else { +// $output .= '\x'.$charAsHex; +// } +// } +// +// return $output; +// } + + + /** + * @group CNAME + */ + public function testCNAME() { + + $testPacket = "\x00\x00\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x04news\x03bbc\x02co\x02uk\x00\x00\x01\x00\x01\xc0\x0c\x00\x05\x00\x01\x00\x00\x01W\x00\x12\x07newswww\x03bbc\x03net\xc0\x18\xc0,\x00\x01\x00\x01\x00\x00\x00\xd2\x00\x04\xd4:\xf6P\xc0,\x00\x01\x00\x01\x00\x00\x00\xd2\x00\x04\xd4:\xf6Q"; + + $decoder = (new DecoderFactory)->create(); + + $responseInterpreter = new ResponseInterpreter($decoder); + $decoded = $responseInterpreter->decode($testPacket); + + list($id, $response) = $decoded; + + //Check the IPV4 result + $interpreted = $responseInterpreter->interpret($response, AddressModes::INET4_ADDR); + list($type, $addr, $ttl) = $interpreted; + $this->assertEquals(AddressModes::INET4_ADDR, $type); + $this->assertSame("212.58.246.80", $addr); + $this->assertSame(210, $ttl); + + + + $interpreted = $responseInterpreter->interpret($response, AddressModes::CNAME); + list($type, $addr, $ttl) = $interpreted; + $this->assertEquals(AddressModes::INET4_ADDR, $type); + $this->assertSame("newswww.bbc.net.uk", $addr); + $this->assertNull($ttl); + + $interpreted = $responseInterpreter->interpret($response, AddressModes::INET6_ADDR); + list($type, $addr, $ttl) = $interpreted; + //This looks borked - it's returning the CNAME but as the asserts above are going to fail + // this won't be reached. + //$type = 8 aka CNAME + //$addr = "newswww.bbc.net.uk" aka CNAME + //$ttl = null + } + } From 155d33475b30fc9b9c6d52933bfcdd13287efbf5 Mon Sep 17 00:00:00 2001 From: Danack Date: Tue, 22 Jul 2014 18:31:44 +0100 Subject: [PATCH 2/3] Added resolver tests. --- test/AddrTest/HostsFileTest.php | 3 +- test/AddrTest/ResolverTest.php | 202 ++++++++++++++++++++++++++++++++ test/fixtures/resolverTest.txt | 8 ++ 3 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 test/AddrTest/ResolverTest.php create mode 100644 test/fixtures/resolverTest.txt 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 + + From aab84bcef062e4bc22615532dd0a12f81af72ba4 Mon Sep 17 00:00:00 2001 From: Danack Date: Tue, 22 Jul 2014 19:39:47 +0100 Subject: [PATCH 3/3] Removed bogus check in test - there is still an issue so test is marked as skipped. --- test/AddrTest/ResponseInterpreterTest.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/test/AddrTest/ResponseInterpreterTest.php b/test/AddrTest/ResponseInterpreterTest.php index 63ea228..e91c59a 100644 --- a/test/AddrTest/ResponseInterpreterTest.php +++ b/test/AddrTest/ResponseInterpreterTest.php @@ -73,7 +73,8 @@ class ResponseInterpreterTest extends \PHPUnit_Framework_TestCase /** * @group CNAME */ - public function testCNAME() { + public function testCNAME() + { $testPacket = "\x00\x00\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x04news\x03bbc\x02co\x02uk\x00\x00\x01\x00\x01\xc0\x0c\x00\x05\x00\x01\x00\x00\x01W\x00\x12\x07newswww\x03bbc\x03net\xc0\x18\xc0,\x00\x01\x00\x01\x00\x00\x00\xd2\x00\x04\xd4:\xf6P\xc0,\x00\x01\x00\x01\x00\x00\x00\xd2\x00\x04\xd4:\xf6Q"; @@ -91,16 +92,9 @@ class ResponseInterpreterTest extends \PHPUnit_Framework_TestCase $this->assertSame("212.58.246.80", $addr); $this->assertSame(210, $ttl); - - - $interpreted = $responseInterpreter->interpret($response, AddressModes::CNAME); - list($type, $addr, $ttl) = $interpreted; - $this->assertEquals(AddressModes::INET4_ADDR, $type); - $this->assertSame("newswww.bbc.net.uk", $addr); - $this->assertNull($ttl); - $interpreted = $responseInterpreter->interpret($response, AddressModes::INET6_ADDR); - list($type, $addr, $ttl) = $interpreted; + $this->markTestSkipped("I am unsure what the correct response should be."); + //list($type, $addr, $ttl) = $interpreted; //This looks borked - it's returning the CNAME but as the asserts above are going to fail // this won't be reached. //$type = 8 aka CNAME