|
| 1 | +<?php |
| 2 | + |
| 3 | +use React\Stream\Stream; |
| 4 | +use Clue\React\Socks\Client; |
| 5 | +use Clue\React\Socks\Server; |
| 6 | +use React\Promise\PromiseInterface; |
| 7 | + |
| 8 | +class FunctionalTest extends TestCase |
| 9 | +{ |
| 10 | + private $loop; |
| 11 | + private $client; |
| 12 | + private $server; |
| 13 | + |
| 14 | + public function setUp() |
| 15 | + { |
| 16 | + $this->loop = React\EventLoop\Factory::create(); |
| 17 | + |
| 18 | + $socket = $this->createSocketServer(); |
| 19 | + $port = $socket->getPort(); |
| 20 | + $this->assertNotEquals(0, $port); |
| 21 | + |
| 22 | + $this->server = new Server($this->loop, $socket); |
| 23 | + $this->client = new Client($this->loop, '127.0.0.1', $port); |
| 24 | + } |
| 25 | + |
| 26 | + public function testConnection() |
| 27 | + { |
| 28 | + $this->assertResolveStream($this->client->getConnection('www.google.com', 80)); |
| 29 | + } |
| 30 | + |
| 31 | + public function testConnectionSocks4() |
| 32 | + { |
| 33 | + $this->server->setProtocolVersion(4); |
| 34 | + $this->client->setProtocolVersion(4); |
| 35 | + |
| 36 | + $this->assertResolveStream($this->client->getConnection('www.google.com', 80)); |
| 37 | + } |
| 38 | + |
| 39 | + public function testConnectionSocks5() |
| 40 | + { |
| 41 | + $this->server->setProtocolVersion(5); |
| 42 | + $this->client->setProtocolVersion(5); |
| 43 | + |
| 44 | + $this->assertResolveStream($this->client->getConnection('www.google.com', 80)); |
| 45 | + } |
| 46 | + |
| 47 | + public function testConnectionInvalidSocks4aRemote() |
| 48 | + { |
| 49 | + $this->client->setProtocolVersion('4a'); |
| 50 | + $this->client->setResolveLocal(false); |
| 51 | + |
| 52 | + $this->assertResolveStream($this->client->getConnection('www.google.com', 80)); |
| 53 | + } |
| 54 | + |
| 55 | + public function testConnectionSocks5Remote() |
| 56 | + { |
| 57 | + $this->client->setProtocolVersion(5); |
| 58 | + $this->client->setResolveLocal(false); |
| 59 | + |
| 60 | + $this->assertResolveStream($this->client->getConnection('www.google.com', 80)); |
| 61 | + } |
| 62 | + |
| 63 | + public function testConnectionAuthentication() |
| 64 | + { |
| 65 | + $this->server->setAuthArray(array('name' => 'pass')); |
| 66 | + $this->client->setAuth('name', 'pass'); |
| 67 | + |
| 68 | + $this->assertResolveStream($this->client->getConnection('www.google.com', 80)); |
| 69 | + } |
| 70 | + |
| 71 | + public function testConnectionAuthenticationUnused() |
| 72 | + { |
| 73 | + $this->client->setAuth('name', 'pass'); |
| 74 | + |
| 75 | + $this->assertResolveStream($this->client->getConnection('www.google.com', 80)); |
| 76 | + } |
| 77 | + |
| 78 | + public function testConnectionInvalidProtocolMismatch() |
| 79 | + { |
| 80 | + $this->server->setProtocolVersion(5); |
| 81 | + $this->client->setProtocolVersion(4); |
| 82 | + |
| 83 | + $this->assertRejectPromise($this->client->getConnection('www.google.com', 80)); |
| 84 | + } |
| 85 | + |
| 86 | + public function testConnectionInvalidNoAuthentication() |
| 87 | + { |
| 88 | + $this->server->setAuthArray(array('name' => 'pass')); |
| 89 | + $this->client->setProtocolVersion(5); |
| 90 | + |
| 91 | + $this->assertRejectPromise($this->client->getConnection('www.google.com', 80)); |
| 92 | + } |
| 93 | + |
| 94 | + public function testConnectionInvalidAuthenticationMismatch() |
| 95 | + { |
| 96 | + $this->server->setAuthArray(array('name' => 'pass')); |
| 97 | + $this->client->setAuth('user', 'other'); |
| 98 | + |
| 99 | + $this->assertRejectPromise($this->client->getConnection('www.google.com', 80)); |
| 100 | + } |
| 101 | + |
| 102 | + public function testConnectorOkay() |
| 103 | + { |
| 104 | + $tcp = $this->client->createConnector(); |
| 105 | + |
| 106 | + $this->assertResolveStream($tcp->create('www.google.com', 80)); |
| 107 | + } |
| 108 | + |
| 109 | + public function testConnectorInvalidDomain() |
| 110 | + { |
| 111 | + $tcp = $this->client->createConnector(); |
| 112 | + |
| 113 | + $this->assertRejectPromise($tcp->create('www.google.commm', 80)); |
| 114 | + } |
| 115 | + |
| 116 | + public function testConnectorInvalidUnboundPortTimeout() |
| 117 | + { |
| 118 | + $this->client->setTimeout(0.1); |
| 119 | + $tcp = $this->client->createConnector(); |
| 120 | + |
| 121 | + $this->assertRejectPromise($tcp->create('www.google.com', 8080)); |
| 122 | + } |
| 123 | + |
| 124 | + public function testSecureConnectorOkay() |
| 125 | + { |
| 126 | + $ssl = $this->client->createSecureConnector(); |
| 127 | + |
| 128 | + $this->assertResolveStream($ssl->create('www.google.com', 443)); |
| 129 | + } |
| 130 | + |
| 131 | + public function testSecureConnectorInvalidPlaintextIsNotSsl() |
| 132 | + { |
| 133 | + $ssl = $this->client->createSecureConnector(); |
| 134 | + |
| 135 | + $this->assertRejectPromise($ssl->create('www.google.com', 80)); |
| 136 | + } |
| 137 | + |
| 138 | + public function testSecureConnectorInvalidUnboundPortTimeout() |
| 139 | + { |
| 140 | + $this->client->setTimeout(0.1); |
| 141 | + $ssl = $this->client->createSecureConnector(); |
| 142 | + |
| 143 | + $this->assertRejectPromise($ssl->create('www.google.com', 8080)); |
| 144 | + } |
| 145 | + |
| 146 | + private function createSocketServer() |
| 147 | + { |
| 148 | + $socket = new React\Socket\Server($this->loop); |
| 149 | + $socket->listen(0); |
| 150 | + |
| 151 | + return $socket; |
| 152 | + } |
| 153 | + |
| 154 | + private function assertResolveStream($promise) |
| 155 | + { |
| 156 | + $this->expectPromiseResolve($promise); |
| 157 | + |
| 158 | + $promise->then(function ($stream) { |
| 159 | + $stream->close(); |
| 160 | + }); |
| 161 | + |
| 162 | + $this->waitFor($promise); |
| 163 | + } |
| 164 | + |
| 165 | + private function assertRejectPromise($promise) |
| 166 | + { |
| 167 | + $this->expectPromiseReject($promise); |
| 168 | + |
| 169 | + $this->setExpectedException('Exception'); |
| 170 | + $this->waitFor($promise); |
| 171 | + } |
| 172 | + |
| 173 | + private function waitFor(PromiseInterface $promise) |
| 174 | + { |
| 175 | + $resolved = null; |
| 176 | + $exception = null; |
| 177 | + |
| 178 | + $promise->then(function ($c) use (&$resolved) { |
| 179 | + $resolved = $c; |
| 180 | + }, function($error) use (&$exception) { |
| 181 | + $exception = $error; |
| 182 | + }); |
| 183 | + |
| 184 | + while ($resolved === null && $exception === null) { |
| 185 | + $this->loop->tick(); |
| 186 | + } |
| 187 | + |
| 188 | + if ($exception !== null) { |
| 189 | + throw $exception; |
| 190 | + } |
| 191 | + |
| 192 | + return $resolved; |
| 193 | + } |
| 194 | +} |
0 commit comments