Add a simple nonce-returning function.
authorGunnar Wrobel <p@rdus.de>
Tue, 30 Nov 2010 04:35:04 +0000 (05:35 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 30 Nov 2010 12:48:26 +0000 (13:48 +0100)
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
framework/Token/test/Horde/Token/AllTests.php [new file with mode: 0644]
framework/Token/test/Horde/Token/Autoload.php [new file with mode: 0644]
framework/Token/test/Horde/Token/Unit/FileTest.php [new file with mode: 0644]
framework/Token/test/Horde/Token/phpunit.xml [new file with mode: 0644]

index 1ce54f8..388f867 100644 (file)
@@ -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 (file)
index 0000000..5140d75
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+/**
+ * All tests for the Horde_Token:: package.
+ *
+ * PHP version 5
+ *
+ * @category   Horde
+ * @package    Token
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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 (file)
index 0000000..5930139
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Setup autoloading for the tests.
+ *
+ * PHP version 5
+ *
+ * Copyright 2009-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 <wrobel@pardus.de>
+ * @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 (file)
index 0000000..9199d11
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Test the file based token backend.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Token
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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 (file)
index 0000000..502d3c9
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit>
+  <filter>
+    <whitelist>
+      <directory suffix=".php">../../../lib</directory>
+    </whitelist>
+  </filter>
+</phpunit>