123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- <?php
- namespace React\Tests\Dns\Resolver;
- use React\Dns\Config\Config;
- use React\Dns\Query\HostsFileExecutor;
- use React\Dns\Resolver\Factory;
- use React\Tests\Dns\TestCase;
- class FactoryTest extends TestCase
- {
- /** @test */
- public function createShouldCreateResolver()
- {
- $factory = new Factory();
- $resolver = $factory->create('8.8.8.8:53');
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- }
- /** @test */
- public function createWithoutSchemeShouldCreateResolverWithSelectiveUdpAndTcpExecutorStack()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $factory = new Factory();
- $resolver = $factory->create('8.8.8.8:53', $loop);
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $coopExecutor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor);
- $ref = new \ReflectionProperty($coopExecutor, 'executor');
- $ref->setAccessible(true);
- $retryExecutor = $ref->getValue($coopExecutor);
- $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor);
- $ref = new \ReflectionProperty($retryExecutor, 'executor');
- $ref->setAccessible(true);
- $selectiveExecutor = $ref->getValue($retryExecutor);
- $this->assertInstanceOf('React\Dns\Query\SelectiveTransportExecutor', $selectiveExecutor);
- // udp below:
- $ref = new \ReflectionProperty($selectiveExecutor, 'datagramExecutor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($selectiveExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $udpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\UdpTransportExecutor', $udpExecutor);
- // tcp below:
- $ref = new \ReflectionProperty($selectiveExecutor, 'streamExecutor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($selectiveExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- }
- /** @test */
- public function createWithUdpSchemeShouldCreateResolverWithUdpExecutorStack()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $factory = new Factory();
- $resolver = $factory->create('udp://8.8.8.8:53', $loop);
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $coopExecutor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor);
- $ref = new \ReflectionProperty($coopExecutor, 'executor');
- $ref->setAccessible(true);
- $retryExecutor = $ref->getValue($coopExecutor);
- $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor);
- $ref = new \ReflectionProperty($retryExecutor, 'executor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($retryExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $udpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\UdpTransportExecutor', $udpExecutor);
- }
- /** @test */
- public function createWithTcpSchemeShouldCreateResolverWithTcpExecutorStack()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $factory = new Factory();
- $resolver = $factory->create('tcp://8.8.8.8:53', $loop);
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $coopExecutor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor);
- $ref = new \ReflectionProperty($coopExecutor, 'executor');
- $ref->setAccessible(true);
- $retryExecutor = $ref->getValue($coopExecutor);
- $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor);
- $ref = new \ReflectionProperty($retryExecutor, 'executor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($retryExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- }
- /** @test */
- public function createWithConfigWithTcpNameserverSchemeShouldCreateResolverWithTcpExecutorStack()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $config = new Config();
- $config->nameservers[] = 'tcp://8.8.8.8:53';
- $factory = new Factory();
- $resolver = $factory->create($config, $loop);
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $coopExecutor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor);
- $ref = new \ReflectionProperty($coopExecutor, 'executor');
- $ref->setAccessible(true);
- $retryExecutor = $ref->getValue($coopExecutor);
- $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor);
- $ref = new \ReflectionProperty($retryExecutor, 'executor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($retryExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- }
- /** @test */
- public function createWithConfigWithTwoNameserversWithTcpSchemeShouldCreateResolverWithFallbackExecutorStack()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $config = new Config();
- $config->nameservers[] = 'tcp://8.8.8.8:53';
- $config->nameservers[] = 'tcp://1.1.1.1:53';
- $factory = new Factory();
- $resolver = $factory->create($config, $loop);
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $coopExecutor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor);
- $ref = new \ReflectionProperty($coopExecutor, 'executor');
- $ref->setAccessible(true);
- $retryExecutor = $ref->getValue($coopExecutor);
- $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor);
- $ref = new \ReflectionProperty($retryExecutor, 'executor');
- $ref->setAccessible(true);
- $fallbackExecutor = $ref->getValue($retryExecutor);
- $this->assertInstanceOf('React\Dns\Query\FallbackExecutor', $fallbackExecutor);
- $ref = new \ReflectionProperty($fallbackExecutor, 'executor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($fallbackExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- $ref = new \ReflectionProperty($tcpExecutor, 'nameserver');
- $ref->setAccessible(true);
- $nameserver = $ref->getValue($tcpExecutor);
- $this->assertEquals('tcp://8.8.8.8:53', $nameserver);
- $ref = new \ReflectionProperty($fallbackExecutor, 'fallback');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($fallbackExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- $ref = new \ReflectionProperty($tcpExecutor, 'nameserver');
- $ref->setAccessible(true);
- $nameserver = $ref->getValue($tcpExecutor);
- $this->assertEquals('tcp://1.1.1.1:53', $nameserver);
- }
- /** @test */
- public function createWithConfigWithThreeNameserversWithTcpSchemeShouldCreateResolverWithNestedFallbackExecutorStack()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $config = new Config();
- $config->nameservers[] = 'tcp://8.8.8.8:53';
- $config->nameservers[] = 'tcp://1.1.1.1:53';
- $config->nameservers[] = 'tcp://9.9.9.9:53';
- $factory = new Factory();
- $resolver = $factory->create($config, $loop);
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $coopExecutor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CoopExecutor', $coopExecutor);
- $ref = new \ReflectionProperty($coopExecutor, 'executor');
- $ref->setAccessible(true);
- $retryExecutor = $ref->getValue($coopExecutor);
- $this->assertInstanceOf('React\Dns\Query\RetryExecutor', $retryExecutor);
- $ref = new \ReflectionProperty($retryExecutor, 'executor');
- $ref->setAccessible(true);
- $fallbackExecutor = $ref->getValue($retryExecutor);
- $this->assertInstanceOf('React\Dns\Query\FallbackExecutor', $fallbackExecutor);
- $ref = new \ReflectionProperty($fallbackExecutor, 'executor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($fallbackExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- $ref = new \ReflectionProperty($tcpExecutor, 'nameserver');
- $ref->setAccessible(true);
- $nameserver = $ref->getValue($tcpExecutor);
- $this->assertEquals('tcp://8.8.8.8:53', $nameserver);
- $ref = new \ReflectionProperty($fallbackExecutor, 'fallback');
- $ref->setAccessible(true);
- $fallbackExecutor = $ref->getValue($fallbackExecutor);
- $this->assertInstanceOf('React\Dns\Query\FallbackExecutor', $fallbackExecutor);
- $ref = new \ReflectionProperty($fallbackExecutor, 'executor');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($fallbackExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- $ref = new \ReflectionProperty($tcpExecutor, 'nameserver');
- $ref->setAccessible(true);
- $nameserver = $ref->getValue($tcpExecutor);
- $this->assertEquals('tcp://1.1.1.1:53', $nameserver);
- $ref = new \ReflectionProperty($fallbackExecutor, 'fallback');
- $ref->setAccessible(true);
- $timeoutExecutor = $ref->getValue($fallbackExecutor);
- $this->assertInstanceOf('React\Dns\Query\TimeoutExecutor', $timeoutExecutor);
- $ref = new \ReflectionProperty($timeoutExecutor, 'executor');
- $ref->setAccessible(true);
- $tcpExecutor = $ref->getValue($timeoutExecutor);
- $this->assertInstanceOf('React\Dns\Query\TcpTransportExecutor', $tcpExecutor);
- $ref = new \ReflectionProperty($tcpExecutor, 'nameserver');
- $ref->setAccessible(true);
- $nameserver = $ref->getValue($tcpExecutor);
- $this->assertEquals('tcp://9.9.9.9:53', $nameserver);
- }
- /** @test */
- public function createShouldThrowWhenNameserverIsInvalid()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $factory = new Factory();
- $this->setExpectedException('InvalidArgumentException');
- $factory->create('///', $loop);
- }
- /** @test */
- public function createShouldThrowWhenConfigHasNoNameservers()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $factory = new Factory();
- $this->setExpectedException('UnderflowException');
- $factory->create(new Config(), $loop);
- }
- /** @test */
- public function createShouldThrowWhenConfigHasInvalidNameserver()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $factory = new Factory();
- $config = new Config();
- $config->nameservers[] = '///';
- $this->setExpectedException('InvalidArgumentException');
- $factory->create($config, $loop);
- }
- /** @test */
- public function createCachedShouldCreateResolverWithCachingExecutor()
- {
- $factory = new Factory();
- $resolver = $factory->createCached('8.8.8.8:53');
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $executor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CachingExecutor', $executor);
- $cache = $this->getCachingExecutorPrivateMemberValue($executor, 'cache');
- $this->assertInstanceOf('React\Cache\ArrayCache', $cache);
- }
- /** @test */
- public function createCachedShouldCreateResolverWithCachingExecutorWithCustomCache()
- {
- $cache = $this->getMockBuilder('React\Cache\CacheInterface')->getMock();
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $factory = new Factory();
- $resolver = $factory->createCached('8.8.8.8:53', $loop, $cache);
- $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
- $executor = $this->getResolverPrivateExecutor($resolver);
- $this->assertInstanceOf('React\Dns\Query\CachingExecutor', $executor);
- $cacheProperty = $this->getCachingExecutorPrivateMemberValue($executor, 'cache');
- $this->assertSame($cache, $cacheProperty);
- }
- private function getResolverPrivateExecutor($resolver)
- {
- $executor = $this->getResolverPrivateMemberValue($resolver, 'executor');
- // extract underlying executor that may be wrapped in multiple layers of hosts file executors
- while ($executor instanceof HostsFileExecutor) {
- $reflector = new \ReflectionProperty('React\Dns\Query\HostsFileExecutor', 'fallback');
- $reflector->setAccessible(true);
- $executor = $reflector->getValue($executor);
- }
- return $executor;
- }
- private function getResolverPrivateMemberValue($resolver, $field)
- {
- $reflector = new \ReflectionProperty('React\Dns\Resolver\Resolver', $field);
- $reflector->setAccessible(true);
- return $reflector->getValue($resolver);
- }
- private function getCachingExecutorPrivateMemberValue($resolver, $field)
- {
- $reflector = new \ReflectionProperty('React\Dns\Query\CachingExecutor', $field);
- $reflector->setAccessible(true);
- return $reflector->getValue($resolver);
- }
- }
|