TrustRoot.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Tests for the TrustRoot module
  4. */
  5. require_once "Auth/OpenID/TrustRoot.php";
  6. require_once "Tests/Auth/OpenID/TestUtil.php";
  7. require_once "PHPUnit.php";
  8. class Tests_Auth_OpenID_TRParseCase extends PHPUnit_TestCase {
  9. function Tests_Auth_OpenID_TRParseCase($desc, $case, $expected)
  10. {
  11. $this->setName($desc);
  12. $this->case = $case;
  13. $this->expected = $expected;
  14. }
  15. function runTest()
  16. {
  17. $is_sane = Auth_OpenID_TrustRoot::isSane($this->case);
  18. $parsed = (bool)Auth_OpenID_TrustRoot::_parse($this->case);
  19. switch ($this->expected) {
  20. case 'sane':
  21. $this->assertTrue($parsed, "Did not parse");
  22. $this->assertTrue($is_sane, "Is not sane");
  23. break;
  24. case 'insane':
  25. $this->assertTrue($parsed, "Did not parse");
  26. $this->assertFalse($is_sane, "Is sane");
  27. break;
  28. default:
  29. $this->assertFalse($parsed, "Did parse");
  30. $this->assertFalse($is_sane, "Is sane");
  31. }
  32. }
  33. }
  34. class Tests_Auth_OpenID_TRMatchCase extends PHPUnit_TestCase {
  35. function Tests_Auth_OpenID_TRMatchCase($desc, $tr, $rt, $matches)
  36. {
  37. $this->setName($desc);
  38. $this->tr = $tr;
  39. $this->rt = $rt;
  40. $this->matches = $matches;
  41. }
  42. function runTest()
  43. {
  44. $matches = Auth_OpenID_TrustRoot::match($this->tr, $this->rt);
  45. $this->assertEquals((bool)$this->matches, (bool)$matches);
  46. }
  47. }
  48. function Tests_Auth_OpenID_parseHeadings($data, $c)
  49. {
  50. $heading_pat = '/(^|\n)' . $c . '{40}\n([^\n]+)\n' . $c . '{40}\n()/';
  51. $offset = 0;
  52. $headings = array();
  53. while (true) {
  54. preg_match($heading_pat, substr($data, $offset), $matches,
  55. PREG_OFFSET_CAPTURE);
  56. if (!$matches) {
  57. break;
  58. }
  59. $start = $matches[0][1];
  60. $heading = $matches[2][0];
  61. $end = $matches[3][1];
  62. $headings[] = array('heading' => $heading,
  63. 'start' => $offset + $start,
  64. 'end' => $offset + $end,
  65. );
  66. $offset += $end;
  67. }
  68. return $headings;
  69. }
  70. function Tests_Auth_OpenID_getSections($data)
  71. {
  72. $headings = Tests_Auth_OpenID_parseHeadings($data, '-');
  73. $sections = array();
  74. $n = count($headings);
  75. for ($i = 0; $i < $n; ) {
  76. $secdata = $headings[$i];
  77. list($numtests, $desc) = explode(': ', $secdata['heading']);
  78. $start = $secdata['end'];
  79. $i += 1;
  80. if ($i < $n) {
  81. $blob = substr($data, $start, $headings[$i]['start'] - $start);
  82. } else {
  83. $blob = substr($data, $start);
  84. }
  85. $lines = explode("\n", trim($blob));
  86. if (count($lines) != $numtests) {
  87. trigger_error('Parse failure: ' . var_export($secdata, true),
  88. E_USER_ERROR);
  89. }
  90. $sections[] = array('desc' => $desc, 'lines' => $lines,);
  91. }
  92. return $sections;
  93. }
  94. function Tests_Auth_OpenID_trParseTests($head, $tests)
  95. {
  96. $tests = array('fail' => $tests[0],
  97. 'insane' => $tests[1],
  98. 'sane' => $tests[2]);
  99. $testobjs = array();
  100. foreach ($tests as $expected => $testdata) {
  101. $lines = $testdata['lines'];
  102. foreach ($lines as $line) {
  103. $desc = sprintf("%s - %s: %s", $head,
  104. $testdata['desc'], var_export($line, true));
  105. $testobjs[] = new Tests_Auth_OpenID_TRParseCase(
  106. $desc, $line, $expected);
  107. }
  108. }
  109. return $testobjs;
  110. }
  111. function Tests_Auth_OpenID_trMatchTests($head, $tests)
  112. {
  113. $tests = array(true => $tests[0], false => $tests[1]);
  114. $testobjs = array();
  115. foreach ($tests as $expected => $testdata) {
  116. $lines = $testdata['lines'];
  117. foreach ($lines as $line) {
  118. $pat = '/^([^ ]+) +([^ ]+)$/';
  119. preg_match($pat, $line, $matches);
  120. list($_, $tr, $rt) = $matches;
  121. $desc = sprintf("%s - %s: %s %s", $head, $testdata['desc'],
  122. var_export($tr, true), var_export($rt, true));
  123. $testobjs[] = new Tests_Auth_OpenID_TRMatchCase(
  124. $desc, $tr, $rt, $expected);
  125. }
  126. }
  127. return $testobjs;
  128. }
  129. function Tests_Auth_OpenID_trustRootTests()
  130. {
  131. $data = Tests_Auth_OpenID_readdata('trustroot.txt');
  132. list($parsehead, $matchhead) = Tests_Auth_OpenID_parseHeadings($data, '=');
  133. $pe = $parsehead['end'];
  134. $parsedata = substr($data, $pe, $matchhead['start'] - $pe);
  135. $parsetests = Tests_Auth_OpenID_getSections($parsedata);
  136. $parsecases = Tests_Auth_OpenID_trParseTests($parsehead['heading'],
  137. $parsetests);
  138. $matchdata = substr($data, $matchhead['end']);
  139. $matchtests = Tests_Auth_OpenID_getSections($matchdata);
  140. $matchcases = Tests_Auth_OpenID_trMatchTests($matchhead['heading'],
  141. $matchtests);
  142. return array_merge($parsecases, $matchcases);
  143. }
  144. class Tests_Auth_OpenID_TrustRoot extends PHPUnit_TestSuite {
  145. function Tests_Auth_OpenID_TrustRoot($name)
  146. {
  147. $this->setName($name);
  148. foreach (Tests_Auth_OpenID_trustRootTests() as $test) {
  149. $this->_addTestByValue($test);
  150. }
  151. }
  152. function _addTestByValue($test) {
  153. $this->addTest($test);
  154. }
  155. }
  156. ?>