123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace React\Tests\Dns;
- use React\Dns\Resolver\Factory;
- use React\Dns\Model\Message;
- use React\EventLoop\Loop;
- class FunctionalResolverTest extends TestCase
- {
- private $resolver;
- /**
- * @before
- */
- public function setUpResolver()
- {
- $factory = new Factory();
- $this->resolver = $factory->create('8.8.8.8');
- }
- public function testResolveLocalhostResolves()
- {
- $promise = $this->resolver->resolve('localhost');
- $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
- Loop::run();
- }
- public function testResolveAllLocalhostResolvesWithArray()
- {
- $promise = $this->resolver->resolveAll('localhost', Message::TYPE_A);
- $promise->then($this->expectCallableOnceWith($this->isType('array')), $this->expectCallableNever());
- Loop::run();
- }
- /**
- * @group internet
- */
- public function testResolveGoogleResolves()
- {
- $promise = $this->resolver->resolve('google.com');
- $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
- Loop::run();
- }
- /**
- * @group internet
- */
- public function testResolveGoogleOverUdpResolves()
- {
- $factory = new Factory();
- $this->resolver = $factory->create('udp://8.8.8.8');
- $promise = $this->resolver->resolve('google.com');
- $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
- Loop::run();
- }
- /**
- * @group internet
- */
- public function testResolveGoogleOverTcpResolves()
- {
- $factory = new Factory();
- $this->resolver = $factory->create('tcp://8.8.8.8');
- $promise = $this->resolver->resolve('google.com');
- $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
- Loop::run();
- }
- /**
- * @group internet
- */
- public function testResolveAllGoogleMxResolvesWithCache()
- {
- $factory = new Factory();
- $this->resolver = $factory->createCached('8.8.8.8');
- $promise = $this->resolver->resolveAll('google.com', Message::TYPE_MX);
- $promise->then($this->expectCallableOnceWith($this->isType('array')), $this->expectCallableNever());
- Loop::run();
- }
- /**
- * @group internet
- */
- public function testResolveAllGoogleCaaResolvesWithCache()
- {
- $factory = new Factory();
- $this->resolver = $factory->createCached('8.8.8.8');
- $promise = $this->resolver->resolveAll('google.com', Message::TYPE_CAA);
- $promise->then($this->expectCallableOnceWith($this->isType('array')), $this->expectCallableNever());
- Loop::run();
- }
- /**
- * @group internet
- */
- public function testResolveInvalidRejects()
- {
- $promise = $this->resolver->resolve('example.invalid');
- Loop::run();
- $exception = null;
- $promise->then(null, function ($reason) use (&$exception) {
- $exception = $reason;
- });
- /** @var \React\Dns\RecordNotFoundException $exception */
- $this->assertInstanceOf('React\Dns\RecordNotFoundException', $exception);
- $this->assertEquals('DNS query for example.invalid (A) returned an error response (Non-Existent Domain / NXDOMAIN)', $exception->getMessage());
- $this->assertEquals(Message::RCODE_NAME_ERROR, $exception->getCode());
- }
- public function testResolveCancelledRejectsImmediately()
- {
- // max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
- ini_set('xdebug.max_nesting_level', 256);
- $promise = $this->resolver->resolve('google.com');
- $promise->cancel();
- $time = microtime(true);
- Loop::run();
- $time = microtime(true) - $time;
- $this->assertLessThan(0.1, $time);
- $exception = null;
- $promise->then(null, function ($reason) use (&$exception) {
- $exception = $reason;
- });
- /** @var \React\Dns\Query\CancellationException $exception */
- $this->assertInstanceOf('React\Dns\Query\CancellationException', $exception);
- $this->assertEquals('DNS query for google.com (A) has been cancelled', $exception->getMessage());
- }
- /**
- * @group internet
- */
- public function testResolveAllInvalidTypeRejects()
- {
- $promise = $this->resolver->resolveAll('google.com', Message::TYPE_PTR);
- Loop::run();
- $exception = null;
- $promise->then(null, function ($reason) use (&$exception) {
- $exception = $reason;
- });
- /** @var \React\Dns\RecordNotFoundException $exception */
- $this->assertInstanceOf('React\Dns\RecordNotFoundException', $exception);
- $this->assertEquals('DNS query for google.com (PTR) did not return a valid answer (NOERROR / NODATA)', $exception->getMessage());
- $this->assertEquals(0, $exception->getCode());
- }
- public function testInvalidResolverDoesNotResolveGoogle()
- {
- $factory = new Factory();
- $this->resolver = $factory->create('255.255.255.255');
- $promise = $this->resolver->resolve('google.com');
- $promise->then($this->expectCallableNever(), $this->expectCallableOnce());
- }
- public function testResolveShouldNotCauseGarbageReferencesWhenUsingInvalidNameserver()
- {
- if (class_exists('React\Promise\When')) {
- $this->markTestSkipped('Not supported on legacy Promise v1 API');
- }
- $factory = new Factory();
- $this->resolver = $factory->create('255.255.255.255');
- while (gc_collect_cycles()) {
- // collect all garbage cycles
- }
- $promise = $this->resolver->resolve('google.com');
- $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
- unset($promise);
- $this->assertEquals(0, gc_collect_cycles());
- }
- public function testResolveCachedShouldNotCauseGarbageReferencesWhenUsingInvalidNameserver()
- {
- if (class_exists('React\Promise\When')) {
- $this->markTestSkipped('Not supported on legacy Promise v1 API');
- }
- $factory = new Factory();
- $this->resolver = $factory->createCached('255.255.255.255');
- while (gc_collect_cycles()) {
- // collect all garbage cycles
- }
- $promise = $this->resolver->resolve('google.com');
- $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
- unset($promise);
- $this->assertEquals(0, gc_collect_cycles());
- }
- public function testCancelResolveShouldNotCauseGarbageReferences()
- {
- if (class_exists('React\Promise\When')) {
- $this->markTestSkipped('Not supported on legacy Promise v1 API');
- }
- $factory = new Factory();
- $this->resolver = $factory->create('127.0.0.1');
- while (gc_collect_cycles()) {
- // collect all garbage cycles
- }
- $promise = $this->resolver->resolve('google.com');
- $promise->cancel();
- $promise = null;
- $this->assertEquals(0, gc_collect_cycles());
- }
- public function testCancelResolveCachedShouldNotCauseGarbageReferences()
- {
- if (class_exists('React\Promise\When')) {
- $this->markTestSkipped('Not supported on legacy Promise v1 API');
- }
- $factory = new Factory();
- $this->resolver = $factory->createCached('127.0.0.1');
- while (gc_collect_cycles()) {
- // collect all garbage cycles
- }
- $promise = $this->resolver->resolve('google.com');
- $promise->cancel();
- $promise = null;
- $this->assertEquals(0, gc_collect_cycles());
- }
- }
|