Passed
Branch master (4b23d6)
by Tim
04:40
created

TestCertificatesTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SimpleSAML\Module\Monitor\Test;
4
5
use SimpleSAML\Module\Monitor\TestCase;
6
use SimpleSAML\Module\Monitor\TestData;
7
use SimpleSAML\Module\Monitor\State;
8
9
/**
10
 * Tests for TestCase\Cert\Data and TestCase\Cert\File
11
 */
12
class TestCertificatesTest extends \PHPUnit\Framework\TestCase
13
{
14
    /** @var string */
15
    private static $certdir;
16
17
    /** @var string */
18
    private static $key;
19
20
    public static function setUpBeforeClass(): void
21
    {
22
        self::$certdir = getcwd() . '/vendor/simplesamlphp/simplesamlphp-test-framework/certificates/pem';
23
        self::$key = self::$certdir . '/selfsigned.example.org_nopasswd.key';
24
    }
25
26
    public function testCertExpired(): void
27
    {
28
        $certFile = self::$certdir . '/expired.example.org.crt';
29
        $cert = file_get_contents($certFile);
30
31
        $testData = new TestData([
32
            'category' => 'Test certificate',
33
            'certData' => $cert,
34
            'certExpirationWarning' => 10,
35
        ]);
36
        $certTest = new TestCase\Cert\Data($testData);
37
        $testResult = $certTest->getTestResult();
38
        $expiration = $testResult->getOutput('expiration');
39
        $this->assertLessThanOrEqual(-1, $expiration);
40
        $this->assertEquals(State::ERROR, $testResult->getState());
41
    }
42
43
    public function testCertAboutToExpire(): void
44
    {
45
        $certFile = self::$certdir . '/signed.example.org.crt';
46
        $certData = file_get_contents($certFile);
47
        $certInfo = openssl_x509_parse($certData);
48
49
        // Calculate the remaining days for the cert
50
        $exp = (int)(($certInfo['validTo_time_t'] - time()) / 86400);
51
52
        $testData = new TestData([
53
            'category' => 'Test certificate',
54
            'certData' => $certData,
55
            'certExpirationWarning' => $exp + 10,
56
        ]);
57
58
        $certTest = new TestCase\Cert\Data($testData);
59
        $testResult = $certTest->getTestResult();
60
        $expiration = $testResult->getOutput('expiration');
61
62
        // Test that remaining days-4 = greater than $expiration, but less than $expiration+10
63
        $this->assertGreaterThanOrEqual($exp - 4, $expiration);
64
        $this->assertEquals(State::WARNING, $testResult->getState());
65
    }
66
67
    public function testCertFileValid(): void
68
    {
69
        $testData = new TestData([
70
            'category' => 'Test certificate',
71
            'certFile' => self::$certdir . '/selfsigned.example.org.crt',
72
            'certExpirationWarning' => 10,
73
        ]);
74
        $certTest = new TestCase\Cert\File($testData);
75
        $testResult = $certTest->getTestResult();
76
        $expiration = $testResult->getOutput('expiration');
77
        $this->assertGreaterThanOrEqual(99, $expiration);
78
        $this->assertEquals(State::OK, $testResult->getState());
79
    }
80
}
81