|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\XML\ds; |
|
6
|
|
|
|
|
7
|
|
|
use DOMDocument; |
|
8
|
|
|
use PHPUnit\Framework\Attributes\{CoversClass, Group}; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
|
11
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
|
12
|
|
|
use SimpleSAML\XML\TestUtils\{SchemaValidationTestTrait, SerializableElementTestTrait}; |
|
13
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\{AbstractDsElement, KeyValue, RSAKeyValue}; |
|
14
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionProperty; |
|
15
|
|
|
|
|
16
|
|
|
use function dirname; |
|
17
|
|
|
use function strval; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class \SimpleSAML\XMLSecurity\Test\XML\ds\KeyValueTest |
|
21
|
|
|
* |
|
22
|
|
|
* @package simplesamlphp/xml-security |
|
23
|
|
|
*/ |
|
24
|
|
|
#[Group('ds')] |
|
25
|
|
|
#[CoversClass(AbstractDsElement::class)] |
|
26
|
|
|
#[CoversClass(KeyValue::class)] |
|
27
|
|
|
final class KeyValueTest extends TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
use SchemaValidationTestTrait; |
|
30
|
|
|
use SerializableElementTestTrait; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** @var \DOMDocument $empty */ |
|
34
|
|
|
protected static DOMDocument $empty; |
|
35
|
|
|
|
|
36
|
|
|
/** @var \DOMDocument $rsaKeyValue */ |
|
37
|
|
|
protected static DOMDocument $rsaKeyValue; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \DOMDocument $encryptionProperty */ |
|
40
|
|
|
protected static DOMDocument $encryptionProperty; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function setUp(): void |
|
46
|
|
|
{ |
|
47
|
|
|
self::$testedClass = KeyValue::class; |
|
48
|
|
|
|
|
49
|
|
|
self::$empty = DOMDocumentFactory::fromString('<ds:KeyValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>'); |
|
50
|
|
|
|
|
51
|
|
|
self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
|
52
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_KeyValue.xml', |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
self::$rsaKeyValue = DOMDocumentFactory::fromFile( |
|
56
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_RSAKeyValue.xml', |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
self::$encryptionProperty = DOMDocumentFactory::fromFile( |
|
60
|
|
|
dirname(__FILE__, 3) . '/resources/xml/xenc_EncryptionProperty.xml', |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testMarshalling(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$keyValue = new KeyValue(RSAKeyValue::fromXML(self::$rsaKeyValue->documentElement)); |
|
70
|
|
|
|
|
71
|
|
|
$rsaKeyValue = $keyValue->getRSAKeyValue(); |
|
72
|
|
|
$this->assertInstanceOf(RSAKeyValue::class, $rsaKeyValue); |
|
73
|
|
|
$this->assertEmpty($keyValue->getElements()); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertEquals($rsaKeyValue->getModulus()->getContent(), 'dGhpcyBpcyBzb21lIHJhbmRvbSBtb2R1bHVzCg=='); |
|
76
|
|
|
$this->assertEquals($rsaKeyValue->getExponent()->getContent(), 'dGhpcyBpcyBzb21lIHJhbmRvbSBleHBvbmVudAo='); |
|
77
|
|
|
|
|
78
|
|
|
$document = self::$empty; |
|
79
|
|
|
$document->documentElement->appendChild($document->importNode(self::$rsaKeyValue->documentElement, true)); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertXmlStringEqualsXmlString($document->saveXML($document->documentElement), strval($keyValue)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testMarshallingWithOtherElement(): void |
|
88
|
|
|
{ |
|
89
|
|
|
$keyValue = new KeyValue(null, EncryptionProperty::fromXML(self::$encryptionProperty->documentElement)); |
|
90
|
|
|
|
|
91
|
|
|
$elements = $keyValue->getElements(); |
|
92
|
|
|
$this->assertEmpty($keyValue->getRSAKeyValue()); |
|
93
|
|
|
$this->assertCount(1, $elements); |
|
94
|
|
|
|
|
95
|
|
|
$element = reset($elements); |
|
96
|
|
|
$this->assertInstanceOf(EncryptionProperty::class, $element); |
|
97
|
|
|
|
|
98
|
|
|
$document = self::$empty; |
|
99
|
|
|
$element->toXML($document->documentElement); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertXmlStringEqualsXmlString($document->saveXML($document->documentElement), strval($keyValue)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testMarshallingEmpty(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->expectException(SchemaViolationException::class); |
|
110
|
|
|
$this->expectExceptionMessage( |
|
111
|
|
|
'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other', |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
new KeyValue(null, null); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testUnmarshallingWithOtherElement(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$document = self::$empty; |
|
123
|
|
|
$document->documentElement->appendChild( |
|
124
|
|
|
$document->importNode(self::$encryptionProperty->documentElement, true), |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$keyValue = KeyValue::fromXML($document->documentElement); |
|
128
|
|
|
|
|
129
|
|
|
$elements = $keyValue->getElements(); |
|
130
|
|
|
$this->assertNull($keyValue->getRSAKeyValue()); |
|
131
|
|
|
$this->assertCount(1, $elements); |
|
132
|
|
|
|
|
133
|
|
|
$element = reset($elements); |
|
134
|
|
|
$this->assertInstanceOf(EncryptionProperty::class, $element); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testUnmarshallingEmpty(): void |
|
141
|
|
|
{ |
|
142
|
|
|
$document = self::$empty; |
|
143
|
|
|
|
|
144
|
|
|
$this->expectException(SchemaViolationException::class); |
|
145
|
|
|
$this->expectExceptionMessage( |
|
146
|
|
|
'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other', |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
KeyValue::fromXML($document->documentElement); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|