1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Tests\Algorithm\RSA; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Gandung\JWT\Algorithm\RSA\RS256; |
7
|
|
|
use Gandung\JWT\Manager\KeyManager; |
8
|
|
|
use Gandung\JWT\Tests\Exception\ConstantFromExternalModuleNotFoundException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class RS256Test extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testIfConstantValueExistsFromOpensslDependency() |
16
|
|
|
{ |
17
|
|
|
if (!defined('OPENSSL_ALGO_SHA256') || |
18
|
|
|
!defined('OPENSSL_ALGO_SHA384') || |
19
|
|
|
!defined('OPENSSL_ALGO_SHA512')) { |
20
|
|
|
throw new ConstantFromExternalModuleNotFoundException( |
21
|
|
|
"Install 'openssl' module first." |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->assertTrue(true); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testCanGetInstance() |
29
|
|
|
{ |
30
|
|
|
$this->assertInstanceOf(RS256::class, new RS256()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testCanSignGivenData() |
34
|
|
|
{ |
35
|
|
|
$key = new KeyManager; |
36
|
|
|
$key->setContentFromCertFile('cert/dummy256.pem'); |
37
|
|
|
$key->setPassphrase('umar123'); |
38
|
|
|
$rs256 = new RS256; |
39
|
|
|
$signature = $rs256->sign('this is a text.', $key); |
40
|
|
|
$this->assertInternalType('string', $signature); |
41
|
|
|
$this->assertNotEmpty($signature); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @expectedException \RuntimeException |
46
|
|
|
*/ |
47
|
|
|
public function testCanRaiseExceptionWhenSignDataWithBadKey() |
48
|
|
|
{ |
49
|
|
|
$key = new KeyManager; |
50
|
|
|
$key->setContentFromCertFile('cert/dummy256.pem'); |
51
|
|
|
$rs256 = new RS256; |
52
|
|
|
$signature = $rs256->sign('this is a text.', $key); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCanVerifySignatureOfGivenData() |
56
|
|
|
{ |
57
|
|
|
$key = new KeyManager; |
58
|
|
|
$key->setContentFromCertFile('cert/dummy256.pem'); |
59
|
|
|
$key->setPassphrase('umar123'); |
60
|
|
|
$rs256 = new RS256; |
61
|
|
|
$signature = $rs256->sign('this is a text.', $key); |
62
|
|
|
$this->assertInternalType('string', $signature); |
63
|
|
|
$this->assertNotEmpty($signature); |
64
|
|
|
$isSignatureMatched = $rs256->verify($signature, 'this is a text.', $key); |
65
|
|
|
$this->assertInternalType('boolean', $isSignatureMatched); |
66
|
|
|
$this->assertTrue($isSignatureMatched); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @expectedException \RuntimeException |
71
|
|
|
*/ |
72
|
|
|
public function testCanRaiseExceptionWhenVerifySignatureOfGivenDataWithBadKey() |
73
|
|
|
{ |
74
|
|
|
$key = new KeyManager; |
75
|
|
|
$key->setContentFromCertFile('cert/dummy256.pem'); |
76
|
|
|
$key->setPassphrase('umar123'); |
77
|
|
|
$rs256 = new RS256; |
78
|
|
|
$signature = $rs256->sign('this is a text.', $key); |
79
|
|
|
$this->assertInternalType('string', $signature); |
80
|
|
|
$this->assertNotEmpty($signature); |
81
|
|
|
$key->setPassphrase(null); |
82
|
|
|
$isSignatureMatched = $rs256->verify($signature, 'this is a text.', $key); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testCanGetAlgorithmAlias() |
86
|
|
|
{ |
87
|
|
|
$rs256 = new RS256; |
88
|
|
|
$this->assertEquals(\Gandung\JWT\Token\Algorithm::RS256, $rs256->getAlgorithmAlias()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.