ConnectorFactoryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. use LeProxy\LeProxy\ConnectorFactory;
  3. class ConnectorFactoryTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function testCoerceProxyUri()
  6. {
  7. $uris = array(
  8. 'host' => 'http://host:8080',
  9. 'host:1234' => 'http://host:1234',
  10. 'socks://host' => 'socks4://host:8080',
  11. 'socks://user:pass@host' => 'socks5://user:pass@host:8080',
  12. 'user@host' => 'http://user:@host:8080',
  13. 'socks4a://10.20.30.40:5060' => 'socks4://10.20.30.40:5060',
  14. './proxy.sock' => 'http+unix://./proxy.sock',
  15. '/tmp/proxy.sock' => 'http+unix:///tmp/proxy.sock',
  16. 'user:pass@./proxy.sock' => 'http+unix://user:pass@./proxy.sock',
  17. 'http+unix://./proxy.sock' => 'http+unix://./proxy.sock',
  18. 'http+unix:///tmp/proxy.sock' => 'http+unix:///tmp/proxy.sock',
  19. 'http+unix://user:pass@./proxy.sock' => 'http+unix://user:pass@./proxy.sock',
  20. 'http+unix://user@./proxy.sock' => 'http+unix://user@./proxy.sock',
  21. 'socks+unix://./proxy.sock' => 'socks+unix://./proxy.sock',
  22. 'socks+unix://user:pass@./proxy.sock' => 'socks5+unix://user:pass@./proxy.sock',
  23. 'socks5+unix://user:pass@./proxy.sock' => 'socks5+unix://user:pass@./proxy.sock',
  24. );
  25. foreach ($uris as $in => $out) {
  26. $this->assertEquals($out, ConnectorFactory::coerceProxyUri($in));
  27. }
  28. }
  29. public function testCoerceProxyUriInvalidThrows()
  30. {
  31. $uris = array(
  32. 'empty' => '',
  33. 'invalid scheme' => 'tcp://test',
  34. 'invalid port' => 'host:port',
  35. 'auth for invalid scheme' => 'socks4://user@host',
  36. 'excessive path' => 'host/root',
  37. 'excessive query' => 'host?query',
  38. 'excessive fragment' => 'host#fragment',
  39. 'excessive dots' => '.../server.sock',
  40. 'auth for invalid socks4+unix' => 'socks4+unix://user:pass@./proxy.sock'
  41. );
  42. foreach ($uris as $uri) {
  43. try {
  44. ConnectorFactory::coerceProxyUri($uri);
  45. $this->fail($uri);
  46. } catch (InvalidArgumentException $e) {
  47. $this->assertTrue(true);
  48. }
  49. }
  50. }
  51. public function testCoerceListenUri()
  52. {
  53. $uris = array(
  54. '' => '0.0.0.0:8080',
  55. '127.0.0.1:1234' => '127.0.0.1:1234',
  56. '127.0.0.1' => '127.0.0.1:8080',
  57. '127.0.0.1:0' => '127.0.0.1:0',
  58. ':1234' => '0.0.0.0:1234',
  59. ':0' => '0.0.0.0:0',
  60. 'user:pass@0.0.0.0:8080' => 'user:pass@0.0.0.0:8080',
  61. 'user:pass@127.0.0.1' => 'user:pass@127.0.0.1:8080',
  62. 'user:pass@:1234' => 'user:pass@0.0.0.0:1234',
  63. '12:34@:45' => '12:34@0.0.0.0:45',
  64. 'user:pass@' => 'user:pass@0.0.0.0:8080',
  65. '[::1]' => '[::1]:8080',
  66. 'user:pass@[::1]' => 'user:pass@[::1]:8080',
  67. './proxy.sock' => './proxy.sock',
  68. '../proxy.sock' => '../proxy.sock',
  69. '/tmp/proxy.sock' => '/tmp/proxy.sock',
  70. 'user:pass@./proxy.sock' => 'user:pass@./proxy.sock',
  71. 'user:pass@/tmp/proxy.sock' => 'user:pass@/tmp/proxy.sock',
  72. );
  73. foreach ($uris as $in => $out) {
  74. $this->assertEquals($out, ConnectorFactory::coerceListenUri($in));
  75. }
  76. }
  77. public function testCoerceListenUriInvalidThrows()
  78. {
  79. $uris = array(
  80. 'invalid port' => '127.0.0.1:port',
  81. 'hostname' => 'localhost:8080',
  82. 'wildcard hostname' => '*:8080',
  83. 'excessive scheme' => 'http://127.0.0.1:8080',
  84. 'excessive path' => '127.0.0.1:8080/root',
  85. 'excessive query' => '127.0.0.1:8080?query',
  86. 'excessive fragment' => '127.0.0.1:8080#fragment',
  87. 'excessive dots' => '.../proxy.sock',
  88. 'path looks like hostname' => 'proxy.sock',
  89. );
  90. foreach ($uris as $uri) {
  91. try {
  92. ConnectorFactory::coerceListenUri($uri);
  93. $this->fail();
  94. } catch (InvalidArgumentException $e) {
  95. $this->assertTrue(true);
  96. }
  97. }
  98. }
  99. public function testCoerceBlockUri()
  100. {
  101. $uris = array(
  102. 'hostname' => 'hostname',
  103. '127.0.0.1:1234' => '127.0.0.1:1234',
  104. '127.0.0.1' => '127.0.0.1',
  105. '*:1234' => '*:1234',
  106. ':1234' => '*:1234',
  107. );
  108. foreach ($uris as $in => $out) {
  109. $this->assertEquals($out, ConnectorFactory::coerceBlockUri($in));
  110. }
  111. }
  112. public function testCoerceBlockUriInvalidThrows()
  113. {
  114. $uris = array(
  115. 'invalid port' => '127.0.0.1:port',
  116. 'excessive scheme' => 'http://127.0.0.1:8080',
  117. 'excessive path' => '127.0.0.1:8080/root',
  118. 'excessive query' => '127.0.0.1:8080?query',
  119. 'excessive fragment' => '127.0.0.1:8080#fragment',
  120. );
  121. foreach ($uris as $uri) {
  122. try {
  123. ConnectorFactory::coerceBlockUri($uri);
  124. $this->fail();
  125. } catch (InvalidArgumentException $e) {
  126. $this->assertTrue(true);
  127. }
  128. }
  129. }
  130. public function testIsIpLocal()
  131. {
  132. $ips = array(
  133. '127.0.0.1' => true,
  134. '127.1.2.3' => true,
  135. '192.168.1.1' => false,
  136. '8.8.8.8' => false,
  137. '::ffff:127.0.0.1' => true,
  138. '::1' => true,
  139. '::2' => false
  140. );
  141. foreach ($ips as $ip => $bool) {
  142. $this->assertEquals($bool, ConnectorFactory::isIpLocal($ip));
  143. }
  144. }
  145. public function testEmptyChainReturnsConnector()
  146. {
  147. $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  148. $connector = ConnectorFactory::createConnectorChain(array(), $loop);
  149. $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector);
  150. $this->assertInstanceOf('React\Socket\Connector', $connector);
  151. }
  152. public function testChainWithMixedProxiesReturnsAnyConnector()
  153. {
  154. $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  155. $connector = ConnectorFactory::createConnectorChain(array('socks://127.0.0.1:1080', 'http://127.0.0.1:1080'), $loop);
  156. $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector);
  157. }
  158. public function testChainWithMultipleProxiesOnlyStartsOneTimeout()
  159. {
  160. $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  161. $loop->expects($this->once())->method('addTimer');
  162. $connector = ConnectorFactory::createConnectorChain(array('socks://127.0.0.1:1080', 'http://127.0.0.1:1080'), $loop);
  163. $connector->connect('google.com:8080');
  164. }
  165. /** @expectedException InvalidArgumentException */
  166. public function testThrowsIfChainContainsInvalidUri()
  167. {
  168. $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  169. ConnectorFactory::createConnectorChain(array('///'), $loop);
  170. }
  171. public function testEmptyBlockPassesThrough()
  172. {
  173. $allow = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  174. $allow->expects($this->once())->method('connect')->with('google.com:80');
  175. $connector = ConnectorFactory::createBlockingConnector(array(), $allow);
  176. $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector);
  177. $connector->connect('google.com:80');
  178. }
  179. public function testBlockDomainsBlocksOnlyDomainsAndSubDomains()
  180. {
  181. $allow = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  182. $allow->expects($this->once())->method('connect')->with('tls://github.com:443');
  183. $connector = ConnectorFactory::createBlockingConnector(array('google.com', 'google.de'), $allow);
  184. $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector);
  185. $this->assertPromiseRejected($connector->connect('google.com:80'));
  186. $this->assertPromiseRejected($connector->connect('tcp://google.com:80'));
  187. $this->assertPromiseRejected($connector->connect('tls://google.com:443'));
  188. $this->assertPromiseRejected($connector->connect('tcp://www.google.com:80'));
  189. $this->assertPromiseRejected($connector->connect('tcp://some.sub.domain.google.de:80'));
  190. $connector->connect('tls://github.com:443');
  191. }
  192. public function testBlockWildcardBlocksOnlyMatchingDomains()
  193. {
  194. $allow = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  195. $allow->expects($this->once())->method('connect')->with('tcp://google.com:80');
  196. $connector = ConnectorFactory::createBlockingConnector(array('*.google.com'), $allow);
  197. $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector);
  198. $this->assertPromiseRejected($connector->connect('test.google.com:80'));
  199. $this->assertPromiseRejected($connector->connect('tcp://test.google.com:80'));
  200. $this->assertPromiseRejected($connector->connect('tls://test.google.com:443'));
  201. $this->assertPromiseRejected($connector->connect('tcp://some.sub.domain.google.com:80'));
  202. $connector->connect('tcp://google.com:80');
  203. }
  204. public function testBlockHttpPort()
  205. {
  206. $allow = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  207. $allow->expects($this->once())->method('connect')->with('tls://google.com:443');
  208. $connector = ConnectorFactory::createBlockingConnector(array('*:80'), $allow);
  209. $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector);
  210. $this->assertPromiseRejected($connector->connect('google.com:80'));
  211. $this->assertPromiseRejected($connector->connect('tcp://google.com:80'));
  212. $this->assertPromiseRejected($connector->connect('tcp://github.com:80'));
  213. $connector->connect('tls://google.com:443');
  214. }
  215. public function testFilterRootDomains()
  216. {
  217. $domains = array('www.google.com', 'google.com', 'deep.example.google.com');
  218. $this->assertEquals(array('google.com'), ConnectorFactory::filterRootDomains($domains));
  219. }
  220. private function assertPromiseRejected($input)
  221. {
  222. $this->assertInstanceOf('React\Promise\PromiseInterface', $input);
  223. $rejected = false;
  224. $input->then(null, function () use (&$rejected) {
  225. $rejected= true;
  226. });
  227. $this->assertTrue($rejected);
  228. }
  229. }