SocksErrorConnectorTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. use LeProxy\LeProxy\SocksErrorConnector;
  3. use React\Promise\Promise;
  4. use LeProxy\LeProxy\ConnectorFactory;
  5. class SocksErrorConnectorTest extends PHPUnit_Framework_TestCase
  6. {
  7. public function testConnectWillBePassedThrough()
  8. {
  9. $promise = new Promise(function () { });
  10. $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  11. $base->expects($this->once())->method('connect')->with('example.com:1234')->willReturn($promise);
  12. $connector = new SocksErrorConnector($base, false);
  13. $ret = $connector->connect('example.com:1234');
  14. $this->assertInstanceOf('React\Promise\PromiseInterface', $ret);
  15. }
  16. public function testConnectWillThrowErrorForBlockedConnection()
  17. {
  18. $promise = \React\Promise\reject(new RuntimeException('test', ConnectorFactory::CODE_BLOCKED));
  19. $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  20. $base->expects($this->once())->method('connect')->with('example.com:1234')->willReturn($promise);
  21. $connector = new SocksErrorConnector($base, false);
  22. $ret = $connector->connect('example.com:1234');
  23. $code = null;
  24. $ret->then(null, function ($e) use (&$code) {
  25. $code = $e->getCode();
  26. });
  27. $this->assertEquals(SOCKET_EACCES, $code);
  28. }
  29. public function testConnectWillThrowErrorForDirectError()
  30. {
  31. $promise = \React\Promise\reject(new RuntimeException('test', SOCKET_ECONNREFUSED));
  32. $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  33. $base->expects($this->once())->method('connect')->with('example.com:1234')->willReturn($promise);
  34. $connector = new SocksErrorConnector($base, false);
  35. $ret = $connector->connect('example.com:1234');
  36. $code = null;
  37. $ret->then(null, function ($e) use (&$code) {
  38. $code = $e->getCode();
  39. });
  40. $this->assertEquals(SOCKET_ECONNREFUSED, $code);
  41. }
  42. public function testConnectWillThrowGenericErrorForNestedError()
  43. {
  44. $promise = \React\Promise\reject(new RuntimeException('test', SOCKET_ECONNREFUSED, new RuntimeException()));
  45. $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  46. $base->expects($this->once())->method('connect')->with('example.com:1234')->willReturn($promise);
  47. $connector = new SocksErrorConnector($base, false);
  48. $ret = $connector->connect('example.com:1234');
  49. $code = null;
  50. $ret->then(null, function ($e) use (&$code) {
  51. $code = $e->getCode();
  52. });
  53. $this->assertEquals(0, $code);
  54. }
  55. public function testConnectWillBePassedThroughWhenLocalModeIsEnabledAndSourceIsLocal()
  56. {
  57. $promise = new Promise(function () { });
  58. $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  59. $base->expects($this->once())->method('connect')->willReturn($promise);
  60. $connector = new SocksErrorConnector($base, true);
  61. $ret = $connector->connect('example.com:1234?source=' . rawurlencode('socks://127.0.0.1'));
  62. $this->assertInstanceOf('React\Promise\PromiseInterface', $ret);
  63. }
  64. public function testConnectWillBePassedThroughWhenLocalModeIsDisabledAndSourceIsRemote()
  65. {
  66. $promise = new Promise(function () { });
  67. $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  68. $base->expects($this->once())->method('connect')->willReturn($promise);
  69. $connector = new SocksErrorConnector($base, false);
  70. $ret = $connector->connect('example.com:1234?source=' . rawurlencode('socks://1.2.3.4'));
  71. $this->assertInstanceOf('React\Promise\PromiseInterface', $ret);
  72. }
  73. public function testConnectWillNotBePassedThroughWhenLocalModeIsEnabledAndSourceIsRemote()
  74. {
  75. $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  76. $base->expects($this->never())->method('connect');
  77. $connector = new SocksErrorConnector($base, true);
  78. $ret = $connector->connect('example.com:1234?source=' . rawurlencode('socks://1.2.3.4'));
  79. $this->assertInstanceOf('React\Promise\PromiseInterface', $ret);
  80. $code = null;
  81. $ret->then(null, function ($e) use (&$code) {
  82. $code = $e->getCode();
  83. });
  84. $this->assertEquals(SOCKET_EACCES, $code);
  85. }
  86. }