Passed
Push — master ( db8110...2b9882 )
by Tim
02:13
created

TestCertificatesTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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 $certdir;
15
    private static $key;
16
17
    public static function setUpBeforeClass()
18
    {
19
        self::$certdir = getcwd().DIRECTORY_SEPARATOR.'vendor/simplesamlphp/simplesamlphp-test-framework/certificates/pem';
20
        self::$key = self::$certdir.DIRECTORY_SEPARATOR.'selfsigned.example.org_nopasswd.key';
21
    }
22
23
    public function testCertExpired()
24
    {
25
        $certFile = self::$certdir.DIRECTORY_SEPARATOR.'expired.example.org.crt';
26
        $cert = file_get_contents($certFile);
27
28
        $testData = new TestData([
29
            'category' => 'Test certificate',
30
            'certData' => $cert,
31
            'certExpirationWarning' => 10,
32
        ]);
33
        $certTest = new TestCase\Cert\Data($testData);
34
        $testResult = $certTest->getTestResult();
35
        $expiration = $testResult->getOutput('expiration');
36
        $this->assertLessThanOrEqual(-1, $expiration);
37
        $this->assertEquals(State::ERROR, $testResult->getState());
38
    }
39
40
    public function testCertAboutToExpire()
41
    {
42
        $certFile = self::$certdir.DIRECTORY_SEPARATOR.'signed.example.org.crt';
43
        $certData = file_get_contents($certFile);
44
        $certInfo = openssl_x509_parse($certData)
45
46
        // Calculate the remaining days for the cert
47
        $exp = (int)(($certInfo['validTo_time_t'] - time()) / 86400);
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE on line 47 at column 8
Loading history...
48
49
        $testData = new TestData([
50
            'category' => 'Test certificate',
51
            'certData' => $certData,
52
            'certExpirationWarning' => 10,
53
        ]);
54
        $certTest = new TestCase\Cert\Data($testData);
55
        $testResult = $certTest->getTestResult();
56
        $expiration = $testResult->getOutput('expiration');
57
58
        // Test that remaining days+4 = greater than $expiration, but less than $expiration+10
59
        $this->assertGreaterThanOrEqual($exp + 4, $expiration);
60
        $this->assertEquals(State::WARNING, $testResult->getState());
61
    }
62
63
    public function testCertFileValid()
64
    {
65
        $testData = new TestData([
66
            'category' => 'Test certificate',
67
            'certFile' => self::$certdir.DIRECTORY_SEPARATOR.'selfsigned.example.org.crt',
68
            'certExpirationWarning' => 10,
69
        ]);
70
        $certTest = new TestCase\Cert\File($testData);
71
        $testResult = $certTest->getTestResult();
72
        $expiration = $testResult->getOutput('expiration');
73
        $this->assertGreaterThanOrEqual(99, $expiration);
74
        $this->assertEquals(State::OK, $testResult->getState());
75
    }
76
}
77