PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Auth/OpenID/TrustRoot.php

http://github.com/openid/php-openid
PHP | 175 lines | 154 code | 18 blank | 3 comment | 8 complexity | 37feb19b1962494d7f55ebae061ee556 MD5 | raw file
Possible License(s): Apache-2.0
  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 __construct($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 __construct($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 = [];
  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[] = [
  62. '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 = [];
  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[] = ['desc' => $desc, 'lines' => $lines,];
  91. }
  92. return $sections;
  93. }
  94. function Tests_Auth_OpenID_trParseTests($head, $tests)
  95. {
  96. $tests = [
  97. 'fail' => $tests[0],
  98. 'insane' => $tests[1],
  99. 'sane' => $tests[2],
  100. ];
  101. $testobjs = [];
  102. foreach ($tests as $expected => $testdata) {
  103. $lines = $testdata['lines'];
  104. foreach ($lines as $line) {
  105. $desc = sprintf("%s - %s: %s", $head,
  106. $testdata['desc'], var_export($line, true));
  107. $testobjs[] = new Tests_Auth_OpenID_TRParseCase(
  108. $desc, $line, $expected);
  109. }
  110. }
  111. return $testobjs;
  112. }
  113. function Tests_Auth_OpenID_trMatchTests($head, $tests)
  114. {
  115. $tests = [true => $tests[0], false => $tests[1]];
  116. $testobjs = [];
  117. foreach ($tests as $expected => $testdata) {
  118. $lines = $testdata['lines'];
  119. foreach ($lines as $line) {
  120. $pat = '/^([^ ]+) +([^ ]+)$/';
  121. preg_match($pat, $line, $matches);
  122. list($_, $tr, $rt) = $matches;
  123. $desc = sprintf("%s - %s: %s %s", $head, $testdata['desc'],
  124. var_export($tr, true), var_export($rt, true));
  125. $testobjs[] = new Tests_Auth_OpenID_TRMatchCase(
  126. $desc, $tr, $rt, $expected);
  127. }
  128. }
  129. return $testobjs;
  130. }
  131. function Tests_Auth_OpenID_trustRootTests()
  132. {
  133. $data = Tests_Auth_OpenID_readdata('trustroot.txt');
  134. list($parsehead, $matchhead) = Tests_Auth_OpenID_parseHeadings($data, '=');
  135. $pe = $parsehead['end'];
  136. $parsedata = substr($data, $pe, $matchhead['start'] - $pe);
  137. $parsetests = Tests_Auth_OpenID_getSections($parsedata);
  138. $parsecases = Tests_Auth_OpenID_trParseTests($parsehead['heading'],
  139. $parsetests);
  140. $matchdata = substr($data, $matchhead['end']);
  141. $matchtests = Tests_Auth_OpenID_getSections($matchdata);
  142. $matchcases = Tests_Auth_OpenID_trMatchTests($matchhead['heading'],
  143. $matchtests);
  144. return array_merge($parsecases, $matchcases);
  145. }
  146. class Tests_Auth_OpenID_TrustRoot extends PHPUnit_Framework_TestSuite {
  147. function __construct($name)
  148. {
  149. $this->setName($name);
  150. foreach (Tests_Auth_OpenID_trustRootTests() as $test) {
  151. $this->_addTestByValue($test);
  152. }
  153. }
  154. function _addTestByValue($test) {
  155. $this->addTest($test);
  156. }
  157. }