[TASK] Re-work/simplify copyright header in PHP files - Part 8
[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 /**
18 * Testcase for SaltFactory
19 *
20 * @author Marcus Krause <marcus#exp2009@t3sec.info>
21 * @since 2009-09-06
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 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance();
39 }
40
41 /**
42 * @test
43 */
44 public function objectInstanceNotNull() {
45 $this->assertNotNull($this->objectInstance);
46 }
47
48 /**
49 * @test
50 */
51 public function objectInstanceExtendsAbstractClass() {
52 $this->assertTrue(is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\AbstractSalt'));
53 }
54
55 /**
56 * @test
57 */
58 public function objectInstanceImplementsInterface() {
59 $this->assertTrue(method_exists($this->objectInstance, 'checkPassword'), 'Missing method checkPassword() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
60 $this->assertTrue(method_exists($this->objectInstance, 'isHashUpdateNeeded'), 'Missing method isHashUpdateNeeded() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
61 $this->assertTrue(method_exists($this->objectInstance, 'isValidSalt'), 'Missing method isValidSalt() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
62 $this->assertTrue(method_exists($this->objectInstance, 'isValidSaltedPW'), 'Missing method isValidSaltedPW() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
63 $this->assertTrue(method_exists($this->objectInstance, 'getHashedPassword'), 'Missing method getHashedPassword() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
64 $this->assertTrue(method_exists($this->objectInstance, 'getSaltLength'), 'Missing method getSaltLength() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
65 }
66
67 /**
68 * @test
69 */
70 public function base64EncodeReturnsProperLength() {
71 // 3 Bytes should result in a 6 char length base64 encoded string
72 // used for MD5 and PHPass salted hashing
73 $byteLength = 3;
74 $reqLengthBase64 = (int)ceil($byteLength * 8 / 6);
75 $randomBytes = \TYPO3\CMS\Core\Utility\GeneralUtility::generateRandomBytes($byteLength);
76 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
77 // 16 Bytes should result in a 22 char length base64 encoded string
78 // used for Blowfish salted hashing
79 $byteLength = 16;
80 $reqLengthBase64 = (int)ceil($byteLength * 8 / 6);
81 $randomBytes = \TYPO3\CMS\Core\Utility\GeneralUtility::generateRandomBytes($byteLength);
82 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
83 }
84
85 /**
86 * @test
87 */
88 public function objectInstanceForMD5Salts() {
89 $saltMD5 = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
90 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltMD5);
91 $this->assertTrue(get_class($this->objectInstance) == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt' || is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'));
92 }
93
94 /**
95 * @test
96 */
97 public function objectInstanceForBlowfishSalts() {
98 $saltBlowfish = '$2a$07$abcdefghijklmnopqrstuuIdQV69PAxWYTgmnoGpe0Sk47GNS/9ZW';
99 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltBlowfish);
100 $this->assertTrue(get_class($this->objectInstance) == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\BlowfishSalt' || is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\BlowfishSalt'));
101 }
102
103 /**
104 * @test
105 */
106 public function objectInstanceForPhpassSalts() {
107 $saltPhpass = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
108 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltPhpass);
109 $this->assertTrue(get_class($this->objectInstance) == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt' || is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt'));
110 }
111
112 /**
113 * @test
114 */
115 public function resettingFactoryInstanceSucceeds() {
116 $defaultClassNameToUse = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod();
117 if ($defaultClassNameToUse == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt') {
118 $saltedPW = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
119 } else {
120 $saltedPW = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
121 }
122 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltedPW);
123 // resetting
124 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL);
125 $this->assertTrue(get_class($this->objectInstance) == $defaultClassNameToUse || is_subclass_of($this->objectInstance, $defaultClassNameToUse));
126 }
127
128 }