[BUGFIX] Added space after template icon
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / Tests / Unit / Salt / SaltFactoryTest.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 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Testcase for SaltFactory
22 */
23 class SaltFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
24 {
25 /**
26 * Keeps instance of object to test.
27 *
28 * @var \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt
29 */
30 protected $objectInstance = null;
31
32 /**
33 * Sets up the fixtures for this testcase.
34 *
35 * @return void
36 */
37 protected function setUp()
38 {
39 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance();
40 }
41
42 /**
43 * @test
44 */
45 public function objectInstanceNotNull()
46 {
47 $this->assertNotNull($this->objectInstance);
48 }
49
50 /**
51 * @test
52 */
53 public function objectInstanceExtendsAbstractClass()
54 {
55 $this->assertTrue(is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt::class));
56 }
57
58 /**
59 * @test
60 */
61 public function objectInstanceImplementsInterface()
62 {
63 $this->assertTrue(method_exists($this->objectInstance, 'checkPassword'), 'Missing method checkPassword() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
64 $this->assertTrue(method_exists($this->objectInstance, 'isHashUpdateNeeded'), 'Missing method isHashUpdateNeeded() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
65 $this->assertTrue(method_exists($this->objectInstance, 'isValidSalt'), 'Missing method isValidSalt() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
66 $this->assertTrue(method_exists($this->objectInstance, 'isValidSaltedPW'), 'Missing method isValidSaltedPW() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
67 $this->assertTrue(method_exists($this->objectInstance, 'getHashedPassword'), 'Missing method getHashedPassword() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
68 $this->assertTrue(method_exists($this->objectInstance, 'getSaltLength'), 'Missing method getSaltLength() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
69 }
70
71 /**
72 * @test
73 */
74 public function base64EncodeReturnsProperLength()
75 {
76 // 3 Bytes should result in a 6 char length base64 encoded string
77 // used for MD5 and PHPass salted hashing
78 $byteLength = 3;
79 $reqLengthBase64 = (int)ceil($byteLength * 8 / 6);
80 $randomBytes = GeneralUtility::makeInstance(Random::class)->generateRandomBytes($byteLength);
81 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
82 // 16 Bytes should result in a 22 char length base64 encoded string
83 // used for Blowfish salted hashing
84 $byteLength = 16;
85 $reqLengthBase64 = (int)ceil($byteLength * 8 / 6);
86 $randomBytes = GeneralUtility::makeInstance(Random::class)->generateRandomBytes($byteLength);
87 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
88 }
89
90 /**
91 * @test
92 */
93 public function objectInstanceForMD5Salts()
94 {
95 $saltMD5 = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
96 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltMD5);
97 $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class));
98 }
99
100 /**
101 * @test
102 */
103 public function objectInstanceForBlowfishSalts()
104 {
105 $saltBlowfish = '$2a$07$abcdefghijklmnopqrstuuIdQV69PAxWYTgmnoGpe0Sk47GNS/9ZW';
106 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltBlowfish);
107 $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class));
108 }
109
110 /**
111 * @test
112 */
113 public function objectInstanceForPhpassSalts()
114 {
115 $saltPhpass = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
116 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltPhpass);
117 $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class));
118 }
119
120 /**
121 * @test
122 */
123 public function objectInstanceForPbkdf2Salts()
124 {
125 $saltPbkdf2 = '$pbkdf2-sha256$6400$0ZrzXitFSGltTQnBWOsdAw$Y11AchqV4b0sUisdZd0Xr97KWoymNE0LNNrnEgY4H9M';
126 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltPbkdf2);
127 $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\Pbkdf2Salt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\Pbkdf2Salt::class));
128 }
129
130 /**
131 * @test
132 */
133 public function resettingFactoryInstanceSucceeds()
134 {
135 $defaultClassNameToUse = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod();
136 if ($defaultClassNameToUse == \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class) {
137 $saltedPW = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
138 } else {
139 $saltedPW = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
140 }
141 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltedPW);
142 // resetting
143 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(null);
144 $this->assertTrue(get_class($this->objectInstance) == $defaultClassNameToUse || is_subclass_of($this->objectInstance, $defaultClassNameToUse));
145 }
146 }