2 namespace TYPO3\CMS\Saltedpasswords\Tests\Unit\Salt
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
18 * Testcase for BlowfishSalt
20 * @author Marcus Krause <marcus#exp2009@t3sec.info>
22 class BlowfishSaltTest
extends \TYPO3\CMS\Core\Tests\UnitTestCase
{
25 * Keeps instance of object to test.
27 * @var \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt
29 protected $objectInstance = NULL;
32 * Sets up the fixtures for this testcase.
36 public function setUp() {
37 $this->objectInstance
= $this->getMock(\TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt
::class, array('dummy'));
41 * Marks tests as skipped if the blowfish method is not available.
45 protected function skipTestIfBlowfishIsNotAvailable() {
46 if (!CRYPT_BLOWFISH
) {
47 $this->markTestSkipped('Blowfish is not supported on your platform.');
54 public function hasCorrectBaseClass() {
55 $hasCorrectBaseClass = get_class($this->objectInstance
) === \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt
::class;
57 if (!$hasCorrectBaseClass && FALSE != get_parent_class($this->objectInstance
)) {
58 $hasCorrectBaseClass = is_subclass_of($this->objectInstance
, \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt
::class);
60 $this->assertTrue($hasCorrectBaseClass);
66 public function nonZeroSaltLength() {
67 $this->assertTrue($this->objectInstance
->getSaltLength() > 0);
73 public function emptyPasswordResultsInNullSaltedPassword() {
75 $this->assertNull($this->objectInstance
->getHashedPassword($password));
81 public function nonEmptyPasswordResultsInNonNullSaltedPassword() {
82 $this->skipTestIfBlowfishIsNotAvailable();
84 $this->assertNotNull($this->objectInstance
->getHashedPassword($password));
90 public function createdSaltedHashOfProperStructure() {
91 $this->skipTestIfBlowfishIsNotAvailable();
92 $password = 'password';
93 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
94 $this->assertTrue($this->objectInstance
->isValidSaltedPW($saltedHashPassword));
100 public function createdSaltedHashOfProperStructureForCustomSaltWithoutSetting() {
101 $this->skipTestIfBlowfishIsNotAvailable();
102 $password = 'password';
103 // custom salt without setting
104 $randomBytes = \TYPO3\CMS\Core\Utility\GeneralUtility
::generateRandomBytes($this->objectInstance
->getSaltLength());
105 $salt = $this->objectInstance
->base64Encode($randomBytes, $this->objectInstance
->getSaltLength());
106 $this->assertTrue($this->objectInstance
->isValidSalt($salt));
107 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password, $salt);
108 $this->assertTrue($this->objectInstance
->isValidSaltedPW($saltedHashPassword));
114 public function createdSaltedHashOfProperStructureForMinimumHashCount() {
115 $this->skipTestIfBlowfishIsNotAvailable();
116 $password = 'password';
117 $minHashCount = $this->objectInstance
->getMinHashCount();
118 $this->objectInstance
->setHashCount($minHashCount);
119 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
120 $this->assertTrue($this->objectInstance
->isValidSaltedPW($saltedHashPassword));
122 $this->objectInstance
->setHashCount(NULL);
126 * Tests authentication procedure with alphabet characters.
128 * Checks if a "plain-text password" is every time mapped to the
129 * same "salted password hash" when using the same salt.
133 public function authenticationWithValidAlphaCharClassPassword() {
134 $this->skipTestIfBlowfishIsNotAvailable();
135 $password = 'aEjOtY';
136 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
137 $this->assertTrue($this->objectInstance
->checkPassword($password, $saltedHashPassword));
141 * Tests authentication procedure with numeric characters.
143 * Checks if a "plain-text password" is every time mapped to the
144 * same "salted password hash" when using the same salt.
148 public function authenticationWithValidNumericCharClassPassword() {
149 $this->skipTestIfBlowfishIsNotAvailable();
151 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
152 $this->assertTrue($this->objectInstance
->checkPassword($password, $saltedHashPassword));
156 * Tests authentication procedure with US-ASCII special characters.
158 * Checks if a "plain-text password" is every time mapped to the
159 * same "salted password hash" when using the same salt.
163 public function authenticationWithValidAsciiSpecialCharClassPassword() {
164 $this->skipTestIfBlowfishIsNotAvailable();
165 $password = ' !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
166 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
167 $this->assertTrue($this->objectInstance
->checkPassword($password, $saltedHashPassword));
171 * Tests authentication procedure with latin1 special characters.
173 * Checks if a "plain-text password" is every time mapped to the
174 * same "salted password hash" when using the same salt.
178 public function authenticationWithValidLatin1SpecialCharClassPassword() {
179 $this->skipTestIfBlowfishIsNotAvailable();
181 for ($i = 160; $i <= 191; $i++
) {
182 $password .= chr($i);
184 $password .= chr(215) . chr(247);
185 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
186 $this->assertTrue($this->objectInstance
->checkPassword($password, $saltedHashPassword));
190 * Tests authentication procedure with latin1 umlauts.
192 * Checks if a "plain-text password" is every time mapped to the
193 * same "salted password hash" when using the same salt.
197 public function authenticationWithValidLatin1UmlautCharClassPassword() {
198 $this->skipTestIfBlowfishIsNotAvailable();
200 for ($i = 192; $i <= 214; $i++
) {
201 $password .= chr($i);
203 for ($i = 216; $i <= 246; $i++
) {
204 $password .= chr($i);
206 for ($i = 248; $i <= 255; $i++
) {
207 $password .= chr($i);
209 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
210 $this->assertTrue($this->objectInstance
->checkPassword($password, $saltedHashPassword));
216 public function authenticationWithNonValidPassword() {
217 $this->skipTestIfBlowfishIsNotAvailable();
218 $password = 'password';
219 $password1 = $password . 'INVALID';
220 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
221 $this->assertFalse($this->objectInstance
->checkPassword($password1, $saltedHashPassword));
227 public function passwordVariationsResultInDifferentHashes() {
228 $this->skipTestIfBlowfishIsNotAvailable();
231 $criticalPwLength = 0;
232 // We're using a constant salt.
233 $saltedHashPasswordCurrent = $salt = $this->objectInstance
->getHashedPassword($pad);
234 for ($i = 0; $i <= 128; $i +
= 8) {
235 $password = str_repeat($pad, max($i, 1));
236 $saltedHashPasswordPrevious = $saltedHashPasswordCurrent;
237 $saltedHashPasswordCurrent = $this->objectInstance
->getHashedPassword($password, $salt);
238 if ($i > 0 && $saltedHashPasswordPrevious === $saltedHashPasswordCurrent) {
239 $criticalPwLength = $i;
243 $this->assertTrue($criticalPwLength == 0 ||
$criticalPwLength > 32, 'Duplicates of hashed passwords with plaintext password of length ' . $criticalPwLength . '+.');
249 public function modifiedMinHashCount() {
250 $minHashCount = $this->objectInstance
->getMinHashCount();
251 $this->objectInstance
->setMinHashCount($minHashCount - 1);
252 $this->assertTrue($this->objectInstance
->getMinHashCount() < $minHashCount);
253 $this->objectInstance
->setMinHashCount($minHashCount +
1);
254 $this->assertTrue($this->objectInstance
->getMinHashCount() > $minHashCount);
260 public function modifiedMaxHashCount() {
261 $maxHashCount = $this->objectInstance
->getMaxHashCount();
262 $this->objectInstance
->setMaxHashCount($maxHashCount +
1);
263 $this->assertTrue($this->objectInstance
->getMaxHashCount() > $maxHashCount);
264 $this->objectInstance
->setMaxHashCount($maxHashCount - 1);
265 $this->assertTrue($this->objectInstance
->getMaxHashCount() < $maxHashCount);
271 public function modifiedHashCount() {
272 $hashCount = $this->objectInstance
->getHashCount();
273 $this->objectInstance
->setMaxHashCount($hashCount +
1);
274 $this->objectInstance
->setHashCount($hashCount +
1);
275 $this->assertTrue($this->objectInstance
->getHashCount() > $hashCount);
276 $this->objectInstance
->setMinHashCount($hashCount - 1);
277 $this->objectInstance
->setHashCount($hashCount - 1);
278 $this->assertTrue($this->objectInstance
->getHashCount() < $hashCount);
280 $this->objectInstance
->setHashCount(NULL);
286 public function updateNecessityForValidSaltedPassword() {
287 $this->skipTestIfBlowfishIsNotAvailable();
288 $password = 'password';
289 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
290 $this->assertFalse($this->objectInstance
->isHashUpdateNeeded($saltedHashPassword));
296 public function updateNecessityForIncreasedHashcount() {
297 $password = 'password';
298 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
299 $increasedHashCount = $this->objectInstance
->getHashCount() +
1;
300 $this->objectInstance
->setMaxHashCount($increasedHashCount);
301 $this->objectInstance
->setHashCount($increasedHashCount);
302 $this->assertTrue($this->objectInstance
->isHashUpdateNeeded($saltedHashPassword));
304 $this->objectInstance
->setHashCount(NULL);
310 public function updateNecessityForDecreasedHashcount() {
311 $this->skipTestIfBlowfishIsNotAvailable();
312 $password = 'password';
313 $saltedHashPassword = $this->objectInstance
->getHashedPassword($password);
314 $decreasedHashCount = $this->objectInstance
->getHashCount() - 1;
315 $this->objectInstance
->setMinHashCount($decreasedHashCount);
316 $this->objectInstance
->setHashCount($decreasedHashCount);
317 $this->assertFalse($this->objectInstance
->isHashUpdateNeeded($saltedHashPassword));
319 $this->objectInstance
->setHashCount(NULL);