HMAC.php 5.0 KB

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