/
AttributeAuthorityDescriptorTest.php
356 lines (301 loc) · 12.2 KB
/
AttributeAuthorityDescriptorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\SAML2\XML\md;
use Exception;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\SAML2\XML\md\AssertionIDRequestService;
use SimpleSAML\SAML2\XML\md\AttributeAuthorityDescriptor;
use SimpleSAML\SAML2\XML\md\AttributeProfile;
use SimpleSAML\SAML2\XML\md\AttributeService;
use SimpleSAML\SAML2\XML\md\NameIDFormat;
use SimpleSAML\SAML2\XML\saml\Attribute;
use SimpleSAML\SAML2\XML\saml\AttributeValue;
use SimpleSAML\Test\SAML2\Constants as C;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XML\Utils as XMLUtils;
use SimpleSAML\XMLSecurity\TestUtils\SignedElementTestTrait;
use function dirname;
use function strval;
/**
* Tests for the AttributeAuthorityDescriptor class.
*
* @covers \SimpleSAML\SAML2\XML\md\AbstractMdElement
* @covers \SimpleSAML\SAML2\XML\md\AbstractSignedMdElement
* @covers \SimpleSAML\SAML2\XML\md\AbstractMetadataDocument
* @covers \SimpleSAML\SAML2\XML\md\AbstractRoleDescriptorType
* @covers \SimpleSAML\SAML2\XML\md\AbstractRoleDescriptor
* @covers \SimpleSAML\SAML2\XML\md\AttributeAuthorityDescriptor
* @package simplesamlphp/saml2
*/
final class AttributeAuthorityDescriptorTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;
use SignedElementTestTrait;
/** @var \SimpleSAML\SAML2\XML\md\AttributeService */
private static AttributeService $as;
/** @var \SimpleSAML\SAML2\XML\md\AssertionIDRequestService */
private static AssertionIDRequestService $aidrs;
/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/saml-schema-metadata-2.0.xsd';
self::$testedClass = AttributeAuthorityDescriptor::class;
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/md_AttributeAuthorityDescriptor.xml',
);
self::$as = new AttributeService(
C::BINDING_SOAP,
"https://IdentityProvider.com/SAML/AA/SOAP",
);
self::$aidrs = new AssertionIDRequestService(
C::BINDING_URI,
"https://IdentityProvider.com/SAML/AA/URI",
);
}
// test marshalling
/**
* Test creating an AttributeAuthorityDescriptor from scratch
*/
public function testMarshalling(): void
{
$attr1 = new Attribute(
"urn:oid:1.3.6.1.4.1.5923.1.1.1.6",
C::NAMEFORMAT_URI,
"eduPersonPrincipalName",
);
$attr2 = new Attribute(
"urn:oid:1.3.6.1.4.1.5923.1.1.1.1",
C::NAMEFORMAT_URI,
'eduPersonAffiliation',
[
new AttributeValue('member'),
new AttributeValue('student'),
new AttributeValue('faculty'),
new AttributeValue('employee'),
new AttributeValue('staff'),
],
);
$aad = new AttributeAuthorityDescriptor(
[self::$as],
[C::NS_SAMLP],
[self::$aidrs],
[
new NameIDFormat(C::NAMEID_X509_SUBJECT_NAME),
new NameIDFormat(C::NAMEID_PERSISTENT),
new NameIDFormat(C::NAMEID_TRANSIENT),
],
[
new AttributeProfile(C::PROFILE_1),
new AttributeProfile(C::PROFILE_2),
],
[$attr1, $attr2],
);
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($aad),
);
}
/**
* Test that creating an AttributeAuthorityDescriptor with no supported protocols fails.
*/
public function testMarshallingWithoutSupportedProtocols(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage(
'At least one protocol must be supported by this md:AttributeAuthorityDescriptor.',
);
new AttributeAuthorityDescriptor([self::$as], []);
}
/**
* Test that creating an AttributeAuthorityDescriptor with an empty supported protocol fails.
*/
public function testMarshallingWithEmptySupportedProtocols(): void
{
$this->expectException(AssertionFailedException::class);
new AttributeAuthorityDescriptor([self::$as], []);
}
/**
* Test that creating an AttributeAuthorityDescriptor with no AttributeService fails.
*/
public function testMarshallingWithoutAttributeServices(): void
{
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage('AttributeAuthorityDescriptor must contain at least one AttributeService.');
new AttributeAuthorityDescriptor([], [C::NS_SAMLP]);
}
/**
* Test that creating an AttributeAuthorityDescriptor with an AttributeService of the wrong type fails.
*/
public function testMarshallingWithWrongAttributeService(): void
{
$this->expectException(InvalidDOMElementException::class);
$this->expectExceptionMessage('AttributeService is not an instance of EndpointType.');
/** @psalm-suppress InvalidArgument */
new AttributeAuthorityDescriptor(['string'], [C::NS_SAMLP]);
}
/**
* Test that creating an AttributeAuthorityDescriptor without optional parameters works.
*/
public function testMarshallingWithoutOptionalParameters(): void
{
$aad = new AttributeAuthorityDescriptor([self::$as], [C::NS_SAMLP]);
$this->assertEmpty($aad->getAssertionIDRequestService());
$this->assertEmpty($aad->getNameIDFormat());
$this->assertEmpty($aad->getID());
$this->assertEmpty($aad->getValidUntil());
$this->assertEmpty($aad->getCacheDuration());
$this->assertEmpty($aad->getExtensions());
$this->assertEmpty($aad->getErrorURL());
$this->assertEmpty($aad->getOrganization());
$this->assertEmpty($aad->getKeyDescriptor());
$this->assertEmpty($aad->getContactPerson());
}
/**
* Test that creating an AttributeAuthorityDescriptor with empty AssertionIDRequestService works.
*/
public function testMarshallingWithEmptyAssertionIDRequestService(): void
{
$aad = new AttributeAuthorityDescriptor([self::$as], [C::NS_SAMLP], []);
$this->assertEmpty($aad->getAssertionIDRequestService());
$this->assertEmpty($aad->getNameIDFormat());
$this->assertEmpty($aad->getID());
$this->assertEmpty($aad->getValidUntil());
$this->assertEmpty($aad->getCacheDuration());
$this->assertEmpty($aad->getExtensions());
$this->assertEmpty($aad->getErrorURL());
$this->assertEmpty($aad->getOrganization());
$this->assertEmpty($aad->getKeyDescriptor());
$this->assertEmpty($aad->getContactPerson());
}
/**
* Test that creating an AttributeAuthorityDescriptor with wrong AssertionIDRequestService fails.
*/
public function testMarshallingWithWrongAssertionIDRequestService(): void
{
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage(
'Expected an instance of SimpleSAML\SAML2\XML\md\AssertionIDRequestService. Got: string',
);
/** @psalm-suppress InvalidArgument */
new AttributeAuthorityDescriptor([self::$as], [C::NS_SAMLP], ['x']);
}
/**
* Test that creating an AttributeAuthorityDescriptor with an empty NameIDFormat fails.
*/
public function testMarshallingWithEmptyNameIDFormat(): void
{
$this->expectException(SchemaViolationException::class);
new AttributeAuthorityDescriptor([self::$as], [C::NS_SAMLP], [self::$aidrs], [new NameIDFormat('')]);
}
/**
* Test that creating an AttributeAuthorityDescriptor with an empty AttributeProfile fails.
*/
public function testMarshallingWithEmptyAttributeProfile(): void
{
$this->expectException(SchemaViolationException::class);
new AttributeAuthorityDescriptor(
[self::$as],
[C::NS_SAMLP],
[self::$aidrs],
[new NameIDFormat(C::NAMEID_TRANSIENT)],
[new AttributeProfile('')],
);
}
/**
* Test that creating an AttributeAuthorityDescriptor with wrong Attribute fails.
*/
public function testMarshallingWithWrongAttribute(): void
{
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage(
'Expected an instance of SimpleSAML\SAML2\XML\saml\Attribute. Got: string',
);
/** @psalm-suppress InvalidArgument */
new AttributeAuthorityDescriptor(
[self::$as],
[C::NS_SAMLP],
[self::$aidrs],
[new NameIDFormat(C::NAMEID_PERSISTENT)],
[new AttributeProfile(C::PROFILE_1)],
['x'],
);
}
// test unmarshalling
/**
* Test creating an AttributeAuthorityDescriptor from XML
*/
public function testUnmarshalling(): void
{
$aad = AttributeAuthorityDescriptor::fromXML(self::$xmlRepresentation->documentElement);
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($aad),
);
}
/**
* Test that creating an AttributeAuthorityDescriptor without any optional element works.
*/
public function testUnmarshallingWithoutOptionalElements(): void
{
$mdns = C::NS_MD;
$document = DOMDocumentFactory::fromString(<<<XML
<md:AttributeAuthorityDescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:md="{$mdns}">
<md:AttributeService Binding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
Location="https://IdentityProvider.com/SAML/AA/SOAP"/>
</md:AttributeAuthorityDescriptor>
XML
);
$aad = AttributeAuthorityDescriptor::fromXML($document->documentElement);
$this->assertEmpty($aad->getAssertionIDRequestService());
$this->assertEmpty($aad->getNameIDFormat());
$this->assertEmpty($aad->getID());
$this->assertEmpty($aad->getValidUntil());
$this->assertEmpty($aad->getCacheDuration());
$this->assertEmpty($aad->getExtensions());
$this->assertEmpty($aad->getErrorURL());
$this->assertEmpty($aad->getOrganization());
$this->assertEmpty($aad->getKeyDescriptor());
$this->assertEmpty($aad->getContactPerson());
}
/**
* Test that creating an AttributeAuthorityDescriptor with an empty NameIDFormat fails.
*/
public function testUnmarshallingWithEmptyNameIDFormat(): void
{
$mdns = C::NS_MD;
$document = DOMDocumentFactory::fromString(<<<XML
<md:AttributeAuthorityDescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:md="{$mdns}">
<md:AttributeService Binding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
Location="https://IdentityProvider.com/SAML/AA/SOAP"/>
<md:NameIDFormat></md:NameIDFormat>
</md:AttributeAuthorityDescriptor>
XML
);
$this->expectException(SchemaViolationException::class);
AttributeAuthorityDescriptor::fromXML($document->documentElement);
}
/**
* Test that creating an AttributeAuthorityDescriptor with an empty AttributeProfile fails.
*/
public function testUnmarshallingWithEmptyAttributeProfile(): void
{
$mdns = C::NS_MD;
$document = DOMDocumentFactory::fromString(<<<XML
<md:AttributeAuthorityDescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:md="{$mdns}">
<md:AttributeService Binding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
Location="https://IdentityProvider.com/SAML/AA/SOAP"/>
<md:AttributeProfile></md:AttributeProfile>
</md:AttributeAuthorityDescriptor>
XML
);
$this->expectException(SchemaViolationException::class);
AttributeAuthorityDescriptor::fromXML($document->documentElement);
}
}