Per Nikerabbit, fixes for r89176:
[lhc/web/wiklou.git] / maintenance / preprocessorFuzzTest.php
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
24 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
25
26 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
27
28 class PPFuzzTester {
29 var $hairs = array(
30 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
31 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
32 '<!--' , '-->',
33 "\n==", "==\n",
34 '|', '=', "\n", ' ', "\t", "\x7f",
35 '~~', '~~~', '~~~~', 'subst:',
36 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
37 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
38
39 // extensions
40 // '<ref>', '</ref>', '<references/>',
41 );
42 var $minLength = 0;
43 var $maxLength = 20;
44 var $maxTemplates = 5;
45 // var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
46 var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
47 var $verbose = false;
48 static $currentTest = false;
49
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 ( MWException $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
78 if ( $this->verbose ) {
79 printf( "Test $passed in %.3f seconds\n", $t );
80 print self::$currentTest->getReport();
81 }
82
83 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
84 if ( $reportMetric > 25 ) {
85 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
86 $reportInterval /= 2;
87 } else {
88 $reportInterval /= 5;
89 }
90 } elseif ( $reportMetric < 4 ) {
91 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
92 $reportInterval *= 5;
93 } else {
94 $reportInterval *= 2;
95 }
96 }
97 if ( $i % $reportInterval == 0 ) {
98 print "$i tests done\n";
99 /*
100 $testReport = self::$currentTest->getReport();
101 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
102 file_put_contents( $filename, "Input:\n$testReport\n" );*/
103 }
104 }
105 }
106
107 function makeInputText( $max = false ) {
108 if ( $max === false ) {
109 $max = $this->maxLength;
110 }
111 $length = mt_rand( $this->minLength, $max );
112 $s = '';
113 for ( $i = 0; $i < $length; $i++ ) {
114 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
115 $s .= $this->hairs[$hairIndex];
116 }
117 // Send through the UTF-8 normaliser
118 // This resolves a few differences between the old preprocessor and the
119 // XML-based one, which doesn't like illegals and converts line endings.
120 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
121 global $wgContLang;
122 $s = $wgContLang->normalize( $s );
123 return $s;
124 }
125
126 function makeTitle() {
127 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
128 }
129
130 /*
131 function pickOutputType() {
132 $count = count( $this->outputTypes );
133 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
134 }*/
135
136 function pickEntryPoint() {
137 $count = count( $this->entryPoints );
138 return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
139 }
140 }
141
142 class PPFuzzTest {
143 var $templates, $mainText, $title, $entryPoint, $output;
144
145 function __construct( $tester ) {
146 global $wgMaxSigChars;
147 $this->parent = $tester;
148 $this->mainText = $tester->makeInputText();
149 $this->title = $tester->makeTitle();
150 // $this->outputType = $tester->pickOutputType();
151 $this->entryPoint = $tester->pickEntryPoint();
152 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
153 $this->fancySig = (bool)mt_rand( 0, 1 );
154 $this->templates = array();
155 }
156
157 function templateHook( $title ) {
158 $titleText = $title->getPrefixedDBkey();
159
160 if ( !isset( $this->templates[$titleText] ) ) {
161 $finalTitle = $title;
162 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
163 // Too many templates
164 $text = false;
165 } else {
166 if ( !mt_rand( 0, 1 ) ) {
167 // Redirect
168 $finalTitle = $this->parent->makeTitle();
169 }
170 if ( !mt_rand( 0, 5 ) ) {
171 // Doesn't exist
172 $text = false;
173 } else {
174 $text = $this->parent->makeInputText();
175 }
176 }
177 $this->templates[$titleText] = array(
178 'text' => $text,
179 'finalTitle' => $finalTitle );
180 }
181 return $this->templates[$titleText];
182 }
183
184 function execute() {
185 global $wgParser, $wgUser;
186
187 $wgUser = new PPFuzzUser;
188 $wgUser->mName = 'Fuzz';
189 $wgUser->mFrom = 'name';
190 $wgUser->ppfz_test = $this;
191
192 $options = ParserOptions::newFromUser( $wgUser );
193 $options->setTemplateCallback( array( $this, 'templateHook' ) );
194 $options->setTimestamp( wfTimestampNow() );
195 $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title, $options );
196 return $this->output;
197 }
198
199 function getReport() {
200 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
201 // "Output type: {$this->outputType}\n" .
202 "Entry point: {$this->entryPoint}\n" .
203 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
204 "Main text: " . var_export( $this->mainText, true ) . "\n";
205 foreach ( $this->templates as $titleText => $template ) {
206 $finalTitle = $template['finalTitle'];
207 if ( $finalTitle != $titleText ) {
208 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
209 } else {
210 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
211 }
212 }
213 $s .= "Output: " . var_export( $this->output, true ) . "\n";
214 return $s;
215 }
216 }
217
218 class PPFuzzUser extends User {
219 var $ppfz_test, $mDataLoaded;
220
221 function load() {
222 if ( $this->mDataLoaded ) {
223 return;
224 }
225 $this->mDataLoaded = true;
226 $this->loadDefaults( $this->mName );
227 }
228
229 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
230 if ( $oname === 'fancysig' ) {
231 return $this->ppfz_test->fancySig;
232 } elseif ( $oname === 'nickname' ) {
233 return $this->ppfz_test->nickname;
234 } else {
235 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
236 }
237 }
238 }
239
240 ini_set( 'memory_limit', '50M' );
241 if ( isset( $args[0] ) ) {
242 $testText = file_get_contents( $args[0] );
243 if ( !$testText ) {
244 print "File not found\n";
245 exit( 1 );
246 }
247 $test = unserialize( $testText );
248 $result = $test->execute();
249 print "Test passed.\n";
250 } else {
251 $tester = new PPFuzzTester;
252 $tester->verbose = isset( $options['verbose'] );
253 $tester->execute();
254 }