[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / Tests / Unit / Salt / BlowfishSaltTest.php
1 <?php
2 namespace TYPO3\CMS\Saltedpasswords\Tests\Unit\Salt;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Crypto\Random;
18
19 /**
20 * Testcase for BlowfishSalt
21 */
22 class BlowfishSaltTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
23 {
24 /**
25 * Keeps instance of object to test.
26 *
27 * @var \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt
28 */
29 protected $objectInstance = null;
30
31 /**
32 * Sets up the fixtures for this testcase.
33 *
34 * @return void
35 */
36 protected function setUp()
37 {
38 $this->objectInstance = $this->getMockBuilder(\TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class)
39 ->setMethods(['dummy'])
40 ->getMock();
41 }
42
43 /**
44 * Marks tests as skipped if the blowfish method is not available.
45 *
46 * @return void
47 */
48 protected function skipTestIfBlowfishIsNotAvailable()
49 {
50 if (!CRYPT_BLOWFISH) {
51 $this->markTestSkipped('Blowfish is not supported on your platform.');
52 }
53 }
54
55 /**
56 * @test
57 */
58 public function hasCorrectBaseClass()
59 {
60 $hasCorrectBaseClass = get_class($this->objectInstance) === \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class;
61 // XCLASS ?
62 if (!$hasCorrectBaseClass && false != get_parent_class($this->objectInstance)) {
63 $hasCorrectBaseClass = is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class);
64 }
65 $this->assertTrue($hasCorrectBaseClass);
66 }
67
68 /**
69 * @test
70 */
71 public function nonZeroSaltLength()
72 {
73 $this->assertTrue($this->objectInstance->getSaltLength() > 0);
74 }
75
76 /**
77 * @test
78 */
79 public function emptyPasswordResultsInNullSaltedPassword()
80 {
81 $password = '';
82 $this->assertNull($this->objectInstance->getHashedPassword($password));
83 }
84
85 /**
86 * @test
87 */
88 public function nonEmptyPasswordResultsInNonNullSaltedPassword()
89 {
90 $this->skipTestIfBlowfishIsNotAvailable();
91 $password = 'a';
92 $this->assertNotNull($this->objectInstance->getHashedPassword($password));
93 }
94
95 /**
96 * @test
97 */
98 public function createdSaltedHashOfProperStructure()
99 {
100 $this->skipTestIfBlowfishIsNotAvailable();
101 $password = 'password';
102 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
103 $this->assertTrue($this->objectInstance->isValidSaltedPW($saltedHashPassword));
104 }
105
106 /**
107 * @test
108 */
109 public function createdSaltedHashOfProperStructureForCustomSaltWithoutSetting()
110 {
111 $this->skipTestIfBlowfishIsNotAvailable();
112 $password = 'password';
113 // custom salt without setting
114 $randomBytes = (new Random())->generateRandomBytes($this->objectInstance->getSaltLength());
115 $salt = $this->objectInstance->base64Encode($randomBytes, $this->objectInstance->getSaltLength());
116 $this->assertTrue($this->objectInstance->isValidSalt($salt));
117 $saltedHashPassword = $this->objectInstance->getHashedPassword($password, $salt);
118 $this->assertTrue($this->objectInstance->isValidSaltedPW($saltedHashPassword));
119 }
120
121 /**
122 * @test
123 */
124 public function createdSaltedHashOfProperStructureForMinimumHashCount()
125 {
126 $this->skipTestIfBlowfishIsNotAvailable();
127 $password = 'password';
128 $minHashCount = $this->objectInstance->getMinHashCount();
129 $this->objectInstance->setHashCount($minHashCount);
130 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
131 $this->assertTrue($this->objectInstance->isValidSaltedPW($saltedHashPassword));
132 // reset hashcount
133 $this->objectInstance->setHashCount(null);
134 }
135
136 /**
137 * Tests authentication procedure with alphabet characters.
138 *
139 * Checks if a "plain-text password" is every time mapped to the
140 * same "salted password hash" when using the same salt.
141 *
142 * @test
143 */
144 public function authenticationWithValidAlphaCharClassPassword()
145 {
146 $this->skipTestIfBlowfishIsNotAvailable();
147 $password = 'aEjOtY';
148 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
149 $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
150 }
151
152 /**
153 * Tests authentication procedure with numeric characters.
154 *
155 * Checks if a "plain-text password" is every time mapped to the
156 * same "salted password hash" when using the same salt.
157 *
158 * @test
159 */
160 public function authenticationWithValidNumericCharClassPassword()
161 {
162 $this->skipTestIfBlowfishIsNotAvailable();
163 $password = '01369';
164 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
165 $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
166 }
167
168 /**
169 * Tests authentication procedure with US-ASCII special characters.
170 *
171 * Checks if a "plain-text password" is every time mapped to the
172 * same "salted password hash" when using the same salt.
173 *
174 * @test
175 */
176 public function authenticationWithValidAsciiSpecialCharClassPassword()
177 {
178 $this->skipTestIfBlowfishIsNotAvailable();
179 $password = ' !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
180 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
181 $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
182 }
183
184 /**
185 * Tests authentication procedure with latin1 special characters.
186 *
187 * Checks if a "plain-text password" is every time mapped to the
188 * same "salted password hash" when using the same salt.
189 *
190 * @test
191 */
192 public function authenticationWithValidLatin1SpecialCharClassPassword()
193 {
194 $this->skipTestIfBlowfishIsNotAvailable();
195 $password = '';
196 for ($i = 160; $i <= 191; $i++) {
197 $password .= chr($i);
198 }
199 $password .= chr(215) . chr(247);
200 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
201 $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
202 }
203
204 /**
205 * Tests authentication procedure with latin1 umlauts.
206 *
207 * Checks if a "plain-text password" is every time mapped to the
208 * same "salted password hash" when using the same salt.
209 *
210 * @test
211 */
212 public function authenticationWithValidLatin1UmlautCharClassPassword()
213 {
214 $this->skipTestIfBlowfishIsNotAvailable();
215 $password = '';
216 for ($i = 192; $i <= 214; $i++) {
217 $password .= chr($i);
218 }
219 for ($i = 216; $i <= 246; $i++) {
220 $password .= chr($i);
221 }
222 for ($i = 248; $i <= 255; $i++) {
223 $password .= chr($i);
224 }
225 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
226 $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
227 }
228
229 /**
230 * @test
231 */
232 public function authenticationWithNonValidPassword()
233 {
234 $this->skipTestIfBlowfishIsNotAvailable();
235 $password = 'password';
236 $password1 = $password . 'INVALID';
237 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
238 $this->assertFalse($this->objectInstance->checkPassword($password1, $saltedHashPassword));
239 }
240
241 /**
242 * @test
243 */
244 public function passwordVariationsResultInDifferentHashes()
245 {
246 $this->skipTestIfBlowfishIsNotAvailable();
247 $pad = 'a';
248 $criticalPwLength = 0;
249 // We're using a constant salt.
250 $saltedHashPasswordCurrent = $salt = $this->objectInstance->getHashedPassword($pad);
251 for ($i = 0; $i <= 128; $i += 8) {
252 $password = str_repeat($pad, max($i, 1));
253 $saltedHashPasswordPrevious = $saltedHashPasswordCurrent;
254 $saltedHashPasswordCurrent = $this->objectInstance->getHashedPassword($password, $salt);
255 if ($i > 0 && $saltedHashPasswordPrevious === $saltedHashPasswordCurrent) {
256 $criticalPwLength = $i;
257 break;
258 }
259 }
260 $this->assertTrue($criticalPwLength == 0 || $criticalPwLength > 32, 'Duplicates of hashed passwords with plaintext password of length ' . $criticalPwLength . '+.');
261 }
262
263 /**
264 * @test
265 */
266 public function modifiedMinHashCount()
267 {
268 $minHashCount = $this->objectInstance->getMinHashCount();
269 $this->objectInstance->setMinHashCount($minHashCount - 1);
270 $this->assertTrue($this->objectInstance->getMinHashCount() < $minHashCount);
271 $this->objectInstance->setMinHashCount($minHashCount + 1);
272 $this->assertTrue($this->objectInstance->getMinHashCount() > $minHashCount);
273 }
274
275 /**
276 * @test
277 */
278 public function modifiedMaxHashCount()
279 {
280 $maxHashCount = $this->objectInstance->getMaxHashCount();
281 $this->objectInstance->setMaxHashCount($maxHashCount + 1);
282 $this->assertTrue($this->objectInstance->getMaxHashCount() > $maxHashCount);
283 $this->objectInstance->setMaxHashCount($maxHashCount - 1);
284 $this->assertTrue($this->objectInstance->getMaxHashCount() < $maxHashCount);
285 }
286
287 /**
288 * @test
289 */
290 public function modifiedHashCount()
291 {
292 $hashCount = $this->objectInstance->getHashCount();
293 $this->objectInstance->setMaxHashCount($hashCount + 1);
294 $this->objectInstance->setHashCount($hashCount + 1);
295 $this->assertTrue($this->objectInstance->getHashCount() > $hashCount);
296 $this->objectInstance->setMinHashCount($hashCount - 1);
297 $this->objectInstance->setHashCount($hashCount - 1);
298 $this->assertTrue($this->objectInstance->getHashCount() < $hashCount);
299 // reset hashcount
300 $this->objectInstance->setHashCount(null);
301 }
302
303 /**
304 * @test
305 */
306 public function updateNecessityForValidSaltedPassword()
307 {
308 $this->skipTestIfBlowfishIsNotAvailable();
309 $password = 'password';
310 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
311 $this->assertFalse($this->objectInstance->isHashUpdateNeeded($saltedHashPassword));
312 }
313
314 /**
315 * @test
316 */
317 public function updateNecessityForIncreasedHashcount()
318 {
319 $password = 'password';
320 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
321 $increasedHashCount = $this->objectInstance->getHashCount() + 1;
322 $this->objectInstance->setMaxHashCount($increasedHashCount);
323 $this->objectInstance->setHashCount($increasedHashCount);
324 $this->assertTrue($this->objectInstance->isHashUpdateNeeded($saltedHashPassword));
325 // reset hashcount
326 $this->objectInstance->setHashCount(null);
327 }
328
329 /**
330 * @test
331 */
332 public function updateNecessityForDecreasedHashcount()
333 {
334 $this->skipTestIfBlowfishIsNotAvailable();
335 $password = 'password';
336 $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
337 $decreasedHashCount = $this->objectInstance->getHashCount() - 1;
338 $this->objectInstance->setMinHashCount($decreasedHashCount);
339 $this->objectInstance->setHashCount($decreasedHashCount);
340 $this->assertFalse($this->objectInstance->isHashUpdateNeeded($saltedHashPassword));
341 // reset hashcount
342 $this->objectInstance->setHashCount(null);
343 }
344 }