Passed
Push — master ( d67182...7e71cc )
by Tim
02:41
created

TestCertificatesTest::testCertFileValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.9666
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor\Test;
4
5
use \SimpleSAML\Modules\Monitor\TestCase as TestCase;
6
use \SimpleSAML\Modules\Monitor\TestData as TestData;
7
use \SimpleSAML\Modules\Monitor\State as State;
8
9
/**
10
 * Tests for TestCase\Cert\Data and TestCase\Cert\File
11
 */
12
class TestCertificatesTest extends \PHPUnit_Framework_TestCase
13
{
14
    private static $key = '../../../vendor/simplesamlphp/simplesamlphp-test-framework/certificates/pem/selfsigned.example.org_nopasswd.key';
0 ignored issues
show
introduced by
The private property $key is not used, and could be removed.
Loading history...
15
16
    private static $dn;
17
18
    public static function setUpBeforeClass()
19
    {
20
        self::$dn = [
21
            'countryName' => 'NL',
22
            'localityName' => 'Amsterdam',
23
            'organizationName' => 'TestOrganization',
24
        ];
25
    }
26
27
    public function testCertExpired()
28
    {
29
        $dn = self::$dn;
30
        $dn['commonName'] = 'expired';
31
32
        $csr = openssl_csr_new($dn, $key, ['digest_alg' => 'sha256']);
33
        $res = openssl_csr_sign($csr, null, $key, $days = -10, ['digest_alg' => 'sha256']);
34
        openssl_x509_export($res, $cert);
35
36
        $testData = new TestData([
37
            'category' => 'Test certificate',
38
            'certData' => $cert,
39
            'certExpirationWarning' => 10,
40
        ]);
41
        $certTest = new TestCase\Cert\Data($testData);
42
        $testResult = $certTest->getTestResult();
43
        $expiration = $testResult->getOutput('expiration');
44
        $this->assertLessThanOrEqual(-10, $expiration);
45
        $this->assertEquals(State::ERROR, $testResult->getState());
46
    }
47
48
    public function testCertAboutToExpire()
49
    {
50
        $dn = self::$dn;
51
        $dn['commonName'] = 'almostexpired';
52
53
        $csr = openssl_csr_new($dn, $key, ['digest_alg' => 'sha256']);
54
        $res = openssl_csr_sign($csr, null, $key, $days = 5, ['digest_alg' => 'sha256']);
55
        openssl_x509_export($res, $cert);
56
57
        $testData = new TestData([
58
            'category' => 'Test certificate',
59
            'certData' => $cert,
60
            'certExpirationWarning' => 10,
61
        ]);
62
        $certTest = new TestCase\Cert\Data($testData);
63
        $testResult = $certTest->getTestResult();
64
        $expiration = $testResult->getOutput('expiration');
65
        $this->assertGreaterThanOrEqual(4, $expiration);
66
        $this->assertEquals(State::WARNING, $testResult->getState());
67
    }
68
69
    public function testCertFileValid()
70
    {
71
        $testData = new TestData([
72
            'category' => 'Test certificate',
73
            'certFile' => '../../../vendor/simplesamlphp/simplesamlphp-test-framework/certificates/pem/selfsigned.example.org.crt',
74
            'certExpirationWarning' => 10,
75
        ]);
76
        $certTest = new TestCase\Cert\File($testData);
77
        $testResult = $certTest->getTestResult();
78
        $expiration = $testResult->getOutput('expiration');
79
        $this->assertGreaterThanOrEqual(99, $expiration);
80
        $this->assertEquals(State::OK, $testResult->getState());
81
    }
82
}
83