Passed
Branch master (c86cc6)
by Tim
04:58 queued 52s
created

KeyValueTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 138
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testMarshallingEmpty() 0 8 1
A setUp() 0 18 1
A testUnmarshallingEmpty() 0 10 1
A testUnmarshallingWithOtherElement() 0 14 1
A testUnmarshalling() 0 7 1
A testMarshallingWithOtherElement() 0 16 1
A testMarshalling() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6
7
use DOMDocument;
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
13
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
14
use SimpleSAML\XMLSecurity\XML\ds\Exponent;
15
use SimpleSAML\XMLSecurity\XML\ds\KeyValue;
16
use SimpleSAML\XMLSecurity\XML\ds\Modulus;
17
use SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue;
18
19
use function dirname;
20
use function strval;
21
22
/**
23
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\KeyValueTest
24
 *
25
 * @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
26
 * @covers \SimpleSAML\XMLSecurity\XML\ds\KeyValue
27
 *
28
 * @package simplesamlphp/xml-security
29
 */
30
final class KeyValueTest extends TestCase
31
{
32
    use SchemaValidationTestTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TestUtils\SchemaValidationTestTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\Test\XML\ds\KeyValueTest: $documentElement, $ownerDocument, $message, $line
Loading history...
33
    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\Test\XML\ds\KeyValueTest.
Loading history...
34
35
36
    /** @var \DOMDocument $empty */
37
    protected static DOMDocument $empty;
38
39
    /** @var \DOMDocument $rsaKeyValue */
40
    protected static DOMDocument $rsaKeyValue;
41
42
    /** @var \DOMDocument $cipherValue */
43
    protected static DOMDocument $cipherValue;
44
45
46
    /**
47
     */
48
    protected function setUp(): void
49
    {
50
        self::$testedClass = KeyValue::class;
51
52
        self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd';
53
54
        self::$empty = DOMDocumentFactory::fromString('<ds:KeyValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>');
55
56
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
57
            dirname(__FILE__, 3) . '/resources/xml/ds_KeyValue.xml',
58
        );
59
60
        self::$rsaKeyValue = DOMDocumentFactory::fromFile(
61
            dirname(__FILE__, 3) . '/resources/xml/ds_RSAKeyValue.xml',
62
        );
63
64
        self::$cipherValue = DOMDocumentFactory::fromFile(
65
            dirname(__FILE__, 3) . '/resources/xml/xenc_CipherValue.xml',
66
        );
67
    }
68
69
70
    /**
71
     */
72
    public function testMarshalling(): void
73
    {
74
        $keyValue = new KeyValue(RSAKeyValue::fromXML(self::$rsaKeyValue->documentElement));
75
76
        $rsaKeyValue = $keyValue->getRSAKeyValue();
77
        $this->assertInstanceOf(RSAKeyValue::class, $rsaKeyValue);
78
        $this->assertEmpty($keyValue->getElements());
79
80
        $this->assertEquals($rsaKeyValue->getModulus()->getContent(), 'dGhpcyBpcyBzb21lIHJhbmRvbSBtb2R1bHVzCg==');
81
        $this->assertEquals($rsaKeyValue->getExponent()->getContent(), 'dGhpcyBpcyBzb21lIHJhbmRvbSBleHBvbmVudAo=');
82
83
        $document = self::$empty;
84
        $document->documentElement->appendChild($document->importNode(self::$rsaKeyValue->documentElement, true));
85
86
        $this->assertXmlStringEqualsXmlString($document->saveXML($document->documentElement), strval($keyValue));
87
    }
88
89
90
    /**
91
     */
92
    public function testMarshallingWithOtherElement(): void
93
    {
94
        $keyValue = new KeyValue(null, Chunk::fromXML(self::$cipherValue->documentElement));
95
96
        $elements = $keyValue->getElements();
97
        $this->assertEmpty($keyValue->getRSAKeyValue());
98
        $this->assertCount(1, $elements);
99
100
        $element = reset($elements);
101
        $this->assertInstanceOf(Chunk::class, $element);
102
        $this->assertEquals($element->getXML()->textContent, '/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
103
104
        $document = self::$empty;
105
        $document->documentElement->appendChild($document->importNode(self::$cipherValue->documentElement, true));
106
107
        $this->assertXmlStringEqualsXmlString($document->saveXML($document->documentElement), strval($keyValue));
108
    }
109
110
111
    /**
112
     */
113
    public function testMarshallingEmpty(): void
114
    {
115
        $this->expectException(SchemaViolationException::class);
116
        $this->expectExceptionMessage(
117
            'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other'
118
        );
119
120
        new KeyValue(null, null);
121
    }
122
123
124
    /**
125
     */
126
    public function testUnmarshalling(): void
127
    {
128
        $keyValue = KeyValue::fromXML(self::$xmlRepresentation->documentElement);
129
130
        $this->assertEquals(
131
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
132
            strval($keyValue),
133
        );
134
    }
135
136
137
    /**
138
     */
139
    public function testUnmarshallingWithOtherElement(): void
140
    {
141
        $document = self::$empty;
142
        $document->documentElement->appendChild($document->importNode(self::$cipherValue->documentElement, true));
143
144
        $keyValue = KeyValue::fromXML($document->documentElement);
145
146
        $elements = $keyValue->getElements();
147
        $this->assertNull($keyValue->getRSAKeyValue());
148
        $this->assertCount(1, $elements);
149
150
        $element = reset($elements);
151
        $this->assertInstanceOf(Chunk::class, $element);
152
        $this->assertEquals($element->getXML()->textContent, '/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
153
    }
154
155
156
    /**
157
     */
158
    public function testUnmarshallingEmpty(): void
159
    {
160
        $document = self::$empty;
161
162
        $this->expectException(SchemaViolationException::class);
163
        $this->expectExceptionMessage(
164
            'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other'
165
        );
166
167
        KeyValue::fromXML($document->documentElement);
168
    }
169
}
170