1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Authenticate; |
4
|
|
|
|
5
|
|
|
use Ds\Authenticate\Password; |
6
|
|
|
use Ds\Authenticate\PasswordInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PasswordTest |
10
|
|
|
* |
11
|
|
|
* @package Tests\Authenticate |
12
|
|
|
*/ |
13
|
|
|
class PasswordTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var PasswordInterface |
17
|
|
|
*/ |
18
|
|
|
public $password; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->password = new Password(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function testNeedsRehash() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$password = 'password'; |
34
|
|
|
$hash = \password_hash('password', PASSWORD_DEFAULT); |
35
|
|
|
$result = $this->password->needsRehash($password, $hash, PASSWORD_DEFAULT, 12); |
36
|
|
|
$this->assertNotEquals($hash, $result); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function testNeedsRehashNoRehash() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$password = 'password'; |
45
|
|
|
$hash = password_hash('password', PASSWORD_DEFAULT, ['cost' => 12]); |
46
|
|
|
$result = $this->password->needsRehash($password, $hash, PASSWORD_DEFAULT, 12); |
47
|
|
|
$this->assertEquals(false, $result); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $password |
52
|
|
|
* @dataProvider hashPasswordProvider |
53
|
|
|
*/ |
54
|
|
|
public function testHash($password) |
55
|
|
|
{ |
56
|
|
|
$hashedPassword = $this->password->hash($password); |
57
|
|
|
$this->assertEquals(true, $this->password->verify($password, $hashedPassword)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $password |
62
|
|
|
* @param string $hashedPassword |
63
|
|
|
* @dataProvider truePasswordProvider |
64
|
|
|
*/ |
65
|
|
|
public function testVerify($password, $hashedPassword) |
66
|
|
|
{ |
67
|
|
|
$passwordHash = $this->password->verify($password, $hashedPassword); |
68
|
|
|
$this->assertEquals($passwordHash, true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $password |
73
|
|
|
* @param string $hashedPassword |
74
|
|
|
* @dataProvider falsePasswordProvider |
75
|
|
|
* @expectedException \Exception |
76
|
|
|
*/ |
77
|
|
|
public function testFailedVerify($password, $hashedPassword) |
78
|
|
|
{ |
79
|
|
|
$this->password->verify($password, $hashedPassword); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function hashPasswordProvider() |
86
|
|
|
{ |
87
|
|
|
return array( |
88
|
|
|
['password'], |
89
|
|
|
['shortpass'], |
90
|
|
|
['rlysht'], |
91
|
|
|
['8978798796785897890908'], |
92
|
|
|
['alongpasswordwithnumbers78890231908132'], |
93
|
|
|
['areallylongpasswordwithnumbersandspecialchars-890988908!98099___%%£213321'], |
94
|
|
|
['somethingsolong&*()&*(&*(&**@((@)_)@__@__@)))))@((((@**@*&&&@**@()(@))@_t132123hatitsalmostcertainlyno____tgoingtobeusedinproduct123312123ionunlesssomeone____decidedthattheyarehappywrittingthiseverytimetheywishtologintothesite_withsomenumbersandspecialcharacters'] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function truePasswordProvider() |
102
|
|
|
{ |
103
|
|
|
return array( |
104
|
|
|
['password',\password_hash('password', PASSWORD_DEFAULT)], |
105
|
|
|
['foobar',\password_hash('foobar', PASSWORD_DEFAULT)], |
106
|
|
|
['234234234cookie',\password_hash('234234234cookie', PASSWORD_DEFAULT)], |
107
|
|
|
['fobatraz56278',\password_hash('fobatraz56278', PASSWORD_DEFAULT)], |
108
|
|
|
['foo__fgg__bat__789',\password_hash('foo__fgg__bat__789', PASSWORD_DEFAULT)], |
109
|
|
|
['passwdsasdads2123ord',\password_hash('passwdsasdads2123ord', PASSWORD_DEFAULT)] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function falsePasswordProvider() |
117
|
|
|
{ |
118
|
|
|
return array( |
119
|
|
|
['password','$2y$10$rUHpBM6hi0TyCBIgGnJYAuBPLe66sadkE.RW'], |
120
|
|
|
['password','cGFzc3dvcmQ='], |
121
|
|
|
['password','5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'], |
122
|
|
|
['password','5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8'], |
123
|
|
|
['password','74dfc2b27acfa364da55f93a5caee29ccad3557247eda238831b3e9bd931b01d77fe994e4f12b9d4cfa92a124461d2065197d8cf7f33fc88566da2db2a4d6eae'], |
124
|
|
|
['password','db4d9992897eda89b50f1d3208db607902da7e79c6f3bc6e6933cc5919068564'], |
125
|
|
|
['password','35c246d5'], |
126
|
|
|
['password','ycbTFtbcTZUqeJ/UuIWO1w=='], |
127
|
|
|
['password','c9c6d316d6dc4d952a789fd4b8858ed7'], |
128
|
|
|
['password','c9:c6:d3:16:d6:dc:4d:95:2a:78:9f:d4:b8:85:8e:d7'], |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.