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

AESEncryptionTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\Alg\Encryption;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
9
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
10
use SimpleSAML\XMLSecurity\Constants as C;
11
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
12
13
/**
14
 * Tests for \SimpleSAML\XMLSecurity\Alg\Encryption\AES.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
class AESEncryptionTest extends TestCase
19
{
20
    /** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */
21
    protected static SymmetricKey $skey;
22
23
    /** @var \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory */
24
    protected static EncryptionAlgorithmFactory $factory;
25
26
    /** @var \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface */
27
    protected static EncryptionAlgorithmInterface $algo;
28
29
30
    public static function setUpBeforeClass(): void
31
    {
32
        self::$skey = new SymmetricKey(hex2bin('7392d8ec40a862fbb02786bab41481b2'));
33
        self::$factory = new EncryptionAlgorithmFactory([]);
34
        self::$algo = self::$factory->getAlgorithm(C::BLOCK_ENC_AES128, self::$skey);
35
    }
36
37
38
    /**
39
     * Test AES encryption.
40
     */
41
    public function testEncrypt(): void
42
    {
43
        $ciphertext = self::$algo->encrypt('plaintext');
44
        $this->assertNotEmpty($ciphertext);
45
        $this->assertEquals(32, strlen($ciphertext));
46
    }
47
48
49
    /**
50
     * Test AES decryption.
51
     */
52
    public function testDecrypt(): void
53
    {
54
        $ciphertext = "r0YRkEixBnAKU032/ux7avHcVTH1CIIyKaPA2qr4KlIs0LVZp5CuwQKRRi6lji4cnaFbH4jETtJhMSEfbpSdvg==";
55
        $plaintext = self::$algo->decrypt(base64_decode($ciphertext));
56
        $this->assertEquals("\n  <Value>\n\tHello, World!\n  </Value>\n", $plaintext);
57
    }
58
}
59