Passed
Push — master ( 6e1344...0156f2 )
by Tim
02:07
created

AgreementMethodTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 9.6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\xenc;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XML\TestUtils\{SerializableElementTestTrait, SchemaValidationTestTrait};
12
use SimpleSAML\XMLSecurity\Constants as C;
13
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
14
use SimpleSAML\XMLSecurity\Utils\XPath;
15
use SimpleSAML\XMLSecurity\XML\ds\{DigestMethod, KeyName, X509Certificate, X509Data, X509SubjectName};
16
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractAgreementMethodType, AbstractXencElement};
17
use SimpleSAML\XMLSecurity\XML\xenc\{AgreementMethod, KANonce, OriginatorKeyInfo, RecipientKeyInfo};
18
19
use function dirname;
20
use function openssl_x509_parse;
21
use function str_replace;
22
use function strval;
23
24
/**
25
 * Class \SimpleSAML\XMLSecurity\Test\XML\xenc\AgreementMethodTest
26
 *
27
 * @package simplesamlphp/xml-security
28
 */
29
#[CoversClass(AbstractXencElement::class)]
30
#[CoversClass(AbstractAgreementMethodType::class)]
31
#[CoversClass(AgreementMethod::class)]
32
final class AgreementMethodTest extends TestCase
33
{
34
    use SchemaValidationTestTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TestUtils\SchemaValidationTestTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\T...enc\AgreementMethodTest: $documentElement, $ownerDocument, $message, $line
Loading history...
35
    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\T...enc\AgreementMethodTest.
Loading history...
36
37
    /** @var string */
38
    private static string $certificate;
39
40
    /** @var string[] */
41
    private static array $certData;
42
43
44
    /**
45
     */
46
    public function setUp(): void
47
    {
48
        self::$testedClass = AgreementMethod::class;
49
50
        self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema.xsd';
51
52
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
53
            dirname(__FILE__, 3) . '/resources/xml/xenc_AgreementMethod.xml',
54
        );
55
56
        self::$certificate = str_replace(
57
            [
58
                '-----BEGIN CERTIFICATE-----',
59
                '-----END CERTIFICATE-----',
60
                '-----BEGIN RSA PUBLIC KEY-----',
61
                '-----END RSA PUBLIC KEY-----',
62
                "\r\n",
63
                "\n",
64
            ],
65
            [
66
                '',
67
                '',
68
                '',
69
                '',
70
                "\n",
71
                '',
72
            ],
73
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
74
        );
75
76
        self::$certData = openssl_x509_parse(
77
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
78
        );
79
    }
80
81
82
    /**
83
     */
84
    public function testMarshalling(): void
85
    {
86
        $kaNonce = new KANonce('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
87
88
        $digestMethod = new DigestMethod(
89
            C::DIGEST_SHA256,
90
            [
91
                new Chunk(DOMDocumentFactory::fromString(
92
                    '<some:Chunk xmlns:some="urn:x-simplesamlphp:namespace">some</some:Chunk>',
93
                )->documentElement),
94
            ],
95
        );
96
97
        $originatorKeyInfo = new OriginatorKeyInfo(
98
            [
99
                new KeyName('testkey'),
100
                new X509Data(
101
                    [
102
                        new X509Certificate(self::$certificate),
103
                        new X509SubjectName(self::$certData['name']),
104
                    ],
105
                ),
106
                new Chunk(DOMDocumentFactory::fromString(
107
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>',
108
                )->documentElement),
109
            ],
110
            'fed123',
111
        );
112
113
        $recipientKeyInfo = new RecipientKeyInfo(
114
            [
115
                new KeyName('testkey'),
116
                new X509Data(
117
                    [
118
                        new X509Certificate(self::$certificate),
119
                        new X509SubjectName(self::$certData['name']),
120
                    ],
121
                ),
122
                new Chunk(DOMDocumentFactory::fromString(
123
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>',
124
                )->documentElement),
125
            ],
126
            'fed654',
127
        );
128
129
        $agreementMethod = new AgreementMethod(
130
            C::XMLENC11_ECDH_ES,
131
            $kaNonce,
132
            $originatorKeyInfo,
133
            $recipientKeyInfo,
134
            [$digestMethod],
135
        );
136
137
        $this->assertEquals(
138
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
139
            strval($agreementMethod),
140
        );
141
    }
142
143
144
    public function testMarshallingElementOrdering(): void
145
    {
146
        $kaNonce = new KANonce('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
147
148
        $digestMethod = new DigestMethod(
149
            C::DIGEST_SHA256,
150
            [
151
                new Chunk(DOMDocumentFactory::fromString(
152
                    '<some:Chunk xmlns:some="urn:x-simplesamlphp:namespace">some</some:Chunk>',
153
                )->documentElement),
154
            ],
155
        );
156
157
        $originatorKeyInfo = new OriginatorKeyInfo(
158
            [
159
                new KeyName('testkey'),
160
                new X509Data(
161
                    [
162
                        new X509Certificate(self::$certificate),
163
                        new X509SubjectName(self::$certData['name']),
164
                    ],
165
                ),
166
                new Chunk(DOMDocumentFactory::fromString(
167
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>',
168
                )->documentElement),
169
            ],
170
            'fed321',
171
        );
172
173
        $recipientKeyInfo = new RecipientKeyInfo(
174
            [
175
                new KeyName('testkey'),
176
                new X509Data(
177
                    [
178
                        new X509Certificate(self::$certificate),
179
                        new X509SubjectName(self::$certData['name']),
180
                    ],
181
                ),
182
                new Chunk(DOMDocumentFactory::fromString(
183
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>',
184
                )->documentElement),
185
            ],
186
            'fed654',
187
        );
188
189
        $agreementMethod = new AgreementMethod(
190
            C::XMLENC11_ECDH_ES,
191
            $kaNonce,
192
            $originatorKeyInfo,
193
            $recipientKeyInfo,
194
            [$digestMethod],
195
        );
196
197
        // Marshall it to a \DOMElement
198
        $agreementMethodElement = $agreementMethod->toXML();
199
200
        $xpCache = XPath::getXPath($agreementMethodElement);
201
202
        // Test for an KA-Nonce
203
        /** @var \DOMElement[] $kaNonceElements */
204
        $kaNonceElements = XPath::xpQuery($agreementMethodElement, './xenc:KA-Nonce', $xpCache);
205
        $this->assertCount(1, $kaNonceElements);
206
207
        // Test ordering of AgreementMethod contents
208
        /** @var \DOMElement[] $agreementMethodElements */
209
        $agreementMethodElements = XPath::xpQuery(
210
            $agreementMethodElement,
211
            './xenc:KA-Nonce/following-sibling::*',
212
            $xpCache,
213
        );
214
215
        $this->assertCount(3, $agreementMethodElements);
216
        $this->assertEquals('ds:DigestMethod', $agreementMethodElements[0]->tagName);
217
        $this->assertEquals('xenc:OriginatorKeyInfo', $agreementMethodElements[1]->tagName);
218
        $this->assertEquals('xenc:RecipientKeyInfo', $agreementMethodElements[2]->tagName);
219
    }
220
}
221