2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SecurityTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
App::import('Core', 'Security');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SecurityTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class SecurityTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* sut property
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var mixed null
|
|
|
|
* @access public
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
var $sut = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* setUp method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function setUp() {
|
|
|
|
$this->sut =& Security::getInstance();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testInactiveMins method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
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());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testGenerateAuthkey method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testGenerateAuthkey() {
|
|
|
|
$this->assertEqual(strlen(Security::generateAuthKey()), 40);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testValidateAuthKey method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidateAuthKey() {
|
|
|
|
$authKey = Security::generateAuthKey();
|
|
|
|
$this->assertTrue(Security::validateAuthKey($authKey));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testHash method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testHash() {
|
2010-03-06 03:06:20 +00:00
|
|
|
$Security =& Security::getInstance();
|
2009-03-21 23:55:39 +00:00
|
|
|
$_hashType = $Security->hashType;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = 'someKey';
|
2008-10-02 00:03:52 +00:00
|
|
|
$hash = 'someHash';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
|
|
|
|
2008-10-02 00:03:52 +00:00
|
|
|
$result = Security::hash($key, null, $hash);
|
|
|
|
$this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
|
|
|
|
|
|
|
|
$result = Security::hash($key, 'sha1', $hash);
|
|
|
|
$this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
|
|
|
|
2008-08-02 14:44:42 +00:00
|
|
|
if (!function_exists('hash') && !function_exists('mhash')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 32);
|
|
|
|
$this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 32);
|
2008-08-02 14:44:42 +00:00
|
|
|
} else {
|
|
|
|
$this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 64);
|
|
|
|
$this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 64);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-21 23:55:39 +00:00
|
|
|
|
|
|
|
Security::setHash($_hashType);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testCipher method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testCipher() {
|
|
|
|
$length = 10;
|
|
|
|
$txt = '';
|
2008-06-05 15:20:45 +00:00
|
|
|
for ($i = 0; $i < $length; $i++) {
|
2008-07-05 14:31:22 +00:00
|
|
|
$txt .= mt_rand(0, 255);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$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, '');
|
2010-03-27 21:19:42 +00:00
|
|
|
|
|
|
|
$txt = 123456;
|
|
|
|
$key = 'my_key';
|
|
|
|
$result = Security::cipher($txt, $key);
|
|
|
|
$this->assertEqual(Security::cipher($result, $key), $txt);
|
|
|
|
|
|
|
|
$txt = '123456';
|
|
|
|
$key = 'my_key';
|
|
|
|
$result = Security::cipher($txt, $key);
|
|
|
|
$this->assertEqual(Security::cipher($result, $key), $txt);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|