summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Net/OpenID/CryptUtil.php64
-rw-r--r--Tests/Net/OpenID/CryptUtil.php189
2 files changed, 168 insertions, 85 deletions
diff --git a/Net/OpenID/CryptUtil.php b/Net/OpenID/CryptUtil.php
index d1514f9..66ebe88 100644
--- a/Net/OpenID/CryptUtil.php
+++ b/Net/OpenID/CryptUtil.php
@@ -617,6 +617,28 @@ class Net_OpenID_GmpMathWrapper extends Net_OpenID_MathWrapper {
$Net_OpenID___mathLibrary = null;
/**
+ * Define the supported extensions. An extension array has keys
+ * 'modules', 'extension', and 'class'. 'modules' is an array of PHP
+ * module names which the loading code will attempt to load. These
+ * values will be suffixed with a library file extension (e.g. ".so").
+ * 'extension' is the name of a PHP extension which will be tested
+ * before 'modules' are loaded. 'class' is the string name of a
+ * Net_OpenID_MathWrapper subclass which should be instantiated if a
+ * given extension is present.
+ *
+ * You can define new math library implementations and add them to
+ * this array.
+ */
+$_Net_OpenID_supported_extensions = array(
+ array('modules' => array('gmp', 'php_gmp'),
+ 'extension' => 'gmp',
+ 'class' => 'Net_OpenID_GmpMathWrapper'),
+ array('modules' => array('bcmath', 'php_bcmath'),
+ 'extension' => 'bcmath',
+ 'class' => 'Net_OpenID_BcMathWrapper')
+ );
+
+ /**
* Net_OpenID_MathLibrary checks for the presence of long number
* extension modules and returns an instance of Net_OpenID_MathWrapper
* which exposes the module's functionality.
@@ -645,38 +667,26 @@ class Net_OpenID_MathLibrary {
*/
function &getLibWrapper()
{
- // Define the supported extensions. An extension array has
- // keys 'modules', 'extension', and 'class'. 'modules' is an
- // array of PHP module names which the loading code will
- // attempt to load. These values will be suffixed with a
- // library file extension (e.g. ".so"). 'extension' is the
- // name of a PHP extension which will be tested before
- // 'modules' are loaded. 'class' is the string name of a
- // Net_OpenID_MathWrapper subclass which should be
- // instantiated if a given extension is present.
- $Net_OpenID_supported_extensions = array(
- array('modules' => array('gmp', 'php_gmp'),
- 'extension' => 'gmp',
- 'class' => 'Net_OpenID_GmpMathWrapper'),
- array('modules' => array('bcmath', 'php_bcmath'),
- 'extension' => 'bcmath',
- 'class' => 'Net_OpenID_BcMathWrapper')
- );
+ if (defined('Net_OpenID_NO_MATH_SUPPORT')) {
+ return null;
+ }
+
+ global $_Net_OpenID_supported_extensions;
// The instance of Net_OpenID_MathWrapper that we choose to
// supply will be stored here, so that subseqent calls to this
// method will return a reference to the same object.
- global $Net_OpenID___mathLibrary;
-
+ global $_Net_OpenID___mathLibrary;
+
// If this method has not been called before, look at
// $Net_OpenID_supported_extensions and try to find an
// extension that works.
- if (!$Net_OpenID___mathLibrary) {
+ if (!$_Net_OpenID___mathLibrary) {
$loaded = false;
$tried = array();
- foreach ($Net_OpenID_supported_extensions as $extension) {
- $tried[] = $extension;
+ foreach ($_Net_OpenID_supported_extensions as $extension) {
+ $tried[] = $extension['extension'];
// See if the extension specified is already loaded.
if ($extension['extension'] &&
@@ -699,7 +709,7 @@ class Net_OpenID_MathLibrary {
// module's functionality.
if ($loaded) {
$classname = $extension['class'];
- $Net_OpenID___mathLibrary = new $classname();
+ $_Net_OpenID___mathLibrary = new $classname();
break;
}
}
@@ -707,14 +717,16 @@ class Net_OpenID_MathLibrary {
// If no extensions were found, fall back to
// Net_OpenID_MathWrapper so at least some platform-size
// math can be performed.
- if (!$Net_OpenID___mathLibrary) {
+ if (!$_Net_OpenID___mathLibrary) {
$triedstr = implode(", ", $tried);
- $msg = 'This PHP has no math library. tried: ' . $triedstr;
+ $msg = 'This PHP installation has no big integer math ' .
+ 'library. Define Net_OpenID_NO_MATH_SUPPORT to use ' .
+ 'this library in dumb mode. Tried: ' . $triedstr;
trigger_error($msg, E_USER_ERROR);
}
}
- return $Net_OpenID___mathLibrary;
+ return $_Net_OpenID___mathLibrary;
}
}
diff --git a/Tests/Net/OpenID/CryptUtil.php b/Tests/Net/OpenID/CryptUtil.php
index d042c37..346ecd8 100644
--- a/Tests/Net/OpenID/CryptUtil.php
+++ b/Tests/Net/OpenID/CryptUtil.php
@@ -16,7 +16,7 @@
require_once('PHPUnit.php');
require_once('Net/OpenID/CryptUtil.php');
-class Tests_Net_OpenID_CryptUtil extends PHPUnit_TestCase {
+class Tests_Net_OpenID_ByteOps extends PHPUnit_TestCase {
function test_length()
{
$cases = array(1, 10, 255);
@@ -41,7 +41,6 @@ class Tests_Net_OpenID_CryptUtil extends PHPUnit_TestCase {
function test_cryptrand()
{
-
$lib =& Net_OpenID_MathLibrary::getLibWrapper();
// It's possible, but HIGHLY unlikely that a correct
@@ -136,54 +135,91 @@ class Tests_Net_OpenID_CryptUtil extends PHPUnit_TestCase {
$this->assertEquals($twice, $case);
}
}
+}
- function test_binaryLongConvert()
- {
-
- $lib =& Net_OpenID_MathLibrary::getLibWrapper();
+class Tests_Net_OpenID_BinLongConvertRnd extends PHPUnit_TestCase {
+ var $lib;
+ var $max;
- $MAX = Net_OpenID_CryptUtil::maxint();
-
- foreach (range(0, 499) as $iteration) {
- $n = $lib->init(0);
- foreach (range(0, 9) as $i) {
- $rnd = Net_OpenID_CryptUtil::randrange($MAX);
- $n = $lib->add($n, $rnd);
- }
+ function Tests_Net_OpenID_BinLongConvertRnd(&$lib, $max)
+ {
+ $this->lib =& $lib;
+ $this->max = $max;
+ }
- $s = Net_OpenID_CryptUtil::longToBinary($n);
- $this->assertTrue(is_string($s));
- $n_prime = Net_OpenID_CryptUtil::binaryToLong($s);
- $this->assertEquals($lib->cmp($n, $n_prime), 0);
+ function runTest()
+ {
+ $n = $this->lib->init(0);
+ foreach (range(0, 9) as $i) {
+ $rnd = Net_OpenID_CryptUtil::randrange($this->max);
+ $n = $this->lib->add($n, $rnd);
}
+ $s = Net_OpenID_CryptUtil::longToBinary($n);
+ $this->assertTrue(is_string($s));
+ $n_prime = Net_OpenID_CryptUtil::binaryToLong($s);
+ $this->assertEquals($this->lib->cmp($n, $n_prime), 0);
+ }
+}
- $cases = array(
- array("\x00", 0),
- array("\x01", 1),
- array("\x00\xFF", 255),
- array("\x00\x80", 128),
- array("\x00\x81", 129),
- array("\x00\x80\x00", 32768),
- array("OpenID is cool",
- "1611215304203901150134421257416556")
- );
+class Tests_Net_OpenID_BinLongConvert extends PHPUnit_TestCase {
+ var $lib;
+ var $bin;
+ var $lng;
- foreach ($cases as $case) {
- list($s, $n) = $case;
- $n_prime = Net_OpenID_CryptUtil::binaryToLong($s);
- $s_prime = Net_OpenID_CryptUtil::longToBinary($n);
- $this->assertEquals($lib->cmp($n, $n_prime), 0);
- $this->assertTrue($s == $s_prime);
- }
+ function Tests_Net_OpenID_BinLongConvert(&$lib, $bin, $lng)
+ {
+ $this->lib =& $lib;
+ $this->bin = $bin;
+ $this->lng = $lng;
}
- function _parseBase64Data()
+ function runTest()
{
- $lib =& Net_OpenID_MathLibrary::getLibWrapper();
+ $n_prime = Net_OpenID_CryptUtil::binaryToLong($this->bin);
+ $s_prime = Net_OpenID_CryptUtil::longToBinary($this->lng);
+ $this->assertEquals($this->lib->cmp($this->lng, $n_prime), 0);
+ $this->assertTrue($this->bin == $s_prime);
+ }
+}
- $lines = file_get_contents('Tests/n2b64', true);
- $this->assertTrue(is_string($lines));
+class Tests_Net_OpenID_Base64ToLong extends PHPUnit_TestCase {
+ var $num;
+ var $b64;
+ var $lib;
+
+ function Tests_Net_OpenID_Base64ToLong(&$lib, $b64, $num)
+ {
+ $this->lib = $lib;
+ $this->b64 = $b64;
+ $this->num = $num;
+ }
+ function runTest()
+ {
+ $actual = Net_OpenID_CryptUtil::base64ToLong($this->b64);
+ $this->assertTrue($this->lib->cmp($this->num, $actual) == 0);
+ }
+}
+
+class Tests_Net_OpenID_LongToBase64 extends Tests_Net_OpenID_Base64ToLong {
+ function Tests_Net_OpenID_LongToBase64(&$lib, $b64, $num)
+ {
+ $this->lib = $lib;
+ $this->b64 = $b64;
+ $this->num = $num;
+ }
+
+ function runTest()
+ {
+ $actual = Net_OpenID_CryptUtil::longToBase64($this->num);
+ $this->assertEquals($this->b64, $actual);
+ }
+}
+
+class Tests_Net_OpenID_CryptUtil extends PHPUnit_TestSuite {
+ function _parseBase64Data()
+ {
+ $lines = file_get_contents('Tests/n2b64', true);
$lines = explode("\n", $lines);
$data = array();
@@ -192,33 +228,68 @@ class Tests_Net_OpenID_CryptUtil extends PHPUnit_TestCase {
continue;
}
list($b64, $ascii) = explode(' ', $line);
- $num = @$lib->init($ascii);
- if ($num === false) {
- $this->assertEquals($lib->type, 'dumb');
- } else {
- $data[$b64] = $num;
- }
+ $data[$b64] = $ascii;
}
return $data;
}
- function test_longToBase64()
+ function Tests_Net_OpenID_CryptUtil($name)
{
- $data = $this->_parseBase64Data();
- foreach ($data as $expected => $num) {
- $actual = Net_OpenID_CryptUtil::longToBase64($num);
- $this->assertEquals($expected, $actual);
- }
- }
+ $this->setName($name);
+
+ if (!defined('Net_OpenID_NO_MATH_SUPPORT')) {
+ $this->addTestSuite('Tests_Net_OpenID_BigInt');
- function test_base64ToLong()
- {
- $lib =& Net_OpenID_MathLibrary::getLibWrapper();
- $data = $this->_parseBase64Data();
- foreach ($data as $b64 => $expected) {
- $actual = Net_OpenID_CryptUtil::base64ToLong($b64);
- $this->assertTrue($lib->cmp($expected, $actual) == 0);
+ $lib =& Net_OpenID_MathLibrary::getLibWrapper();
+ $max = Net_OpenID_CryptUtil::maxint();
+ $upper = defined('Tests_Net_OpenID_thorough') ? 499 : 3;
+
+ foreach (range(0, $upper) as $iteration) {
+ $test = new Tests_Net_OpenID_BinLongConvertRnd($lib, $max);
+ $test->setName("BinLongConvertRnd " . strval($iteration));
+ $this->addTest($test);
+ }
+
+ $cases = array(
+ "\x00" => 0,
+ "\x01" => 1,
+ "\x00\xFF" => 255,
+ "\x00\x80" => 128,
+ "\x00\x81" => 129,
+ "\x00\x80\x00" => 32768,
+ "OpenID is cool" => "1611215304203901150134421257416556"
+ );
+
+ foreach ($cases as $bin => $lng_m) {
+ $lng = $lib->init($lng_m);
+ $test = new Tests_Net_OpenID_BinLongConvert($lib, $bin, $lng);
+ $test->setName('BinLongConvert ' . bin2hex($bin));
+ $this->addTest($test);
+ }
+
+ $count = defined('Tests_Net_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_Net_OpenID_Base64ToLong($lib, $b64, $num);
+ $test->setName("B64->Long $num_s");
+ $this->addTest($test);
+
+ $test = new Tests_Net_OpenID_LongToBase64($lib, $b64, $num);
+ $test->setName("Long->B64 $num_s");
+ $this->addTest($test);
+ }
}
+
+ $this->addTestSuite('Tests_Net_OpenID_ByteOps');
}
}