Passed
Branch master (c86cc6)
by Tim
03:54
created

PublicKeyTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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 SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
9
use SimpleSAML\XMLSecurity\Exception\IOException;
10
use SimpleSAML\XMLSecurity\Key\PublicKey;
11
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
12
13
use function file_get_contents;
14
use function openssl_pkey_get_details;
15
use function openssl_pkey_get_public;
16
17
/**
18
 * Tests for SimpleSAML\XMLSecurity\Key\PublicKey.
19
 *
20
 * @package SimpleSAML\XMLSecurity\Key
21
 */
22
final class PublicKeyTest extends TestCase
23
{
24
    /** @var array */
25
    protected static $pubKey = [];
26
27
    /** @var string */
28
    protected static string $f;
29
30
31
    /**
32
     * Initialize the test by loading the file ourselves.
33
     */
34
    public static function setUpBeforeClass(): void
35
    {
36
        self::$f = PEMCertificatesMock::getPlainPublicKey(PEMCertificatesMock::PUBLIC_KEY);
37
        self::$pubKey = openssl_pkey_get_details(openssl_pkey_get_public(self::$f));
38
    }
39
40
    /**
41
     * Cover basic creation and retrieval.
42
     */
43
    public function testCreation(): void
44
    {
45
        $k = new PublicKey(PEM::fromString(self::$f));
46
        $keyDetails = openssl_pkey_get_details(openssl_pkey_get_public($k->getMaterial()));
47
        $this->assertEquals(self::$pubKey['key'], $keyDetails['key']);
48
    }
49
50
51
    /**
52
     * Test creation from a file containing the PEM-encoded public key.
53
     */
54
    public function testFromFile(): void
55
    {
56
        $k = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY);
57
        $keyDetails = openssl_pkey_get_details(openssl_pkey_get_public($k->getMaterial()));
58
        $this->assertEquals(self::$pubKey['key'], $keyDetails['key']);
59
    }
60
61
62
    /**
63
     * Test failure to create key from missing file.
64
     */
65
    public function testFromMissingFile(): void
66
    {
67
        $this->expectException(IOException::class);
68
        @PublicKey::fromFile('foo/bar');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for fromFile(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

68
        /** @scrutinizer ignore-unhandled */ @PublicKey::fromFile('foo/bar');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
69
    }
70
71
72
    /**
73
     * Test creation from the RSA public key details (modulus and exponent).
74
     */
75
    public function testFromDetails(): void
76
    {
77
        $k = PublicKey::fromDetails(self::$pubKey['rsa']['n'], self::$pubKey['rsa']['e']);
78
        $keyDetails = openssl_pkey_get_details(openssl_pkey_get_public($k->getMaterial()));
79
        $this->assertEquals(self::$pubKey['key'], $keyDetails['key']);
80
    }
81
}
82