Passed
Pull Request — master (#60)
by Tim
02:12
created

AgreementMethodTest::testMarshallingElementOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 75
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 42
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 75
rs 9.248

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\xenc;
6
7
use PHPUnit\Framework\Attributes\{CoversClass, Group};
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\XML\Type\{AnyURIValue, Base64BinaryValue, IDValue, StringValue};
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
15
use SimpleSAML\XMLSecurity\Utils\XPath;
16
use SimpleSAML\XMLSecurity\XML\ds\{DigestMethod, KeyName, X509Certificate, X509Data, X509SubjectName};
17
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractAgreementMethodType, AbstractXencElement};
18
use SimpleSAML\XMLSecurity\XML\xenc\{AgreementMethod, KANonce, OriginatorKeyInfo, RecipientKeyInfo};
19
20
use function dirname;
21
use function openssl_x509_parse;
22
use function str_replace;
23
use function strval;
24
25
/**
26
 * Class \SimpleSAML\XMLSecurity\Test\XML\xenc\AgreementMethodTest
27
 *
28
 * @package simplesamlphp/xml-security
29
 */
30
#[Group('xenc')]
31
#[CoversClass(AbstractXencElement::class)]
32
#[CoversClass(AbstractAgreementMethodType::class)]
33
#[CoversClass(AgreementMethod::class)]
34
final class AgreementMethodTest extends TestCase
35
{
36
    use SchemaValidationTestTrait;
37
    use SerializableElementTestTrait;
38
39
    /** @var string */
40
    private static string $certificate;
41
42
    /** @var string[] */
43
    private static array $certData;
44
45
46
    /**
47
     */
48
    public function setUp(): void
49
    {
50
        self::$testedClass = AgreementMethod::class;
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(
87
            Base64BinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
88
        );
89
90
        $digestMethod = new DigestMethod(
91
            AnyURIValue::fromString(C::DIGEST_SHA256),
92
            [
93
                new Chunk(DOMDocumentFactory::fromString(
94
                    '<some:Chunk xmlns:some="urn:x-simplesamlphp:namespace">some</some:Chunk>',
95
                )->documentElement),
96
            ],
97
        );
98
99
        $originatorKeyInfo = new OriginatorKeyInfo(
100
            [
101
                new KeyName(
102
                    StringValue::fromString('testkey'),
103
                ),
104
                new X509Data(
105
                    [
106
                        new X509Certificate(
107
                            Base64BinaryValue::fromString(self::$certificate),
108
                        ),
109
                        new X509SubjectName(
110
                            StringValue::fromString(self::$certData['name']),
111
                        ),
112
                    ],
113
                ),
114
                new Chunk(DOMDocumentFactory::fromString(
115
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>',
116
                )->documentElement),
117
            ],
118
            IDValue::fromString('fed123'),
119
        );
120
121
        $recipientKeyInfo = new RecipientKeyInfo(
122
            [
123
                new KeyName(
124
                    StringValue::fromString('testkey'),
125
                ),
126
                new X509Data(
127
                    [
128
                        new X509Certificate(
129
                            Base64BinaryValue::fromString(self::$certificate),
130
                        ),
131
                        new X509SubjectName(
132
                            StringValue::fromString(self::$certData['name']),
133
                        ),
134
                    ],
135
                ),
136
                new Chunk(DOMDocumentFactory::fromString(
137
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>',
138
                )->documentElement),
139
            ],
140
            IDValue::fromString('fed654'),
141
        );
142
143
        $agreementMethod = new AgreementMethod(
144
            AnyURIValue::fromString(C::KEY_AGREEMENT_ECDH_ES),
145
            $kaNonce,
146
            $originatorKeyInfo,
147
            $recipientKeyInfo,
148
            [$digestMethod],
149
        );
150
151
        $this->assertEquals(
152
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
153
            strval($agreementMethod),
154
        );
155
    }
156
157
158
    public function testMarshallingElementOrdering(): void
159
    {
160
        $kaNonce = new KANonce(
161
            Base64BinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
162
        );
163
164
        $digestMethod = new DigestMethod(
165
            AnyURIValue::fromString(C::DIGEST_SHA256),
166
            [
167
                new Chunk(DOMDocumentFactory::fromString(
168
                    '<some:Chunk xmlns:some="urn:x-simplesamlphp:namespace">some</some:Chunk>',
169
                )->documentElement),
170
            ],
171
        );
172
173
        $originatorKeyInfo = new OriginatorKeyInfo(
174
            [
175
                new KeyName(
176
                    StringValue::fromString('testkey'),
177
                ),
178
                new X509Data(
179
                    [
180
                        new X509Certificate(
181
                            Base64BinaryValue::fromString(self::$certificate),
182
                        ),
183
                        new X509SubjectName(
184
                            StringValue::fromString(self::$certData['name']),
185
                        ),
186
                    ],
187
                ),
188
                new Chunk(DOMDocumentFactory::fromString(
189
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>',
190
                )->documentElement),
191
            ],
192
            IDValue::fromString('fed321'),
193
        );
194
195
        $recipientKeyInfo = new RecipientKeyInfo(
196
            [
197
                new KeyName(
198
                    StringValue::fromString('testkey'),
199
                ),
200
                new X509Data(
201
                    [
202
                        new X509Certificate(
203
                            Base64BinaryValue::fromString(self::$certificate),
204
                        ),
205
                        new X509SubjectName(
206
                            StringValue::fromString(self::$certData['name']),
207
                        ),
208
                    ],
209
                ),
210
                new Chunk(DOMDocumentFactory::fromString(
211
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>',
212
                )->documentElement),
213
            ],
214
            IDValue::fromString('fed654'),
215
        );
216
217
        $agreementMethod = new AgreementMethod(
218
            AnyURIValue::fromString(C::KEY_AGREEMENT_ECDH_ES),
219
            $kaNonce,
220
            $originatorKeyInfo,
221
            $recipientKeyInfo,
222
            [$digestMethod],
223
        );
224
225
        // Marshall it to a \DOMElement
226
        $agreementMethodElement = $agreementMethod->toXML();
227
228
        $xpCache = XPath::getXPath($agreementMethodElement);
229
230
        // Test for an KA-Nonce
231
        /** @var \DOMElement[] $kaNonceElements */
232
        $kaNonceElements = XPath::xpQuery($agreementMethodElement, './xenc:KA-Nonce', $xpCache);
233
        $this->assertCount(1, $kaNonceElements);
234
235
        // Test ordering of AgreementMethod contents
236
        /** @var \DOMElement[] $agreementMethodElements */
237
        $agreementMethodElements = XPath::xpQuery(
238
            $agreementMethodElement,
239
            './xenc:KA-Nonce/following-sibling::*',
240
            $xpCache,
241
        );
242
243
        $this->assertCount(3, $agreementMethodElements);
244
        $this->assertEquals('ds:DigestMethod', $agreementMethodElements[0]->tagName);
245
        $this->assertEquals('xenc:OriginatorKeyInfo', $agreementMethodElements[1]->tagName);
246
        $this->assertEquals('xenc:RecipientKeyInfo', $agreementMethodElements[2]->tagName);
247
    }
248
}
249