Passed
Push — master ( 8dceab...2d1996 )
by Benjamin
12:28
created

SslOptionsTest::testToStreamContextWithHostname()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 30
rs 9.584
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::assertFalse($options->getAllowSelfSigned());
27
        self::assertNull($options->getCaFile());
28
        self::assertNull($options->getCiphers());
29
30
        // test setters
31
        $options->setVerifyPeer(false);
32
        $options->setAllowSelfSigned(true);
33
        $options->setCaFile('/path/to/ca');
34
        $options->setCiphers('ALL:!ADH:@STRENGTH');
35
36
        self::assertFalse($options->getVerifyPeer());
37
        self::assertTrue($options->getAllowSelfSigned());
38
        self::assertEquals('/path/to/ca', $options->getCaFile());
39
        self::assertEquals('ALL:!ADH:@STRENGTH', $options->getCiphers());
40
    }
41
42
    public function testToStreamContext(): void
43
    {
44
        $options = new SslOptions();
45
46
        self::assertEquals([
47
            'ssl' => [
48
                'verify_peer' => true,
49
                'allow_self_signed' => false,
50
            ]
51
        ], $options->toStreamContext());
52
53
        $options->setVerifyPeer(false);
54
        $options->setAllowSelfSigned(true);
55
        $options->setCaFile('/path/to/ca');
56
        $options->setCiphers('ALL:!ADH:@STRENGTH');
57
58
        self::assertEquals([
59
            'ssl' => [
60
                'verify_peer' => false,
61
                'allow_self_signed' => true,
62
                'cafile' => '/path/to/ca',
63
                'ciphers' => 'ALL:!ADH:@STRENGTH'
64
            ]
65
        ], $options->toStreamContext());
66
67
        $options->setCaFile(null);
68
        $options->setCiphers(null);
69
70
        self::assertEquals([
71
            'ssl' => [
72
                'verify_peer' => false,
73
                'allow_self_signed' => true,
74
            ]
75
        ], $options->toStreamContext());
76
    }
77
78
    public function testToStreamContextWithHostname(): void
79
    {
80
        $options = new SslOptions();
81
        $peerNameKey = PHP_VERSION_ID < 50600 ? 'CN_match' : 'peer_name';
82
        $sniPeerNameKey = PHP_VERSION_ID < 50600 ? 'SNI_server_name' : 'peer_name';
83
        $host = 'test.local';
84
85
        $options->setVerifyPeer(false);
86
        $context = $options->toStreamContext($host);
87
88
        self::assertArrayHasKey('ssl', $context);
89
        self::assertArrayHasKey('SNI_enabled', $context['ssl']);
90
        self::assertArrayNotHasKey('CN_match', $context['ssl']);
91
        self::assertArrayHasKey($sniPeerNameKey, $context['ssl']);
92
93
        self::assertEquals(true, $context['ssl']['SNI_enabled']);
94
        self::assertEquals($host, $context['ssl'][$sniPeerNameKey]);
95
96
97
        $options->setVerifyPeer(true);
98
        $context = $options->toStreamContext($host);
99
100
        self::assertArrayHasKey('ssl', $context);
101
        self::assertArrayHasKey('SNI_enabled', $context['ssl']);
102
        self::assertArrayHasKey($peerNameKey, $context['ssl']);
103
        self::assertArrayHasKey($sniPeerNameKey, $context['ssl']);
104
105
        self::assertEquals(true, $context['ssl']['SNI_enabled']);
106
        self::assertEquals($host, $context['ssl'][$peerNameKey]);
107
        self::assertEquals($host, $context['ssl'][$sniPeerNameKey]);
108
    }
109
}
110