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