BigMath.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Tests for the BigMath functions.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005-2008 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. require_once 'PHPUnit.php';
  15. require_once 'Auth/OpenID/BigMath.php';
  16. require_once 'Tests/Auth/OpenID/TestUtil.php';
  17. class Tests_Auth_OpenID_BinLongConvertRnd extends PHPUnit_TestCase {
  18. var $lib;
  19. var $max;
  20. function Tests_Auth_OpenID_BinLongConvertRnd(&$lib, $max)
  21. {
  22. $this->lib =& $lib;
  23. $this->max = $max;
  24. }
  25. function runTest()
  26. {
  27. $n = $this->lib->init(0);
  28. foreach (range(0, 9) as $i) {
  29. $rnd = $this->lib->rand($this->max);
  30. $n = $this->lib->add($n, $rnd);
  31. }
  32. $s = $this->lib->longToBinary($n);
  33. $this->assertTrue(is_string($s));
  34. $n_prime = $this->lib->binaryToLong($s);
  35. $this->assertEquals($this->lib->cmp($n, $n_prime), 0);
  36. }
  37. }
  38. class Tests_Auth_OpenID_BinLongConvert extends PHPUnit_TestCase {
  39. var $lib;
  40. var $bin;
  41. var $lng;
  42. function Tests_Auth_OpenID_BinLongConvert(&$lib, $bin, $lng)
  43. {
  44. $this->lib =& $lib;
  45. $this->bin = $bin;
  46. $this->lng = $lng;
  47. }
  48. function runTest()
  49. {
  50. $n_prime = $this->lib->binaryToLong($this->bin);
  51. $s_prime = $this->lib->longToBinary($this->lng);
  52. $this->assertEquals($this->lib->cmp($this->lng, $n_prime), 0);
  53. $this->assertTrue($this->bin == $s_prime);
  54. }
  55. }
  56. class Tests_Auth_OpenID_Base64ToLong extends PHPUnit_TestCase {
  57. var $num;
  58. var $b64;
  59. var $lib;
  60. function Tests_Auth_OpenID_Base64ToLong(&$lib, $b64, $num)
  61. {
  62. $this->lib = $lib;
  63. $this->b64 = $b64;
  64. $this->num = $num;
  65. }
  66. function runTest()
  67. {
  68. $actual = $this->lib->base64ToLong($this->b64);
  69. $this->assertTrue($this->lib->cmp($this->num, $actual) == 0);
  70. }
  71. }
  72. class Tests_Auth_OpenID_LongToBase64 extends Tests_Auth_OpenID_Base64ToLong {
  73. function Tests_Auth_OpenID_LongToBase64(&$lib, $b64, $num)
  74. {
  75. $this->lib = $lib;
  76. $this->b64 = $b64;
  77. $this->num = $num;
  78. }
  79. function runTest()
  80. {
  81. $actual = $this->lib->longToBase64($this->num);
  82. $this->assertEquals($this->b64, $actual);
  83. }
  84. }
  85. class Tests_Auth_OpenID_Rand extends PHPUnit_TestCase {
  86. function Tests_Auth_OpenID_Rand(&$lib)
  87. {
  88. $this->lib =& $lib;
  89. }
  90. function runTest()
  91. {
  92. $stop = $this->lib->pow(2, 128);
  93. $a = $this->lib->rand($stop);
  94. $b = $this->lib->rand($stop);
  95. $this->assertFalse($this->lib->cmp($b, $a) == 0, "Same: $a $b");
  96. $n = $this->lib->init(Tests_Auth_OpenID_maxint());
  97. $n = $this->lib->add($n, 1);
  98. // Make sure that we can generate random numbers that are
  99. // larger than platform int size
  100. $result = $this->lib->rand($n);
  101. // What can we say about the result?
  102. }
  103. }
  104. /**
  105. * Computes the maximum integer value for this PHP installation.
  106. *
  107. * @return int $max_int_value The maximum integer value for this
  108. * PHP installation
  109. */
  110. function Tests_Auth_OpenID_maxint()
  111. {
  112. /* assumes largest integer is of form 2^n - 1 */
  113. $to_test = pow(2, 16);
  114. while (1) {
  115. $last = $to_test;
  116. $to_test = 2 * $to_test;
  117. if (($to_test < $last) || (!is_int($to_test))) {
  118. return($last + ($last - 1));
  119. }
  120. }
  121. }
  122. class Tests_Auth_OpenID_BigMath extends PHPUnit_TestSuite {
  123. function _parseBase64Data()
  124. {
  125. $lines = Tests_Auth_OpenID_readlines('n2b64');
  126. $data = array();
  127. foreach ($lines as $line) {
  128. $line = trim($line);
  129. if (!$line) {
  130. continue;
  131. }
  132. list($b64, $ascii) = explode(' ', $line);
  133. $data[$b64] = $ascii;
  134. }
  135. return $data;
  136. }
  137. function _addB64Tests()
  138. {
  139. $lib =& Auth_OpenID_getMathLib();
  140. $count = defined('Tests_Auth_OpenID_thorough') ? -1 : 2;
  141. $data = $this->_parseBase64Data();
  142. foreach ($data as $b64 => $num_s) {
  143. // Only test the first few unless thorough is defined
  144. if (strlen($num_s) > 5) {
  145. if ($count == 0) {
  146. break;
  147. } else {
  148. $count -= 1;
  149. }
  150. }
  151. $num = $lib->init($num_s);
  152. $test = new Tests_Auth_OpenID_Base64ToLong($lib, $b64, $num);
  153. $test->setName("B64->Long $num_s");
  154. $this->addTest($test);
  155. $test = new Tests_Auth_OpenID_LongToBase64($lib, $b64, $num);
  156. $test->setName("Long->B64 $num_s");
  157. $this->addTest($test);
  158. }
  159. }
  160. function _addBinLongTests()
  161. {
  162. $lib =& Auth_OpenID_getMathLib();
  163. $max = Tests_Auth_OpenID_maxint();
  164. $upper = defined('Tests_Auth_OpenID_thorough') ? 499 : 3;
  165. foreach (range(0, $upper) as $iteration) {
  166. $test = new Tests_Auth_OpenID_BinLongConvertRnd($lib, $max);
  167. $test->setName("BinLongConvertRnd " . strval($iteration));
  168. $this->addTest($test);
  169. }
  170. $cases = array(
  171. array("\x00", 0),
  172. array("\x01", 1),
  173. array("\x7F", 127),
  174. array("\x00\x80", 128),
  175. array("\x00\x81", 129),
  176. array("\x00\xFF", 255),
  177. array("\x00\x80\x00", 32768),
  178. array("OpenID is cool",
  179. "1611215304203901150134421257416556")
  180. );
  181. foreach ($cases as $case) {
  182. list($bin, $lng_m) = $case;
  183. $lng = $lib->init($lng_m);
  184. $test = new Tests_Auth_OpenID_BinLongConvert($lib, $bin, $lng);
  185. $test->setName('BinLongConvert ' . bin2hex($bin));
  186. $this->addTest($test);
  187. }
  188. }
  189. function Tests_Auth_OpenID_BigMath($name)
  190. {
  191. $this->setName($name);
  192. if (!defined('Auth_OpenID_NO_MATH_SUPPORT')) {
  193. $this->addTestSuite('Tests_Auth_OpenID_BigInt');
  194. $this->_addB64Tests();
  195. $this->_addBinLongTests();
  196. $test = new Tests_Auth_OpenID_Rand(Auth_OpenID_getMathLib());
  197. $test->setName('Big number rand');
  198. $this->addTest($test);
  199. }
  200. }
  201. }
  202. ?>