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