PageRenderTime 32ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Auth/OpenID/HMAC.php

https://github.com/duaiwe/php-openid
PHP | 117 lines | 80 code | 18 blank | 19 comment | 12 complexity | 4aeedaa12bdb8e422171f8ce6e4d6578 MD5 | raw file
Possible License(s): Apache-2.0
  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/TestSuite.php';
  17. require_once 'Tests/Auth/OpenID/TestUtil.php';
  18. class Auth_OpenID_HMACSuite extends Auth_OpenID_TestSuite {
  19. public static function suite() {
  20. $suite = new Auth_OpenID_NonceSuite();
  21. $suite->addTestSuite('Auth_OpenID_HMAC_SHA1Test');
  22. $suite->addTestSuite('Auth_OpenID_HMAC_SHA256Test');
  23. return $suite;
  24. }
  25. }
  26. abstract class Auth_OpenID_HMAC_TestCase extends PHPUnit_Framework_TestCase {
  27. function _strConvert($s)
  28. {
  29. $repeat_pat = '/^0x([a-f0-9]{2}) repeated (\d+) times$/';
  30. if (preg_match($repeat_pat, $s, $match)) {
  31. $c = chr(hexdec($match[1]));
  32. $n = $match[2];
  33. $data = '';
  34. for ($i = 0; $i < $n; $i++) {
  35. $data .= $c;
  36. }
  37. } elseif (substr($s, 0, 2) == "0x") {
  38. $data = pack('H*', substr($s, 2, strlen($s) - 1));
  39. } elseif (preg_match('/^"(.*)"$/', $s, $match)) {
  40. $data = $match[1];
  41. } else {
  42. $data = $s;
  43. }
  44. return $data;
  45. }
  46. function clean_config($raw, $digest_len) {
  47. $data = array();
  48. foreach ($raw as $k => $v) {
  49. $clean = array();
  50. $clean['key'] = $this->_strConvert($v['key']);
  51. if (array_key_exists('key_len', $v)) {
  52. if (Auth_OpenID::bytes($clean['key']) != $v['key_len']) {
  53. trigger_error('Bad key length', E_USER_ERROR);
  54. }
  55. }
  56. $clean['data'] = $this->_strConvert($v['data']);
  57. if (array_key_exists('data_len', $v)) {
  58. if (Auth_OpenID::bytes($clean['data']) != $v['data_len']) {
  59. trigger_error('Bad data length', E_USER_ERROR);
  60. }
  61. }
  62. $clean['digest'] = $this->_strConvert($v['digest']);
  63. if (Auth_OpenID::bytes($clean['digest']) != $digest_len) {
  64. $l = Auth_OpenID::bytes($clean['digest']);
  65. trigger_error("Bad digest length: $l", E_USER_ERROR);
  66. }
  67. $data[] = $clean;
  68. }
  69. return $data;
  70. }
  71. }
  72. class Auth_OpenID_HMAC_SHA1Test extends Auth_OpenID_HMAC_TestCase {
  73. function hmac_data() {
  74. $config = parse_ini_file('Tests/Auth/OpenID/data/hmac-sha1.ini', true);
  75. $config = $this->clean_config($config, 20);
  76. return $config;
  77. }
  78. /**
  79. * @dataProvider hmac_data
  80. */
  81. function test_hmac($key, $data, $expected) {
  82. $actual = Auth_OpenID_HMACSHA1($key, $data);
  83. $this->assertEquals(bin2hex($expected), bin2hex($actual));
  84. }
  85. }
  86. class Auth_OpenID_HMAC_SHA256Test extends Auth_OpenID_HMAC_TestCase {
  87. function hmac_data() {
  88. $config = parse_ini_file('Tests/Auth/OpenID/data/hmac-sha256.ini', true);
  89. $config = $this->clean_config($config, 32);
  90. return $config;
  91. }
  92. /**
  93. * @dataProvider hmac_data
  94. */
  95. function test_hmac($key, $data, $expected) {
  96. $actual = Auth_OpenID_HMACSHA256($key, $data);
  97. $this->assertEquals(bin2hex($expected), bin2hex($actual));
  98. }
  99. }