Passed
Push — master ( feb95e...9f38d8 )
by Tim
01:59
created

X509DataTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
c 0
b 0
f 0
dl 0
loc 87
rs 10
wmc 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
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\SchemaValidationTestTrait;
12
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
15
use SimpleSAML\XMLSecurity\Key;
16
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
17
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
18
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
19
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
20
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerName;
21
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial;
22
use SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber;
23
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
24
use SimpleSAML\XMLSecurity\XML\dsig11\X509Digest;
25
26
use function base64_encode;
27
use function dirname;
28
use function hex2bin;
29
use function openssl_x509_parse;
30
use function str_replace;
31
use function strval;
32
33
/**
34
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\X509DataTest
35
 *
36
 * @package simplesamlphp/xml-security
37
 */
38
#[CoversClass(AbstractDsElement::class)]
39
#[CoversClass(X509Data::class)]
40
final class X509DataTest extends TestCase
41
{
42
    use SchemaValidationTestTrait;
43
    use SerializableElementTestTrait;
44
45
    /** @var string */
46
    private static string $certificate;
47
48
    /** @var array<string, mixed> */
49
    private static array $certData;
50
51
    /** @var string */
52
    private static string $digest;
53
54
    /**
55
     */
56
    public static function setUpBeforeClass(): void
57
    {
58
        self::$testedClass = X509Data::class;
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
        $key = new Key\X509Certificate(PEM::fromString(PEMCertificatesMock::getPlainCertificate()));
89
        /** @var string $binary */
90
        $binary = hex2bin($key->getRawThumbprint(C::DIGEST_SHA256));
91
        self::$digest = base64_encode($binary);
92
    }
93
94
95
    /**
96
     */
97
    public function testMarshalling(): void
98
    {
99
        $x509data = new X509Data(
100
            [
101
                new Chunk(
102
                    DOMDocumentFactory::fromString(
103
                        '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
104
                    )->documentElement,
105
                ),
106
                new X509Certificate(self::$certificate),
107
                new X509IssuerSerial(
108
                    new X509IssuerName(sprintf(
109
                        'C=US,ST=Hawaii,L=Honolulu,O=SimpleSAMLphp HQ,CN=SimpleSAMLphp Testing CA,emailAddress=%s',
110
                        '[email protected]',
111
                    )),
112
                    new X509SerialNumber('2'),
113
                ),
114
                new X509SubjectName(self::$certData['name']),
115
                new X509Digest(self::$digest, C::DIGEST_SHA256),
116
                new Chunk(DOMDocumentFactory::fromString(
117
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">other</ssp:Chunk>',
118
                )->documentElement),
119
            ],
120
        );
121
122
        $this->assertEquals(
123
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
124
            strval($x509data),
125
        );
126
    }
127
}
128