CryptUtil.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Tests for the CryptUtil 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.php';
  15. require_once 'Auth/OpenID/CryptUtil.php';
  16. class Tests_Auth_OpenID_CryptUtil extends PHPUnit_Framework_TestCase {
  17. function test_length()
  18. {
  19. $cases = array(1, 10, 255);
  20. foreach ($cases as $length) {
  21. $data = Auth_OpenID_CryptUtil::getBytes($length);
  22. $this->assertEquals(Auth_OpenID::bytes($data), $length);
  23. }
  24. }
  25. function test_different()
  26. {
  27. $num_iterations = 100;
  28. $data_length = 20;
  29. $data = Auth_OpenID_CryptUtil::getBytes($num_iterations);
  30. for ($i = 0; $i < $num_iterations; $i++) {
  31. $last = $data;
  32. $data = Auth_OpenID_CryptUtil::getBytes($data_length);
  33. $this->assertFalse($data == $last);
  34. }
  35. }
  36. function test_cryptrand()
  37. {
  38. // It's possible, but HIGHLY unlikely that a correct
  39. // implementation will fail by returning the same number twice
  40. $s = Auth_OpenID_CryptUtil::getBytes(32);
  41. $t = Auth_OpenID_CryptUtil::getBytes(32);
  42. $this->assertEquals(Auth_OpenID::bytes($s), 32);
  43. $this->assertEquals(Auth_OpenID::bytes($t), 32);
  44. $this->assertFalse($s == $t);
  45. }
  46. }