DiffieHellman.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Tests for the Diffie-Hellman key exchange implementation in the
  4. * OpenID 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/DiffieHellman.php';
  16. require_once 'Tests/Auth/OpenID/TestUtil.php';
  17. class Tests_Auth_OpenID_DiffieHellman_CheckCases extends PHPUnit_Framework_TestCase {
  18. function Tests_Auth_OpenID_DiffieHellman_CheckCases($cases, $n)
  19. {
  20. $this->cases = $cases;
  21. $this->n = $n;
  22. }
  23. function runTest()
  24. {
  25. $this->assertEquals($this->n, count($this->cases));
  26. }
  27. }
  28. class Tests_Auth_OpenID_DiffieHellman_Private extends PHPUnit_Framework_TestCase {
  29. function Tests_Auth_OpenID_DiffieHellman_Private($name, $input, $expected)
  30. {
  31. $this->setName("$name");
  32. $this->input = $input;
  33. $this->expected = $expected;
  34. }
  35. function runTest()
  36. {
  37. $lib =& Auth_OpenID_getMathLib();
  38. $dh = new Auth_OpenID_DiffieHellman(null, null, $this->input);
  39. $this->assertEquals($lib->cmp($this->expected, $dh->getPublicKey()), 0);
  40. }
  41. }
  42. class Tests_Auth_OpenID_DiffieHellman_Exch extends PHPUnit_Framework_TestCase {
  43. function Tests_Auth_OpenID_DiffieHellman_Exch($name, $p1, $p2, $shared)
  44. {
  45. $this->setName("$name");
  46. $this->p1 = $p1;
  47. $this->p2 = $p2;
  48. $this->shared = $shared;
  49. }
  50. function runTest()
  51. {
  52. $lib = Auth_OpenID_getMathLib();
  53. $shared = $lib->init($this->shared);
  54. $dh1 = new Auth_OpenID_DiffieHellman(null, null, $this->p1);
  55. $dh2 = new Auth_OpenID_DiffieHellman(null, null, $this->p2);
  56. $sh1 = $dh1->getSharedSecret($dh2->getPublicKey());
  57. $sh2 = $dh2->getSharedSecret($dh1->getPublicKey());
  58. $this->assertEquals($lib->cmp($shared, $sh1), 0);
  59. $this->assertEquals($lib->cmp($shared, $sh2), 0);
  60. }
  61. }
  62. class Tests_Auth_OpenID_DiffieHellman extends PHPUnit_Framework_TestSuite {
  63. function _readPrivateTestCases()
  64. {
  65. $lines = Tests_Auth_OpenID_readlines('dhpriv');
  66. $cases = array();
  67. foreach ($lines as $line) {
  68. $case = array();
  69. if (!preg_match('/^(\d+) (\d+)\n$/', $line, $case)) {
  70. trigger_error("Bad test input: $line", E_USER_ERROR);
  71. }
  72. $c = count($case);
  73. if ($c != 3) {
  74. trigger_error("Wrong number of elements in parsed case: $c",
  75. E_USER_ERROR);
  76. }
  77. array_shift($case);
  78. $cases[] = $case;
  79. }
  80. return $cases;
  81. }
  82. function _readExchTestCases()
  83. {
  84. $lines = Tests_Auth_OpenID_readlines('dhexch');
  85. $cases = array();
  86. foreach ($lines as $line) {
  87. $case = array();
  88. if (!preg_match('/^(\d+) (\d+) (\d+)\n$/', $line, $case)) {
  89. trigger_error("Bad test input: $line", E_USER_ERROR);
  90. }
  91. $c = count($case);
  92. if ($c != 4) {
  93. trigger_error("Wrong number of elements in parsed case: $c",
  94. E_USER_ERROR);
  95. }
  96. array_shift($case);
  97. $cases[] = $case;
  98. }
  99. return $cases;
  100. }
  101. function Tests_Auth_OpenID_DiffieHellman($name)
  102. {
  103. $this->setName($name);
  104. $priv_cases = Tests_Auth_OpenID_DiffieHellman::_readPrivateTestCases();
  105. $sanity = new Tests_Auth_OpenID_DiffieHellman_CheckCases(
  106. $priv_cases, 29);
  107. $sanity->setName('Check parsing of priv test data');
  108. $this->addTest($sanity);
  109. $exch_cases = Tests_Auth_OpenID_DiffieHellman::_readExchTestCases();
  110. $sanity = new Tests_Auth_OpenID_DiffieHellman_CheckCases(
  111. $exch_cases, 25);
  112. $sanity->setName('Check parsing of exch test data');
  113. $this->addTest($sanity);
  114. if (!defined('Auth_OpenID_NO_MATH_SUPPORT')) {
  115. if (defined('Tests_Auth_OpenID_thorough')) {
  116. $npriv = count($priv_cases);
  117. $nexch = count($exch_cases);
  118. } else {
  119. $npriv = 1;
  120. $nexch = 3;
  121. }
  122. for ($i = 0; $i < $npriv; $i++) {
  123. list($input, $expected) = $priv_cases[$i];
  124. $one = new Tests_Auth_OpenID_DiffieHellman_Private(
  125. "DHPriv $i", $input, $expected);
  126. $this->addTest($one);
  127. }
  128. for ($i = 0; $i < $nexch; $i++) {
  129. $case = $exch_cases[$i];
  130. $one = new Tests_Auth_OpenID_DiffieHellman_Exch(
  131. $i, $case[0], $case[1], $case[2]);
  132. $this->addTest($one);
  133. }
  134. }
  135. }
  136. }