|
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; |
|
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
|
|
|
|