Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"react/socket": "^1.12"
},
"require-dev": {
"clue/block-react": "^1.5",
"clue/connection-manager-extra": "^1.3",
"phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36",
"react/async": "^4 || ^3 || ^2",
"react/event-loop": "^1.2",
"react/http": "^1.6"
},
Expand Down
73 changes: 64 additions & 9 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Clue\Tests\React\Socks;

use Clue\React\Block;
use Clue\React\Socks\Client;
use Clue\React\Socks\Server;
use React\EventLoop\Loop;
use React\Promise\Promise;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
Expand All @@ -20,6 +18,7 @@ class FunctionalTest extends TestCase
private $connector;
private $client;

private $socket;
private $port;
private $server;

Expand All @@ -28,20 +27,28 @@ class FunctionalTest extends TestCase
*/
public function setUpClientServer()
{
$socket = new SocketServer('127.0.0.1:0');
$address = $socket->getAddress();
$this->socket = new SocketServer('127.0.0.1:0');
$address = $this->socket->getAddress();
if (strpos($address, '://') === false) {
$address = 'tcp://' . $address;
}
$this->port = parse_url($address, PHP_URL_PORT);
$this->assertNotEquals(0, $this->port);

$this->server = new Server();
$this->server->listen($socket);
$this->server->listen($this->socket);
$this->connector = new TcpConnector();
$this->client = new Client('127.0.0.1:' . $this->port, $this->connector);
}

/**
* @after
*/
public function closeSocket()
{
$this->socket->close();
}

/** @group internet */
public function testConnection()
{
Expand Down Expand Up @@ -117,6 +124,8 @@ public function testConnectionSocksOverTls()
$this->client = new Client(str_replace('tls:', 'sockss:', $socket->getAddress()), $this->connector);

$this->assertResolveStream($this->client->connect('www.google.com:80'));

$socket->close();
}

/**
Expand Down Expand Up @@ -146,6 +155,8 @@ public function testConnectionSocksOverTlsUsesPeerNameFromSocksUri()
$this->client = new Client(str_replace('tls:', 'sockss:', $socket->getAddress()), $this->connector);

$this->assertResolveStream($this->client->connect('www.google.com:80'));

$socket->close();
}

/** @group internet */
Expand All @@ -166,6 +177,8 @@ public function testConnectionSocksOverUnix()
$this->assertResolveStream($this->client->connect('www.google.com:80'));

unlink($path);

$socket->close();
}

/** @group internet */
Expand All @@ -186,6 +199,8 @@ public function testConnectionSocks5OverUnix()
$this->assertResolveStream($this->client->connect('www.google.com:80'));

unlink($path);

$socket->close();
}

/** @group internet */
Expand All @@ -206,6 +221,8 @@ public function testConnectionSocksWithAuthenticationOverUnix()
$this->assertResolveStream($this->client->connect('www.google.com:80'));

unlink($path);

$socket->close();
}

/** @group internet */
Expand All @@ -220,6 +237,8 @@ public function testConnectionAuthenticationFromUri()
$this->client = new Client('name:pass@127.0.0.1:' . $this->port, $this->connector);

$this->assertResolveStream($this->client->connect('www.google.com:80'));

$socket->close();
}

/** @group internet */
Expand All @@ -244,6 +263,8 @@ public function testConnectionAuthenticationCallback()

$this->assertResolveStream($this->client->connect('www.google.com:80'));
$this->assertEquals(1, $called);

$socket->close();
}

/** @group internet */
Expand All @@ -257,6 +278,9 @@ public function testConnectionAuthenticationCallbackWillNotBeInvokedIfClientsSen
});

$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function () use ($socket) {
$socket->close();
});
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -278,6 +302,8 @@ public function testConnectionAuthenticationFromUriEncoded()
$this->client = new Client(rawurlencode('name') . ':' . rawurlencode('p@ss:w0rd') . '@127.0.0.1:' . $this->port, $this->connector);

$this->assertResolveStream($this->client->connect('www.google.com:80'));

$socket->close();
}

/** @group internet */
Expand All @@ -292,6 +318,8 @@ public function testConnectionAuthenticationFromUriWithOnlyUserAndNoPassword()
$this->client = new Client('empty@127.0.0.1:' . $this->port, $this->connector);

$this->assertResolveStream($this->client->connect('www.google.com:80'));

$socket->close();
}

/** @group internet */
Expand All @@ -306,6 +334,8 @@ public function testConnectionAuthenticationEmptyPassword()
$this->client = new Client('user@127.0.0.1:' . $this->port, $this->connector);

$this->assertResolveStream($this->client->connect('www.google.com:80'));

$socket->close();
}

/** @group internet */
Expand All @@ -321,6 +351,9 @@ public function testConnectionInvalidNoAuthenticationOverLegacySocks4()
$this->server = new Server(null, null, array('name' => 'pass'));

$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function () use ($socket) {
$socket->close();
});
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -334,6 +367,9 @@ public function testConnectionInvalidNoAuthentication()
$this->server = new Server(null, null, array('name' => 'pass'));

$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function () use ($socket) {
$socket->close();
});
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -347,6 +383,9 @@ public function testConnectionInvalidAuthenticationMismatch()
$this->server = new Server(null, null, array('name' => 'pass'));

$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function () use ($socket) {
$socket->close();
});
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -362,6 +401,9 @@ public function testConnectionInvalidAuthenticatorReturnsFalse()
});

$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function () use ($socket) {
$socket->close();
});
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -377,6 +419,9 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal
});

$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function () use ($socket) {
$socket->close();
});
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -388,10 +433,13 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal
public function testConnectionInvalidAuthenticatorReturnsPromiseRejected()
{
$this->server = new Server(null, null, function () {
return \React\Promise\reject();
return \React\Promise\reject(new \RuntimeException());
});

$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function () use ($socket) {
$socket->close();
});
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand Down Expand Up @@ -457,7 +505,12 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateFailsWit

$ssl = new SecureConnector($this->client, null, array('verify_peer' => true));

$this->assertRejectPromise($ssl->connect($socket->getAddress()));
$promise = $ssl->connect($socket->getAddress());
$promise->then(null, function () use ($socket) {
$socket->close();
});

$this->assertRejectPromise($promise);
}

public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWithoutVerifyPeer()
Expand All @@ -480,6 +533,8 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWit

$this->assertResolveStream($ssl->connect($socket->getAddress()));
$this->assertResolveStream($promise);

$socket->close();
}

/** @group internet */
Expand Down Expand Up @@ -511,7 +566,7 @@ private function assertResolveStream($promise)
$stream->close();
});

Block\await($promise, Loop::get(), 2.0);
\React\Async\await($promise);
}

private function assertRejectPromise($promise, $message = null, $code = null)
Expand All @@ -528,6 +583,6 @@ private function assertRejectPromise($promise, $message = null, $code = null)
$this->setExpectedException('Exception', $message, $code);
}

Block\await($promise, Loop::get(), 2.0);
\React\Async\await($promise);
}
Comment thread
clue marked this conversation as resolved.
}
Loading