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

OriginatorKeyInfoTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 117
rs 10
wmc 4
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;
12
use SimpleSAML\XML\Type\{Base64BinaryValue, IDValue, StringValue};
13
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
14
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
15
use SimpleSAML\XMLSecurity\Type\CryptoBinaryValue;
16
use SimpleSAML\XMLSecurity\XML\ds\{
17
    AbstractDsElement,
18
    AbstractKeyInfoType,
19
    KeyName,
20
    MgmtData,
21
    PGPData,
22
    PGPKeyID,
23
    PGPKeyPacket,
24
    SPKIData,
25
    SPKISexp,
26
    X509Certificate,
27
    X509Data,
28
    X509SubjectName,
29
};
30
use SimpleSAML\XMLSecurity\XML\xenc\{CarriedKeyName, OriginatorKeyInfo, P, Seed};
31
32
use function dirname;
33
use function openssl_x509_parse;
34
use function str_replace;
35
use function strval;
36
37
/**
38
 * Class \SimpleSAML\XMLSecurity\Test\XML\xenc\OriginatorKeyInfoTest
39
 *
40
 * @package simplesamlphp/xml-security
41
 */
42
#[Group('xenc')]
43
#[CoversClass(AbstractDsElement::class)]
44
#[CoversClass(AbstractKeyInfoType::class)]
45
#[CoversClass(OriginatorKeyInfo::class)]
46
final class OriginatorKeyInfoTest extends TestCase
47
{
48
    use SerializableElementTestTrait;
49
50
    /** @var string */
51
    private static string $certificate;
52
53
    /** @var string[] */
54
    private static array $certData;
55
56
57
    /**
58
     */
59
    public function setUp(): void
60
    {
61
        self::$testedClass = OriginatorKeyInfo::class;
62
63
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
64
            dirname(__FILE__, 3) . '/resources/xml/xenc_OriginatorKeyInfo.xml',
65
        );
66
67
        self::$certificate = str_replace(
68
            [
69
                '-----BEGIN CERTIFICATE-----',
70
                '-----END CERTIFICATE-----',
71
                '-----BEGIN RSA PUBLIC KEY-----',
72
                '-----END RSA PUBLIC KEY-----',
73
                "\r\n",
74
                "\n",
75
            ],
76
            [
77
                '',
78
                '',
79
                '',
80
                '',
81
                "\n",
82
                '',
83
            ],
84
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
85
        );
86
87
        self::$certData = openssl_x509_parse(
88
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
89
        );
90
    }
91
92
93
    /**
94
     */
95
    public function testMarshalling(): void
96
    {
97
        $SPKISexp1 = new SPKISexp(
98
            Base64BinaryValue::fromString('GpM6'),
99
        );
100
        $seed = new Seed(
101
            CryptoBinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
102
        );
103
        $SPKISexp2 = new SPKISexp(
104
            Base64BinaryValue::fromString('GpM7'),
105
        );
106
        $SPKISexp3 = new SPKISexp(
107
            Base64BinaryValue::fromString('GpM8'),
108
        );
109
        $carriedKeyName = new CarriedKeyName(
110
            StringValue::fromString('Some label'),
111
        );
112
113
        $originatorKeyInfo = new OriginatorKeyInfo(
114
            [
115
                new KeyName(
116
                    StringValue::fromString('testkey'),
117
                ),
118
                new X509Data(
119
                    [
120
                        new X509Certificate(
121
                            Base64BinaryValue::fromString(self::$certificate),
122
                        ),
123
                        new X509SubjectName(
124
                            StringValue::fromString(self::$certData['name']),
125
                        ),
126
                    ],
127
                ),
128
                new PGPData(
129
                    new PGPKeyID(
130
                        Base64BinaryValue::fromString('GpM7'),
131
                    ),
132
                    new PGPKeyPacket(
133
                        Base64BinaryValue::fromString('GpM8'),
134
                    ),
135
                    [
136
                        new P(
137
                            CryptoBinaryValue::fromString('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='),
138
                        ),
139
                    ],
140
                ),
141
                new SPKIData([
142
                    [$SPKISexp1, $seed],
143
                    [$SPKISexp2, null],
144
                    [$SPKISexp3, $carriedKeyName],
145
                ]),
146
                new MgmtData(
147
                    StringValue::fromString('ManagementData'),
148
                ),
149
                new Chunk(DOMDocumentFactory::fromString(
150
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
151
                )->documentElement),
152
            ],
153
            IDValue::fromString('fed654'),
154
        );
155
156
        $this->assertEquals(
157
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
158
            strval($originatorKeyInfo),
159
        );
160
    }
161
162
163
    /**
164
     */
165
    public function testMarshallingEmpty(): void
166
    {
167
        $this->expectException(InvalidArgumentException::class);
168
        $this->expectExceptionMessage('xenc:OriginatorKeyInfo cannot be empty');
169
170
        new OriginatorKeyInfo([]);
171
    }
172
173
174
    /**
175
     */
176
    public function testUnmarshallingEmpty(): void
177
    {
178
        $document = DOMDocumentFactory::fromString(
179
            '<xenc:OriginatorKeyInfo xmlns:xenc="' . OriginatorKeyInfo::NS . '"/>',
180
        );
181
182
        $this->expectException(InvalidArgumentException::class);
183
        $this->expectExceptionMessage('xenc:OriginatorKeyInfo cannot be empty');
184
185
        OriginatorKeyInfo::fromXML($document->documentElement);
186
    }
187
}
188