forked from reactphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureIntegrationTest.php
More file actions
235 lines (188 loc) · 8.01 KB
/
SecureIntegrationTest.php
File metadata and controls
235 lines (188 loc) · 8.01 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace React\Tests\Socket;
use React\EventLoop\Factory as LoopFactory;
use React\Socket\TcpServer;
use React\Socket\SecureServer;
use React\Socket\TcpConnector;
use React\Socket\SecureConnector;
use Clue\React\Block;
use React\Promise\Promise;
use Evenement\EventEmitterInterface;
use React\Promise\Deferred;
use React\Socket\ConnectionInterface;
class SecureIntegrationTest extends TestCase
{
const TIMEOUT = 2;
private $loop;
private $server;
private $connector;
private $address;
/**
* @before
*/
public function setUpConnector()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on legacy HHVM');
}
$this->loop = LoopFactory::create();
$this->server = new TcpServer(0, $this->loop);
$this->server = new SecureServer($this->server, $this->loop, array(
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$this->address = $this->server->getAddress();
$this->connector = new SecureConnector(new TcpConnector($this->loop), $this->loop, array('verify_peer' => false));
}
/**
* @after
*/
public function tearDownServer()
{
if ($this->server !== null) {
$this->server->close();
$this->server = null;
}
}
public function testConnectToServer()
{
$client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT);
/* @var $client ConnectionInterface */
$client->close();
// if we reach this, then everything is good
$this->assertNull(null);
}
public function testConnectToServerEmitsConnection()
{
$promiseServer = $this->createPromiseForEvent($this->server, 'connection', $this->expectCallableOnce());
$promiseClient = $this->connector->connect($this->address);
list($_, $client) = Block\awaitAll(array($promiseServer, $promiseClient), $this->loop, self::TIMEOUT);
/* @var $client ConnectionInterface */
$client->close();
}
public function testSendSmallDataToServerReceivesOneChunk()
{
// server expects one connection which emits one data event
$received = new Deferred();
$this->server->on('connection', function (ConnectionInterface $peer) use ($received) {
$peer->on('data', function ($chunk) use ($received) {
$received->resolve($chunk);
});
});
$client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT);
/* @var $client ConnectionInterface */
$client->write('hello');
// await server to report one "data" event
$data = Block\await($received->promise(), $this->loop, self::TIMEOUT);
$client->close();
$this->assertEquals('hello', $data);
}
public function testSendDataWithEndToServerReceivesAllData()
{
// PHP can report EOF on TLS 1.3 stream before consuming all data, so
// we explicitly use older TLS version instead. Selecting TLS version
// requires PHP 5.6+, so skip legacy versions if TLS 1.3 is supported.
// Continue if TLS 1.3 is not supported anyway.
if ($this->supportsTls13()) {
if (!defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
$this->markTestSkipped('TLS 1.3 supported, but this legacy PHP version does not support explicit choice');
}
$this->connector = new SecureConnector(new TcpConnector($this->loop), $this->loop, array(
'verify_peer' => false,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
));
}
$disconnected = new Deferred();
$this->server->on('connection', function (ConnectionInterface $peer) use ($disconnected) {
$received = '';
$peer->on('data', function ($chunk) use (&$received) {
$received .= $chunk;
});
$peer->on('close', function () use (&$received, $disconnected) {
$disconnected->resolve($received);
});
});
$client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT);
/* @var $client ConnectionInterface */
$data = str_repeat('a', 200000);
$client->end($data);
// await server to report connection "close" event
$received = Block\await($disconnected->promise(), $this->loop, self::TIMEOUT);
$this->assertEquals(strlen($data), strlen($received));
$this->assertEquals($data, $received);
}
public function testSendDataWithoutEndingToServerReceivesAllData()
{
$server = $this->server;
$promise = new Promise(function ($resolve, $reject) use ($server) {
$server->on('connection', function (ConnectionInterface $connection) use ($resolve) {
$received = '';
$connection->on('data', function ($chunk) use (&$received, $resolve) {
$received .= $chunk;
if (strlen($received) >= 200000) {
$resolve($received);
}
});
});
});
$data = str_repeat('d', 200000);
$this->connector->connect($this->address)->then(function (ConnectionInterface $connection) use ($data) {
$connection->write($data);
});
$received = Block\await($promise, $this->loop, self::TIMEOUT);
$this->assertEquals(strlen($data), strlen($received));
$this->assertEquals($data, $received);
}
public function testConnectToServerWhichSendsSmallDataReceivesOneChunk()
{
$this->server->on('connection', function (ConnectionInterface $peer) {
$peer->write('hello');
});
$client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT);
/* @var $client ConnectionInterface */
// await client to report one "data" event
$receive = $this->createPromiseForEvent($client, 'data', $this->expectCallableOnceWith('hello'));
Block\await($receive, $this->loop, self::TIMEOUT);
$client->close();
}
public function testConnectToServerWhichSendsDataWithEndReceivesAllData()
{
$data = str_repeat('b', 100000);
$this->server->on('connection', function (ConnectionInterface $peer) use ($data) {
$peer->end($data);
});
$client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT);
/* @var $client ConnectionInterface */
// await data from client until it closes
$received = $this->buffer($client, $this->loop, self::TIMEOUT);
$this->assertEquals($data, $received);
}
public function testConnectToServerWhichSendsDataWithoutEndingReceivesAllData()
{
$data = str_repeat('c', 100000);
$this->server->on('connection', function (ConnectionInterface $peer) use ($data) {
$peer->write($data);
});
$promise = $this->connector->connect($this->address);
$promise = new Promise(function ($resolve, $reject) use ($promise) {
$promise->then(function (ConnectionInterface $connection) use ($resolve) {
$received = 0;
$connection->on('data', function ($chunk) use (&$received, $resolve) {
$received += strlen($chunk);
if ($received >= 100000) {
$resolve($received);
}
});
}, $reject);
});
$received = Block\await($promise, $this->loop, self::TIMEOUT);
$this->assertEquals(strlen($data), $received);
}
private function createPromiseForEvent(EventEmitterInterface $emitter, $event, $fn)
{
return new Promise(function ($resolve) use ($emitter, $event, $fn) {
$emitter->on($event, function () use ($resolve, $fn) {
$resolve(call_user_func_array($fn, func_get_args()));
});
});
}
}