1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the php-gelf package. |
6
|
|
|
* |
7
|
|
|
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Gelf\Test\Transport; |
14
|
|
|
|
15
|
|
|
use Gelf\Transport\SslOptions; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
class SslOptionsTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testState(): void |
21
|
|
|
{ |
22
|
|
|
$options = new SslOptions(); |
23
|
|
|
|
24
|
|
|
// test sane defaults |
25
|
|
|
self::assertTrue($options->getVerifyPeer()); |
26
|
|
|
self::assertTrue($options->getVerifyPeerName()); |
27
|
|
|
self::assertFalse($options->getAllowSelfSigned()); |
28
|
|
|
self::assertNull($options->getCaFile()); |
29
|
|
|
self::assertNull($options->getCiphers()); |
30
|
|
|
|
31
|
|
|
// test setters |
32
|
|
|
$options->setVerifyPeer(false); |
33
|
|
|
$options->setVerifyPeerName(false); |
34
|
|
|
$options->setAllowSelfSigned(true); |
35
|
|
|
$options->setCaFile('/path/to/ca'); |
36
|
|
|
$options->setCiphers('ALL:!ADH:@STRENGTH'); |
37
|
|
|
|
38
|
|
|
self::assertFalse($options->getVerifyPeer()); |
39
|
|
|
self::assertFalse($options->getVerifyPeerName()); |
40
|
|
|
self::assertTrue($options->getAllowSelfSigned()); |
41
|
|
|
self::assertEquals('/path/to/ca', $options->getCaFile()); |
42
|
|
|
self::assertEquals('ALL:!ADH:@STRENGTH', $options->getCiphers()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testToStreamContext(): void |
46
|
|
|
{ |
47
|
|
|
$options = new SslOptions(); |
48
|
|
|
|
49
|
|
|
self::assertEquals([ |
50
|
|
|
'ssl' => [ |
51
|
|
|
'verify_peer' => true, |
52
|
|
|
'verify_peer_name' => true, |
53
|
|
|
'allow_self_signed' => false, |
54
|
|
|
] |
55
|
|
|
], $options->toStreamContext()); |
56
|
|
|
|
57
|
|
|
$options->setVerifyPeer(false); |
58
|
|
|
$options->setVerifyPeerName(false); |
59
|
|
|
$options->setAllowSelfSigned(true); |
60
|
|
|
$options->setCaFile('/path/to/ca'); |
61
|
|
|
$options->setCiphers('ALL:!ADH:@STRENGTH'); |
62
|
|
|
|
63
|
|
|
self::assertEquals([ |
64
|
|
|
'ssl' => [ |
65
|
|
|
'verify_peer' => false, |
66
|
|
|
'verify_peer_name' => false, |
67
|
|
|
'allow_self_signed' => true, |
68
|
|
|
'cafile' => '/path/to/ca', |
69
|
|
|
'ciphers' => 'ALL:!ADH:@STRENGTH' |
70
|
|
|
] |
71
|
|
|
], $options->toStreamContext()); |
72
|
|
|
|
73
|
|
|
$options->setCaFile(null); |
74
|
|
|
$options->setCiphers(null); |
75
|
|
|
|
76
|
|
|
self::assertEquals([ |
77
|
|
|
'ssl' => [ |
78
|
|
|
'verify_peer' => false, |
79
|
|
|
'verify_peer_name' => false, |
80
|
|
|
'allow_self_signed' => true, |
81
|
|
|
] |
82
|
|
|
], $options->toStreamContext()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testToStreamContextWithHostname(): void |
86
|
|
|
{ |
87
|
|
|
$options = new SslOptions(); |
88
|
|
|
$peerNameKey = PHP_VERSION_ID < 50600 ? 'CN_match' : 'peer_name'; |
89
|
|
|
$sniPeerNameKey = PHP_VERSION_ID < 50600 ? 'SNI_server_name' : 'peer_name'; |
90
|
|
|
$host = 'test.local'; |
91
|
|
|
|
92
|
|
|
$options->setVerifyPeer(false); |
93
|
|
|
$context = $options->toStreamContext($host); |
94
|
|
|
|
95
|
|
|
self::assertArrayHasKey('ssl', $context); |
96
|
|
|
self::assertArrayHasKey('SNI_enabled', $context['ssl']); |
97
|
|
|
self::assertArrayNotHasKey('CN_match', $context['ssl']); |
98
|
|
|
self::assertArrayHasKey($sniPeerNameKey, $context['ssl']); |
99
|
|
|
|
100
|
|
|
self::assertEquals(true, $context['ssl']['SNI_enabled']); |
101
|
|
|
self::assertEquals($host, $context['ssl'][$sniPeerNameKey]); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$options->setVerifyPeer(true); |
105
|
|
|
$context = $options->toStreamContext($host); |
106
|
|
|
|
107
|
|
|
self::assertArrayHasKey('ssl', $context); |
108
|
|
|
self::assertArrayHasKey('SNI_enabled', $context['ssl']); |
109
|
|
|
self::assertArrayHasKey($peerNameKey, $context['ssl']); |
110
|
|
|
self::assertArrayHasKey($sniPeerNameKey, $context['ssl']); |
111
|
|
|
|
112
|
|
|
self::assertEquals(true, $context['ssl']['SNI_enabled']); |
113
|
|
|
self::assertEquals($host, $context['ssl'][$peerNameKey]); |
114
|
|
|
self::assertEquals($host, $context['ssl'][$sniPeerNameKey]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|