DnsConnectorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace React\Tests\Socket;
  3. use React\Promise;
  4. use React\Promise\Deferred;
  5. use React\Socket\DnsConnector;
  6. class DnsConnectorTest extends TestCase
  7. {
  8. private $tcp;
  9. private $resolver;
  10. private $connector;
  11. /**
  12. * @before
  13. */
  14. public function setUpMocks()
  15. {
  16. $this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  17. $this->resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
  18. $this->connector = new DnsConnector($this->tcp, $this->resolver);
  19. }
  20. public function testPassByResolverIfGivenIp()
  21. {
  22. $this->resolver->expects($this->never())->method('resolve');
  23. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  24. $promise = $this->connector->connect('127.0.0.1:80');
  25. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  26. }
  27. public function testPassThroughResolverIfGivenHost()
  28. {
  29. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
  30. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  31. $promise = $this->connector->connect('google.com:80');
  32. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  33. }
  34. public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
  35. {
  36. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('::1')));
  37. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  38. $promise = $this->connector->connect('google.com:80');
  39. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  40. }
  41. public function testPassByResolverIfGivenCompleteUri()
  42. {
  43. $this->resolver->expects($this->never())->method('resolve');
  44. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  45. $promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
  46. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  47. }
  48. public function testPassThroughResolverIfGivenCompleteUri()
  49. {
  50. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
  51. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  52. $promise = $this->connector->connect('scheme://google.com:80/path?query#fragment');
  53. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  54. }
  55. public function testPassThroughResolverIfGivenExplicitHost()
  56. {
  57. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
  58. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  59. $promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de');
  60. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  61. }
  62. public function testRejectsImmediatelyIfUriIsInvalid()
  63. {
  64. $this->resolver->expects($this->never())->method('resolve');
  65. $this->tcp->expects($this->never())->method('connect');
  66. $promise = $this->connector->connect('////');
  67. $promise->then(null, $this->expectCallableOnceWithException(
  68. 'InvalidArgumentException',
  69. 'Given URI "////" is invalid (EINVAL)',
  70. defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
  71. ));
  72. }
  73. public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithRuntimeException()
  74. {
  75. $promise = Promise\reject(new \RuntimeException('Connection to tcp://1.2.3.4:80 failed: Connection failed', 42));
  76. $this->resolver->expects($this->never())->method('resolve');
  77. $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($promise);
  78. $promise = $this->connector->connect('1.2.3.4:80');
  79. $exception = null;
  80. $promise->then(null, function ($reason) use (&$exception) {
  81. $exception = $reason;
  82. });
  83. assert($exception instanceof \RuntimeException);
  84. $this->assertInstanceOf('RuntimeException', $exception);
  85. $this->assertEquals('Connection to tcp://1.2.3.4:80 failed: Connection failed', $exception->getMessage());
  86. $this->assertEquals(42, $exception->getCode());
  87. $this->assertNull($exception->getPrevious());
  88. $this->assertNotEquals('', $exception->getTraceAsString());
  89. }
  90. public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithInvalidArgumentException()
  91. {
  92. $promise = Promise\reject(new \InvalidArgumentException('Invalid', 42));
  93. $this->resolver->expects($this->never())->method('resolve');
  94. $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($promise);
  95. $promise = $this->connector->connect('1.2.3.4:80');
  96. $exception = null;
  97. $promise->then(null, function ($reason) use (&$exception) {
  98. $exception = $reason;
  99. });
  100. assert($exception instanceof \InvalidArgumentException);
  101. $this->assertInstanceOf('InvalidArgumentException', $exception);
  102. $this->assertEquals('Invalid', $exception->getMessage());
  103. $this->assertEquals(42, $exception->getCode());
  104. $this->assertNull($exception->getPrevious());
  105. $this->assertNotEquals('', $exception->getTraceAsString());
  106. }
  107. public function testConnectRejectsWithOriginalHostnameInMessageAfterResolvingIfTcpConnectorRejectsWithRuntimeException()
  108. {
  109. $promise = Promise\reject(new \RuntimeException('Connection to tcp://1.2.3.4:80?hostname=example.com failed: Connection failed', 42));
  110. $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\resolve('1.2.3.4'));
  111. $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($promise);
  112. $promise = $this->connector->connect('example.com:80');
  113. $exception = null;
  114. $promise->then(null, function ($reason) use (&$exception) {
  115. $exception = $reason;
  116. });
  117. assert($exception instanceof \RuntimeException);
  118. $this->assertInstanceOf('RuntimeException', $exception);
  119. $this->assertEquals('Connection to tcp://example.com:80 failed: Connection to tcp://1.2.3.4:80 failed: Connection failed', $exception->getMessage());
  120. $this->assertEquals(42, $exception->getCode());
  121. $this->assertInstanceOf('RuntimeException', $exception->getPrevious());
  122. $this->assertNotEquals('', $exception->getTraceAsString());
  123. }
  124. public function testConnectRejectsWithOriginalExceptionAfterResolvingIfTcpConnectorRejectsWithInvalidArgumentException()
  125. {
  126. $promise = Promise\reject(new \InvalidArgumentException('Invalid', 42));
  127. $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\resolve('1.2.3.4'));
  128. $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($promise);
  129. $promise = $this->connector->connect('example.com:80');
  130. $exception = null;
  131. $promise->then(null, function ($reason) use (&$exception) {
  132. $exception = $reason;
  133. });
  134. assert($exception instanceof \InvalidArgumentException);
  135. $this->assertInstanceOf('InvalidArgumentException', $exception);
  136. $this->assertEquals('Invalid', $exception->getMessage());
  137. $this->assertEquals(42, $exception->getCode());
  138. $this->assertNull($exception->getPrevious());
  139. $this->assertNotEquals('', $exception->getTraceAsString());
  140. }
  141. public function testSkipConnectionIfDnsFails()
  142. {
  143. $promise = Promise\reject(new \RuntimeException('DNS error'));
  144. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->willReturn($promise);
  145. $this->tcp->expects($this->never())->method('connect');
  146. $promise = $this->connector->connect('example.invalid:80');
  147. $exception = null;
  148. $promise->then(null, function ($reason) use (&$exception) {
  149. $exception = $reason;
  150. });
  151. assert($exception instanceof \RuntimeException);
  152. $this->assertInstanceOf('RuntimeException', $exception);
  153. $this->assertEquals('Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error', $exception->getMessage());
  154. $this->assertEquals(0, $exception->getCode());
  155. $this->assertInstanceOf('RuntimeException', $exception->getPrevious());
  156. $this->assertNotEquals('', $exception->getTraceAsString());
  157. }
  158. public function testRejectionExceptionUsesPreviousExceptionIfDnsFails()
  159. {
  160. $exception = new \RuntimeException();
  161. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->willReturn(Promise\reject($exception));
  162. $promise = $this->connector->connect('example.invalid:80');
  163. $promise->then(null, function ($e) {
  164. throw $e->getPrevious();
  165. })->then(null, $this->expectCallableOnceWith($this->identicalTo($exception)));
  166. }
  167. public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
  168. {
  169. $pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
  170. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->will($this->returnValue($pending));
  171. $this->tcp->expects($this->never())->method('connect');
  172. $promise = $this->connector->connect('example.com:80');
  173. $promise->cancel();
  174. $exception = null;
  175. $promise->then(null, function ($reason) use (&$exception) {
  176. $exception = $reason;
  177. });
  178. assert($exception instanceof \RuntimeException);
  179. $this->assertInstanceOf('RuntimeException', $exception);
  180. $this->assertEquals('Connection to tcp://example.com:80 cancelled during DNS lookup (ECONNABORTED)', $exception->getMessage());
  181. $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
  182. $this->assertNull($exception->getPrevious());
  183. $this->assertNotEquals('', $exception->getTraceAsString());
  184. }
  185. public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
  186. {
  187. $pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
  188. $this->resolver->expects($this->never())->method('resolve');
  189. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($pending);
  190. $promise = $this->connector->connect('1.2.3.4:80');
  191. $promise->cancel();
  192. }
  193. public function testCancelDuringTcpConnectionCancelsTcpConnectionAfterDnsIsResolved()
  194. {
  195. $pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
  196. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(Promise\resolve('1.2.3.4'));
  197. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending);
  198. $promise = $this->connector->connect('example.com:80');
  199. $promise->cancel();
  200. }
  201. public function testCancelDuringTcpConnectionCancelsTcpConnectionWithTcpRejectionAfterDnsIsResolved()
  202. {
  203. $first = new Deferred();
  204. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($first->promise());
  205. $pending = new Promise\Promise(function () { }, function () {
  206. throw new \RuntimeException(
  207. 'Connection cancelled',
  208. defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
  209. );
  210. });
  211. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending);
  212. $promise = $this->connector->connect('example.com:80');
  213. $first->resolve('1.2.3.4');
  214. $promise->cancel();
  215. $exception = null;
  216. $promise->then(null, function ($reason) use (&$exception) {
  217. $exception = $reason;
  218. });
  219. assert($exception instanceof \RuntimeException);
  220. $this->assertInstanceOf('RuntimeException', $exception);
  221. $this->assertEquals('Connection to tcp://example.com:80 failed: Connection cancelled', $exception->getMessage());
  222. $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
  223. $this->assertInstanceOf('RuntimeException', $exception->getPrevious());
  224. $this->assertNotEquals('', $exception->getTraceAsString());
  225. }
  226. public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences()
  227. {
  228. if (class_exists('React\Promise\When')) {
  229. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  230. }
  231. while (gc_collect_cycles()) {
  232. // collect all garbage cycles
  233. }
  234. $dns = new Deferred();
  235. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
  236. $this->tcp->expects($this->never())->method('connect');
  237. $promise = $this->connector->connect('example.com:80');
  238. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  239. $dns->reject(new \RuntimeException('DNS failed'));
  240. unset($promise, $dns);
  241. $this->assertEquals(0, gc_collect_cycles());
  242. }
  243. public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferences()
  244. {
  245. if (class_exists('React\Promise\When')) {
  246. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  247. }
  248. while (gc_collect_cycles()) {
  249. // collect all garbage cycles
  250. }
  251. $dns = new Deferred();
  252. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
  253. $tcp = new Deferred();
  254. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
  255. $promise = $this->connector->connect('example.com:80');
  256. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  257. $dns->resolve('1.2.3.4');
  258. $tcp->reject(new \RuntimeException('Connection failed'));
  259. unset($promise, $dns, $tcp);
  260. $this->assertEquals(0, gc_collect_cycles());
  261. }
  262. public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAgain()
  263. {
  264. if (class_exists('React\Promise\When')) {
  265. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  266. }
  267. while (gc_collect_cycles()) {
  268. // collect all garbage cycles
  269. }
  270. $dns = new Deferred();
  271. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
  272. $tcp = new Deferred();
  273. $dns->promise()->then(function () use ($tcp) {
  274. $tcp->reject(new \RuntimeException('Connection failed'));
  275. });
  276. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
  277. $promise = $this->connector->connect('example.com:80');
  278. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  279. $dns->resolve('1.2.3.4');
  280. unset($promise, $dns, $tcp);
  281. $this->assertEquals(0, gc_collect_cycles());
  282. }
  283. public function testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences()
  284. {
  285. if (class_exists('React\Promise\When')) {
  286. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  287. }
  288. while (gc_collect_cycles()) {
  289. // collect all garbage cycles
  290. }
  291. $dns = new Deferred(function () {
  292. throw new \RuntimeException();
  293. });
  294. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
  295. $this->tcp->expects($this->never())->method('connect');
  296. $promise = $this->connector->connect('example.com:80');
  297. $promise->cancel();
  298. unset($promise, $dns);
  299. $this->assertEquals(0, gc_collect_cycles());
  300. }
  301. public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences()
  302. {
  303. if (class_exists('React\Promise\When')) {
  304. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  305. }
  306. while (gc_collect_cycles()) {
  307. // collect all garbage cycles
  308. }
  309. $dns = new Deferred();
  310. $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
  311. $tcp = new Promise\Promise(function () { }, function () {
  312. throw new \RuntimeException('Connection cancelled');
  313. });
  314. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp);
  315. $promise = $this->connector->connect('example.com:80');
  316. $dns->resolve('1.2.3.4');
  317. $promise->cancel();
  318. unset($promise, $dns, $tcp);
  319. $this->assertEquals(0, gc_collect_cycles());
  320. }
  321. }