Don't use deprecated User::checkPassword() in TestUser
[mediawiki.git] / maintenance / preprocessorFuzzTest.php
blobaa5d69d95d9498a4357f906b704b3b5531ff87ad
1 <?php
2 /**
3 * Performs fuzz-style testing of MediaWiki's preprocessor.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 $optionsWithoutArgs = [ 'verbose' ];
25 require_once __DIR__ . '/commandLine.inc';
27 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
29 class PPFuzzTester {
30 public $hairs = [
31 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
32 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
33 '<!--', '-->',
34 "\n==", "==\n",
35 '|', '=', "\n", ' ', "\t", "\x7f",
36 '~~', '~~~', '~~~~', 'subst:',
37 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
38 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
40 // extensions
41 // '<ref>', '</ref>', '<references/>',
43 public $minLength = 0;
44 public $maxLength = 20;
45 public $maxTemplates = 5;
46 // public $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
47 public $entryPoints = [ 'testSrvus', 'testPst', 'testPreprocess' ];
48 public $verbose = false;
50 private static $currentTest = false;
52 function execute() {
53 if ( !file_exists( 'results' ) ) {
54 mkdir( 'results' );
56 if ( !is_dir( 'results' ) ) {
57 echo "Unable to create 'results' directory\n";
58 exit( 1 );
60 $overallStart = microtime( true );
61 $reportInterval = 1000;
62 for ( $i = 1; true; $i++ ) {
63 $t = -microtime( true );
64 try {
65 self::$currentTest = new PPFuzzTest( $this );
66 self::$currentTest->execute();
67 $passed = 'passed';
68 } catch ( Exception $e ) {
69 $testReport = self::$currentTest->getReport();
70 $exceptionReport = $e->getText();
71 $hash = md5( $testReport );
72 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
73 file_put_contents( "results/ppft-$hash.fail",
74 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
75 print "Test $hash failed\n";
76 $passed = 'failed';
78 $t += microtime( true );
80 if ( $this->verbose ) {
81 printf( "Test $passed in %.3f seconds\n", $t );
82 print self::$currentTest->getReport();
85 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
86 if ( $reportMetric > 25 ) {
87 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
88 $reportInterval /= 2;
89 } else {
90 $reportInterval /= 5;
92 } elseif ( $reportMetric < 4 ) {
93 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
94 $reportInterval *= 5;
95 } else {
96 $reportInterval *= 2;
99 if ( $i % $reportInterval == 0 ) {
100 print "$i tests done\n";
102 $testReport = self::$currentTest->getReport();
103 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
104 file_put_contents( $filename, "Input:\n$testReport\n" );*/
109 function makeInputText( $max = false ) {
110 if ( $max === false ) {
111 $max = $this->maxLength;
113 $length = mt_rand( $this->minLength, $max );
114 $s = '';
115 for ( $i = 0; $i < $length; $i++ ) {
116 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
117 $s .= $this->hairs[$hairIndex];
119 // Send through the UTF-8 normaliser
120 // This resolves a few differences between the old preprocessor and the
121 // XML-based one, which doesn't like illegals and converts line endings.
122 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
123 global $wgContLang;
124 $s = $wgContLang->normalize( $s );
126 return $s;
129 function makeTitle() {
130 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
134 function pickOutputType() {
135 $count = count( $this->outputTypes );
136 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
139 function pickEntryPoint() {
140 $count = count( $this->entryPoints );
142 return $this->entryPoints[mt_rand( 0, $count - 1 )];
146 class PPFuzzTest {
147 public $templates, $mainText, $title, $entryPoint, $output;
149 function __construct( $tester ) {
150 global $wgMaxSigChars;
151 $this->parent = $tester;
152 $this->mainText = $tester->makeInputText();
153 $this->title = $tester->makeTitle();
154 // $this->outputType = $tester->pickOutputType();
155 $this->entryPoint = $tester->pickEntryPoint();
156 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
157 $this->fancySig = (bool)mt_rand( 0, 1 );
158 $this->templates = [];
162 * @param Title $title
163 * @return array
165 function templateHook( $title ) {
166 $titleText = $title->getPrefixedDBkey();
168 if ( !isset( $this->templates[$titleText] ) ) {
169 $finalTitle = $title;
170 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
171 // Too many templates
172 $text = false;
173 } else {
174 if ( !mt_rand( 0, 1 ) ) {
175 // Redirect
176 $finalTitle = $this->parent->makeTitle();
178 if ( !mt_rand( 0, 5 ) ) {
179 // Doesn't exist
180 $text = false;
181 } else {
182 $text = $this->parent->makeInputText();
185 $this->templates[$titleText] = [
186 'text' => $text,
187 'finalTitle' => $finalTitle ];
190 return $this->templates[$titleText];
193 function execute() {
194 global $wgParser, $wgUser;
196 $wgUser = new PPFuzzUser;
197 $wgUser->mName = 'Fuzz';
198 $wgUser->mFrom = 'name';
199 $wgUser->ppfz_test = $this;
201 $options = ParserOptions::newFromUser( $wgUser );
202 $options->setTemplateCallback( [ $this, 'templateHook' ] );
203 $options->setTimestamp( wfTimestampNow() );
204 $this->output = call_user_func(
205 [ $wgParser, $this->entryPoint ],
206 $this->mainText,
207 $this->title,
208 $options
211 return $this->output;
214 function getReport() {
215 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
216 // "Output type: {$this->outputType}\n" .
217 "Entry point: {$this->entryPoint}\n" .
218 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
219 ' ' . var_export( $this->nickname, true ) . "\n" .
220 "Main text: " . var_export( $this->mainText, true ) . "\n";
221 foreach ( $this->templates as $titleText => $template ) {
222 $finalTitle = $template['finalTitle'];
223 if ( $finalTitle != $titleText ) {
224 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
225 } else {
226 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
229 $s .= "Output: " . var_export( $this->output, true ) . "\n";
231 return $s;
235 class PPFuzzUser extends User {
236 public $ppfz_test, $mDataLoaded;
238 function load() {
239 if ( $this->mDataLoaded ) {
240 return;
242 $this->mDataLoaded = true;
243 $this->loadDefaults( $this->mName );
246 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
247 if ( $oname === 'fancysig' ) {
248 return $this->ppfz_test->fancySig;
249 } elseif ( $oname === 'nickname' ) {
250 return $this->ppfz_test->nickname;
251 } else {
252 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
257 ini_set( 'memory_limit', '50M' );
258 if ( isset( $args[0] ) ) {
259 $testText = file_get_contents( $args[0] );
260 if ( !$testText ) {
261 print "File not found\n";
262 exit( 1 );
264 $test = unserialize( $testText );
265 $result = $test->execute();
266 print "Test passed.\n";
267 } else {
268 $tester = new PPFuzzTester;
269 $tester->verbose = isset( $options['verbose'] );
270 $tester->execute();