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