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

HMACTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testVerify() 0 23 1
A setUpBeforeClass() 0 4 1
A testSetUnknownDigest() 0 4 1
A testSign() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\Backend;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\XMLSecurity\Backend\HMAC;
9
use SimpleSAML\XMLSecurity\Constants as C;
10
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
11
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
12
13
use function bin2hex;
14
use function hex2bin;
15
16
/**
17
 * Test for SimpleSAML\XMLSecurity\Backend\HMAC.
18
 *
19
 * @package SimpleSAML\XMLSecurity\Backend
20
 */
21
final class HMACTest extends TestCase
22
{
23
    public const PLAINTEXT = "plaintext";
24
25
    public const SIGNATURE = "61b85d9e800ed0eca556a304cc9e1ac7ae8eecb3";
26
27
    public const SECRET = 'secret key';
28
29
    /** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */
30
    protected static SymmetricKey $key;
31
32
    /** @var \SimpleSAML\XMLSecurity\Backend\HMAC */
33
    protected static HMAC $backend;
34
35
36
    /**
37
     * Initialize shared key.
38
     */
39
    public static function setUpBeforeClass(): void
40
    {
41
        self::$key = new SymmetricKey(self::SECRET);
42
        self::$backend = new HMAC();
43
    }
44
45
46
    /**
47
     * Test signing of messages.
48
     */
49
    public function testSign(): void
50
    {
51
        self::$backend->setDigestAlg(C::DIGEST_SHA1);
52
        $this->assertEquals(self::SIGNATURE, bin2hex(self::$backend->sign(self::$key, self::PLAINTEXT)));
53
    }
54
55
56
    /**
57
     * Test for wrong digests.
58
     */
59
    public function testSetUnknownDigest(): void
60
    {
61
        $this->expectException(InvalidArgumentException::class);
62
        self::$backend->setDigestAlg('foo');
63
    }
64
65
66
    /**
67
     * Test verification of signatures.
68
     */
69
    public function testVerify(): void
70
    {
71
        // test successful verification
72
        self::$backend->setDigestAlg(C::DIGEST_SHA1);
73
        $this->assertTrue(self::$backend->verify(self::$key, self::PLAINTEXT, hex2bin(self::SIGNATURE)));
74
75
        // test failure to verify with different plaintext
76
        $this->assertFalse(self::$backend->verify(self::$key, 'foo', hex2bin(self::SIGNATURE)));
77
78
        // test failure to verify with different signature
79
        $this->assertFalse(self::$backend->verify(
80
            self::$key,
81
            self::PLAINTEXT,
82
            hex2bin('12345678901234567890abcdefabcdef12345678'),
83
        ));
84
85
        // test failure to verify with wrong key
86
        $key = new SymmetricKey('wrong secret');
87
        $this->assertFalse(self::$backend->verify($key, self::PLAINTEXT, hex2bin(self::SIGNATURE)));
88
89
        // test failure to verify with wrong digest algorithm
90
        self::$backend->setDigestAlg(C::DIGEST_RIPEMD160);
91
        $this->assertFalse(self::$backend->verify(self::$key, self::PLAINTEXT, hex2bin(self::SIGNATURE)));
92
    }
93
}
94