Nonce.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Tests for the Nonce implementation.
  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 2006 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. require_once 'PHPUnit.php';
  15. require_once 'Auth/OpenID/Nonce.php';
  16. define('Tests_Auth_OpenID_nonce_re',
  17. '/\A\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ/');
  18. class Tests_Auth_OpenID_Nonce extends PHPUnit_TestSuite {
  19. function Tests_Auth_OpenID_Nonce()
  20. {
  21. $this->addTestSuite('Tests_Auth_OpenID_NonceTests');
  22. $this->makeSplitTests();
  23. $this->makeCheckTimestampTests();
  24. $this->setName('Tests_Auth_OpenID_Nonce');
  25. }
  26. function makeSplitTests()
  27. {
  28. $cases = array(
  29. '',
  30. '1970-01-01T00:00:00+1:00',
  31. '1969-01-01T00:00:00Z',
  32. '1970-00-01T00:00:00Z',
  33. '1970.01-01T00:00:00Z',
  34. 'Thu Sep 7 13:29:31 PDT 2006',
  35. 'monkeys',
  36. );
  37. foreach ($cases as $nonce_str) {
  38. $this->_mkSplitTest($nonce_str);
  39. }
  40. }
  41. function _mkSplitTest($nonce_str)
  42. {
  43. $test = new Tests_Auth_OpenID_Nonce_BadSplitCase($nonce_str);
  44. $test->setName('BadNonceSplit ' . var_export($nonce_str, true));
  45. $this->addTest($test);
  46. }
  47. function makeCheckTimestampTests()
  48. {
  49. $cases = array(
  50. // exact, no allowed skew
  51. array('1970-01-01T00:00:00Z', 0, 0, true),
  52. // exact, large skew
  53. array('1970-01-01T00:00:00Z', 1000, 0, true),
  54. // no allowed skew, one second old
  55. array('1970-01-01T00:00:00Z', 0, 1, false),
  56. // many seconds old, outside of skew
  57. array('1970-01-01T00:00:00Z', 10, 50, false),
  58. // one second old, one second skew allowed
  59. array('1970-01-01T00:00:00Z', 1, 1, true),
  60. // One second in the future, one second skew allowed
  61. array('1970-01-01T00:00:02Z', 1, 1, true),
  62. // two seconds in the future, one second skew allowed
  63. array('1970-01-01T00:00:02Z', 1, 0, false),
  64. // malformed nonce string
  65. array('monkeys', 0, 0, false)
  66. );
  67. foreach ($cases as $case) {
  68. $this->_mkCheckTest($case);
  69. }
  70. }
  71. function _mkCheckTest($case)
  72. {
  73. list($nonce_str, $skew, $now, $expected) = $case;
  74. $test = new Tests_Auth_OpenID_Nonce_TimestampCase(
  75. $nonce_str, $skew, $now, $expected);
  76. $test->setName('CheckTimestamp ' . var_export($nonce_str, true));
  77. $this->addTest($test);
  78. }
  79. }
  80. class Tests_Auth_OpenID_Nonce_TimestampCase extends PHPUnit_TestCase {
  81. function Tests_Auth_OpenID_Nonce_TimestampCase(
  82. $nonce_str, $skew, $now, $expected)
  83. {
  84. $this->nonce_string = $nonce_str;
  85. $this->allowed_skew = $skew;
  86. $this->now = $now;
  87. $this->expected = $expected;
  88. }
  89. function runTest()
  90. {
  91. $actual = Auth_OpenID_checkTimestamp($this->nonce_string,
  92. $this->allowed_skew,
  93. $this->now);
  94. $this->assertEquals($this->expected, $actual);
  95. }
  96. }
  97. class Tests_Auth_OpenID_NonceTests extends PHPUnit_TestCase {
  98. function test_mkNonce()
  99. {
  100. $nonce_str = Auth_OpenID_mkNonce();
  101. $this->assertTrue(preg_match(Tests_Auth_OpenID_nonce_re, $nonce_str));
  102. }
  103. function test_mkNonce_when()
  104. {
  105. $nonce_str = Auth_OpenID_mkNonce(0);
  106. $this->assertTrue(preg_match(Tests_Auth_OpenID_nonce_re, $nonce_str));
  107. $tpart = substr($nonce_str, 0, 20);
  108. $this->assertEquals('1970-01-01T00:00:00Z', $tpart);
  109. }
  110. function test_splitNonce()
  111. {
  112. $s = '1970-01-01T00:00:00Z';
  113. $expected_t = 0;
  114. $expected_salt = '';
  115. list($actual_t, $actual_salt) = Auth_OpenID_splitNonce($s);
  116. $this->assertEquals($expected_t, $actual_t);
  117. $this->assertEquals($expected_salt, $actual_salt);
  118. }
  119. function test_mkSplit()
  120. {
  121. $t = 42;;
  122. $nonce_str = Auth_OpenID_mkNonce($t);
  123. $this->assertTrue(preg_match(Tests_Auth_OpenID_nonce_re, $nonce_str));
  124. list($et, $salt) = Auth_OpenID_splitNonce($nonce_str);
  125. $this->assertEquals(6, strlen($salt));
  126. $this->assertEquals($et, $t);
  127. }
  128. }
  129. class Tests_Auth_OpenID_Nonce_BadSplitCase extends PHPUnit_TestCase {
  130. function Tests_Auth_OpenID_Nonce_BadSplitCase($nonce_str)
  131. {
  132. $this->nonce_str = $nonce_str;
  133. }
  134. function runTest()
  135. {
  136. $result = Auth_OpenID_splitNonce($this->nonce_str);
  137. $this->assertNull($result);
  138. }
  139. }
  140. ?>