Merge "Http::getProxy() method to get proxy configuration"
[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 $optionsWithoutArgs = [ 'verbose' ];
25 require_once __DIR__ . '/commandLine.inc';
26
27 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
28
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',
39
40 // extensions
41 // '<ref>', '</ref>', '<references/>',
42 ];
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;
49
50 private static $currentTest = false;
51
52 function execute() {
53 if ( !file_exists( 'results' ) ) {
54 mkdir( 'results' );
55 }
56 if ( !is_dir( 'results' ) ) {
57 echo "Unable to create 'results' directory\n";
58 exit( 1 );
59 }
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';
77 }
78 $t += microtime( true );
79
80 if ( $this->verbose ) {
81 printf( "Test $passed in %.3f seconds\n", $t );
82 print self::$currentTest->getReport();
83 }
84
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;
91 }
92 } elseif ( $reportMetric < 4 ) {
93 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
94 $reportInterval *= 5;
95 } else {
96 $reportInterval *= 2;
97 }
98 }
99 if ( $i % $reportInterval == 0 ) {
100 print "$i tests done\n";
101 /*
102 $testReport = self::$currentTest->getReport();
103 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
104 file_put_contents( $filename, "Input:\n$testReport\n" );*/
105 }
106 }
107 }
108
109 function makeInputText( $max = false ) {
110 if ( $max === false ) {
111 $max = $this->maxLength;
112 }
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];
118 }
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 );
125
126 return $s;
127 }
128
129 function makeTitle() {
130 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
131 }
132
133 /*
134 function pickOutputType() {
135 $count = count( $this->outputTypes );
136 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
137 }*/
138
139 function pickEntryPoint() {
140 $count = count( $this->entryPoints );
141
142 return $this->entryPoints[mt_rand( 0, $count - 1 )];
143 }
144 }
145
146 class PPFuzzTest {
147 public $templates, $mainText, $title, $entryPoint, $output;
148
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 = [];
159 }
160
161 /**
162 * @param Title $title
163 * @return array
164 */
165 function templateHook( $title ) {
166 $titleText = $title->getPrefixedDBkey();
167
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();
177 }
178 if ( !mt_rand( 0, 5 ) ) {
179 // Doesn't exist
180 $text = false;
181 } else {
182 $text = $this->parent->makeInputText();
183 }
184 }
185 $this->templates[$titleText] = [
186 'text' => $text,
187 'finalTitle' => $finalTitle ];
188 }
189
190 return $this->templates[$titleText];
191 }
192
193 function execute() {
194 global $wgParser, $wgUser;
195
196 $wgUser = new PPFuzzUser;
197 $wgUser->mName = 'Fuzz';
198 $wgUser->mFrom = 'name';
199 $wgUser->ppfz_test = $this;
200
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
209 );
210
211 return $this->output;
212 }
213
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";
227 }
228 }
229 $s .= "Output: " . var_export( $this->output, true ) . "\n";
230
231 return $s;
232 }
233 }
234
235 class PPFuzzUser extends User {
236 public $ppfz_test, $mDataLoaded;
237
238 function load() {
239 if ( $this->mDataLoaded ) {
240 return;
241 }
242 $this->mDataLoaded = true;
243 $this->loadDefaults( $this->mName );
244 }
245
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 );
253 }
254 }
255 }
256
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 );
263 }
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();
271 }