dccb0be6921bbd0d0768cd350cded393cdb3ca48
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / Tests / Unit / Salts / SaltFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Saltedpasswords\Tests\Unit\Salts;
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 * Contains class "tx_saltedpasswords_salts_factory"
31 * that provides a salted hashing method factory.
32 */
33 /**
34 * Testcases for class tx_saltedpasswords_salts_factory.
35 *
36 * @author Marcus Krause <marcus#exp2009@t3sec.info>
37 * @since 2009-09-06
38 * @package TYPO3
39 * @subpackage tx_saltedpasswords
40 */
41 class SaltFactoryTest extends \tx_phpunit_testcase {
42
43 /**
44 * Keeps instance of object to test.
45 *
46 * @var \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt
47 */
48 protected $objectInstance = NULL;
49
50 /**
51 * Sets up the fixtures for this testcase.
52 *
53 * @return void
54 */
55 protected function setUp() {
56 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance();
57 }
58
59 /**
60 * Tears down objects and settings created in this testcase.
61 *
62 * @return void
63 */
64 public function tearDown() {
65 unset($this->objectInstance);
66 }
67
68 /**
69 * @test
70 */
71 public function objectInstanceNotNull() {
72 $this->assertNotNull($this->objectInstance);
73 }
74
75 /**
76 * @test
77 */
78 public function objectInstanceExtendsAbstractClass() {
79 $this->assertTrue(is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\AbstractSalt'));
80 }
81
82 /**
83 * @test
84 */
85 public function objectInstanceImplementsInterface() {
86 $this->assertTrue(method_exists($this->objectInstance, 'checkPassword'), 'Missing method checkPassword() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
87 $this->assertTrue(method_exists($this->objectInstance, 'isHashUpdateNeeded'), 'Missing method isHashUpdateNeeded() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
88 $this->assertTrue(method_exists($this->objectInstance, 'isValidSalt'), 'Missing method isValidSalt() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
89 $this->assertTrue(method_exists($this->objectInstance, 'isValidSaltedPW'), 'Missing method isValidSaltedPW() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
90 $this->assertTrue(method_exists($this->objectInstance, 'getHashedPassword'), 'Missing method getHashedPassword() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
91 $this->assertTrue(method_exists($this->objectInstance, 'getSaltLength'), 'Missing method getSaltLength() from interface TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface.');
92 }
93
94 /**
95 * @test
96 */
97 public function base64EncodeReturnsProperLength() {
98 // 3 Bytes should result in a 6 char length base64 encoded string
99 // used for MD5 and PHPass salted hashing
100 $byteLength = 3;
101 $reqLengthBase64 = intval(ceil($byteLength * 8 / 6));
102 $randomBytes = \TYPO3\CMS\Core\Utility\GeneralUtility::generateRandomBytes($byteLength);
103 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
104 // 16 Bytes should result in a 22 char length base64 encoded string
105 // used for Blowfish salted hashing
106 $byteLength = 16;
107 $reqLengthBase64 = intval(ceil($byteLength * 8 / 6));
108 $randomBytes = \TYPO3\CMS\Core\Utility\GeneralUtility::generateRandomBytes($byteLength);
109 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
110 }
111
112 /**
113 * @test
114 */
115 public function objectInstanceForMD5Salts() {
116 $saltMD5 = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
117 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltMD5);
118 $this->assertTrue(get_class($this->objectInstance) == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt' || is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'));
119 }
120
121 /**
122 * @test
123 */
124 public function objectInstanceForBlowfishSalts() {
125 $saltBlowfish = '$2a$07$abcdefghijklmnopqrstuuIdQV69PAxWYTgmnoGpe0Sk47GNS/9ZW';
126 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltBlowfish);
127 $this->assertTrue(get_class($this->objectInstance) == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\BlowfishSalt' || is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\BlowfishSalt'));
128 }
129
130 /**
131 * @test
132 */
133 public function objectInstanceForPhpassSalts() {
134 $saltPhpass = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
135 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltPhpass);
136 $this->assertTrue(get_class($this->objectInstance) == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt' || is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt'));
137 }
138
139 /**
140 * @test
141 */
142 public function resettingFactoryInstanceSucceeds() {
143 $defaultClassNameToUse = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod();
144 $saltedPW = '';
145 if ($defaultClassNameToUse == 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt') {
146 $saltedPW = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
147 } else {
148 $saltedPW = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
149 }
150 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltedPW);
151 // resetting
152 $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL);
153 $this->assertTrue(get_class($this->objectInstance) == $defaultClassNameToUse || is_subclass_of($this->objectInstance, $defaultClassNameToUse));
154 }
155
156 }
157
158
159 ?>