Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / tests / tx_saltedpasswords_salts_factoryTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Marcus Krause <marcus#exp2009@t3sec.info>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains class "tx_saltedpasswords_salts_factory"
29 * that provides a salted hashing method factory.
30 *
31 * $Id$
32 */
33
34 /**
35 * Testcases for class tx_saltedpasswords_salts_factory.
36 *
37 * @author Marcus Krause <marcus#exp2009@t3sec.info>
38 *
39 * @since 2009-09-06
40 * @package TYPO3
41 * @subpackage tx_saltedpasswords
42 */
43 class tx_saltedpasswords_salts_factoryTest extends tx_phpunit_testcase {
44
45
46 /**
47 * Keeps instance of object to test.
48 *
49 * @var tx_saltedpasswords_abstract_salts
50 */
51 protected $objectInstance = NULL;
52
53 /**
54 * Sets up the fixtures for this testcase.
55 *
56 * @return void
57 */
58 protected function setUp() {
59 $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance();
60 }
61
62 /**
63 * Tears down objects and settings created in this testcase.
64 *
65 * @return void
66 */
67 public function tearDown() {
68 unset($this->objectInstance);
69 }
70
71 /**
72 * @test
73 */
74 public function objectInstanceNotNull() {
75 $this->assertNotNull($this->objectInstance);
76 }
77
78 /**
79 * @test
80 */
81 public function objectInstanceExtendsAbstractClass() {
82 $this->assertTrue(is_subclass_of($this->objectInstance, 'tx_saltedpasswords_abstract_salts'));
83 }
84
85 /**
86 * @test
87 */
88 public function objectInstanceImplementsInterface() {
89 $this->assertTrue(method_exists($this->objectInstance, 'checkPassword'), 'Missing method checkPassword() from interface tx_saltedpasswords_salts.');
90 $this->assertTrue(method_exists($this->objectInstance, 'isHashUpdateNeeded'), 'Missing method isHashUpdateNeeded() from interface tx_saltedpasswords_salts.');
91 $this->assertTrue(method_exists($this->objectInstance, 'isValidSalt') , 'Missing method isValidSalt() from interface tx_saltedpasswords_salts.');
92 $this->assertTrue(method_exists($this->objectInstance, 'isValidSaltedPW') , 'Missing method isValidSaltedPW() from interface tx_saltedpasswords_salts.');
93 $this->assertTrue(method_exists($this->objectInstance, 'getHashedPassword'), 'Missing method getHashedPassword() from interface tx_saltedpasswords_salts.');
94 $this->assertTrue(method_exists($this->objectInstance, 'getSaltLength'), 'Missing method getSaltLength() from interface tx_saltedpasswords_salts.');
95 }
96
97 /**
98 * @test
99 */
100 public function base64EncodeReturnsProperLength() {
101 // 3 Bytes should result in a 6 char length base64 encoded string
102 // used for MD5 and PHPass salted hashing
103 $byteLength = 3;
104 $reqLengthBase64 = intval(ceil(($byteLength * 8) / 6));
105 $randomBytes = t3lib_div::generateRandomBytes($byteLength);
106 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
107
108 // 16 Bytes should result in a 22 char length base64 encoded string
109 // used for Blowfish salted hashing
110 $byteLength = 16;
111 $reqLengthBase64 = intval(ceil(($byteLength * 8) / 6));
112 $randomBytes = t3lib_div::generateRandomBytes($byteLength);
113 $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
114 }
115
116 /**
117 * @test
118 */
119 public function objectInstanceForMD5Salts() {
120 $saltMD5 = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
121 $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltMD5);
122
123 $this->assertTrue((get_class($this->objectInstance) == 'tx_saltedpasswords_salts_md5') || (is_subclass_of($this->objectInstance, 'tx_saltedpasswords_salts_md5')) );
124 }
125
126 /**
127 * @test
128 */
129 public function objectInstanceForBlowfishSalts() {
130 $saltBlowfish = '$2a$07$abcdefghijklmnopqrstuuIdQV69PAxWYTgmnoGpe0Sk47GNS/9ZW';
131 $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltBlowfish);
132 $this->assertTrue((get_class($this->objectInstance) == 'tx_saltedpasswords_salts_blowfish') || (is_subclass_of($this->objectInstance, 'tx_saltedpasswords_salts_blowfish')) );
133 }
134
135 /**
136 * @test
137 */
138 public function objectInstanceForPhpassSalts() {
139 $saltPhpass = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
140 $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltPhpass);
141 $this->assertTrue((get_class($this->objectInstance) == 'tx_saltedpasswords_salts_phpass') || (is_subclass_of($this->objectInstance, 'tx_saltedpasswords_salts_phpass')) );
142 }
143
144 /**
145 * @test
146 */
147 public function resettingFactoryInstanceSucceeds() {
148 $defaultClassNameToUse = tx_saltedpasswords_div::getDefaultSaltingHashingMethod();
149
150 $saltedPW = '';
151 if ($defaultClassNameToUse == 'tx_saltedpasswords_salts_md5') {
152 $saltedPW = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
153 } else {
154 $saltedPW = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
155 }
156 $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltedPW);
157
158 // resetting
159 $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance(NULL);
160 $this->assertTrue((get_class($this->objectInstance) == $defaultClassNameToUse) || (is_subclass_of($this->objectInstance, $defaultClassNameToUse)));
161 }
162 }
163 ?>