Passed
Push — master ( feb95e...9f38d8 )
by Tim
01:59
created

DerivedKeyTest::testMarshallingElementOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 33
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 57
rs 9.392

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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