TrustRoot.php 5.3 KB

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