Passed
Push — master ( 2d3781...f48916 )
by Tim
11:07 queued 06:44
created

DerivedKeyTest::testMarshalling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 25
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 41
rs 9.52
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\xenc11;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\DOMDocumentFactory;
10
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
11
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12
use SimpleSAML\XMLSecurity\Constants as C;
13
use SimpleSAML\XMLSecurity\Utils\XPath as XPathUtils;
14
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
15
use SimpleSAML\XMLSecurity\XML\ds\Transform;
16
use SimpleSAML\XMLSecurity\XML\ds\Transforms;
17
use SimpleSAML\XMLSecurity\XML\ds\XPath;
18
use SimpleSAML\XMLSecurity\XML\xenc\DataReference;
19
use SimpleSAML\XMLSecurity\XML\xenc\KeyReference;
20
use SimpleSAML\XMLSecurity\XML\xenc\ReferenceList;
21
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractDerivedKeyType;
22
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
23
use SimpleSAML\XMLSecurity\XML\xenc11\DerivedKey;
24
use SimpleSAML\XMLSecurity\XML\xenc11\DerivedKeyName;
25
use SimpleSAML\XMLSecurity\XML\xenc11\KeyDerivationMethod;
26
use SimpleSAML\XMLSecurity\XML\xenc11\MasterKeyName;
27
28
use function dirname;
29
use function strval;
30
31
/**
32
 * Class \SimpleSAML\XMLSecurity\Test\XML\xenc11\DerivedKeyTest
33
 *
34
 * @covers \SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element
35
 * @covers \SimpleSAML\XMLSecurity\XML\xenc11\AbstractDerivedKeyType
36
 * @covers \SimpleSAML\XMLSecurity\XML\xenc11\DerivedKey
37
 *
38
 * @package simplesamlphp/xml-security
39
 */
40
#[CoversClass(AbstractXenc11Element::class)]
41
#[CoversClass(AbstractDerivedKeyType::class)]
42
#[CoversClass(DerivedKey::class)]
43
final class DerivedKeyTest extends TestCase
44
{
45
    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...L\xenc11\DerivedKeyTest: $documentElement, $ownerDocument, $message, $line
Loading history...
46
    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...L\xenc11\DerivedKeyTest.
Loading history...
47
48
    /**
49
     */
50
    public static function setUpBeforeClass(): void
51
    {
52
        self::$testedClass = DerivedKey::class;
53
54
        self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema-11.xsd';
55
56
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
57
            dirname(__FILE__, 3) . '/resources/xml/xenc11_DerivedKey.xml',
58
        );
59
    }
60
61
62
    // marshalling
63
64
65
    /**
66
     */
67
    public function testMarshalling(): void
68
    {
69
        $alg = 'http://www.w3.org/2009/xmlenc11#ConcatKDF';
70
        $keyName = new KeyName('testkey');
71
72
        $keyDerivationMethod = new KeyDerivationMethod($alg, [$keyName]);
73
74
        $transformData = new Transform(
75
            C::XPATH10_URI,
76
            new XPath('self::xenc:EncryptedData[@Id="example1"]'),
77
        );
78
        $transformKey = new Transform(
79
            C::XPATH10_URI,
80
            new XPath('self::xenc:EncryptedKey[@Id="example1"]'),
81
        );
82
83
        $referenceList = new ReferenceList(
84
            [
85
                new DataReference('#Encrypted_DATA_ID', [new Transforms([$transformData])]),
86
            ],
87
            [
88
                new KeyReference('#Encrypted_KEY_ID', [new Transforms([$transformKey])]),
89
            ],
90
        );
91
92
        $derivedKeyName = new DerivedKeyName('phpunit');
93
        $masterKeyName = new MasterKeyName('phpunit');
94
95
        $derivedKey = new DerivedKey(
96
            'phpunit',
97
            'phpunit',
98
            'urn:x-simplesamlphp:type',
99
            $keyDerivationMethod,
100
            $referenceList,
101
            $derivedKeyName,
102
            $masterKeyName,
103
        );
104
105
        $this->assertEquals(
106
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
107
            strval($derivedKey),
108
        );
109
    }
110
111
112
    /**
113
     */
114
    public function testMarshallingElementOrder(): void
115
    {
116
        $alg = 'http://www.w3.org/2009/xmlenc11#ConcatKDF';
117
        $keyName = new KeyName('testkey');
118
119
        $keyDerivationMethod = new KeyDerivationMethod($alg, [$keyName]);
120
121
        $transformData = new Transform(
122
            C::XPATH10_URI,
123
            new XPath('self::xenc:EncryptedData[@Id="example1"]'),
124
        );
125
        $transformKey = new Transform(
126
            C::XPATH10_URI,
127
            new XPath('self::xenc:EncryptedKey[@Id="example1"]'),
128
        );
129
130
        $referenceList = new ReferenceList(
131
            [
132
                new DataReference('#Encrypted_DATA_ID', [new Transforms([$transformData])]),
133
            ],
134
            [
135
                new KeyReference('#Encrypted_KEY_ID', [new Transforms([$transformKey])]),
136
            ],
137
        );
138
139
        $derivedKeyName = new DerivedKeyName('phpunit');
140
        $masterKeyName = new MasterKeyName('phpunit');
141
142
        $derivedKey = new DerivedKey(
143
            'phpunit',
144
            'phpunit',
145
            'urn:x-simplesamlphp:type',
146
            $keyDerivationMethod,
147
            $referenceList,
148
            $derivedKeyName,
149
            $masterKeyName,
150
        );
151
152
        $dkElement = $derivedKey->toXML();
153
        $xpCache = XPathUtils::getXPath($dkElement);
154
155
        // Test for a KeyDerivationMethod
156
        /** @var \DOMElement[] $keyDerivationMethodElements */
157
        $keyDerivationMethodElements = XPathUtils::xpQuery($dkElement, './xenc11:KeyDerivationMethod', $xpCache);
158
        $this->assertCount(1, $keyDerivationMethodElements);
159
160
        // Test ordering of DerivedKey contents
161
        /** @var \DOMElement[] $dkElements */
162
        $dkElements = XPathUtils::xpQuery($dkElement, './xenc11:KeyDerivationMethod/following-sibling::*', $xpCache);
163
164
        $this->assertCount(3, $dkElements);
165
        $this->assertEquals('xenc:ReferenceList', $dkElements[0]->tagName);
166
        $this->assertEquals('xenc11:DerivedKeyName', $dkElements[1]->tagName);
167
        $this->assertEquals('xenc11:MasterKeyName', $dkElements[2]->tagName);
168
    }
169
170
171
    /**
172
     * Adding an empty DerivedKey element should yield an empty element.
173
     */
174
    public function testMarshallingEmptyElement(): void
175
    {
176
        $xenc11_ns = DerivedKey::NS;
177
        $derivedKey = new DerivedKey();
178
        $this->assertEquals(
179
            "<xenc11:DerivedKey xmlns:xenc11=\"$xenc11_ns\"/>",
180
            strval($derivedKey),
181
        );
182
        $this->assertTrue($derivedKey->isEmptyElement());
183
    }
184
}
185