From 8890b4793ca7d9b18e052ea2ffd5901af2d17ed7 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Tue, 30 Nov 2010 05:35:04 +0100 Subject: [PATCH] Add a simple nonce-returning function. I avoided Horde_Nonce for now as I got doubts whether Horde really needs the complexity of Horde_Nonce. --- framework/Token/lib/Horde/Token/Base.php | 11 ++++ framework/Token/test/Horde/Token/AllTests.php | 50 +++++++++++++++ framework/Token/test/Horde/Token/Autoload.php | 23 +++++++ framework/Token/test/Horde/Token/Unit/FileTest.php | 73 ++++++++++++++++++++++ framework/Token/test/Horde/Token/phpunit.xml | 8 +++ 5 files changed, 165 insertions(+) create mode 100644 framework/Token/test/Horde/Token/AllTests.php create mode 100644 framework/Token/test/Horde/Token/Autoload.php create mode 100644 framework/Token/test/Horde/Token/Unit/FileTest.php create mode 100644 framework/Token/test/Horde/Token/phpunit.xml diff --git a/framework/Token/lib/Horde/Token/Base.php b/framework/Token/lib/Horde/Token/Base.php index 1ce54f8b4..388f867d3 100644 --- a/framework/Token/lib/Horde/Token/Base.php +++ b/framework/Token/lib/Horde/Token/Base.php @@ -97,6 +97,17 @@ abstract class Horde_Token_Base abstract public function purge(); /** + * Return a "number used once" (a concatenation of a timestamp and a random + * numer). + * + * @return string A string of 6 bytes. + */ + public function getNonce() + { + return pack('N', time()) . pack('n', mt_rand()); + } + + /** * Encodes the remote address. * * @return string Encoded address. diff --git a/framework/Token/test/Horde/Token/AllTests.php b/framework/Token/test/Horde/Token/AllTests.php new file mode 100644 index 000000000..5140d7523 --- /dev/null +++ b/framework/Token/test/Horde/Token/AllTests.php @@ -0,0 +1,50 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Token + */ + +/** + * Define the main method + */ +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Horde_Token_AllTests::main'); +} + +/** + * Prepare the test setup. + */ +require_once 'Horde/Test/AllTests.php'; + +/** + * Combine the tests for this package. + * + * Copyright 2007-2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Horde + * @package Token + * @subpackage UnitTests + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Token + */ +class Horde_Token_AllTests extends Horde_Test_AllTests +{ +} + +Horde_Token_AllTests::init('Horde_Token', __FILE__); + +if (PHPUnit_MAIN_METHOD == 'Horde_Token_AllTests::main') { + Horde_Token_AllTests::main(); +} diff --git a/framework/Token/test/Horde/Token/Autoload.php b/framework/Token/test/Horde/Token/Autoload.php new file mode 100644 index 000000000..5930139ae --- /dev/null +++ b/framework/Token/test/Horde/Token/Autoload.php @@ -0,0 +1,23 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Token + */ + +require_once 'Horde/Test/Autoload.php'; + +/** Catch strict standards */ +error_reporting(E_ALL | E_STRICT); diff --git a/framework/Token/test/Horde/Token/Unit/FileTest.php b/framework/Token/test/Horde/Token/Unit/FileTest.php new file mode 100644 index 000000000..9199d116f --- /dev/null +++ b/framework/Token/test/Horde/Token/Unit/FileTest.php @@ -0,0 +1,73 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Token + */ + +/** + * Prepare the test setup. + */ +require_once dirname(__FILE__) . '/../Autoload.php'; + +/** + * Base for session testing. + * + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Horde + * @package Token + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Token + */ +class Horde_Token_Unit_Storage_FileTest extends PHPUnit_Framework_TestCase +{ + public function tearDown() + { + if (!empty($this->_temp_dir)) { + $this->_rrmdir($this->_temp_dir); + } + } + + public function testNonces() + { + $t = new Horde_Token_File(); + $this->assertEquals(6, strlen($t->getNonce())); + } + + private function _getTemporaryDirectory() + { + $this->_temp_dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR + . 'Horde_Token_' . mt_rand(); + mkdir($this->_temp_dir); + return $this->_temp_dir; + } + + private function _rrmdir($dir) + { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != '.' && $object != '..') { + if (filetype($dir . DIRECTORY_SEPARATOR . $object) == 'dir') { + $this->_rrmdir($dir . DIRECTORY_SEPARATOR . $object); + } else { + unlink($dir . DIRECTORY_SEPARATOR . $object); + } + } + } + reset($objects); + rmdir($dir); + } + } +} \ No newline at end of file diff --git a/framework/Token/test/Horde/Token/phpunit.xml b/framework/Token/test/Horde/Token/phpunit.xml new file mode 100644 index 000000000..502d3c9b8 --- /dev/null +++ b/framework/Token/test/Horde/Token/phpunit.xml @@ -0,0 +1,8 @@ + + + + + ../../../lib + + + -- 2.11.0