forked from clue/reactphp-ssh-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSshProcessConnectorTest.php
More file actions
152 lines (117 loc) · 5.18 KB
/
SshProcessConnectorTest.php
File metadata and controls
152 lines (117 loc) · 5.18 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Clue\Tests\React\SshProxy;
use Clue\React\SshProxy\SshProcessConnector;
class SshProcessConnectorTest extends TestCase
{
/**
* @after
*/
public function tearDownSSHClientProcess()
{
// Skip timer-based teardown for PHP 5.3 where React\Promise\Timer is not available
if (!class_exists('React\\Promise\\Timer\\TimeoutException')) {
return;
}
// run loop in order to shut down SSH client process again
\React\Async\await(\React\Promise\Timer\sleep(0.001));
}
// Helper method to check if Timer functions are available
private function hasTimerSupport()
{
return class_exists('React\\Promise\\Timer\\TimeoutException');
}
public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$connector = new SshProcessConnector('host');
$ref = new \ReflectionProperty($connector, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($connector);
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}
public function testConstructorAcceptsUri()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new SshProcessConnector('host', $loop);
$ref = new \ReflectionProperty($connector, 'cmd');
$ref->setAccessible(true);
$this->assertEquals('exec ssh -vv -o BatchMode=yes \'host\'', $ref->getValue($connector));
}
public function testConstructorAcceptsUriWithDefaultPortWillNotBeAddedToCommand()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new SshProcessConnector('host:22', $loop);
$ref = new \ReflectionProperty($connector, 'cmd');
$ref->setAccessible(true);
$this->assertEquals('exec ssh -vv -o BatchMode=yes \'host\'', $ref->getValue($connector));
}
public function testConstructorAcceptsUriWithUserAndCustomPort()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new SshProcessConnector('user@host:2222', $loop);
$ref = new \ReflectionProperty($connector, 'cmd');
$ref->setAccessible(true);
$this->assertEquals('exec ssh -vv -o BatchMode=yes -p 2222 \'user@host\'', $ref->getValue($connector));
}
public function testConstructorAcceptsUriWithPasswordWillPrefixSshCommandWithSshpassAndWithoutBatchModeOption()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new SshProcessConnector('user:pass@host', $loop);
$ref = new \ReflectionProperty($connector, 'cmd');
$ref->setAccessible(true);
$this->assertEquals('exec sshpass -p \'pass\' ssh -vv \'user@host\'', $ref->getValue($connector));
}
public function testConstructorThrowsForInvalidUri()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$this->setExpectedException('InvalidArgumentException');
new SshProcessConnector('///', $loop);
}
public function testConstructorThrowsForInvalidUser()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$this->setExpectedException('InvalidArgumentException');
new SshProcessConnector('-invalid@host', $loop);
}
public function testConstructorThrowsForInvalidPass()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$this->setExpectedException('InvalidArgumentException');
new SshProcessConnector('user:-invalid@host', $loop);
}
public function testConstructorThrowsForInvalidHost()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$this->setExpectedException('InvalidArgumentException');
new SshProcessConnector('-host', $loop);
}
/**
* @doesNotPerformAssertions
*/
public function testConstructorAcceptsHostWithLeadingDashWhenPrefixedWithUser()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new SshProcessConnector('user@-host', $loop);
}
public function testConnectReturnsRejectedPromiseForInvalidUri()
{
if (!$this->hasTimerSupport()) {
$this->markTestSkipped('No Timer support available');
return;
}
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new SshProcessConnector('host', $loop);
$promise = $connector->connect('///');
$promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('InvalidArgumentException')));
}
public function testConnectReturnsRejectedPromiseForInvalidHost()
{
if (!$this->hasTimerSupport()) {
$this->markTestSkipped('No Timer support available');
return;
}
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new SshProcessConnector('host', $loop);
$promise = $connector->connect('-host:80');
$promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('InvalidArgumentException')));
}
}