preprocessorFuzzTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Performs fuzz-style testing of MediaWiki's preprocessor.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. $optionsWithoutArgs = [ 'verbose' ];
  25. require_once __DIR__ . '/commandLine.inc';
  26. $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
  27. class PPFuzzTester {
  28. public $hairs = [
  29. '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
  30. '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
  31. '<!--', '-->',
  32. "\n==", "==\n",
  33. '|', '=', "\n", ' ', "\t", "\x7f",
  34. '~~', '~~~', '~~~~', 'subst:',
  35. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  36. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  37. // extensions
  38. // '<ref>', '</ref>', '<references/>',
  39. ];
  40. public $minLength = 0;
  41. public $maxLength = 20;
  42. public $maxTemplates = 5;
  43. // public $outputTypes = [ 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' ];
  44. public $entryPoints = [ 'testSrvus', 'testPst', 'testPreprocess' ];
  45. public $verbose = false;
  46. /**
  47. * @var bool|PPFuzzTest
  48. */
  49. private static $currentTest = false;
  50. function execute() {
  51. if ( !file_exists( 'results' ) ) {
  52. mkdir( 'results' );
  53. }
  54. if ( !is_dir( 'results' ) ) {
  55. echo "Unable to create 'results' directory\n";
  56. exit( 1 );
  57. }
  58. $overallStart = microtime( true );
  59. $reportInterval = 1000;
  60. for ( $i = 1; true; $i++ ) {
  61. $t = -microtime( true );
  62. try {
  63. self::$currentTest = new PPFuzzTest( $this );
  64. self::$currentTest->execute();
  65. $passed = 'passed';
  66. } catch ( Exception $e ) {
  67. $testReport = self::$currentTest->getReport();
  68. $exceptionReport = $e->getText();
  69. $hash = md5( $testReport );
  70. file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
  71. file_put_contents( "results/ppft-$hash.fail",
  72. "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
  73. print "Test $hash failed\n";
  74. $passed = 'failed';
  75. }
  76. $t += microtime( true );
  77. if ( $this->verbose ) {
  78. printf( "Test $passed in %.3f seconds\n", $t );
  79. print self::$currentTest->getReport();
  80. }
  81. $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
  82. if ( $reportMetric > 25 ) {
  83. if ( substr( $reportInterval, 0, 1 ) === '1' ) {
  84. $reportInterval /= 2;
  85. } else {
  86. $reportInterval /= 5;
  87. }
  88. } elseif ( $reportMetric < 4 ) {
  89. if ( substr( $reportInterval, 0, 1 ) === '1' ) {
  90. $reportInterval *= 5;
  91. } else {
  92. $reportInterval *= 2;
  93. }
  94. }
  95. if ( $i % $reportInterval == 0 ) {
  96. print "$i tests done\n";
  97. /*
  98. $testReport = self::$currentTest->getReport();
  99. $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
  100. file_put_contents( $filename, "Input:\n$testReport\n" );*/
  101. }
  102. }
  103. }
  104. function makeInputText( $max = false ) {
  105. if ( $max === false ) {
  106. $max = $this->maxLength;
  107. }
  108. $length = mt_rand( $this->minLength, $max );
  109. $s = '';
  110. for ( $i = 0; $i < $length; $i++ ) {
  111. $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
  112. $s .= $this->hairs[$hairIndex];
  113. }
  114. // Send through the UTF-8 normaliser
  115. // This resolves a few differences between the old preprocessor and the
  116. // XML-based one, which doesn't like illegals and converts line endings.
  117. // It's done by the MW UI, so it's a reasonably legitimate thing to do.
  118. $s = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $s );
  119. return $s;
  120. }
  121. function makeTitle() {
  122. return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
  123. }
  124. /*
  125. function pickOutputType() {
  126. $count = count( $this->outputTypes );
  127. return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
  128. }*/
  129. function pickEntryPoint() {
  130. $count = count( $this->entryPoints );
  131. return $this->entryPoints[mt_rand( 0, $count - 1 )];
  132. }
  133. }
  134. class PPFuzzTest {
  135. public $templates, $mainText, $title, $entryPoint, $output;
  136. function __construct( $tester ) {
  137. global $wgMaxSigChars;
  138. $this->parent = $tester;
  139. $this->mainText = $tester->makeInputText();
  140. $this->title = $tester->makeTitle();
  141. // $this->outputType = $tester->pickOutputType();
  142. $this->entryPoint = $tester->pickEntryPoint();
  143. $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
  144. $this->fancySig = (bool)mt_rand( 0, 1 );
  145. $this->templates = [];
  146. }
  147. /**
  148. * @param Title $title
  149. * @return array
  150. */
  151. function templateHook( $title ) {
  152. $titleText = $title->getPrefixedDBkey();
  153. if ( !isset( $this->templates[$titleText] ) ) {
  154. $finalTitle = $title;
  155. if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
  156. // Too many templates
  157. $text = false;
  158. } else {
  159. if ( !mt_rand( 0, 1 ) ) {
  160. // Redirect
  161. $finalTitle = $this->parent->makeTitle();
  162. }
  163. if ( !mt_rand( 0, 5 ) ) {
  164. // Doesn't exist
  165. $text = false;
  166. } else {
  167. $text = $this->parent->makeInputText();
  168. }
  169. }
  170. $this->templates[$titleText] = [
  171. 'text' => $text,
  172. 'finalTitle' => $finalTitle ];
  173. }
  174. return $this->templates[$titleText];
  175. }
  176. function execute() {
  177. global $wgParser, $wgUser;
  178. $wgUser = new PPFuzzUser;
  179. $wgUser->mName = 'Fuzz';
  180. $wgUser->mFrom = 'name';
  181. $wgUser->ppfz_test = $this;
  182. $options = ParserOptions::newFromUser( $wgUser );
  183. $options->setTemplateCallback( [ $this, 'templateHook' ] );
  184. $options->setTimestamp( wfTimestampNow() );
  185. $this->output = call_user_func(
  186. [ $wgParser, $this->entryPoint ],
  187. $this->mainText,
  188. $this->title,
  189. $options
  190. );
  191. return $this->output;
  192. }
  193. function getReport() {
  194. $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
  195. // "Output type: {$this->outputType}\n" .
  196. "Entry point: {$this->entryPoint}\n" .
  197. "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
  198. ' ' . var_export( $this->nickname, true ) . "\n" .
  199. "Main text: " . var_export( $this->mainText, true ) . "\n";
  200. foreach ( $this->templates as $titleText => $template ) {
  201. $finalTitle = $template['finalTitle'];
  202. if ( $finalTitle != $titleText ) {
  203. $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
  204. } else {
  205. $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
  206. }
  207. }
  208. $s .= "Output: " . var_export( $this->output, true ) . "\n";
  209. return $s;
  210. }
  211. }
  212. class PPFuzzUser extends User {
  213. public $ppfz_test, $mDataLoaded;
  214. function load() {
  215. if ( $this->mDataLoaded ) {
  216. return;
  217. }
  218. $this->mDataLoaded = true;
  219. $this->loadDefaults( $this->mName );
  220. }
  221. function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
  222. if ( $oname === 'fancysig' ) {
  223. return $this->ppfz_test->fancySig;
  224. } elseif ( $oname === 'nickname' ) {
  225. return $this->ppfz_test->nickname;
  226. } else {
  227. return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
  228. }
  229. }
  230. }
  231. ini_set( 'memory_limit', '50M' );
  232. if ( isset( $args[0] ) ) {
  233. $testText = file_get_contents( $args[0] );
  234. if ( !$testText ) {
  235. print "File not found\n";
  236. exit( 1 );
  237. }
  238. $test = unserialize( $testText );
  239. $result = $test->execute();
  240. print "Test passed.\n";
  241. } else {
  242. $tester = new PPFuzzTester;
  243. $tester->verbose = isset( $options['verbose'] );
  244. $tester->execute();
  245. }