forked from reactphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureServerTest.php
More file actions
202 lines (140 loc) · 6.9 KB
/
SecureServerTest.php
File metadata and controls
202 lines (140 loc) · 6.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace React\Tests\Socket;
use React\Socket\SecureServer;
use React\Socket\TcpServer;
use React\Promise\Promise;
class SecureServerTest extends TestCase
{
/**
* @before
*/
public function setUpSkipTest()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on legacy HHVM');
}
}
public function testCtorThrowsForInvalidLoop()
{
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
new SecureServer($tcp, 'loop');
}
public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
$server = new SecureServer($tcp);
$ref = new \ReflectionProperty($server, 'encryption');
$ref->setAccessible(true);
$encryption = $ref->getValue($server);
$ref = new \ReflectionProperty($encryption, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($encryption);
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}
public function testGetAddressWillBePassedThroughToTcpServer()
{
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
$tcp->expects($this->once())->method('getAddress')->willReturn('tcp://127.0.0.1:1234');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$server = new SecureServer($tcp, $loop, array());
$this->assertEquals('tls://127.0.0.1:1234', $server->getAddress());
}
public function testGetAddressWillReturnNullIfTcpServerReturnsNull()
{
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
$tcp->expects($this->once())->method('getAddress')->willReturn(null);
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$server = new SecureServer($tcp, $loop, array());
$this->assertNull($server->getAddress());
}
public function testPauseWillBePassedThroughToTcpServer()
{
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
$tcp->expects($this->once())->method('pause');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$server = new SecureServer($tcp, $loop, array());
$server->pause();
}
public function testResumeWillBePassedThroughToTcpServer()
{
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
$tcp->expects($this->once())->method('resume');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$server = new SecureServer($tcp, $loop, array());
$server->resume();
}
public function testCloseWillBePassedThroughToTcpServer()
{
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
$tcp->expects($this->once())->method('close');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$server = new SecureServer($tcp, $loop, array());
$server->close();
}
public function testConnectionWillBeClosedWithErrorIfItIsNotAStream()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$tcp = new TcpServer(0, $loop);
$connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$connection->expects($this->once())->method('close');
$server = new SecureServer($tcp, $loop, array());
$server->on('error', $this->expectCallableOnce());
$tcp->emit('connection', array($connection));
}
public function testConnectionWillTryToEnableEncryptionAndWaitForHandshake()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$tcp = new TcpServer(0, $loop);
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
$connection->expects($this->once())->method('getRemoteAddress')->willReturn('tcp://127.0.0.1:1234');
$connection->expects($this->never())->method('close');
$server = new SecureServer($tcp, $loop, array());
$pending = new Promise(function () { });
$encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock();
$encryption->expects($this->once())->method('enable')->willReturn($pending);
$ref = new \ReflectionProperty($server, 'encryption');
$ref->setAccessible(true);
$ref->setValue($server, $encryption);
$ref = new \ReflectionProperty($server, 'context');
$ref->setAccessible(true);
$ref->setValue($server, array());
$server->on('error', $this->expectCallableNever());
$server->on('connection', $this->expectCallableNever());
$tcp->emit('connection', array($connection));
}
public function testConnectionWillBeClosedWithErrorIfEnablingEncryptionFails()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$tcp = new TcpServer(0, $loop);
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
$connection->expects($this->once())->method('getRemoteAddress')->willReturn('tcp://127.0.0.1:1234');
$connection->expects($this->once())->method('close');
$server = new SecureServer($tcp, $loop, array());
$error = new \RuntimeException('Original');
$encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock();
$encryption->expects($this->once())->method('enable')->willReturn(\React\Promise\reject($error));
$ref = new \ReflectionProperty($server, 'encryption');
$ref->setAccessible(true);
$ref->setValue($server, $encryption);
$ref = new \ReflectionProperty($server, 'context');
$ref->setAccessible(true);
$ref->setValue($server, array());
$error = null;
$server->on('error', $this->expectCallableOnce());
$server->on('error', function ($e) use (&$error) {
$error = $e;
});
$tcp->emit('connection', array($connection));
$this->assertInstanceOf('RuntimeException', $error);
$this->assertEquals('Connection from tcp://127.0.0.1:1234 failed during TLS handshake: Original', $error->getMessage());
}
public function testSocketErrorWillBeForwarded()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$tcp = new TcpServer(0, $loop);
$server = new SecureServer($tcp, $loop, array());
$server->on('error', $this->expectCallableOnce());
$tcp->emit('error', array(new \RuntimeException('test')));
}
}