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

EncryptionMethodTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 55
dl 0
loc 135
rs 10
c 5
b 2
f 0
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\xenc;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XML\Exception\MissingAttributeException;
12
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\Utils\XPath;
15
use SimpleSAML\XMLSecurity\XML\xenc\AbstractEncryptionMethod;
16
use SimpleSAML\XMLSecurity\XML\xenc\AbstractXencElement;
17
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod;
18
use SimpleSAML\XMLSecurity\XML\xenc\KeySize;
19
use SimpleSAML\XMLSecurity\XML\xenc\OAEPparams;
20
21
use function dirname;
22
use function strval;
23
24
/**
25
 * Tests for the xenc:EncryptionMethod element.
26
 *
27
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractXencElement
28
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractEncryptionMethod
29
 * @covers \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod
30
 * @package simplesamlphp/xml-security
31
 */
32
#[CoversClass(AbstractXencElement::class)]
33
#[CoversClass(AbstractEncryptionMethod::class)]
34
#[CoversClass(EncryptionMethod::class)]
35
final class EncryptionMethodTest extends TestCase
36
{
37
    use SerializableElementTestTrait;
38
39
    /**
40
     */
41
    public static function setUpBeforeClass(): void
42
    {
43
        self::$testedClass = EncryptionMethod::class;
44
45
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
46
            dirname(__FILE__, 3) . '/resources/xml/xenc_EncryptionMethod.xml',
47
        );
48
    }
49
50
51
    // test marshalling
52
53
54
    /**
55
     * Test creating an EncryptionMethod object from scratch.
56
     */
57
    public function testMarshalling(): void
58
    {
59
        $alg = 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p';
60
        $chunkXml = DOMDocumentFactory::fromString('<other:Element xmlns:other="urn:other:enc">Value</other:Element>');
61
        /** @var \DOMElement $chunkElt */
62
        $chunkElt = $chunkXml->documentElement;
63
        $chunk = Chunk::fromXML($chunkElt);
64
65
        $em = new EncryptionMethod($alg, new KeySize(10), new OAEPparams('9lWu3Q=='), [$chunk]);
66
67
        $this->assertEquals(
68
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
69
            strval($em),
70
        );
71
    }
72
73
74
    /**
75
     * Test that creating an EncryptionMethod object from scratch works when no optional elements have been specified.
76
     */
77
    public function testMarshallingWithoutOptionalParameters(): void
78
    {
79
        $em = new EncryptionMethod('http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p');
80
        $document = DOMDocumentFactory::fromString(
81
            '<xenc:EncryptionMethod xmlns:xenc="' . C::NS_XENC .
82
            '" Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>',
83
        );
84
85
        $this->assertNull($em->getKeySize());
86
        $this->assertNull($em->getOAEPParams());
87
        $this->assertEmpty($em->getElements());
88
        $this->assertEquals(
89
            $document->saveXML($document->documentElement),
90
            strval($em),
91
        );
92
    }
93
94
95
    public function testMarshallingElementOrdering(): void
96
    {
97
        $alg = 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p';
98
        $chunkXml = DOMDocumentFactory::fromString('<other:Element xmlns:other="urn:other:enc">Value</other:Element>');
99
        /** @var \DOMElement $chunkElt */
100
        $chunkElt = $chunkXml->documentElement;
101
        $chunk = Chunk::fromXML($chunkElt);
102
103
        $em = new EncryptionMethod($alg, new KeySize(10), new OAEPparams('9lWu3Q=='), [$chunk]);
104
105
        // Marshall it to a \DOMElement
106
        $emElement = $em->toXML();
107
108
        $xpCache = XPath::getXPath($emElement);
109
110
        // Test for a KeySize
111
        /** @var \DOMElement[] $keySizeElements */
112
        $keySizeElements = XPath::xpQuery($emElement, './xenc:KeySize', $xpCache);
113
        $this->assertCount(1, $keySizeElements);
114
        $this->assertEquals('10', $keySizeElements[0]->textContent);
115
116
        // Test ordering of EncryptionMethod contents
117
        /** @var \DOMElement[] $emElements */
118
        $emElements = XPath::xpQuery($emElement, './xenc:KeySize/following-sibling::*', $xpCache);
119
120
        $this->assertCount(2, $emElements);
121
        $this->assertEquals('xenc:OAEPparams', $emElements[0]->tagName);
122
        $this->assertEquals('other:Element', $emElements[1]->tagName);
123
    }
124
125
126
    // test unmarshalling
127
128
129
    /**
130
     * Test that creating an EncryptionMethod object from XML without an Algorithm attribute fails.
131
     */
132
    public function testUnmarshallingWithoutAlgorithm(): void
133
    {
134
        $xmlRepresentation = clone self::$xmlRepresentation;
135
        /** @var \DOMElement $xmlRepresentation */
136
        $xmlRepresentation = $xmlRepresentation->documentElement;
137
        $xmlRepresentation->removeAttribute('Algorithm');
138
139
        $this->expectException(MissingAttributeException::class);
140
        $this->expectExceptionMessage('Missing \'Algorithm\' attribute on xenc:EncryptionMethod.');
141
        EncryptionMethod::fromXML($xmlRepresentation);
142
    }
143
144
145
    /**
146
     * Test that creating an EncryptionMethod object from XML works if no optional elements are present.
147
     */
148
    public function testUnmarshallingWithoutOptionalParameters(): void
149
    {
150
        $xencns = C::NS_XENC;
151
        $document = DOMDocumentFactory::fromString(
152
            <<<XML
153
<xenc:EncryptionMethod xmlns:xenc="{$xencns}" Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
154
XML
155
            ,
156
        );
157
158
        /** @var \DOMElement @element */
159
        $element = $document->documentElement;
160
        $em = EncryptionMethod::fromXML($element);
161
        $this->assertNull($em->getKeySize());
162
        $this->assertNull($em->getOAEPParams());
163
        $this->assertEmpty($em->getElements());
164
        $this->assertEquals(
165
            $document->saveXML($document->documentElement),
166
            strval($em),
167
        );
168
    }
169
}
170