Passed
Pull Request — master (#61)
by
unknown
12:50
created

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