Passed
Branch master (c86cc6)
by Tim
01:57
created

X509DataTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 32
rs 9.6
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 DOMDocument;
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\Assert\AssertionFailedException;
10
use SimpleSAML\XML\Chunk;
11
use SimpleSAML\XML\DOMDocumentFactory;
12
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
13
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
14
use SimpleSAML\XML\Utils as XMLUtils;
15
use SimpleSAML\XMLSecurity\Constants as C;
16
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
17
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
18
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
19
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerName;
20
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial;
21
use SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber;
22
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
23
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
24
25
use function base64_encode;
26
use function dirname;
27
use function hex2bin;
28
use function openssl_x509_parse;
29
use function str_replace;
30
use function strval;
31
32
/**
33
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\X509DataTest
34
 *
35
 * @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
36
 * @covers \SimpleSAML\XMLSecurity\XML\ds\X509Data
37
 *
38
 * @package simplesamlphp/xml-security
39
 */
40
final class X509DataTest extends TestCase
41
{
42
    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\X509DataTest: $documentElement, $ownerDocument, $message, $line
Loading history...
43
    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\X509DataTest.
Loading history...
44
45
    /** @var string */
46
    private static string $certificate;
47
48
    /** @var string[] */
49
    private static array $certData;
50
51
52
    /**
53
     */
54
    public static function setUpBeforeClass(): void
55
    {
56
        self::$testedClass = X509Data::class;
57
58
        self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd';
59
60
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
61
            dirname(__FILE__, 3) . '/resources/xml/ds_X509Data.xml',
62
        );
63
64
        self::$certificate = str_replace(
65
            [
66
                '-----BEGIN CERTIFICATE-----',
67
                '-----END CERTIFICATE-----',
68
                '-----BEGIN RSA PUBLIC KEY-----',
69
                '-----END RSA PUBLIC KEY-----',
70
                "\r\n",
71
                "\n",
72
            ],
73
            [
74
                '',
75
                '',
76
                '',
77
                '',
78
                "\n",
79
                ''
80
            ],
81
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
82
        );
83
84
        self::$certData = openssl_x509_parse(
85
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
86
        );
87
    }
88
89
90
    /**
91
     */
92
    public function testMarshalling(): void
93
    {
94
        $x509data = new X509Data(
95
            [
96
                new Chunk(
97
                    DOMDocumentFactory::fromString(
98
                        '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>'
99
                    )->documentElement,
100
                ),
101
                new X509Certificate(self::$certificate),
102
                new X509IssuerSerial(
103
                    new X509IssuerName(sprintf(
104
                        'C=US,ST=Hawaii,L=Honolulu,O=SimpleSAMLphp HQ,CN=SimpleSAMLphp Testing CA,emailAddress=%s',
105
                        '[email protected]'
106
                    )),
107
                    new X509SerialNumber('2'),
108
                ),
109
                new X509SubjectName(self::$certData['name']),
110
                new Chunk(DOMDocumentFactory::fromString(
111
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">other</ssp:Chunk>'
112
                )->documentElement)
113
            ],
114
        );
115
116
        $this->assertEquals(
117
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
118
            strval($x509data),
119
        );
120
    }
121
122
123
    /**
124
     */
125
    public function testUnmarshalling(): void
126
    {
127
        $x509data = X509Data::fromXML(self::$xmlRepresentation->documentElement);
128
129
        $this->assertEquals(
130
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
131
            strval($x509data),
132
        );
133
    }
134
}
135