cakephp2-php8/cake/tests/cases/libs/security.test.php
davidpersson 3c3b852099 Changing package of tests from "cake.tests" to "cake".
Moving tearDown et al. to the top of each test class.
Adding short descriptions to docblocks.
Adding missing docblocks.
Removing unnecessary blank lines and whitespaces.
Updating labels of test groups.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8116 3807eeeb-6ff5-0310-8944-8be069107fe0
2009-03-18 17:55:58 +00:00

158 lines
No EOL
4.3 KiB
PHP

<?php
/* SVN FILE: $Id$ */
/**
* SecurityTest file
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 1.2.0.5432
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', 'Security');
/**
* SecurityTest class
*
* @package cake
* @subpackage cake.tests.cases.libs
*/
class SecurityTest extends CakeTestCase {
/**
* sut property
*
* @var mixed null
* @access public
*/
var $sut = null;
/**
* setUp method
*
* @access public
* @return void
*/
function setUp() {
$this->sut =& Security::getInstance();
}
/**
* testInactiveMins method
*
* @access public
* @return void
*/
function testInactiveMins() {
Configure::write('Security.level', 'high');
$this->assertEqual(10, Security::inactiveMins());
Configure::write('Security.level', 'medium');
$this->assertEqual(100, Security::inactiveMins());
Configure::write('Security.level', 'low');
$this->assertEqual(300, Security::inactiveMins());
}
/**
* testGenerateAuthkey method
*
* @access public
* @return void
*/
function testGenerateAuthkey() {
$this->assertEqual(strlen(Security::generateAuthKey()), 40);
}
/**
* testValidateAuthKey method
*
* @access public
* @return void
*/
function testValidateAuthKey() {
$authKey = Security::generateAuthKey();
$this->assertTrue(Security::validateAuthKey($authKey));
}
/**
* testHash method
*
* @access public
* @return void
*/
function testHash() {
$key = 'someKey';
$hash = 'someHash';
$this->assertIdentical(strlen(Security::hash($key, null, false)), 40);
$this->assertIdentical(strlen(Security::hash($key, 'sha1', false)), 40);
$this->assertIdentical(strlen(Security::hash($key, null, true)), 40);
$this->assertIdentical(strlen(Security::hash($key, 'sha1', true)), 40);
$result = Security::hash($key, null, $hash);
$this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
$result = Security::hash($key, 'sha1', $hash);
$this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
$hashType = 'sha1';
Security::setHash($hashType);
$this->assertIdentical($this->sut->hashType, $hashType);
$this->assertIdentical(strlen(Security::hash($key, null, true)), 40);
$this->assertIdentical(strlen(Security::hash($key, null, false)), 40);
$this->assertIdentical(strlen(Security::hash($key, 'md5', false)), 32);
$this->assertIdentical(strlen(Security::hash($key, 'md5', true)), 32);
$hashType = 'md5';
Security::setHash($hashType);
$this->assertIdentical($this->sut->hashType, $hashType);
$this->assertIdentical(strlen(Security::hash($key, null, false)), 32);
$this->assertIdentical(strlen(Security::hash($key, null, true)), 32);
if (!function_exists('hash') && !function_exists('mhash')) {
$this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 32);
$this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 32);
} else {
$this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 64);
$this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 64);
}
}
/**
* testCipher method
*
* @access public
* @return void
*/
function testCipher() {
$length = 10;
$txt = '';
for ($i = 0; $i < $length; $i++) {
$txt .= mt_rand(0, 255);
}
$key = 'my_key';
$result = Security::cipher($txt, $key);
$this->assertEqual(Security::cipher($result, $key), $txt);
$txt = '';
$key = 'my_key';
$result = Security::cipher($txt, $key);
$this->assertEqual(Security::cipher($result, $key), $txt);
$txt = 'some_text';
$key = '';
$result = Security::cipher($txt, $key);
$this->assertError();
$this->assertIdentical($result, '');
}
}
?>