/
ClientTlsContextTest.php
290 lines (240 loc) · 8.63 KB
/
ClientTlsContextTest.php
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
namespace Amp\Socket\Test;
use Amp\Socket\Certificate;
use Amp\Socket\ClientTlsContext;
use PHPUnit\Framework\TestCase;
class ClientTlsContextTest extends TestCase {
public function minimumVersionDataProvider() {
return [
[ClientTlsContext::TLSv1_0],
[ClientTlsContext::TLSv1_1],
[ClientTlsContext::TLSv1_2],
];
}
/**
* @dataProvider minimumVersionDataProvider
*/
public function testWithMinimumVersion($version) {
$context = new ClientTlsContext;
$clonedContext = $context->withMinimumVersion($version);
$this->assertSame(ClientTlsContext::TLSv1_0, $context->getMinimumVersion());
$this->assertSame($version, $clonedContext->getMinimumVersion());
}
public function minimumVersionInvalidDataProvider() {
return [
[-1],
];
}
/**
* @dataProvider minimumVersionInvalidDataProvider
* @expectedException \Error
* @expectedExceptionMessage Invalid minimum version, only TLSv1.0, TLSv1.1 or TLSv1.2 allowed
*/
public function testWithMinimumVersionInvalid($version) {
(new ClientTlsContext)->withMinimumVersion($version);
}
public function peerNameDataProvider() {
return [
[null],
['test'],
];
}
/**
* @dataProvider peerNameDataProvider
*/
public function testWithPeerName($peerName) {
$context = new ClientTlsContext;
$clonedContext = $context->withPeerName($peerName);
$this->assertNull($context->getPeerName());
$this->assertSame($peerName, $clonedContext->getPeerName());
}
public function testWithPeerVerification() {
$context = new ClientTlsContext;
$clonedContext = $context->withPeerVerification();
$this->assertTrue($context->hasPeerVerification());
$this->assertTrue($clonedContext->hasPeerVerification());
}
public function testWithoutPeerVerification() {
$context = new ClientTlsContext;
$clonedContext = $context->withoutPeerVerification();
$this->assertTrue($context->hasPeerVerification());
$this->assertFalse($clonedContext->hasPeerVerification());
}
public function certificateDataProvider() {
return [
[null],
[new Certificate('cert.pem')],
];
}
/**
* @dataProvider certificateDataProvider
*/
public function testWithCertificate($certificate) {
$context = new ClientTlsContext;
$clonedContext = $context->withCertificate($certificate);
$this->assertNull($context->getCertificate());
$this->assertSame($certificate, $clonedContext->getCertificate());
}
public function verifyDepthDataProvider() {
return [
[0],
[123],
];
}
/**
* @dataProvider verifyDepthDataProvider
*/
public function testWithVerificationDepth($verifyDepth) {
$context = new ClientTlsContext;
$clonedContext = $context->withVerificationDepth($verifyDepth);
$this->assertSame(10, $context->getVerificationDepth());
$this->assertSame($verifyDepth, $clonedContext->getVerificationDepth());
}
public function verifyDepthInvalidDataProvider() {
return [
[-1],
[-123],
];
}
/**
* @dataProvider verifyDepthInvalidDataProvider
* @expectedException \Error
* @expectedExceptionMessageRegExp /Invalid verification depth (.*), must be greater than or equal to 0/
*/
public function testWithVerificationDepthInvalid($verifyDepth) {
(new ClientTlsContext)->withVerificationDepth($verifyDepth);
}
public function ciphersDataProvider() {
return [
['ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256'],
['DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256'],
];
}
/**
* @dataProvider ciphersDataProvider
*/
public function testWithCiphers($ciphers) {
$context = new ClientTlsContext;
$clonedContext = $context->withCiphers($ciphers);
$this->assertSame(\OPENSSL_DEFAULT_STREAM_CIPHERS, $context->getCiphers());
$this->assertSame($ciphers, $clonedContext->getCiphers());
}
public function caFileDataProvider() {
return [
[null],
['test'],
];
}
/**
* @dataProvider caFileDataProvider
*/
public function testWithCaFile($caFile) {
$context = new ClientTlsContext;
$clonedContext = $context->withCaFile($caFile);
$this->assertNull($context->getCaFile());
$this->assertSame($caFile, $clonedContext->getCaFile());
}
public function caPathDataProvider() {
return [
[null],
['test'],
];
}
/**
* @dataProvider caPathDataProvider
*/
public function testWithCaPath($caPath) {
$context = new ClientTlsContext;
$clonedContext = $context->withCaPath($caPath);
$this->assertNull($context->getCaPath());
$this->assertSame($caPath, $clonedContext->getCaPath());
}
public function testWithPeerCapturing() {
$context = new ClientTlsContext;
$clonedContext = $context->withPeerCapturing();
$this->assertFalse($context->hasPeerCapturing());
$this->assertTrue($clonedContext->hasPeerCapturing());
}
public function testWithoutPeerCapturing() {
$context = new ClientTlsContext;
$clonedContext = $context->withoutPeerCapturing();
$this->assertFalse($context->hasPeerCapturing());
$this->assertFalse($clonedContext->hasPeerCapturing());
}
public function testWithSni() {
$context = new ClientTlsContext;
$clonedContext = $context->withSni();
$this->assertTrue($context->hasSni());
$this->assertTrue($clonedContext->hasSni());
}
public function testWithoutSni() {
$context = new ClientTlsContext;
$clonedContext = $context->withoutSni();
$this->assertTrue($context->hasSni());
$this->assertFalse($clonedContext->hasSni());
}
public function invalidSecurityLevelDataProvider() {
return [
[-1],
[6],
];
}
/**
* @dataProvider invalidSecurityLevelDataProvider
*/
public function testWithSecurityLevelInvalid($level) {
$this->expectException(\Error::class);
$this->expectExceptionMessage("Invalid security level ({$level}), must be between 0 and 5.");
(new ClientTlsContext)->withSecurityLevel($level);
}
public function validSecurityLevelDataProvider() {
return [
[0],
[1],
[2],
[3],
[4],
[5],
];
}
/**
* @dataProvider validSecurityLevelDataProvider
*/
public function testWithSecurityLevelValid($level) {
if (\OPENSSL_VERSION_NUMBER >= 0x10100000) {
$value = (new ClientTlsContext)
->withSecurityLevel($level)
->getSecurityLevel();
$this->assertSame($level, $value);
} else {
$this->expectException(\Error::class);
$this->expectExceptionMessage("Can't set a security level, as PHP is compiled with OpenSSL < 1.1.0.");
(new ClientTlsContext)->withSecurityLevel($level);
}
}
public function testWithSecurityLevelDefaultValue() {
if (\OPENSSL_VERSION_NUMBER >= 0x10100000) {
$this->assertSame(2, (new ClientTlsContext)->getSecurityLevel());
} else {
$this->assertSame(0, (new ClientTlsContext)->getSecurityLevel());
}
}
public function testStreamContextArray() {
$context = (new ClientTlsContext)
->withCaPath("/var/foobar");
$contextArray = $context->toStreamContextArray();
unset($contextArray['ssl']['security_level']); // present depending on OpenSSL version
$this->assertSame(["ssl" => [
"crypto_method" => $context->toStreamCryptoMethod(),
"peer_name" => $context->getPeerName(),
"verify_peer" => $context->hasPeerVerification(),
"verify_peer_name" => $context->hasPeerVerification(),
"verify_depth" => $context->getVerificationDepth(),
"ciphers" => $context->getCiphers(),
"capture_peer_cert" => $context->hasPeerCapturing(),
"capture_peer_cert_chain" => $context->hasPeerCapturing(),
"SNI_enabled" => $context->hasSni(),
"capath" => $context->getCaPath(),
]], $contextArray);
}
}