CryptUtil.php 1.5 KB

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