TYPO3 API  SVNRelease
tx_saltedpasswords_salts_factoryTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2011 Marcus Krause <marcus#exp2009@t3sec.info>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Contains class "tx_saltedpasswords_salts_factory"
00029  * that provides a salted hashing method factory.
00030  *
00031  * $Id: tx_saltedpasswords_salts_factoryTest.php 10120 2011-01-18 20:03:36Z ohader $
00032  */
00033 
00034 /**
00035  * Testcases for class tx_saltedpasswords_salts_factory.
00036  *
00037  * @author      Marcus Krause <marcus#exp2009@t3sec.info>
00038  *
00039  * @since       2009-09-06
00040  * @package     TYPO3
00041  * @subpackage  tx_saltedpasswords
00042  */
00043 class tx_saltedpasswords_salts_factoryTest extends tx_phpunit_testcase {
00044 
00045 
00046     /**
00047      * Keeps instance of object to test.
00048      *
00049      * @var tx_saltedpasswords_abstract_salts
00050      */
00051     protected $objectInstance = NULL;
00052 
00053     /**
00054      * Sets up the fixtures for this testcase.
00055      *
00056      * @return  void
00057      */
00058     protected function setUp() {
00059         $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance();
00060     }
00061 
00062     /**
00063      * Tears down objects and settings created in this testcase.
00064      *
00065      * @return  void
00066      */
00067     public function tearDown() {
00068         unset($this->objectInstance);
00069     }
00070 
00071     /**
00072      * @test
00073      */
00074     public function objectInstanceNotNull() {
00075         $this->assertNotNull($this->objectInstance);
00076     }
00077 
00078     /**
00079      * @test
00080      */
00081     public function objectInstanceExtendsAbstractClass() {
00082         $this->assertTrue(is_subclass_of($this->objectInstance, 'tx_saltedpasswords_abstract_salts'));
00083     }
00084 
00085     /**
00086      * @test
00087      */
00088     public function objectInstanceImplementsInterface() {
00089         $this->assertTrue(method_exists($this->objectInstance, 'checkPassword'), 'Missing method checkPassword() from interface tx_saltedpasswords_salts.');
00090         $this->assertTrue(method_exists($this->objectInstance, 'isHashUpdateNeeded'), 'Missing method isHashUpdateNeeded() from interface tx_saltedpasswords_salts.');
00091         $this->assertTrue(method_exists($this->objectInstance, 'isValidSalt') , 'Missing method isValidSalt() from interface tx_saltedpasswords_salts.');
00092         $this->assertTrue(method_exists($this->objectInstance, 'isValidSaltedPW') , 'Missing method isValidSaltedPW() from interface tx_saltedpasswords_salts.');
00093         $this->assertTrue(method_exists($this->objectInstance, 'getHashedPassword'), 'Missing method getHashedPassword() from interface tx_saltedpasswords_salts.');
00094         $this->assertTrue(method_exists($this->objectInstance, 'getSaltLength'), 'Missing method getSaltLength() from interface tx_saltedpasswords_salts.');
00095     }
00096 
00097     /**
00098      * @test
00099      */
00100     public function base64EncodeReturnsProperLength() {
00101             // 3 Bytes should result in a 6 char length base64 encoded string
00102             // used for MD5 and PHPass salted hashing
00103         $byteLength = 3;
00104         $reqLengthBase64 = intval(ceil(($byteLength * 8) / 6));
00105         $randomBytes = t3lib_div::generateRandomBytes($byteLength);
00106         $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
00107 
00108             // 16 Bytes should result in a 22 char length base64 encoded string
00109             // used for Blowfish salted hashing
00110         $byteLength = 16;
00111         $reqLengthBase64 = intval(ceil(($byteLength * 8) / 6));
00112         $randomBytes = t3lib_div::generateRandomBytes($byteLength);
00113         $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
00114     }
00115 
00116     /**
00117      * @test
00118      */
00119     public function objectInstanceForMD5Salts() {
00120         $saltMD5 = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
00121         $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltMD5);
00122 
00123         $this->assertTrue((get_class($this->objectInstance) == 'tx_saltedpasswords_salts_md5') || (is_subclass_of($this->objectInstance, 'tx_saltedpasswords_salts_md5')) );
00124     }
00125 
00126     /**
00127      * @test
00128      */
00129     public function objectInstanceForBlowfishSalts() {
00130         $saltBlowfish = '$2a$07$abcdefghijklmnopqrstuuIdQV69PAxWYTgmnoGpe0Sk47GNS/9ZW';
00131         $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltBlowfish);
00132         $this->assertTrue((get_class($this->objectInstance) == 'tx_saltedpasswords_salts_blowfish') || (is_subclass_of($this->objectInstance, 'tx_saltedpasswords_salts_blowfish')) );
00133     }
00134 
00135     /**
00136      * @test
00137      */
00138     public function objectInstanceForPhpassSalts() {
00139         $saltPhpass = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
00140         $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltPhpass);
00141         $this->assertTrue((get_class($this->objectInstance) == 'tx_saltedpasswords_salts_phpass') || (is_subclass_of($this->objectInstance, 'tx_saltedpasswords_salts_phpass')) );
00142     }
00143 
00144     /**
00145      * @test
00146      */
00147     public function resettingFactoryInstanceSucceeds() {
00148         $defaultClassNameToUse = tx_saltedpasswords_div::getDefaultSaltingHashingMethod();
00149 
00150         $saltedPW = '';
00151         if ($defaultClassNameToUse == 'tx_saltedpasswords_salts_md5') {
00152             $saltedPW = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
00153         } else {
00154             $saltedPW = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
00155         }
00156         $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance($saltedPW);
00157 
00158             // resetting
00159         $this->objectInstance = tx_saltedpasswords_salts_factory::getSaltingInstance(NULL);
00160         $this->assertTrue((get_class($this->objectInstance) == $defaultClassNameToUse) || (is_subclass_of($this->objectInstance, $defaultClassNameToUse)));
00161     }
00162 }
00163 ?>