Passed
Pull Request — master (#60)
by Tim
02:12
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, Group};
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\{Chunk, DOMDocumentFactory};
10
use SimpleSAML\XML\TestUtils\{SchemaValidationTestTrait, SerializableElementTestTrait};
11
use SimpleSAML\XML\Type\{AnyURIValue, Base64BinaryValue, IntegerValue, StringValue};
12
use SimpleSAML\XMLSecurity\Constants as C;
13
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
14
use SimpleSAML\XMLSecurity\Key;
15
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
16
use SimpleSAML\XMLSecurity\XML\ds\{AbstractDsElement, X509Certificate, X509Data};
17
use SimpleSAML\XMLSecurity\XML\ds\{X509IssuerName, X509IssuerSerial, X509SerialNumber, X509SubjectName};
18
use SimpleSAML\XMLSecurity\XML\dsig11\X509Digest;
19
20
use function base64_encode;
21
use function dirname;
22
use function hex2bin;
23
use function openssl_x509_parse;
24
use function str_replace;
25
use function strval;
26
27
/**
28
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\X509DataTest
29
 *
30
 * @package simplesamlphp/xml-security
31
 */
32
#[Group('ds')]
33
#[CoversClass(AbstractDsElement::class)]
34
#[CoversClass(X509Data::class)]
35
final class X509DataTest extends TestCase
36
{
37
    use SchemaValidationTestTrait;
38
    use SerializableElementTestTrait;
39
40
    /** @var string */
41
    private static string $certificate;
42
43
    /** @var array<string, mixed> */
44
    private static array $certData;
45
46
    /** @var string */
47
    private static string $digest;
48
49
    /**
50
     */
51
    public static function setUpBeforeClass(): void
52
    {
53
        self::$testedClass = X509Data::class;
54
55
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
56
            dirname(__FILE__, 3) . '/resources/xml/ds_X509Data.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
        $key = new Key\X509Certificate(PEM::fromString(PEMCertificatesMock::getPlainCertificate()));
84
        /** @var string $binary */
85
        $binary = hex2bin($key->getRawThumbprint(C::DIGEST_SHA256));
86
        self::$digest = base64_encode($binary);
87
    }
88
89
90
    /**
91
     */
92
    public function testMarshalling(): void
93
    {
94
        $x509data = new X509Data(
95
            [
96
                new X509Certificate(
97
                    Base64BinaryValue::fromString(self::$certificate),
98
                ),
99
                new X509IssuerSerial(
100
                    new X509IssuerName(
101
                        StringValue::fromString(sprintf(
102
                            'C=%s,ST=%s,L=%s,O=%s,CN=%s,emailAddress=%s',
103
                            'US',
104
                            'Hawaii',
105
                            'Honolulu',
106
                            'SimpleSAMLphp HQ',
107
                            'SimpleSAMLphp Testing CA',
108
                            '[email protected]',
109
                        )),
110
                    ),
111
                    new X509SerialNumber(
112
                        IntegerValue::fromString('2'),
113
                    ),
114
                ),
115
                new X509SubjectName(
116
                    StringValue::fromString(self::$certData['name']),
117
                ),
118
                new X509Digest(
119
                    Base64BinaryValue::fromString(self::$digest),
120
                    AnyURIValue::fromString(C::DIGEST_SHA256),
121
                ),
122
            ],
123
            [
124
                new Chunk(
125
                    DOMDocumentFactory::fromString(
126
                        '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
127
                    )->documentElement,
128
                ),
129
                new Chunk(DOMDocumentFactory::fromString(
130
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">other</ssp:Chunk>',
131
                )->documentElement),
132
            ],
133
        );
134
135
        $this->assertEquals(
136
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
137
            strval($x509data),
138
        );
139
    }
140
}
141