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