1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\Utils; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SimpleSAML\XMLSecurity\Utils\Certificate; |
9
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \SimpleSAML\XMLSecurity\Utils\Certificate |
13
|
|
|
* @package simplesamlphp/xml-security |
14
|
|
|
*/ |
15
|
|
|
final class CertificateTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @group utilities |
19
|
|
|
* @test |
20
|
|
|
*/ |
21
|
|
|
public function testValidStructure(): void |
22
|
|
|
{ |
23
|
|
|
$result = Certificate::hasValidStructure( |
24
|
|
|
PEMCertificatesMock::getPlainPublicKey(PEMCertificatesMock::PUBLIC_KEY), |
25
|
|
|
); |
26
|
|
|
$this->assertTrue($result); |
27
|
|
|
|
28
|
|
|
$result = Certificate::hasValidStructure( |
29
|
|
|
PEMCertificatesMock::getPlainPublicKey(PEMCertificatesMock::BROKEN_PUBLIC_KEY), |
30
|
|
|
); |
31
|
|
|
$this->assertFalse($result); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @group utilities |
37
|
|
|
* @test |
38
|
|
|
*/ |
39
|
|
|
public function testConvertToCertificate(): void |
40
|
|
|
{ |
41
|
|
|
$result = Certificate::convertToCertificate(PEMCertificatesMock::getPlainCertificateContents()); |
42
|
|
|
// the formatted public key in PEMCertificatesMock is stored with unix newlines |
43
|
|
|
$this->assertEquals(trim(PEMCertificatesMock::getPlainCertificate()), $result); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
*/ |
49
|
|
|
public function testParseIssuer(): void |
50
|
|
|
{ |
51
|
|
|
// Test string input |
52
|
|
|
$result = Certificate::parseIssuer('test'); |
53
|
|
|
$this->assertEquals('test', $result); |
54
|
|
|
|
55
|
|
|
// Test array input |
56
|
|
|
$result = Certificate::parseIssuer( |
57
|
|
|
[ |
58
|
|
|
'C' => 'US', |
59
|
|
|
'S' => 'Hawaii', |
60
|
|
|
'L' => 'Honolulu', |
61
|
|
|
'O' => 'SimpleSAMLphp HQ', |
62
|
|
|
'CN' => 'SimpleSAMLphp Testing CA', |
63
|
|
|
], |
64
|
|
|
); |
65
|
|
|
$this->assertEquals('CN=SimpleSAMLphp Testing CA,O=SimpleSAMLphp HQ,L=Honolulu,S=Hawaii,C=US', $result); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|