forked from clue/reactphp-ssh-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionalSshProcessConnectorTest.php
More file actions
113 lines (89 loc) · 3.97 KB
/
FunctionalSshProcessConnectorTest.php
File metadata and controls
113 lines (89 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Clue\Tests\React\SshProxy;
use Clue\React\SshProxy\SshProcessConnector;
class FunctionalSshProcessConnectorTest extends TestCase
{
const TIMEOUT = 10.0;
private $connector;
/**
* @before
*/
public function setUpConnector()
{
$url = getenv('SSH_PROXY');
if ($url === false) {
$this->markTestSkipped('No SSH_PROXY env set');
}
$this->connector = new SshProcessConnector($url);
}
/**
* @before
*/
public function checkTimerSupport()
{
// Skip this test for PHP 5.3 where React\Promise\Timer isn't available
if (!class_exists('React\\Promise\\Timer')) {
$this->markTestSkipped('No Timer support available');
}
}
public function testConnectInvalidProxyUriWillReturnRejectedPromise()
{
$this->connector = new SshProcessConnector(getenv('SSH_PROXY') . '.invalid');
$promise = $this->connector->connect('example.com:80');
$this->setExpectedException('RuntimeException', 'Connection to example.com:80 failed because SSH client died');
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT));
}
public function testConnectInvalidTargetWillReturnRejectedPromise()
{
$promise = $this->connector->connect('example.invalid:80');
$this->setExpectedException('RuntimeException', 'Connection to example.invalid:80 rejected:');
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT));
}
public function testCancelConnectWillReturnRejectedPromise()
{
$promise = $this->connector->connect('example.com:80');
$promise->cancel();
$this->setExpectedException('RuntimeException', 'Connection to example.com:80 cancelled while waiting for SSH client');
\React\Async\await(\React\Promise\Timer\timeout($promise, 0));
}
public function testConnectValidTargetWillReturnPromiseWhichResolvesToConnection()
{
$promise = $this->connector->connect('example.com:80');
$connection = \React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT));
$this->assertInstanceOf('React\Socket\ConnectionInterface', $connection);
$this->assertTrue($connection->isReadable());
$this->assertTrue($connection->isWritable());
$connection->close();
}
public function testConnectPendingWillNotInheritActiveFileDescriptors()
{
$server = stream_socket_server('tcp://127.0.0.1:0');
$address = stream_socket_get_name($server, false);
// ensure that we can not listen on the same address twice
$copy = @stream_socket_server('tcp://' . $address);
if ($copy !== false) {
fclose($server);
fclose($copy);
$this->markTestSkipped('Platform does not prevent binding to same address (Windows?)');
}
$promise = $this->connector->connect('example.com:80');
// close server and ensure we can start a new server on the previous address
// the pending SSH connection process should not inherit the existing server socket
fclose($server);
$server = @stream_socket_server('tcp://' . $address);
if ($server === false) {
// There's a very short race condition where the forked php process
// first has to `dup()` the file descriptor specs before invoking
// `exec()` to switch to the actual `ssh` child process. We don't
// need to wait for the child process to be ready, but only for the
// forked process to close the file descriptors. This happens ~80%
// of times on single core machines and almost never on multi core
// systems, so simply wait 5ms (plenty of time!) and retry again.
usleep(5000);
$server = stream_socket_server('tcp://' . $address);
}
$this->assertTrue(is_resource($server));
fclose($server);
$promise->cancel();
}
}