Passed
Push — master ( 5a07d0...30f015 )
by Tim
23:49 queued 11:01
created

KeyValueTest::testUnmarshalling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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);
0 ignored issues
show
Bug introduced by
It seems like $elements can also be of type iterable; however, parameter $array of reset() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
        $element = reset(/** @scrutinizer ignore-type */ $elements);
Loading history...
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 testUnmarshallingWithOtherElement(): void
127
    {
128
        $document = self::$empty;
129
        $document->documentElement->appendChild($document->importNode(self::$cipherValue->documentElement, true));
130
131
        $keyValue = KeyValue::fromXML($document->documentElement);
132
133
        $elements = $keyValue->getElements();
134
        $this->assertNull($keyValue->getRSAKeyValue());
135
        $this->assertCount(1, $elements);
136
137
        $element = reset($elements);
0 ignored issues
show
Bug introduced by
It seems like $elements can also be of type iterable; however, parameter $array of reset() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        $element = reset(/** @scrutinizer ignore-type */ $elements);
Loading history...
138
        $this->assertInstanceOf(Chunk::class, $element);
139
        $this->assertEquals($element->getXML()->textContent, '/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
140
    }
141
142
143
    /**
144
     */
145
    public function testUnmarshallingEmpty(): void
146
    {
147
        $document = self::$empty;
148
149
        $this->expectException(SchemaViolationException::class);
150
        $this->expectExceptionMessage(
151
            'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other'
152
        );
153
154
        KeyValue::fromXML($document->documentElement);
155
    }
156
}
157