Passed
Push — master ( be09b3...67068d )
by Tim
13:28
created

KeyInfoTest::testUnmarshalling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\XML\Chunk;
9
use SimpleSAML\XML\DOMDocumentFactory;
10
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
11
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
14
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
15
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
16
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
17
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
18
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
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\ds\KeyInfoTest
27
 *
28
 * @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
29
 * @covers \SimpleSAML\XMLSecurity\XML\ds\KeyInfo
30
 *
31
 * @package simplesamlphp/xml-security
32
 */
33
final class KeyInfoTest extends TestCase
34
{
35
    use SchemaValidationTestTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TestUtils\SchemaValidationTestTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\Test\XML\ds\KeyInfoTest: $documentElement, $ownerDocument, $message, $line
Loading history...
36
    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\Test\XML\ds\KeyInfoTest.
Loading history...
37
38
    /** @var string */
39
    private static string $certificate;
40
41
    /** @var string[] */
42
    private static array $certData;
43
44
45
    /**
46
     */
47
    public function setUp(): void
48
    {
49
        self::$testedClass = KeyInfo::class;
50
51
        self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd';
52
53
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
54
            dirname(__FILE__, 3) . '/resources/xml/ds_KeyInfo.xml',
55
        );
56
57
        self::$certificate = str_replace(
58
            [
59
                '-----BEGIN CERTIFICATE-----',
60
                '-----END CERTIFICATE-----',
61
                '-----BEGIN RSA PUBLIC KEY-----',
62
                '-----END RSA PUBLIC KEY-----',
63
                "\r\n",
64
                "\n",
65
            ],
66
            [
67
                '',
68
                '',
69
                '',
70
                '',
71
                "\n",
72
                ''
73
            ],
74
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
75
        );
76
77
        self::$certData = openssl_x509_parse(
78
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
79
        );
80
    }
81
82
83
    /**
84
     */
85
    public function testMarshalling(): void
86
    {
87
        $keyInfo = new KeyInfo(
88
            [
89
                new KeyName('testkey'),
90
                new X509Data(
91
                    [
92
                        new X509Certificate(self::$certificate),
93
                        new X509SubjectName(self::$certData['name']),
94
                    ],
95
                ),
96
                new Chunk(DOMDocumentFactory::fromString(
97
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>'
98
                )->documentElement),
99
            ],
100
            'fed654',
101
        );
102
103
        $this->assertEquals(
104
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
105
            strval($keyInfo),
106
        );
107
    }
108
109
110
    /**
111
     */
112
    public function testMarshallingEmpty(): void
113
    {
114
        $this->expectException(InvalidArgumentException::class);
115
        $this->expectExceptionMessage('ds:KeyInfo cannot be empty');
116
117
        $keyInfo = new KeyInfo([]);
0 ignored issues
show
Unused Code introduced by
The assignment to $keyInfo is dead and can be removed.
Loading history...
118
    }
119
120
121
    /**
122
     */
123
    public function testUnmarshallingEmpty(): void
124
    {
125
        $document = DOMDocumentFactory::fromString('<ds:KeyInfo xmlns:ds="' . KeyInfo::NS . '"/>');
126
127
        $this->expectException(InvalidArgumentException::class);
128
        $this->expectExceptionMessage('ds:KeyInfo cannot be empty');
129
130
        $keyInfo = KeyInfo::fromXML($document->documentElement);
0 ignored issues
show
Unused Code introduced by
The assignment to $keyInfo is dead and can be removed.
Loading history...
131
    }
132
}
133