1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<?php
/**
* Tests for the Nonce implementation.
*
* PHP versions 4 and 5
*
* LICENSE: See the COPYING file included in this distribution.
*
* @package OpenID
* @author JanRain, Inc. <openid@janrain.com>
* @copyright 2006 Janrain, Inc.
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*/
require_once 'PHPUnit.php';
require_once 'Auth/OpenID/Nonce.php';
define('Tests_Auth_OpenID_nonce_re',
'/\A\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ/');
class Tests_Auth_OpenID_Nonce extends PHPUnit_TestSuite {
function Tests_Auth_OpenID_Nonce()
{
$this->addTestSuite('Tests_Auth_OpenID_NonceTests');
$this->makeSplitTests();
$this->makeCheckTimestampTests();
$this->setName('Tests_Auth_OpenID_Nonce');
}
function makeSplitTests()
{
$cases = array(
'',
'1970-01-01T00:00:00+1:00',
'1969-01-01T00:00:00Z',
'1970-00-01T00:00:00Z',
'1970.01-01T00:00:00Z',
'Thu Sep 7 13:29:31 PDT 2006',
'monkeys',
);
foreach ($cases as $nonce_str) {
$this->_mkSplitTest($nonce_str);
}
}
function _mkSplitTest($nonce_str)
{
$test = new Tests_Auth_OpenID_Nonce_BadSplitCase($nonce_str);
$test->setName('BadNonceSplit ' . var_export($nonce_str, true));
$this->addTest($test);
}
function makeCheckTimestampTests()
{
$cases = array(
// exact, no allowed skew
array('1970-01-01T00:00:00Z', 0, 0, true),
// exact, large skew
array('1970-01-01T00:00:00Z', 1000, 0, true),
// no allowed skew, one second old
array('1970-01-01T00:00:00Z', 0, 1, false),
// many seconds old, outside of skew
array('1970-01-01T00:00:00Z', 10, 50, false),
// one second old, one second skew allowed
array('1970-01-01T00:00:00Z', 1, 1, true),
// One second in the future, one second skew allowed
array('1970-01-01T00:00:02Z', 1, 1, true),
// two seconds in the future, one second skew allowed
array('1970-01-01T00:00:02Z', 1, 0, false),
// malformed nonce string
array('monkeys', 0, 0, false)
);
foreach ($cases as $case) {
$this->_mkCheckTest($case);
}
}
function _mkCheckTest($case)
{
list($nonce_str, $skew, $now, $expected) = $case;
$test = new Tests_Auth_OpenID_Nonce_TimestampCase(
$nonce_str, $skew, $now, $expected);
$test->setName('CheckTimestamp ' . var_export($nonce_str, true));
$this->addTest($test);
}
}
class Tests_Auth_OpenID_Nonce_TimestampCase extends PHPUnit_TestCase {
function Tests_Auth_OpenID_Nonce_TimestampCase(
$nonce_str, $skew, $now, $expected)
{
$this->nonce_string = $nonce_str;
$this->allowed_skew = $skew;
$this->now = $now;
$this->expected = $expected;
}
function runTest()
{
$actual = Auth_OpenID_checkTimestamp($this->nonce_string,
$this->allowed_skew,
$this->now);
$this->assertEquals($this->expected, $actual);
}
}
class Tests_Auth_OpenID_NonceTests extends PHPUnit_TestCase {
function test_mkNonce()
{
$nonce_str = Auth_OpenID_mkNonce();
$this->assertTrue(preg_match(Tests_Auth_OpenID_nonce_re, $nonce_str));
}
function test_mkNonce_when()
{
$nonce_str = Auth_OpenID_mkNonce(0);
$this->assertTrue(preg_match(Tests_Auth_OpenID_nonce_re, $nonce_str));
$tpart = substr($nonce_str, 0, 20);
$this->assertEquals('1970-01-01T00:00:00Z', $tpart);
}
function test_splitNonce()
{
$s = '1970-01-01T00:00:00Z';
$expected_t = 0;
$expected_salt = '';
list($actual_t, $actual_salt) = Auth_OpenID_splitNonce($s);
$this->assertEquals($expected_t, $actual_t);
$this->assertEquals($expected_salt, $actual_salt);
}
function test_mkSplit()
{
$t = 42;;
$nonce_str = Auth_OpenID_mkNonce($t);
$this->assertTrue(preg_match(Tests_Auth_OpenID_nonce_re, $nonce_str));
list($et, $salt) = Auth_OpenID_splitNonce($nonce_str);
$this->assertEquals(6, strlen($salt));
$this->assertEquals($et, $t);
}
}
class Tests_Auth_OpenID_Nonce_BadSplitCase extends PHPUnit_TestCase {
function Tests_Auth_OpenID_Nonce_BadSplitCase($nonce_str)
{
$this->nonce_str = $nonce_str;
}
function runTest()
{
$result = Auth_OpenID_splitNonce($this->nonce_str);
$this->assertNull($result);
}
}
|