DiffieHellman.php 4.8 KB

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