MediaWiki REL1_34
preprocessorFuzzTest.php
Go to the documentation of this file.
1<?php
25
26use Wikimedia\TestingAccessWrapper;
27
28$optionsWithoutArgs = [ 'verbose' ];
29require_once __DIR__ . '/commandLine.inc';
30
31$wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
32
34 public $hairs = [
35 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
36 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
37 '<!--', '-->',
38 "\n==", "==\n",
39 '|', '=', "\n", ' ', "\t", "\x7f",
40 '~~', '~~~', '~~~~', 'subst:',
41 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
42 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
43
44 // extensions
45 // '<ref>', '</ref>', '<references/>',
46 ];
47 public $minLength = 0;
48 public $maxLength = 20;
49 public $maxTemplates = 5;
50 // public $outputTypes = [ 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' ];
51 public $entryPoints = [ 'fuzzTestSrvus', 'fuzzTestPst', 'fuzzTestPreprocess' ];
52 public $verbose = false;
53
57 private static $currentTest = false;
58
59 function execute() {
60 if ( !file_exists( 'results' ) ) {
61 mkdir( 'results' );
62 }
63 if ( !is_dir( 'results' ) ) {
64 echo "Unable to create 'results' directory\n";
65 exit( 1 );
66 }
67 $overallStart = microtime( true );
68 $reportInterval = 1000;
69 for ( $i = 1; true; $i++ ) {
70 $t = -microtime( true );
71 try {
72 self::$currentTest = new PPFuzzTest( $this );
73 self::$currentTest->execute();
74 $passed = 'passed';
75 } catch ( Exception $e ) {
76 $testReport = self::$currentTest->getReport();
77 $exceptionReport = $e instanceof MWException ? $e->getText() : (string)$e;
78 $hash = md5( $testReport );
79 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
80 file_put_contents( "results/ppft-$hash.fail",
81 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
82 print "Test $hash failed\n";
83 $passed = 'failed';
84 }
85 $t += microtime( true );
86
87 if ( $this->verbose ) {
88 printf( "Test $passed in %.3f seconds\n", $t );
89 print self::$currentTest->getReport();
90 }
91
92 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
93 if ( $reportMetric > 25 ) {
94 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
95 $reportInterval /= 2;
96 } else {
97 $reportInterval /= 5;
98 }
99 } elseif ( $reportMetric < 4 ) {
100 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
101 $reportInterval *= 5;
102 } else {
103 $reportInterval *= 2;
104 }
105 }
106 if ( $i % $reportInterval == 0 ) {
107 print "$i tests done\n";
108 /*
109 $testReport = self::$currentTest->getReport();
110 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
111 file_put_contents( $filename, "Input:\n$testReport\n" );*/
112 }
113 }
114 }
115
116 function makeInputText( $max = false ) {
117 if ( $max === false ) {
118 $max = $this->maxLength;
119 }
120 $length = mt_rand( $this->minLength, $max );
121 $s = '';
122 for ( $i = 0; $i < $length; $i++ ) {
123 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
124 $s .= $this->hairs[$hairIndex];
125 }
126 // Send through the UTF-8 normaliser
127 // This resolves a few differences between the old preprocessor and the
128 // XML-based one, which doesn't like illegals and converts line endings.
129 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
130 $s = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $s );
131
132 return $s;
133 }
134
135 function makeTitle() {
136 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
137 }
138
139 /*
140 function pickOutputType() {
141 $count = count( $this->outputTypes );
142 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
143 }*/
144
145 function pickEntryPoint() {
146 $count = count( $this->entryPoints );
147
148 return $this->entryPoints[mt_rand( 0, $count - 1 )];
149 }
150}
151
154
156 private $parent;
158 public $nickname;
160 public $fancySig;
161
165 function __construct( $tester ) {
166 global $wgMaxSigChars;
167 $this->parent = $tester;
168 $this->mainText = $tester->makeInputText();
169 $this->title = $tester->makeTitle();
170 // $this->outputType = $tester->pickOutputType();
171 $this->entryPoint = $tester->pickEntryPoint();
172 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
173 $this->fancySig = (bool)mt_rand( 0, 1 );
174 $this->templates = [];
175 }
176
181 function templateHook( $title ) {
182 $titleText = $title->getPrefixedDBkey();
183
184 if ( !isset( $this->templates[$titleText] ) ) {
185 $finalTitle = $title;
186 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
187 // Too many templates
188 $text = false;
189 } else {
190 if ( !mt_rand( 0, 1 ) ) {
191 // Redirect
192 $finalTitle = $this->parent->makeTitle();
193 }
194 if ( !mt_rand( 0, 5 ) ) {
195 // Doesn't exist
196 $text = false;
197 } else {
198 $text = $this->parent->makeInputText();
199 }
200 }
201 $this->templates[$titleText] = [
202 'text' => $text,
203 'finalTitle' => $finalTitle ];
204 }
205
206 return $this->templates[$titleText];
207 }
208
209 function execute() {
210 global $wgUser;
211
212 $wgUser = new PPFuzzUser;
213 $wgUser->mName = 'Fuzz';
214 $wgUser->mFrom = 'name';
215 $wgUser->ppfz_test = $this;
216
217 $options = ParserOptions::newFromUser( $wgUser );
218 $options->setTemplateCallback( [ $this, 'templateHook' ] );
219 $options->setTimestamp( wfTimestampNow() );
220 $this->output = call_user_func(
221 [ TestingAccessWrapper::newFromObject(
222 MediaWikiServices::getInstance()->getParser()
223 ), $this->entryPoint ],
224 $this->mainText,
225 $this->title,
226 $options
227 );
228
229 return $this->output;
230 }
231
232 function getReport() {
233 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
234// "Output type: {$this->outputType}\n" .
235 "Entry point: {$this->entryPoint}\n" .
236 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
237 ' ' . var_export( $this->nickname, true ) . "\n" .
238 "Main text: " . var_export( $this->mainText, true ) . "\n";
239 foreach ( $this->templates as $titleText => $template ) {
240 $finalTitle = $template['finalTitle'];
241 if ( $finalTitle != $titleText ) {
242 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
243 } else {
244 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
245 }
246 }
247 $s .= "Output: " . var_export( $this->output, true ) . "\n";
248
249 return $s;
250 }
251}
252
253class PPFuzzUser extends User {
255
256 function load( $flags = null ) {
257 if ( $this->mDataLoaded ) {
258 return;
259 }
260 $this->mDataLoaded = true;
261 $this->loadDefaults( $this->mName );
262 }
263
264 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
265 if ( $oname === 'fancysig' ) {
266 return $this->ppfz_test->fancySig;
267 } elseif ( $oname === 'nickname' ) {
268 return $this->ppfz_test->nickname;
269 } else {
270 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
271 }
272 }
273}
274
275ini_set( 'memory_limit', '50M' );
276if ( isset( $args[0] ) ) {
277 $testText = file_get_contents( $args[0] );
278 if ( !$testText ) {
279 print "File not found\n";
280 exit( 1 );
281 }
282 $test = unserialize( $testText );
283 $result = $test->execute();
284 print "Test passed.\n";
285} else {
286 $tester = new PPFuzzTester;
287 $tester->verbose = isset( $options['verbose'] );
288 $tester->execute();
289}
serialize()
unserialize( $serialized)
$wgMaxSigChars
Maximum number of Unicode characters in signature.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
if( $line===false) $args
Definition cdb.php:64
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
PPFuzzTester $parent
static bool PPFuzzTest $currentTest
makeInputText( $max=false)
load( $flags=null)
Load the user table data for this object from the source given by mFrom.
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
loadDefaults( $name=false, $actorId=null)
Set cached properties to default.
Definition User.php:1236
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
$tester verbose
$wgHooks['BeforeParserFetchTemplateAndtitle'][]