FunctionalResolverTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace React\Tests\Dns;
  3. use React\Dns\Resolver\Factory;
  4. use React\Dns\Model\Message;
  5. use React\EventLoop\Loop;
  6. class FunctionalResolverTest extends TestCase
  7. {
  8. private $resolver;
  9. /**
  10. * @before
  11. */
  12. public function setUpResolver()
  13. {
  14. $factory = new Factory();
  15. $this->resolver = $factory->create('8.8.8.8');
  16. }
  17. public function testResolveLocalhostResolves()
  18. {
  19. $promise = $this->resolver->resolve('localhost');
  20. $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
  21. Loop::run();
  22. }
  23. public function testResolveAllLocalhostResolvesWithArray()
  24. {
  25. $promise = $this->resolver->resolveAll('localhost', Message::TYPE_A);
  26. $promise->then($this->expectCallableOnceWith($this->isType('array')), $this->expectCallableNever());
  27. Loop::run();
  28. }
  29. /**
  30. * @group internet
  31. */
  32. public function testResolveGoogleResolves()
  33. {
  34. $promise = $this->resolver->resolve('google.com');
  35. $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
  36. Loop::run();
  37. }
  38. /**
  39. * @group internet
  40. */
  41. public function testResolveGoogleOverUdpResolves()
  42. {
  43. $factory = new Factory();
  44. $this->resolver = $factory->create('udp://8.8.8.8');
  45. $promise = $this->resolver->resolve('google.com');
  46. $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
  47. Loop::run();
  48. }
  49. /**
  50. * @group internet
  51. */
  52. public function testResolveGoogleOverTcpResolves()
  53. {
  54. $factory = new Factory();
  55. $this->resolver = $factory->create('tcp://8.8.8.8');
  56. $promise = $this->resolver->resolve('google.com');
  57. $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
  58. Loop::run();
  59. }
  60. /**
  61. * @group internet
  62. */
  63. public function testResolveAllGoogleMxResolvesWithCache()
  64. {
  65. $factory = new Factory();
  66. $this->resolver = $factory->createCached('8.8.8.8');
  67. $promise = $this->resolver->resolveAll('google.com', Message::TYPE_MX);
  68. $promise->then($this->expectCallableOnceWith($this->isType('array')), $this->expectCallableNever());
  69. Loop::run();
  70. }
  71. /**
  72. * @group internet
  73. */
  74. public function testResolveAllGoogleCaaResolvesWithCache()
  75. {
  76. $factory = new Factory();
  77. $this->resolver = $factory->createCached('8.8.8.8');
  78. $promise = $this->resolver->resolveAll('google.com', Message::TYPE_CAA);
  79. $promise->then($this->expectCallableOnceWith($this->isType('array')), $this->expectCallableNever());
  80. Loop::run();
  81. }
  82. /**
  83. * @group internet
  84. */
  85. public function testResolveInvalidRejects()
  86. {
  87. $promise = $this->resolver->resolve('example.invalid');
  88. Loop::run();
  89. $exception = null;
  90. $promise->then(null, function ($reason) use (&$exception) {
  91. $exception = $reason;
  92. });
  93. /** @var \React\Dns\RecordNotFoundException $exception */
  94. $this->assertInstanceOf('React\Dns\RecordNotFoundException', $exception);
  95. $this->assertEquals('DNS query for example.invalid (A) returned an error response (Non-Existent Domain / NXDOMAIN)', $exception->getMessage());
  96. $this->assertEquals(Message::RCODE_NAME_ERROR, $exception->getCode());
  97. }
  98. public function testResolveCancelledRejectsImmediately()
  99. {
  100. // max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
  101. ini_set('xdebug.max_nesting_level', 256);
  102. $promise = $this->resolver->resolve('google.com');
  103. $promise->cancel();
  104. $time = microtime(true);
  105. Loop::run();
  106. $time = microtime(true) - $time;
  107. $this->assertLessThan(0.1, $time);
  108. $exception = null;
  109. $promise->then(null, function ($reason) use (&$exception) {
  110. $exception = $reason;
  111. });
  112. /** @var \React\Dns\Query\CancellationException $exception */
  113. $this->assertInstanceOf('React\Dns\Query\CancellationException', $exception);
  114. $this->assertEquals('DNS query for google.com (A) has been cancelled', $exception->getMessage());
  115. }
  116. /**
  117. * @group internet
  118. */
  119. public function testResolveAllInvalidTypeRejects()
  120. {
  121. $promise = $this->resolver->resolveAll('google.com', Message::TYPE_PTR);
  122. Loop::run();
  123. $exception = null;
  124. $promise->then(null, function ($reason) use (&$exception) {
  125. $exception = $reason;
  126. });
  127. /** @var \React\Dns\RecordNotFoundException $exception */
  128. $this->assertInstanceOf('React\Dns\RecordNotFoundException', $exception);
  129. $this->assertEquals('DNS query for google.com (PTR) did not return a valid answer (NOERROR / NODATA)', $exception->getMessage());
  130. $this->assertEquals(0, $exception->getCode());
  131. }
  132. public function testInvalidResolverDoesNotResolveGoogle()
  133. {
  134. $factory = new Factory();
  135. $this->resolver = $factory->create('255.255.255.255');
  136. $promise = $this->resolver->resolve('google.com');
  137. $promise->then($this->expectCallableNever(), $this->expectCallableOnce());
  138. }
  139. public function testResolveShouldNotCauseGarbageReferencesWhenUsingInvalidNameserver()
  140. {
  141. if (class_exists('React\Promise\When')) {
  142. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  143. }
  144. $factory = new Factory();
  145. $this->resolver = $factory->create('255.255.255.255');
  146. while (gc_collect_cycles()) {
  147. // collect all garbage cycles
  148. }
  149. $promise = $this->resolver->resolve('google.com');
  150. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  151. unset($promise);
  152. $this->assertEquals(0, gc_collect_cycles());
  153. }
  154. public function testResolveCachedShouldNotCauseGarbageReferencesWhenUsingInvalidNameserver()
  155. {
  156. if (class_exists('React\Promise\When')) {
  157. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  158. }
  159. $factory = new Factory();
  160. $this->resolver = $factory->createCached('255.255.255.255');
  161. while (gc_collect_cycles()) {
  162. // collect all garbage cycles
  163. }
  164. $promise = $this->resolver->resolve('google.com');
  165. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  166. unset($promise);
  167. $this->assertEquals(0, gc_collect_cycles());
  168. }
  169. public function testCancelResolveShouldNotCauseGarbageReferences()
  170. {
  171. if (class_exists('React\Promise\When')) {
  172. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  173. }
  174. $factory = new Factory();
  175. $this->resolver = $factory->create('127.0.0.1');
  176. while (gc_collect_cycles()) {
  177. // collect all garbage cycles
  178. }
  179. $promise = $this->resolver->resolve('google.com');
  180. $promise->cancel();
  181. $promise = null;
  182. $this->assertEquals(0, gc_collect_cycles());
  183. }
  184. public function testCancelResolveCachedShouldNotCauseGarbageReferences()
  185. {
  186. if (class_exists('React\Promise\When')) {
  187. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  188. }
  189. $factory = new Factory();
  190. $this->resolver = $factory->createCached('127.0.0.1');
  191. while (gc_collect_cycles()) {
  192. // collect all garbage cycles
  193. }
  194. $promise = $this->resolver->resolve('google.com');
  195. $promise->cancel();
  196. $promise = null;
  197. $this->assertEquals(0, gc_collect_cycles());
  198. }
  199. }