Passed
Branch master (c86cc6)
by Tim
11:28
created

SignatureTest::testMarshalling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 38
rs 9.52
c 0
b 0
f 0
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\XML\Chunk;
9
use SimpleSAML\XML\DOMDocumentFactory;
10
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
11
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12
use SimpleSAML\XMLSecurity\Utils\XPath;
13
use SimpleSAML\XMLSecurity\XML\ds\DsObject;
14
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
15
use SimpleSAML\XMLSecurity\XML\ds\Signature;
16
use SimpleSAML\XMLSecurity\XML\ds\SignatureValue;
17
use SimpleSAML\XMLSecurity\XML\ds\SignedInfo;
18
19
use function dirname;
20
use function strval;
21
22
/**
23
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\SignatureTest
24
 *
25
 * @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
26
 * @covers \SimpleSAML\XMLSecurity\XML\ds\Signature
27
 *
28
 * @package simplesamlphp/xml-security
29
 */
30
final class SignatureTest extends TestCase
31
{
32
    use SchemaValidationTestTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TestUtils\SchemaValidationTestTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\Test\XML\ds\SignatureTest: $documentElement, $ownerDocument, $message, $line
Loading history...
33
    use SerializableElementTestTrait;
0 ignored issues
show
Bug introduced by
The trait SimpleSAML\XML\TestUtils...lizableElementTestTrait requires the property $documentElement which is not provided by SimpleSAML\XMLSecurity\Test\XML\ds\SignatureTest.
Loading history...
34
35
    /**
36
     * Set up the test.
37
     */
38
    public static function setUpBeforeClass(): void
39
    {
40
        self::$testedClass = Signature::class;
41
42
        self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd';
43
44
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
45
            dirname(__FILE__, 3) . '/resources/xml/ds_Signature.xml',
46
        );
47
    }
48
49
50
    /**
51
     * Test creating a SignatureValue from scratch.
52
     */
53
    public function testMarshalling(): void
54
    {
55
        $signature = new Signature(
56
            SignedInfo::fromXML(
57
                DOMDocumentFactory::fromFile(
58
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfo.xml',
59
                )->documentElement,
60
            ),
61
            SignatureValue::fromXML(
62
                DOMDocumentFactory::fromFile(
63
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignatureValue.xml',
64
                )->documentElement,
65
            ),
66
            KeyInfo::fromXML(
67
                DOMDocumentFactory::fromFile(
68
                    dirname(__FILE__, 3) . '/resources/xml/ds_KeyInfo.xml',
69
                )->documentElement,
70
            ),
71
            [
72
                new DsObject(
73
                    null,
74
                    null,
75
                    null,
76
                    [
77
                        new Chunk(
78
                            DOMDocumentFactory::fromString(
79
                                '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
80
                            )->documentElement,
81
                        ),
82
                    ],
83
                ),
84
            ],
85
            'def456',
86
        );
87
88
        $this->assertEquals(
89
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
90
            strval($signature),
91
        );
92
    }
93
94
95
    /**
96
     */
97
    public function testMarshallingElementOrdering(): void
98
    {
99
        $signature = new Signature(
100
            SignedInfo::fromXML(
101
                DOMDocumentFactory::fromFile(
102
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfo.xml',
103
                )->documentElement,
104
            ),
105
            SignatureValue::fromXML(
106
                DOMDocumentFactory::fromFile(
107
                    dirname(__FILE__, 3) . '/resources/xml/ds_SignatureValue.xml',
108
                )->documentElement,
109
            ),
110
            KeyInfo::fromXML(
111
                DOMDocumentFactory::fromFile(
112
                    dirname(__FILE__, 3) . '/resources/xml/ds_KeyInfo.xml',
113
                )->documentElement,
114
            ),
115
            [
116
                new DsObject(
117
                    null,
118
                    null,
119
                    null,
120
                    [
121
                        new Chunk(
122
                            DOMDocumentFactory::fromString(
123
                                '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
124
                            )->documentElement,
125
                        ),
126
                    ],
127
                ),
128
            ],
129
            'def456',
130
        );
131
132
        $signatureElement = $signature->toXML();
133
        $xpCache = XPath::getXPath($signatureElement);
134
135
        $signedInfo = XPath::xpQuery($signatureElement, './ds:SignedInfo', $xpCache);
136
        $this->assertCount(1, $signedInfo);
137
138
        /** @psalm-var \DOMElement[] $signatureElements */
139
        $signatureElements = XPath::xpQuery($signatureElement, './ds:SignedInfo/following-sibling::*', $xpCache);
140
141
        // Test ordering of Signature contents
142
        $this->assertCount(3, $signatureElements);
143
        $this->assertEquals('ds:SignatureValue', $signatureElements[0]->tagName);
144
        $this->assertEquals('ds:KeyInfo', $signatureElements[1]->tagName);
145
        $this->assertEquals('ds:Object', $signatureElements[2]->tagName);
146
    }
147
148
149
    /**
150
     * Test creating a SignatureValue object from XML.
151
     */
152
    public function testUnmarshalling(): void
153
    {
154
        $signature = Signature::fromXML(self::$xmlRepresentation->documentElement);
155
        $this->assertEquals('def456', $signature->getId());
156
157
        $signedInfo = $signature->getSignedInfo();
158
        $this->assertInstanceOf(SignedInfo::class, $signedInfo);
159
160
        $signatureValue = $signature->getSignatureValue();
161
        $this->assertInstanceOf(SignatureValue::class, $signatureValue);
162
163
        $keyInfo = $signature->getKeyInfo();
164
        $this->assertInstanceOf(KeyInfo::class, $keyInfo);
165
166
        $objects = $signature->getObjects();
167
        $this->assertCount(1, $objects);
168
169
        $this->assertEquals(
170
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
171
            strval($signature),
172
        );
173
    }
174
}
175