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