1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\XML\ds; |
6
|
|
|
|
7
|
|
|
use DOMDocument; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use SimpleSAML\Assert\AssertionFailedException; |
10
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
11
|
|
|
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait; |
12
|
|
|
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; |
13
|
|
|
use SimpleSAML\XMLSecurity\Test\XML\XMLDumper; |
14
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
15
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate; |
16
|
|
|
|
17
|
|
|
use function dirname; |
18
|
|
|
use function str_replace; |
19
|
|
|
use function strval; |
20
|
|
|
use function substr; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class \SimpleSAML\XMLSecurity\Test\XML\ds\X509CertificateTest |
24
|
|
|
* |
25
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement |
26
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\ds\X509Certificate |
27
|
|
|
* |
28
|
|
|
* @package simplesamlphp/xml-security |
29
|
|
|
*/ |
30
|
|
|
final class X509CertificateTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
use SerializableElementTestTrait; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private static string $certificate; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
*/ |
40
|
|
|
public static function setUpBeforeClass(): void |
41
|
|
|
{ |
42
|
|
|
self::$testedClass = X509Certificate::class; |
43
|
|
|
|
44
|
|
|
self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
45
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_X509Certificate.xml', |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
self::$certificate = str_replace( |
49
|
|
|
[ |
50
|
|
|
'-----BEGIN CERTIFICATE-----', |
51
|
|
|
'-----END CERTIFICATE-----', |
52
|
|
|
'-----BEGIN RSA PUBLIC KEY-----', |
53
|
|
|
'-----END RSA PUBLIC KEY-----', |
54
|
|
|
"\r\n", |
55
|
|
|
"\n", |
56
|
|
|
], |
57
|
|
|
[ |
58
|
|
|
'', |
59
|
|
|
'', |
60
|
|
|
'', |
61
|
|
|
'', |
62
|
|
|
"\n", |
63
|
|
|
'' |
64
|
|
|
], |
65
|
|
|
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
*/ |
72
|
|
|
public function testMarshalling(): void |
73
|
|
|
{ |
74
|
|
|
$x509cert = new X509Certificate(self::$certificate); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( |
77
|
|
|
XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation), |
78
|
|
|
strval($x509cert), |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
*/ |
85
|
|
|
public function testMarshallingInvalidBase64(): void |
86
|
|
|
{ |
87
|
|
|
$certificate = str_replace(substr(self::$certificate, 1), '', self::$certificate); |
88
|
|
|
$this->expectException(AssertionFailedException::class); |
89
|
|
|
new X509Certificate($certificate); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
*/ |
95
|
|
|
public function testUnmarshalling(): void |
96
|
|
|
{ |
97
|
|
|
$x509cert = X509Certificate::fromXML(self::$xmlRepresentation->documentElement); |
98
|
|
|
|
99
|
|
|
$this->assertEquals( |
100
|
|
|
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
101
|
|
|
strval($x509cert), |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|