Nonce.php 5.0 KB

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