Completed
Push — master ( f36908...d7724a )
by Dan
03:01
created

HashHmacTest::testVerifyHashNotString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Tests\Ds\Authenticate;
4
5
use Ds\Authenticate\HashHmac;
6
7
class HashHmacTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var HashHmac
11
     */
12
    public $auth;
13
    
14
    public $reflector;
15
16
    public $algo;
17
18
    public $data;
19
20
    /**
21
     * @var HashHmac
22
     */
23
    public $hmac;
24
25
    protected function setUp()
26
    {
27
28
        $this->auth = new HashHmac('privateKey');
29
        $this->reflector = new \ReflectionClass('Ds\Authenticate\HashHmac');
30
31
        $this->algo = 'sha512';
32
        $this->data = ['foo' => 'bar'];
33
        $this->privateKey = 'privateKey';
0 ignored issues
show
Bug introduced by
The property privateKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
35
        $this->hmac = new HashHmac($this->privateKey,$this->algo);
36
37
    }
38
39
    public function testHash()
40
    {
41
        $actual = $this->hmac->create($this->algo, $this->data, $this->privateKey);
42
        $expected = hash_hmac($this->algo, json_decode($this->data) , $this->privateKey);
43
        $result = $this->hmac->verify($actual, $expected);
44
        $this->assertEquals(true, $result);
45
    }
46
47
    public function testCompareHash()
48
    {
49
        $encodedHash = $this->hmac->getEncodedHash($this->data);
50
        $this->assertEquals(true, $this->auth->compareHash($encodedHash,$this->data));
51
    }
52
53
    public function testCompareHashInvalidAlgo()
54
    {
55
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
        $hash = 'shafoo2=647a6cbf9b30cb8cc3fd5147a8fd30a19775fe3290e3dd0e729ae2f1e8a485f833469df616374fa923fcc3533545a11e6deba25a63bcd1ec81c316c090890ddc=10f3e8ed9c5c8b33cadda267882fa6449f5af465bc1dc420=1457010889';
57
        $encoded = base64_encode($hash);
58
        $this->auth->compareHash($encoded, $this->data);
59
    }
60
61
    public function testCompareHashNonMatch()
62
    {
63
64
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
        $encodedHash = $this->hmac->getEncodedHash($this->data);
66
        $differentPayload = ['foos' => 'bat'];
67
        $this->auth->compareHash($encodedHash, $differentPayload);
68
    }
69
70
    public function testVerify()
71
    {
72
        $hash1 = hash_hmac($this->algo, json_encode($this->data), 'key');
73
        $hash2 = hash_hmac($this->algo, json_encode($this->data), 'key');
74
75
        $expected = true;
76
        $actual = $this->hmac->verify($hash1, $hash2);
77
78
        $this->assertEquals($expected, $actual);
79
    }
80
81
    public function testVerifyHashWrongKey()
82
    {
83
84
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
85
        $hash1 = hash_hmac($this->algo, json_encode($this->data), 'key');
86
        $hash2 = hash_hmac($this->algo, json_encode($this->data), 'key2');
87
        $this->hmac->verify($hash1, $hash2);
88
    }
89
90
    public function testVerifyHashNotString()
91
    {
92
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
93
        $hash1 = 23809.123;
94
        $hash2 = 23809.123;
95
        $this->hmac->verify($hash1, $hash2);
96
    }
97
98
    public function testVerifyHashWrongData()
99
    {
100
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
101
        $data = md5('someOtherStuff');
102
        $hash1 = hash_hmac($this->algo, $this->data, 'key');
103
        $hash2 = hash_hmac($this->algo, $data, 'key');
104
        $this->hmac->verify($hash1, $hash2);
105
    }
106
107
    public function testGetPrivateKey()
108
    {
109
        $expected = 'privateKey';
110
        $actual = $this->auth->getPrivateKey();
111
        $this->assertEquals($expected, $actual);
112
    }
113
114
    public function testSetPrivateKey()
115
    {
116
        $expected = 'newKey';
117
        $newKey = $this->auth->setPrivateKey($expected);
118
        $this->assertEquals($expected, $newKey->getPrivateKey());
119
    }
120
121
    public function testGetAlgorithm()
122
    {
123
        $expected = 'sha512';
124
        $actual = $this->auth->getAlgorithm();
125
        $this->assertEquals($expected, $actual);
126
    }
127
128
    public function testVerifyAlgorithm()
129
    {
130
        $method = $this->reflector->getMethod('verifyAlgorithm');
131
        $method->setAccessible(true);
132
        $actual = $method->invokeArgs($this->auth, ['sha256']);
133
        $this->assertEquals($actual, 'sha256');
134
    }
135
136
    public function testVerifyAlgorithmException()
137
    {
138
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
139
140
        $method = $this->reflector->getMethod('verifyAlgorithm');
141
        $method->setAccessible(true);
142
        $method->invokeArgs($this->auth, ['sha111']);
143
    }
144
145
}
146