SecureConnectorTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace React\Tests\Socket;
  3. use React\Promise;
  4. use React\Promise\Deferred;
  5. use React\Socket\SecureConnector;
  6. class SecureConnectorTest extends TestCase
  7. {
  8. private $loop;
  9. private $tcp;
  10. private $connector;
  11. /**
  12. * @before
  13. */
  14. public function setUpConnector()
  15. {
  16. if (defined('HHVM_VERSION')) {
  17. $this->markTestSkipped('Not supported on legacy HHVM');
  18. }
  19. $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  20. $this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  21. $this->connector = new SecureConnector($this->tcp, $this->loop);
  22. }
  23. public function testCtorThrowsForInvalidLoop()
  24. {
  25. $this->setExpectedException('InvalidArgumentException', 'Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
  26. new SecureConnector($this->tcp, 'loop');
  27. }
  28. public function testConstructWithoutLoopAssignsLoopAutomatically()
  29. {
  30. $connector = new SecureConnector($this->tcp);
  31. $ref = new \ReflectionProperty($connector, 'streamEncryption');
  32. $ref->setAccessible(true);
  33. $streamEncryption = $ref->getValue($connector);
  34. $ref = new \ReflectionProperty($streamEncryption, 'loop');
  35. $ref->setAccessible(true);
  36. $loop = $ref->getValue($streamEncryption);
  37. $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
  38. }
  39. public function testConnectionWillWaitForTcpConnection()
  40. {
  41. $pending = new Promise\Promise(function () { });
  42. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->will($this->returnValue($pending));
  43. $promise = $this->connector->connect('example.com:80');
  44. $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
  45. }
  46. public function testConnectionWithCompleteUriWillBePassedThroughExpectForScheme()
  47. {
  48. $pending = new Promise\Promise(function () { });
  49. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80/path?query#fragment'))->will($this->returnValue($pending));
  50. $this->connector->connect('tls://example.com:80/path?query#fragment');
  51. }
  52. public function testConnectionToInvalidSchemeWillReject()
  53. {
  54. $this->tcp->expects($this->never())->method('connect');
  55. $promise = $this->connector->connect('tcp://example.com:80');
  56. $promise->then(null, $this->expectCallableOnceWithException(
  57. 'InvalidArgumentException',
  58. 'Given URI "tcp://example.com:80" is invalid (EINVAL)',
  59. defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
  60. ));
  61. }
  62. public function testConnectWillRejectWithTlsUriWhenUnderlyingConnectorRejects()
  63. {
  64. $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn(\React\Promise\reject(new \RuntimeException(
  65. 'Connection to tcp://example.com:80 failed: Connection refused (ECONNREFUSED)',
  66. defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111
  67. )));
  68. $promise = $this->connector->connect('example.com:80');
  69. $exception = null;
  70. $promise->then(null, function ($reason) use (&$exception) {
  71. $exception = $reason;
  72. });
  73. assert($exception instanceof \RuntimeException);
  74. $this->assertInstanceOf('RuntimeException', $exception);
  75. $this->assertEquals('Connection to tls://example.com:80 failed: Connection refused (ECONNREFUSED)', $exception->getMessage());
  76. $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode());
  77. $this->assertInstanceOf('RuntimeException', $exception->getPrevious());
  78. $this->assertNotEquals('', $exception->getTraceAsString());
  79. }
  80. public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorRejectsWithInvalidArgumentException()
  81. {
  82. $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn(\React\Promise\reject(new \InvalidArgumentException(
  83. 'Invalid',
  84. 42
  85. )));
  86. $promise = $this->connector->connect('example.com:80');
  87. $exception = null;
  88. $promise->then(null, function ($reason) use (&$exception) {
  89. $exception = $reason;
  90. });
  91. assert($exception instanceof \InvalidArgumentException);
  92. $this->assertInstanceOf('InvalidArgumentException', $exception);
  93. $this->assertEquals('Invalid', $exception->getMessage());
  94. $this->assertEquals(42, $exception->getCode());
  95. $this->assertNull($exception->getPrevious());
  96. $this->assertNotEquals('', $exception->getTraceAsString());
  97. }
  98. public function testCancelDuringTcpConnectionCancelsTcpConnection()
  99. {
  100. $pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
  101. $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn($pending);
  102. $promise = $this->connector->connect('example.com:80');
  103. $promise->cancel();
  104. }
  105. public function testCancelDuringTcpConnectionCancelsTcpConnectionAndRejectsWithTcpRejection()
  106. {
  107. $pending = new Promise\Promise(function () { }, function () { throw new \RuntimeException(
  108. 'Connection to tcp://example.com:80 cancelled (ECONNABORTED)',
  109. defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
  110. ); });
  111. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->will($this->returnValue($pending));
  112. $promise = $this->connector->connect('example.com:80');
  113. $promise->cancel();
  114. $exception = null;
  115. $promise->then(null, function ($reason) use (&$exception) {
  116. $exception = $reason;
  117. });
  118. assert($exception instanceof \RuntimeException);
  119. $this->assertInstanceOf('RuntimeException', $exception);
  120. $this->assertEquals('Connection to tls://example.com:80 cancelled (ECONNABORTED)', $exception->getMessage());
  121. $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
  122. $this->assertInstanceOf('RuntimeException', $exception->getPrevious());
  123. $this->assertNotEquals('', $exception->getTraceAsString());
  124. }
  125. public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream()
  126. {
  127. $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
  128. $connection->expects($this->once())->method('close');
  129. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection));
  130. $promise = $this->connector->connect('example.com:80');
  131. $exception = null;
  132. $promise->then(null, function ($reason) use (&$exception) {
  133. $exception = $reason;
  134. });
  135. assert($exception instanceof \UnexpectedValueException);
  136. $this->assertInstanceOf('UnexpectedValueException', $exception);
  137. $this->assertEquals('Base connector does not use internal Connection class exposing stream resource', $exception->getMessage());
  138. $this->assertEquals(0, $exception->getCode());
  139. $this->assertNull($exception->getPrevious());
  140. $this->assertNotEquals('', $exception->getTraceAsString());
  141. }
  142. public function testStreamEncryptionWillBeEnabledAfterConnecting()
  143. {
  144. $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
  145. $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock();
  146. $encryption->expects($this->once())->method('enable')->with($connection)->willReturn(new \React\Promise\Promise(function () { }));
  147. $ref = new \ReflectionProperty($this->connector, 'streamEncryption');
  148. $ref->setAccessible(true);
  149. $ref->setValue($this->connector, $encryption);
  150. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection));
  151. $this->connector->connect('example.com:80');
  152. }
  153. public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConnection()
  154. {
  155. $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
  156. $connection->expects($this->once())->method('close');
  157. $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock();
  158. $encryption->expects($this->once())->method('enable')->willReturn(Promise\reject(new \RuntimeException('TLS error', 123)));
  159. $ref = new \ReflectionProperty($this->connector, 'streamEncryption');
  160. $ref->setAccessible(true);
  161. $ref->setValue($this->connector, $encryption);
  162. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection));
  163. $promise = $this->connector->connect('example.com:80');
  164. $exception = null;
  165. $promise->then(null, function ($reason) use (&$exception) {
  166. $exception = $reason;
  167. });
  168. assert($exception instanceof \RuntimeException);
  169. $this->assertInstanceOf('RuntimeException', $exception);
  170. $this->assertEquals('Connection to tls://example.com:80 failed during TLS handshake: TLS error', $exception->getMessage());
  171. $this->assertEquals(123, $exception->getCode());
  172. $this->assertNull($exception->getPrevious());
  173. $this->assertNotEquals('', $exception->getTraceAsString());
  174. }
  175. public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnection()
  176. {
  177. $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
  178. $connection->expects($this->once())->method('close');
  179. $pending = new Promise\Promise(function () { }, function () {
  180. throw new \Exception('Ignored');
  181. });
  182. $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock();
  183. $encryption->expects($this->once())->method('enable')->willReturn($pending);
  184. $ref = new \ReflectionProperty($this->connector, 'streamEncryption');
  185. $ref->setAccessible(true);
  186. $ref->setValue($this->connector, $encryption);
  187. $deferred = new Deferred();
  188. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn($deferred->promise());
  189. $promise = $this->connector->connect('example.com:80');
  190. $deferred->resolve($connection);
  191. $promise->cancel();
  192. $exception = null;
  193. $promise->then(null, function ($reason) use (&$exception) {
  194. $exception = $reason;
  195. });
  196. assert($exception instanceof \RuntimeException);
  197. $this->assertInstanceOf('RuntimeException', $exception);
  198. $this->assertEquals('Connection to tls://example.com:80 cancelled during TLS handshake (ECONNABORTED)', $exception->getMessage());
  199. $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
  200. $this->assertNull($exception->getPrevious());
  201. $this->assertNotEquals('', $exception->getTraceAsString());
  202. }
  203. public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences()
  204. {
  205. if (class_exists('React\Promise\When')) {
  206. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  207. }
  208. while (gc_collect_cycles()) {
  209. // collect all garbage cycles
  210. }
  211. $tcp = new Deferred();
  212. $this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise());
  213. $promise = $this->connector->connect('example.com:80');
  214. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  215. $tcp->reject(new \RuntimeException());
  216. unset($promise, $tcp);
  217. $this->assertEquals(0, gc_collect_cycles());
  218. }
  219. public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferences()
  220. {
  221. if (class_exists('React\Promise\When')) {
  222. $this->markTestSkipped('Not supported on legacy Promise v1 API');
  223. }
  224. while (gc_collect_cycles()) {
  225. // collect all garbage cycles
  226. }
  227. $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
  228. $tcp = new Deferred();
  229. $this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise());
  230. $tls = new Deferred();
  231. $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock();
  232. $encryption->expects($this->once())->method('enable')->willReturn($tls->promise());
  233. $ref = new \ReflectionProperty($this->connector, 'streamEncryption');
  234. $ref->setAccessible(true);
  235. $ref->setValue($this->connector, $encryption);
  236. $promise = $this->connector->connect('example.com:80');
  237. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  238. $tcp->resolve($connection);
  239. $tls->reject(new \RuntimeException());
  240. unset($promise, $tcp, $tls);
  241. $this->assertEquals(0, gc_collect_cycles());
  242. }
  243. }