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