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

testMarshallingElementOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 42
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\xenc;
6
7
use DOMDocument;
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\KeyInfo;
15
use SimpleSAML\XMLSecurity\XML\xenc\CipherData;
16
use SimpleSAML\XMLSecurity\XML\xenc\CipherValue;
17
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
18
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
19
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod;
20
21
use function dirname;
22
use function strval;
23
24
/**
25
 * Class \SimpleSAML\XMLSecurity\Test\XML\xenc\EncryptedDataTest
26
 *
27
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractXencElement
28
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractEncryptedType
29
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
30
 *
31
 * @package simplesamlphp/xml-security
32
 */
33
final class EncryptedDataTest extends TestCase
34
{
35
    use SchemaValidationTestTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TestUtils\SchemaValidationTestTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\T...\xenc\EncryptedDataTest: $documentElement, $ownerDocument, $message, $line
Loading history...
36
    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\T...\xenc\EncryptedDataTest.
Loading history...
37
38
    /**
39
     */
40
    public static function setUpBeforeClass(): void
41
    {
42
        self::$testedClass = EncryptedData::class;
43
44
        self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema.xsd';
45
46
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
47
            dirname(__FILE__, 3) . '/resources/xml/xenc_EncryptedData.xml',
48
        );
49
    }
50
51
52
    // marshalling
53
54
55
    /**
56
     */
57
    public function testMarshalling(): void
58
    {
59
        $encryptedData = new EncryptedData(
60
            new CipherData(new CipherValue('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')),
61
            'MyID',
62
            'http://www.w3.org/2001/04/xmlenc#Element',
63
            'text/plain',
64
            'urn:x-simplesamlphp:encoding',
65
            new EncryptionMethod('http://www.w3.org/2001/04/xmlenc#aes128-cbc'),
66
            new KeyInfo(
67
                [
68
                    new EncryptedKey(
69
                        new CipherData(new CipherValue('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')),
70
                        null,
71
                        null,
72
                        null,
73
                        null,
74
                        null,
75
                        null,
76
                        new EncryptionMethod('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'),
77
                    ),
78
                ],
79
            ),
80
        );
81
82
        $this->assertEquals(
83
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
84
            strval($encryptedData),
85
        );
86
    }
87
88
89
    /**
90
     */
91
    public function testMarshallingElementOrdering(): void
92
    {
93
        $encryptedData = new EncryptedData(
94
            new CipherData(new CipherValue('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')),
95
            'MyID',
96
            'http://www.w3.org/2001/04/xmlenc#Element',
97
            'text/plain',
98
            'urn:x-simplesamlphp:encoding',
99
            new EncryptionMethod('http://www.w3.org/2001/04/xmlenc#aes128-cbc'),
100
            new KeyInfo(
101
                [
102
                    new EncryptedKey(
103
                        new CipherData(new CipherValue('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')),
104
                        null,
105
                        null,
106
                        null,
107
                        null,
108
                        null,
109
                        null,
110
                        new EncryptionMethod('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'),
111
                    ),
112
                ],
113
            ),
114
        );
115
116
        $encryptedDataElement = $encryptedData->toXML();
117
        $xpCache = XPath::getXPath($encryptedDataElement);
118
119
        // Test for an EncryptionMethod
120
        $encryptedDataElements = XPath::xpQuery($encryptedDataElement, './xenc:EncryptionMethod', $xpCache);
121
        $this->assertCount(1, $encryptedDataElements);
122
123
        // Test ordering of EncryptedData contents
124
        /** @psalm-var \DOMElement[] $encryptedDataElements */
125
        $encryptedDataElements = XPath::xpQuery(
126
            $encryptedDataElement,
127
            './xenc:EncryptionMethod/following-sibling::*',
128
            $xpCache
129
        );
130
        $this->assertCount(2, $encryptedDataElements);
131
        $this->assertEquals('ds:KeyInfo', $encryptedDataElements[0]->tagName);
132
        $this->assertEquals('xenc:CipherData', $encryptedDataElements[1]->tagName);
133
134
        // EncryptionProperties is currently not supported
135
        //$this->assertEquals('xenc:EncryptionProperties', $encryptedDataElements[2]->tagName);
136
    }
137
138
139
    // unmarshalling
140
141
142
    /**
143
     */
144
    public function testUnmarshalling(): void
145
    {
146
        $encryptedData = EncryptedData::fromXML(self::$xmlRepresentation->documentElement);
147
148
        $this->assertEquals(
149
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
150
            strval($encryptedData),
151
        );
152
    }
153
}
154