Passed
Pull Request — master (#61)
by
unknown
12:50
created

X509CertificateTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\Key;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
9
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
10
use SimpleSAML\XMLSecurity\Key\X509Certificate;
11
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
12
13
use function openssl_pkey_get_details;
14
use function openssl_pkey_get_public;
15
use function openssl_x509_fingerprint;
16
use function openssl_x509_parse;
17
use function openssl_x509_read;
18
19
/**
20
 * Test for SimpleSAML\XMLSecurity\Key\X509Certificate
21
 *
22
 * @package SimpleSAML\XMLSecurity\Key
23
 */
24
final class X509CertificateTest extends TestCase
25
{
26
    /** @var array<string, string|int> */
27
    protected static array $cert = [];
28
29
    /** @var string */
30
    protected static string $f;
31
32
    /** @var \SimpleSAML\XMLSecurity\Key\X509Certificate */
33
    protected static X509Certificate $c;
34
35
36
    /**
37
     * Initialize the test by loading the file ourselves.
38
     */
39
    public static function setUpBeforeClass(): void
40
    {
41
        self::$f = PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::CERTIFICATE);
42
        self::$cert = openssl_pkey_get_details(openssl_pkey_get_public(openssl_x509_read(self::$f)));
43
        self::$c = new X509Certificate(PEM::fromString(self::$f));
44
    }
45
46
47
    /**
48
     * Cover basic creation and retrieval.
49
     */
50
    public function testCreation(): void
51
    {
52
        $pubDetails = openssl_pkey_get_details(openssl_pkey_get_public(self::$c->getMaterial()));
53
        $this->assertEquals(self::$cert['key'], $pubDetails['key']);
54
    }
55
56
57
    /**
58
     * Test for retrieval of the PEM-encoded certificate.
59
     */
60
    public function testGetCertificate(): void
61
    {
62
        $this->assertEquals(self::$f, self::$c->getMaterial());
63
    }
64
65
66
    /**
67
     * Test for retrieval of the certificate's details.
68
     */
69
    public function testGetCertificateDetails(): void
70
    {
71
        $this->assertEquals(openssl_x509_parse(self::$f), self::$c->getCertificateDetails());
72
    }
73
74
75
    /**
76
     * Test thumbprint generation from a certificate.
77
     */
78
    public function testGetRawThumbprint(): void
79
    {
80
        $f = openssl_x509_fingerprint(self::$f);
81
        $this->assertEquals($f, self::$c->getRawThumbprint());
82
    }
83
84
85
    /**
86
     * Test thumbprint generation with an invalid digest algorithm.
87
     */
88
    public function testGetRawThumbprintWithWrongAlg(): void
89
    {
90
        $this->expectException(UnsupportedAlgorithmException::class);
91
        self::$c->getRawThumbprint('invalid');
92
    }
93
94
95
    /**
96
     * Test creation from a file containing the PEM-encoded certificate.
97
     */
98
    public function testFromFile(): void
99
    {
100
        $c = PEMCertificatesMock::getCertificate(PEMCertificatesMock::CERTIFICATE);
101
        $pubDetails = openssl_pkey_get_details(openssl_pkey_get_public($c->getMaterial()));
102
        $this->assertEquals(self::$cert['key'], $pubDetails['key']);
103
    }
104
}
105