HMAC.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Tests for the HMAC-SHA1 utility functions used by the OpenID
  4. * library.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: See the COPYING file included in this distribution.
  9. *
  10. * @package OpenID
  11. * @author JanRain, Inc. <openid@janrain.com>
  12. * @copyright 2005-2008 Janrain, Inc.
  13. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  14. */
  15. require_once 'PHPUnit.php';
  16. require_once 'Auth/OpenID/HMAC.php';
  17. require_once 'Tests/Auth/OpenID/TestUtil.php';
  18. class Tests_Auth_OpenID_HMAC_TestCase extends PHPUnit_TestCase {
  19. function Tests_Auth_OpenID_HMAC_TestCase(
  20. $name, $key, $data, $expected, $hmac_func)
  21. {
  22. $this->setName($name);
  23. $this->key = $key;
  24. $this->data = $data;
  25. $this->expected = $expected;
  26. $this->hmac_func = $hmac_func;
  27. }
  28. function runTest()
  29. {
  30. $actual = call_user_func($this->hmac_func, $this->key, $this->data);
  31. $this->assertEquals(bin2hex($this->expected), bin2hex($actual));
  32. }
  33. }
  34. class Tests_Auth_OpenID_HMAC extends PHPUnit_TestSuite {
  35. function _strConvert($s)
  36. {
  37. $repeat_pat = '/^0x([a-f0-9]{2}) repeated (\d+) times$/';
  38. if (preg_match($repeat_pat, $s, $match)) {
  39. $c = chr(hexdec($match[1]));
  40. $n = $match[2];
  41. $data = '';
  42. for ($i = 0; $i < $n; $i++) {
  43. $data .= $c;
  44. }
  45. } elseif (substr($s, 0, 2) == "0x") {
  46. $data = pack('H*', substr($s, 2, strlen($s) - 1));
  47. } elseif (preg_match('/^"(.*)"$/', $s, $match)) {
  48. $data = $match[1];
  49. } else {
  50. trigger_error("Bad data format: $s", E_USER_ERROR);
  51. }
  52. return $data;
  53. }
  54. function _readTestCases($test_file_name, $digest_len)
  55. {
  56. $lines = Tests_Auth_OpenID_readlines($test_file_name);
  57. $cases = array();
  58. $case = array();
  59. foreach ($lines as $line) {
  60. if ($line{0} == "#") {
  61. continue;
  62. }
  63. // Blank line separates test cases
  64. if ($line == "\n") {
  65. $cases[] = $case;
  66. $case = array();
  67. } else {
  68. $match = array();
  69. $pat = '/^([a-z0-9_-]+) =\s+(.*?)\n$/';
  70. if (!preg_match($pat, $line, $match)) {
  71. trigger_error("Bad test input: $line", E_USER_ERROR);
  72. }
  73. $c = count($match);
  74. if ($c != 3) {
  75. trigger_error(
  76. "Wrong number of elements in parsed case: $c",
  77. E_USER_ERROR);
  78. return false;
  79. }
  80. $key = $match[1];
  81. $value = $match[2];
  82. $case[$key] = $value;
  83. }
  84. }
  85. if (count($case)) {
  86. $cases[] = $case;
  87. }
  88. $final = array();
  89. // Normalize strings and check data integrity
  90. foreach ($cases as $case) {
  91. $clean = array();
  92. $clean["key"] =
  93. Tests_Auth_OpenID_HMAC::_strConvert($case["key"]);
  94. if (defined(@$case["key_len"])) {
  95. if (Auth_OpenID::bytes($clean["key"]) != $case["key_len"]) {
  96. trigger_error("Bad key length", E_USER_ERROR);
  97. }
  98. }
  99. $clean["data"] =
  100. Tests_Auth_OpenID_HMAC::_strConvert($case["data"]);
  101. if (defined(@$case["data_len"])) {
  102. if (Auth_OpenID::bytes($clean["data"]) != $case["data_len"]) {
  103. trigger_error("Bad data length", E_USER_ERROR);
  104. }
  105. }
  106. $clean["digest"] =
  107. Tests_Auth_OpenID_HMAC::_strConvert($case["digest"]);
  108. if (Auth_OpenID::bytes($clean["digest"]) != $digest_len) {
  109. $l = Auth_OpenID::bytes($clean["digest"]);
  110. trigger_error("Bad digest length: $l", E_USER_ERROR);
  111. }
  112. $clean['test_case'] = $case['test_case'];
  113. $final[] = $clean;
  114. }
  115. return $final;
  116. }
  117. function Tests_Auth_OpenID_HMAC($name)
  118. {
  119. $this->setName($name);
  120. $hash_test_defs = array(array(
  121. 'Auth_OpenID_HMACSHA1', 'hmac-sha1.txt', 20));
  122. if (Auth_OpenID_HMACSHA256_SUPPORTED) {
  123. $hash_test_defs[] =
  124. array('Auth_OpenID_HMACSHA256', 'hmac-sha256.txt', 32);
  125. }
  126. foreach ($hash_test_defs as $params) {
  127. list($hash_func, $filename, $hash_len) = $params;
  128. $cases = $this->_readTestCases($filename, $hash_len);
  129. foreach ($cases as $case) {
  130. $test = new Tests_Auth_OpenID_HMAC_TestCase(
  131. $case['test_case'],
  132. $case['key'],
  133. $case['data'],
  134. $case['digest'],
  135. $hash_func);
  136. $digest = $case['digest'];
  137. $this->_addTestByValue($test);
  138. }
  139. }
  140. }
  141. function _addTestByValue($test) {
  142. $this->addTest($test);
  143. }
  144. }
  145. ?>