Passed
Push — master ( bb5c60...87d27f )
by Tim
01:35
created

TestCertificatesTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testCertFileValid() 0 22 1
A testCertExpired() 0 19 1
A testCertAboutToExpire() 0 19 1
A tearDownAfterClass() 0 5 1
A setUpBeforeClass() 0 12 1
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;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Modules\Monitor\State was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
15
16
    private static $dn;
17
18
    public static function setUpBeforeClass()
19
    {
20
        self::$key = openssl_pkey_new([
21
            'digest_alg' => 'sha256',
22
            'private_key_bits' => '1024',
23
            'private_key_type' => OPENSSL_KEYTYPE_RSA,
24
        ]);
25
26
        self::$dn = [
27
            'countryName' => 'NL',
28
            'localityName' => 'Amsterdam',
29
            'organizationName' => 'TestOrganization',
30
        ];
31
    }
32
33
    public static function tearDownAfterClass()
34
    {
35
        self::$key = null;
36
        self::$dn = null;
37
        unlink(sys_get_temp_dir().'/validcert.crt');
38
    }
39
40
    public function testCertExpired()
41
    {
42
        $dn = self::$dn;
43
        $dn['commonName'] = 'expired';
44
45
        $csr = openssl_csr_new($dn, $key, ['digest_alg' => 'sha256']);
46
        $res = openssl_csr_sign($csr, null, $key, $days = -10, ['digest_alg' => 'sha256']);
47
        openssl_x509_export($res, $cert);
48
49
        $testData = new TestData([
50
            'category' => 'Test certificate',
51
            'certData' => $cert,
52
            'certExpirationWarning' => 10,
53
        ]);
54
        $certTest = new TestCase\Cert\Data($testData);
55
        $testResult = $certTest->getTestResult();
56
        $expiration = $testResult->getOutput('expiration');
57
        $this->assertLessThanOrEqual(-10, $expiration);
58
        $this->assertEquals(State::ERROR, $testResult->getState());
59
    }
60
61
    public function testCertAboutToExpire()
62
    {
63
        $dn = self::$dn;
64
        $dn['commonName'] = 'almostexpired';
65
66
        $csr = openssl_csr_new($dn, $key, ['digest_alg' => 'sha256']);
67
        $res = openssl_csr_sign($csr, null, $key, $days = 5, ['digest_alg' => 'sha256']);
68
        openssl_x509_export($res, $cert);
69
70
        $testData = new TestData([
71
            'category' => 'Test certificate',
72
            'certData' => $cert,
73
            'certExpirationWarning' => 10,
74
        ]);
75
        $certTest = new TestCase\Cert\Data($testData);
76
        $testResult = $certTest->getTestResult();
77
        $expiration = $testResult->getOutput('expiration');
78
        $this->assertGreaterThanOrEqual(4, $expiration);
79
        $this->assertEquals(State::WARNING, $testResult->getState());
80
    }
81
82
    public function testCertFileValid()
83
    {
84
        $dn = self::$dn;
85
        $dn['commonName'] = 'valid';
86
87
        $csr = openssl_csr_new($dn, $key, ['digest_alg' => 'sha256']);
88
        $res = openssl_csr_sign($csr, null, $key, $days = 100, ['digest_alg' => 'sha256']);
89
        openssl_x509_export($res, $cert);
90
91
        $certFile = sys_get_temp_dir().'/validcert.crt';
92
        file_put_contents($certFile, $cert);
93
94
        $testData = new TestData([
95
            'category' => 'Test certificate',
96
            'certFile' => $certFile,
97
            'certExpirationWarning' => 10,
98
        ]);
99
        $certTest = new TestCase\Cert\File($testData);
100
        $testResult = $certTest->getTestResult();
101
        $expiration = $testResult->getOutput('expiration');
102
        $this->assertGreaterThanOrEqual(99, $expiration);
103
        $this->assertEquals(State::OK, $testResult->getState());
104
    }
105
}
106