Passed
Pull Request — master (#60)
by Tim
02:12
created

EncryptedDataTest::testMarshallingElementOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 42
rs 9.472
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\xenc;
6
7
use PHPUnit\Framework\Attributes\{CoversClass, Group};
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\DOMDocumentFactory;
10
use SimpleSAML\XML\TestUtils\{SchemaValidationTestTrait, SerializableElementTestTrait};
11
use SimpleSAML\XML\Type\{AnyURIValue, Base64BinaryValue, IDValue, StringValue};
12
use SimpleSAML\XMLSecurity\Constants as C;
13
use SimpleSAML\XMLSecurity\Utils\XPath;
14
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
15
use SimpleSAML\XMLSecurity\XML\xenc\{
16
    AbstractEncryptedType,
17
    AbstractXencElement,
18
    CipherData,
19
    CipherValue,
20
    EncryptedData,
21
    EncryptedKey,
22
    EncryptionMethod,
23
};
24
25
use function dirname;
26
use function strval;
27
28
/**
29
 * Class \SimpleSAML\XMLSecurity\Test\XML\xenc\EncryptedDataTest
30
 *
31
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractXencElement
32
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractEncryptedType
33
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
34
 *
35
 * @package simplesamlphp/xml-security
36
 */
37
#[Group('xenc')]
38
#[CoversClass(AbstractXencElement::class)]
39
#[CoversClass(AbstractEncryptedType::class)]
40
#[CoversClass(EncryptedData::class)]
41
final class EncryptedDataTest extends TestCase
42
{
43
    use SchemaValidationTestTrait;
44
    use SerializableElementTestTrait;
45
46
    /**
47
     */
48
    public static function setUpBeforeClass(): void
49
    {
50
        self::$testedClass = EncryptedData::class;
51
52
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
53
            dirname(__FILE__, 3) . '/resources/xml/xenc_EncryptedData.xml',
54
        );
55
    }
56
57
58
    // marshalling
59
60
61
    /**
62
     */
63
    public function testMarshalling(): void
64
    {
65
        $encryptedData = new EncryptedData(
66
            new CipherData(
67
                new CipherValue(
68
                    Base64BinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
69
                ),
70
            ),
71
            IDValue::fromString('MyID'),
72
            AnyURIValue::fromString(C::XMLENC_ELEMENT),
73
            StringValue::fromString('text/plain'),
74
            AnyURIValue::fromString('urn:x-simplesamlphp:encoding'),
75
            new EncryptionMethod(
76
                AnyURIValue::fromString(C::BLOCK_ENC_AES128),
77
            ),
78
            new KeyInfo(
79
                [
80
                    new EncryptedKey(
81
                        new CipherData(
82
                            new CipherValue(
83
                                Base64BinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
84
                            ),
85
                        ),
86
                        null,
87
                        null,
88
                        null,
89
                        null,
90
                        null,
91
                        null,
92
                        new EncryptionMethod(
93
                            AnyURIValue::fromString(C::SIG_RSA_SHA256),
94
                        ),
95
                    ),
96
                ],
97
            ),
98
        );
99
100
        $this->assertEquals(
101
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
102
            strval($encryptedData),
103
        );
104
    }
105
106
107
    /**
108
     */
109
    public function testMarshallingElementOrdering(): void
110
    {
111
        $encryptedData = new EncryptedData(
112
            new CipherData(
113
                new CipherValue(
114
                    Base64BinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
115
                ),
116
            ),
117
            IDValue::fromString('MyID'),
118
            AnyURIValue::fromString(C::XMLENC_ELEMENT),
119
            StringValue::fromString('text/plain'),
120
            AnyURIValue::fromString('urn:x-simplesamlphp:encoding'),
121
            new EncryptionMethod(
122
                AnyURIValue::fromString(C::BLOCK_ENC_AES128),
123
            ),
124
            new KeyInfo(
125
                [
126
                    new EncryptedKey(
127
                        new CipherData(
128
                            new CipherValue(
129
                                Base64BinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
130
                            ),
131
                        ),
132
                        null,
133
                        null,
134
                        null,
135
                        null,
136
                        null,
137
                        null,
138
                        new EncryptionMethod(
139
                            AnyURIValue::fromString(C::SIG_RSA_SHA256),
140
                        ),
141
                    ),
142
                ],
143
            ),
144
        );
145
146
        $encryptedDataElement = $encryptedData->toXML();
147
        $xpCache = XPath::getXPath($encryptedDataElement);
148
149
        // Test for an EncryptionMethod
150
        $encryptedDataElements = XPath::xpQuery($encryptedDataElement, './xenc:EncryptionMethod', $xpCache);
151
        $this->assertCount(1, $encryptedDataElements);
152
153
        // Test ordering of EncryptedData contents
154
        /** @var \DOMElement[] $encryptedDataElements */
155
        $encryptedDataElements = XPath::xpQuery(
156
            $encryptedDataElement,
157
            './xenc:EncryptionMethod/following-sibling::*',
158
            $xpCache,
159
        );
160
        $this->assertCount(2, $encryptedDataElements);
161
        $this->assertEquals('ds:KeyInfo', $encryptedDataElements[0]->tagName);
162
        $this->assertEquals('xenc:CipherData', $encryptedDataElements[1]->tagName);
163
164
        // EncryptionProperties is currently not supported
165
        //$this->assertEquals('xenc:EncryptionProperties', $encryptedDataElements[2]->tagName);
166
    }
167
}
168