SecureConnectorTest.php 13 KB

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