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

X509DataTest::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\TestUtils\PEMCertificatesMock;
13
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
14
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
15
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerName;
16
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial;
17
use SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber;
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\X509DataTest
27
 *
28
 * @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
29
 * @covers \SimpleSAML\XMLSecurity\XML\ds\X509Data
30
 *
31
 * @package simplesamlphp/xml-security
32
 */
33
final class X509DataTest 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\X509DataTest: $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\X509DataTest.
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 static function setUpBeforeClass(): void
48
    {
49
        self::$testedClass = X509Data::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_X509Data.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
        $x509data = new X509Data(
88
            [
89
                new Chunk(
90
                    DOMDocumentFactory::fromString(
91
                        '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>'
92
                    )->documentElement,
93
                ),
94
                new X509Certificate(self::$certificate),
95
                new X509IssuerSerial(
96
                    new X509IssuerName(sprintf(
97
                        'C=US,ST=Hawaii,L=Honolulu,O=SimpleSAMLphp HQ,CN=SimpleSAMLphp Testing CA,emailAddress=%s',
98
                        '[email protected]'
99
                    )),
100
                    new X509SerialNumber('2'),
101
                ),
102
                new X509SubjectName(self::$certData['name']),
103
                new Chunk(DOMDocumentFactory::fromString(
104
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">other</ssp:Chunk>'
105
                )->documentElement)
106
            ],
107
        );
108
109
        $this->assertEquals(
110
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
111
            strval($x509data),
112
        );
113
    }
114
}
115