Passed
Branch master (c86cc6)
by Tim
11:28
created

X509CertificateTest::testGetCertificateDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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