File: MetaDataStorageSourceTest.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (127 lines) | stat: -rw-r--r-- 4,999 bytes parent folder | download | duplicates (3)
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
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Metadata;

use Exception;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Metadata\MetaDataStorageSource;

/**
 * Class MetaDataStorageSourceTest
 */

class MetaDataStorageSourceTest extends TestCase
{
    /**
     * Test \SimpleSAML\Metadata\MetaDataStorageSourceTest::getConfig XML bad source
     * @return void
     */
    public function testBadXMLSource(): void
    {
        $this->expectException(Exception::class);
        MetaDataStorageSource::getSource(["type" => "xml", "foo" => "baa"]);
    }


    /**
     * Test \SimpleSAML\Metadata\MetaDataStorageSourceTest::getConfig invalid static XML source
     * @return void
     */
    public function testInvalidStaticXMLSource(): void
    {
        $this->expectException(Exception::class);
        $strTestXML = "
<EntityDescriptor ID=\"_12345678-90ab-cdef-1234-567890abcdef\" entityID=\"https://saml.idp/entityid\" xmlns=\"urn:oasis:names:tc:SAML:2.0:metadata\">
</EntityDescriptor>
";
        MetaDataStorageSource::getSource(["type" => "xml", "xml" => $strTestXML]);
    }


    /**
     * Test \SimpleSAML\Metadata\MetaDataStorageSourceTest::getConfig XML static XML source
     * @return void
     */
    public function testStaticXMLSource(): void
    {
        $testEntityId = "https://saml.idp/entityid";
        $strTestXML = self::generateIdpMetadataXml($testEntityId);

        // The primary test here is that - in contrast to the others above - this loads without error
        // As a secondary thing, check that the entity ID from the static source provided can be extracted
        $source = MetaDataStorageSource::getSource(["type" => "xml", "xml" => $strTestXML]);
        $idpSet = $source->getMetadataSet("saml20-idp-remote");
        $this->assertArrayHasKey(
            $testEntityId,
            $idpSet,
            "Did not extract expected IdP entity ID from static XML source"
        );

        // Finally verify that a different entity ID does not get loaded
        $this->assertCount(1, $idpSet, "Unexpectedly got metadata for an alternate entity than that defined");
    }


    /**
     * Test loading multiple entities
     * @return void
     */
    public function testLoadEntitiesStaticXMLSource(): void
    {
        $c = [
            'key' => 'value'
        ];
        Configuration::loadFromArray($c, '', 'simplesaml');
        $entityId1 = "https://example.com";
        $xml1 = self::generateIdpMetadataXml($entityId1);
        $entityId2 = "https://saml.idp/entity";
        $xml2 = self::generateIdpMetadataXml($entityId2);
        $strTestXML = "
        <EntitiesDescriptor xmlns=\"urn:oasis:names:tc:SAML:2.0:metadata\">
        $xml1
        $xml2
        </EntitiesDescriptor>
        ";
        $source = MetaDataStorageSource::getSource(["type" => "xml", "xml" => $strTestXML]);
        // search that is a single entity
        $entities = $source->getMetaDataForEntities([$entityId2], "saml20-idp-remote");
        $this->assertCount(1, $entities, 'Only 1 entity loaded');
        $this->assertArrayHasKey($entityId2, $entities);
        // search for multiple entities
        $entities = $source->getMetaDataForEntities([$entityId1, 'no-such-entity', $entityId2], "saml20-idp-remote");
        $this->assertCount(2, $entities, 'Only 2 of the entities are found');
        $this->assertArrayHasKey($entityId1, $entities);
        $this->assertArrayHasKey($entityId2, $entities);
        // search for non-existant entities
        $entities = $source->getMetaDataForEntities(['no-such-entity'], "saml20-idp-remote");
        $this->assertCount(0, $entities, 'no matches expected');
    }


    /**
     * @param string $entityId
     * @return string
     */
    public static function generateIdpMetadataXml(string $entityId): string
    {
        return "
<EntityDescriptor ID=\"_12345678-90ab-cdef-1234-567890abcdef\" entityID=\"$entityId\" xmlns=\"urn:oasis:names:tc:SAML:2.0:metadata\">
<RoleDescriptor xsi:type=\"fed:ApplicationServiceType\"
protocolSupportEnumeration=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512 http://schemas.xmlsoap.org/ws/2005/02/trust http://docs.oasis-open.org/wsfed/federation/200706\"
ServiceDisplayName=\"SimpleSAMLphp Test\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:fed=\"http://docs.oasis-open.org/wsfed/federation/200706\">
<NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:persistent</NameIDFormat>
<SingleSignOnService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect\" Location=\"https://saml.idp/sso/\"/>
<SingleLogoutService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect\" Location=\"https://saml.idp/logout/\"/>
</RoleDescriptor>
<IDPSSODescriptor protocolSupportEnumeration=\"urn:oasis:names:tc:SAML:2.0:protocol\">
<SingleSignOnService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect\" Location=\"https://saml.idp/sso/\"/>
</IDPSSODescriptor>
</EntityDescriptor>
";
    }
}