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