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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
<?php
/**
* Tests for the BigMath functions.
*
* PHP versions 4 and 5
*
* LICENSE: See the COPYING file included in this distribution.
*
* @package OpenID
* @author JanRain, Inc. <openid@janrain.com>
* @copyright 2005 Janrain, Inc.
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*/
require_once 'PHPUnit.php';
require_once 'Auth/OpenID/BigMath.php';
require_once 'Tests/Auth/OpenID/TestUtil.php';
class Tests_Auth_OpenID_BinLongConvertRnd extends PHPUnit_TestCase {
var $lib;
var $max;
function Tests_Auth_OpenID_BinLongConvertRnd(&$lib, $max)
{
$this->lib =& $lib;
$this->max = $max;
}
function runTest()
{
$n = $this->lib->init(0);
foreach (range(0, 9) as $i) {
$rnd = $this->lib->rand($this->max);
$n = $this->lib->add($n, $rnd);
}
$s = $this->lib->longToBinary($n);
$this->assertTrue(is_string($s));
$n_prime = $this->lib->binaryToLong($s);
$this->assertEquals($this->lib->cmp($n, $n_prime), 0);
}
}
class Tests_Auth_OpenID_BinLongConvert extends PHPUnit_TestCase {
var $lib;
var $bin;
var $lng;
function Tests_Auth_OpenID_BinLongConvert(&$lib, $bin, $lng)
{
$this->lib =& $lib;
$this->bin = $bin;
$this->lng = $lng;
}
function runTest()
{
$n_prime = $this->lib->binaryToLong($this->bin);
$s_prime = $this->lib->longToBinary($this->lng);
$this->assertEquals($this->lib->cmp($this->lng, $n_prime), 0);
$this->assertTrue($this->bin == $s_prime);
}
}
class Tests_Auth_OpenID_Base64ToLong extends PHPUnit_TestCase {
var $num;
var $b64;
var $lib;
function Tests_Auth_OpenID_Base64ToLong(&$lib, $b64, $num)
{
$this->lib = $lib;
$this->b64 = $b64;
$this->num = $num;
}
function runTest()
{
$actual = $this->lib->base64ToLong($this->b64);
$this->assertTrue($this->lib->cmp($this->num, $actual) == 0);
}
}
class Tests_Auth_OpenID_LongToBase64 extends Tests_Auth_OpenID_Base64ToLong {
function Tests_Auth_OpenID_LongToBase64(&$lib, $b64, $num)
{
$this->lib = $lib;
$this->b64 = $b64;
$this->num = $num;
}
function runTest()
{
$actual = $this->lib->longToBase64($this->num);
$this->assertEquals($this->b64, $actual);
}
}
class Tests_Auth_OpenID_Rand extends PHPUnit_TestCase {
function Tests_Auth_OpenID_Rand(&$lib)
{
$this->lib =& $lib;
}
function runTest()
{
$stop = $this->lib->pow(2, 128);
$a = $this->lib->rand($stop);
$b = $this->lib->rand($stop);
$this->assertFalse($this->lib->cmp($b, $a) == 0, "Same: $a $b");
$n = $this->lib->init(Tests_Auth_OpenID_maxint());
$n = $this->lib->add($n, 1);
// Make sure that we can generate random numbers that are
// larger than platform int size
$result = $this->lib->rand($n);
// What can we say about the result?
}
}
/**
* Computes the maximum integer value for this PHP installation.
*
* @return int $max_int_value The maximum integer value for this
* PHP installation
*/
function Tests_Auth_OpenID_maxint()
{
/* assumes largest integer is of form 2^n - 1 */
$to_test = pow(2, 16);
while (1) {
$last = $to_test;
$to_test = 2 * $to_test;
if (($to_test < $last) || (!is_int($to_test))) {
return($last + ($last - 1));
}
}
}
class Tests_Auth_OpenID_BigMath extends PHPUnit_TestSuite {
function _parseBase64Data()
{
$lines = Tests_Auth_OpenID_readlines('n2b64');
$data = array();
foreach ($lines as $line) {
$line = trim($line);
if (!$line) {
continue;
}
list($b64, $ascii) = explode(' ', $line);
$data[$b64] = $ascii;
}
return $data;
}
function _addB64Tests()
{
$lib =& Auth_OpenID_getMathLib();
$count = defined('Tests_Auth_OpenID_thorough') ? -1 : 2;
$data = $this->_parseBase64Data();
foreach ($data as $b64 => $num_s) {
// Only test the first few unless thorough is defined
if (strlen($num_s) > 5) {
if ($count == 0) {
break;
} else {
$count -= 1;
}
}
$num = $lib->init($num_s);
$test = new Tests_Auth_OpenID_Base64ToLong($lib, $b64, $num);
$test->setName("B64->Long $num_s");
$this->addTest($test);
$test = new Tests_Auth_OpenID_LongToBase64($lib, $b64, $num);
$test->setName("Long->B64 $num_s");
$this->addTest($test);
}
}
function _addBinLongTests()
{
$lib =& Auth_OpenID_getMathLib();
$max = Tests_Auth_OpenID_maxint();
$upper = defined('Tests_Auth_OpenID_thorough') ? 499 : 3;
foreach (range(0, $upper) as $iteration) {
$test = new Tests_Auth_OpenID_BinLongConvertRnd($lib, $max);
$test->setName("BinLongConvertRnd " . strval($iteration));
$this->addTest($test);
}
$cases = array(
array("\x00", 0),
array("\x01", 1),
array("\x7F", 127),
array("\x00\x80", 128),
array("\x00\x81", 129),
array("\x00\xFF", 255),
array("\x00\x80\x00", 32768),
array("OpenID is cool",
"1611215304203901150134421257416556")
);
foreach ($cases as $case) {
list($bin, $lng_m) = $case;
$lng = $lib->init($lng_m);
$test = new Tests_Auth_OpenID_BinLongConvert($lib, $bin, $lng);
$test->setName('BinLongConvert ' . bin2hex($bin));
$this->addTest($test);
}
}
function Tests_Auth_OpenID_BigMath($name)
{
$this->setName($name);
if (!defined('Auth_OpenID_NO_MATH_SUPPORT')) {
$this->addTestSuite('Tests_Auth_OpenID_BigInt');
$this->_addB64Tests();
$this->_addBinLongTests();
$test = new Tests_Auth_OpenID_Rand(Auth_OpenID_getMathLib());
$test->setName('Big number rand');
$this->addTest($test);
}
}
}
?>
|