Passed
Push — master ( be09b3...67068d )
by Tim
13:28
created

EncryptedDataTest::testUnmarshalling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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