PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/maintenance/preprocessorFuzzTest.php

https://github.com/tav/confluence
PHP | 237 lines | 189 code | 23 blank | 25 comment | 28 complexity | 8278afdd8376d8121e2c1f732af3cd71 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup Maintenance
  5. */
  6. require_once( dirname( __FILE__ ). '/../maintenance/commandLine.inc' );
  7. $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
  8. class PPFuzzTester {
  9. var $hairs = array(
  10. '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
  11. '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
  12. '<!--' , '-->',
  13. "\n==", "==\n",
  14. '|', '=', "\n", ' ', "\t", "\x7f",
  15. '~~', '~~~', '~~~~', 'subst:',
  16. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  17. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  18. // extensions
  19. //'<ref>', '</ref>', '<references/>',
  20. );
  21. var $minLength = 0;
  22. var $maxLength = 20;
  23. var $maxTemplates = 5;
  24. //var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
  25. var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
  26. var $verbose = false;
  27. static $currentTest = false;
  28. function execute() {
  29. if ( !file_exists( 'results' ) ) {
  30. mkdir( 'results' );
  31. }
  32. if ( !is_dir( 'results' ) ) {
  33. echo "Unable to create 'results' directory\n";
  34. exit( 1 );
  35. }
  36. $overallStart = microtime( true );
  37. $reportInterval = 1000;
  38. for ( $i = 1; true; $i++ ) {
  39. $t = -microtime( true );
  40. try {
  41. self::$currentTest = new PPFuzzTest( $this );
  42. self::$currentTest->execute();
  43. $passed = 'passed';
  44. } catch ( MWException $e ) {
  45. $testReport = self::$currentTest->getReport();
  46. $exceptionReport = $e->getText();
  47. $hash = md5( $testReport );
  48. file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
  49. file_put_contents( "results/ppft-$hash.fail",
  50. "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
  51. print "Test $hash failed\n";
  52. $passed = 'failed';
  53. }
  54. $t += microtime( true );
  55. if ( $this->verbose ) {
  56. printf( "Test $passed in %.3f seconds\n", $t );
  57. print self::$currentTest->getReport();
  58. }
  59. $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
  60. if ( $reportMetric > 25 ) {
  61. if ( substr( $reportInterval, 0, 1 ) === '1' ) {
  62. $reportInterval /= 2;
  63. } else {
  64. $reportInterval /= 5;
  65. }
  66. } elseif ( $reportMetric < 4 ) {
  67. if ( substr( $reportInterval, 0, 1 ) === '1' ) {
  68. $reportInterval *= 5;
  69. } else {
  70. $reportInterval *= 2;
  71. }
  72. }
  73. if ( $i % $reportInterval == 0 ) {
  74. print "$i tests done\n";
  75. /*
  76. $testReport = self::$currentTest->getReport();
  77. $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
  78. file_put_contents( $filename, "Input:\n$testReport\n" );*/
  79. }
  80. }
  81. wfLogProfilingData();
  82. }
  83. function makeInputText( $max = false ) {
  84. if ( $max === false ) {
  85. $max = $this->maxLength;
  86. }
  87. $length = mt_rand( $this->minLength, $max );
  88. $s = '';
  89. for ( $i = 0; $i < $length; $i++ ) {
  90. $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
  91. $s .= $this->hairs[$hairIndex];
  92. }
  93. // Send through the UTF-8 normaliser
  94. // This resolves a few differences between the old preprocessor and the
  95. // XML-based one, which doesn't like illegals and converts line endings.
  96. // It's done by the MW UI, so it's a reasonably legitimate thing to do.
  97. $s = UtfNormal::cleanUp( $s );
  98. return $s;
  99. }
  100. function makeTitle() {
  101. return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
  102. }
  103. /*
  104. function pickOutputType() {
  105. $count = count( $this->outputTypes );
  106. return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
  107. }*/
  108. function pickEntryPoint() {
  109. $count = count( $this->entryPoints );
  110. return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
  111. }
  112. }
  113. class PPFuzzTest {
  114. var $templates, $mainText, $title, $entryPoint, $output;
  115. function __construct( $tester ) {
  116. global $wgMaxSigChars;
  117. $this->parent = $tester;
  118. $this->mainText = $tester->makeInputText();
  119. $this->title = $tester->makeTitle();
  120. //$this->outputType = $tester->pickOutputType();
  121. $this->entryPoint = $tester->pickEntryPoint();
  122. $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10);
  123. $this->fancySig = (bool)mt_rand( 0, 1 );
  124. $this->templates = array();
  125. }
  126. function templateHook( $title ) {
  127. $titleText = $title->getPrefixedDBkey();
  128. if ( !isset( $this->templates[$titleText] ) ) {
  129. $finalTitle = $title;
  130. if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
  131. // Too many templates
  132. $text = false;
  133. } else {
  134. if ( !mt_rand( 0, 1 ) ) {
  135. // Redirect
  136. $finalTitle = $this->parent->makeTitle();
  137. }
  138. if ( !mt_rand( 0, 5 ) ) {
  139. // Doesn't exist
  140. $text = false;
  141. } else {
  142. $text = $this->parent->makeInputText();
  143. }
  144. }
  145. $this->templates[$titleText] = array(
  146. 'text' => $text,
  147. 'finalTitle' => $finalTitle );
  148. }
  149. return $this->templates[$titleText];
  150. }
  151. function execute() {
  152. global $wgParser, $wgUser;
  153. $wgUser = new PPFuzzUser;
  154. $wgUser->mName = 'Fuzz';
  155. $wgUser->mFrom = 'name';
  156. $wgUser->ppfz_test = $this;
  157. $options = new ParserOptions;
  158. $options->setTemplateCallback( array( $this, 'templateHook' ) );
  159. $options->setTimestamp( wfTimestampNow() );
  160. $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title->getPrefixedText(), $options );
  161. return $this->output;
  162. }
  163. function getReport() {
  164. $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
  165. // "Output type: {$this->outputType}\n" .
  166. "Entry point: {$this->entryPoint}\n" .
  167. "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
  168. "Main text: " . var_export( $this->mainText, true ) . "\n";
  169. foreach ( $this->templates as $titleText => $template ) {
  170. $finalTitle = $template['finalTitle'];
  171. if ( $finalTitle != $titleText ) {
  172. $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
  173. } else {
  174. $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
  175. }
  176. }
  177. $s .= "Output: " . var_export( $this->output, true ) . "\n";
  178. return $s;
  179. }
  180. }
  181. class PPFuzzUser extends User {
  182. var $ppfz_test;
  183. function load() {
  184. if ( $this->mDataLoaded ) {
  185. return;
  186. }
  187. $this->mDataLoaded = true;
  188. $this->loadDefaults( $this->mName );
  189. }
  190. function getOption( $option, $defaultOverride = '' ) {
  191. if ( $option === 'fancysig' ) {
  192. return $this->ppfz_test->fancySig;
  193. } elseif ( $option === 'nickname' ) {
  194. return $this->ppfz_test->nickname;
  195. } else {
  196. return parent::getOption( $option, $defaultOverride );
  197. }
  198. }
  199. }
  200. ini_set( 'memory_limit', '50M' );
  201. if ( isset( $args[0] ) ) {
  202. $testText = file_get_contents( $args[0] );
  203. if ( !$testText ) {
  204. print "File not found\n";
  205. exit( 1 );
  206. }
  207. $test = unserialize( $testText );
  208. $result = $test->execute();
  209. print "Test passed.\n";
  210. } else {
  211. $tester = new PPFuzzTester;
  212. $tester->verbose = isset( $options['verbose'] );
  213. $tester->execute();
  214. }