Passed
Push — master ( feb95e...9f38d8 )
by Tim
01:59
created

SignatureTest::testMarshallingElementOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 31
c 3
b 1
f 0
nc 1
nop 0
dl 0
loc 49
rs 9.424
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
12
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
13
use SimpleSAML\XMLSecurity\Utils\XPath;
14
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15
use SimpleSAML\XMLSecurity\XML\ds\DsObject;
16
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
17
use SimpleSAML\XMLSecurity\XML\ds\Signature;
18
use SimpleSAML\XMLSecurity\XML\ds\SignatureValue;
19
use SimpleSAML\XMLSecurity\XML\ds\SignedInfo;
20
21
use function dirname;
22
use function strval;
23
24
/**
25
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\SignatureTest
26
 *
27
 * @package simplesamlphp/xml-security
28
 */
29
#[CoversClass(AbstractDsElement::class)]
30
#[CoversClass(Signature::class)]
31
final class SignatureTest extends TestCase
32
{
33
    use SchemaValidationTestTrait;
34
    use SerializableElementTestTrait;
35
36
    /**
37
     * Set up the test.
38
     */
39
    public static function setUpBeforeClass(): void
40
    {
41
        self::$testedClass = Signature::class;
42
43
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
44
            dirname(__FILE__, 3) . '/resources/xml/ds_Signature.xml',
45
        );
46
    }
47
48
49
    /**
50
     * Test creating a SignatureValue from scratch.
51
     */
52
    public function testMarshalling(): void
53
    {
54
        $signature = new Signature(
55
            SignedInfo::fromXML(
56
                DOMDocumentFactory::fromFile(
57
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfo.xml',
58
                )->documentElement,
59
            ),
60
            SignatureValue::fromXML(
61
                DOMDocumentFactory::fromFile(
62
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignatureValue.xml',
63
                )->documentElement,
64
            ),
65
            KeyInfo::fromXML(
66
                DOMDocumentFactory::fromFile(
67
                    dirname(__FILE__, 3) . '/resources/xml/ds_KeyInfo.xml',
68
                )->documentElement,
69
            ),
70
            [
71
                new DsObject(
72
                    null,
73
                    null,
74
                    null,
75
                    [
76
                        new Chunk(
77
                            DOMDocumentFactory::fromString(
78
                                '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
79
                            )->documentElement,
80
                        ),
81
                    ],
82
                ),
83
            ],
84
            'def456',
85
        );
86
87
        $this->assertEquals(
88
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
89
            strval($signature),
90
        );
91
    }
92
93
94
    /**
95
     */
96
    public function testMarshallingElementOrdering(): void
97
    {
98
        $signature = new Signature(
99
            SignedInfo::fromXML(
100
                DOMDocumentFactory::fromFile(
101
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfo.xml',
102
                )->documentElement,
103
            ),
104
            SignatureValue::fromXML(
105
                DOMDocumentFactory::fromFile(
106
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignatureValue.xml',
107
                )->documentElement,
108
            ),
109
            KeyInfo::fromXML(
110
                DOMDocumentFactory::fromFile(
111
                    dirname(__FILE__, 3) . '/resources/xml/ds_KeyInfo.xml',
112
                )->documentElement,
113
            ),
114
            [
115
                new DsObject(
116
                    null,
117
                    null,
118
                    null,
119
                    [
120
                        new Chunk(
121
                            DOMDocumentFactory::fromString(
122
                                '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
123
                            )->documentElement,
124
                        ),
125
                    ],
126
                ),
127
            ],
128
            'def456',
129
        );
130
131
        $signatureElement = $signature->toXML();
132
        $xpCache = XPath::getXPath($signatureElement);
133
134
        $signedInfo = XPath::xpQuery($signatureElement, './ds:SignedInfo', $xpCache);
135
        $this->assertCount(1, $signedInfo);
136
137
        /** @var \DOMElement[] $signatureElements */
138
        $signatureElements = XPath::xpQuery($signatureElement, './ds:SignedInfo/following-sibling::*', $xpCache);
139
140
        // Test ordering of Signature contents
141
        $this->assertCount(3, $signatureElements);
142
        $this->assertEquals('ds:SignatureValue', $signatureElements[0]->tagName);
143
        $this->assertEquals('ds:KeyInfo', $signatureElements[1]->tagName);
144
        $this->assertEquals('ds:Object', $signatureElements[2]->tagName);
145
    }
146
}
147