Updating composer/semver (1.5.1 => 1.5.2)
[mediawiki.git] / maintenance / preprocessorFuzzTest.php
blobe9ae4e220592f95818c5fcfbfb4de2a232626733
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 use MediaWiki\MediaWikiServices;
26 use Wikimedia\TestingAccessWrapper;
28 $optionsWithoutArgs = [ 'verbose' ];
29 require_once __DIR__ . '/CommandLineInc.php';
31 class PPFuzzTester {
32 public $hairs = [
33 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
34 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
35 '<!--', '-->',
36 "\n==", "==\n",
37 '|', '=', "\n", ' ', "\t", "\x7f",
38 '~~', '~~~', '~~~~', 'subst:',
39 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
40 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
42 // extensions
43 // '<ref>', '</ref>', '<references/>',
45 public $minLength = 0;
46 public $maxLength = 20;
47 public $maxTemplates = 5;
48 // public $outputTypes = [ 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' ];
49 public $entryPoints = [ 'fuzzTestSrvus', 'fuzzTestPst', 'fuzzTestPreprocess' ];
50 public $verbose = false;
52 /**
53 * @var bool|PPFuzzTest
55 private static $currentTest = false;
57 public function execute() {
58 if ( !file_exists( 'results' ) ) {
59 mkdir( 'results' );
61 if ( !is_dir( 'results' ) ) {
62 echo "Unable to create 'results' directory\n";
63 exit( 1 );
65 $overallStart = microtime( true );
66 $reportInterval = 1000;
67 // @phan-suppress-next-line PhanInfiniteLoop
68 for ( $i = 1; true; $i++ ) {
69 $t = -microtime( true );
70 try {
71 self::$currentTest = new PPFuzzTest( $this );
72 self::$currentTest->execute();
73 $passed = 'passed';
74 } catch ( Exception $e ) {
75 $testReport = self::$currentTest->getReport();
76 $exceptionReport = $e instanceof MWException ? $e->getText() : (string)$e;
77 $hash = md5( $testReport );
78 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
79 file_put_contents( "results/ppft-$hash.fail",
80 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
81 print "Test $hash failed\n";
82 $passed = 'failed';
84 $t += microtime( true );
86 if ( $this->verbose ) {
87 printf( "Test $passed in %.3f seconds\n", $t );
88 print self::$currentTest->getReport();
91 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
92 if ( $reportMetric > 25 ) {
93 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
94 $reportInterval /= 2;
95 } else {
96 $reportInterval /= 5;
98 } elseif ( $reportMetric < 4 ) {
99 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
100 $reportInterval *= 5;
101 } else {
102 $reportInterval *= 2;
105 if ( $i % $reportInterval == 0 ) {
106 print "$i tests done\n";
108 $testReport = self::$currentTest->getReport();
109 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
110 file_put_contents( $filename, "Input:\n$testReport\n" );*/
115 public function makeInputText( $max = false ) {
116 if ( $max === false ) {
117 $max = $this->maxLength;
119 $length = mt_rand( $this->minLength, $max );
120 $s = '';
121 for ( $i = 0; $i < $length; $i++ ) {
122 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
123 $s .= $this->hairs[$hairIndex];
125 // Send through the UTF-8 normaliser
126 // This resolves a few differences between the old preprocessor and the
127 // XML-based one, which doesn't like illegals and converts line endings.
128 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
129 $s = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $s );
131 return $s;
134 public function makeTitle() {
135 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
139 public function pickOutputType() {
140 $count = count( $this->outputTypes );
141 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
144 public function pickEntryPoint() {
145 $count = count( $this->entryPoints );
147 return $this->entryPoints[mt_rand( 0, $count - 1 )];
151 class PPFuzzTest {
153 * @var array[]
154 * @phan-var array<string,array{text:string|false,finalTitle:Title}>
156 public $templates;
157 public $mainText, $title, $entryPoint, $output;
159 /** @var PPFuzzTester */
160 private $parent;
161 /** @var string */
162 public $nickname;
163 /** @var bool */
164 public $fancySig;
167 * @param PPFuzzTester $tester
169 public function __construct( $tester ) {
170 global $wgMaxSigChars;
171 $this->parent = $tester;
172 $this->mainText = $tester->makeInputText();
173 $this->title = $tester->makeTitle();
174 // $this->outputType = $tester->pickOutputType();
175 $this->entryPoint = $tester->pickEntryPoint();
176 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
177 $this->fancySig = (bool)mt_rand( 0, 1 );
178 $this->templates = [];
182 * @param Title $title
183 * @return array
185 public function templateHook( $title ) {
186 $titleText = $title->getPrefixedDBkey();
188 if ( !isset( $this->templates[$titleText] ) ) {
189 $finalTitle = $title;
190 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
191 // Too many templates
192 $text = false;
193 } else {
194 if ( !mt_rand( 0, 1 ) ) {
195 // Redirect
196 $finalTitle = $this->parent->makeTitle();
198 if ( !mt_rand( 0, 5 ) ) {
199 // Doesn't exist
200 $text = false;
201 } else {
202 $text = $this->parent->makeInputText();
205 $this->templates[$titleText] = [
206 'text' => $text,
207 'finalTitle' => $finalTitle ];
210 return $this->templates[$titleText];
213 public function execute() {
214 global $wgUser;
216 $user = new PPFuzzUser;
217 $user->mName = 'Fuzz';
218 $user->mFrom = 'name';
219 $user->ppfz_test = $this;
221 $wgUser = $user;
223 $options = ParserOptions::newFromUser( $user );
224 $options->setTemplateCallback( [ $this, 'templateHook' ] );
225 $options->setTimestamp( wfTimestampNow() );
226 $this->output = call_user_func(
227 [ TestingAccessWrapper::newFromObject(
228 MediaWikiServices::getInstance()->getParser()
229 ), $this->entryPoint ],
230 $this->mainText,
231 $this->title,
232 $options
235 return $this->output;
238 public function getReport() {
239 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
240 // "Output type: {$this->outputType}\n" .
241 "Entry point: {$this->entryPoint}\n" .
242 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
243 ' ' . var_export( $this->nickname, true ) . "\n" .
244 "Main text: " . var_export( $this->mainText, true ) . "\n";
245 foreach ( $this->templates as $titleText => $template ) {
246 $finalTitle = $template['finalTitle'];
247 if ( $finalTitle != $titleText ) {
248 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
249 } else {
250 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
253 $s .= "Output: " . var_export( $this->output, true ) . "\n";
255 return $s;
259 class PPFuzzUser extends User {
260 public $ppfz_test, $mDataLoaded;
262 public function load( $flags = null ) {
263 if ( $this->mDataLoaded ) {
264 return;
266 $this->mDataLoaded = true;
267 $this->loadDefaults( $this->mName );
270 public function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
271 if ( $oname === 'fancysig' ) {
272 return $this->ppfz_test->fancySig;
273 } elseif ( $oname === 'nickname' ) {
274 return $this->ppfz_test->nickname;
275 } else {
276 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
281 ini_set( 'memory_limit', '50M' );
282 if ( isset( $args[0] ) ) {
283 $testText = file_get_contents( $args[0] );
284 if ( !$testText ) {
285 print "File not found\n";
286 exit( 1 );
288 $test = unserialize( $testText );
289 $result = $test->execute();
290 print "Test passed.\n";
291 } else {
292 $tester = new PPFuzzTester;
293 $tester->verbose = isset( $options['verbose'] );
294 $tester->execute();