MediaWiki  master
preprocessorFuzzTest.php
Go to the documentation of this file.
1 <?php
25 
26 use Wikimedia\TestingAccessWrapper;
27 
28 $optionsWithoutArgs = [ 'verbose' ];
29 require_once __DIR__ . '/commandLine.inc';
30 
31 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
32 
33 class PPFuzzTester {
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  public 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  // @phan-suppress-next-line PhanInfiniteLoop
70  for ( $i = 1; true; $i++ ) {
71  $t = -microtime( true );
72  try {
73  self::$currentTest = new PPFuzzTest( $this );
74  self::$currentTest->execute();
75  $passed = 'passed';
76  } catch ( Exception $e ) {
77  $testReport = self::$currentTest->getReport();
78  $exceptionReport = $e instanceof MWException ? $e->getText() : (string)$e;
79  $hash = md5( $testReport );
80  file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
81  file_put_contents( "results/ppft-$hash.fail",
82  "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
83  print "Test $hash failed\n";
84  $passed = 'failed';
85  }
86  $t += microtime( true );
87 
88  if ( $this->verbose ) {
89  printf( "Test $passed in %.3f seconds\n", $t );
90  print self::$currentTest->getReport();
91  }
92 
93  $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
94  if ( $reportMetric > 25 ) {
95  if ( substr( $reportInterval, 0, 1 ) === '1' ) {
96  $reportInterval /= 2;
97  } else {
98  $reportInterval /= 5;
99  }
100  } elseif ( $reportMetric < 4 ) {
101  if ( substr( $reportInterval, 0, 1 ) === '1' ) {
102  $reportInterval *= 5;
103  } else {
104  $reportInterval *= 2;
105  }
106  }
107  if ( $i % $reportInterval == 0 ) {
108  print "$i tests done\n";
109  /*
110  $testReport = self::$currentTest->getReport();
111  $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
112  file_put_contents( $filename, "Input:\n$testReport\n" );*/
113  }
114  }
115  }
116 
117  public function makeInputText( $max = false ) {
118  if ( $max === false ) {
119  $max = $this->maxLength;
120  }
121  $length = mt_rand( $this->minLength, $max );
122  $s = '';
123  for ( $i = 0; $i < $length; $i++ ) {
124  $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
125  $s .= $this->hairs[$hairIndex];
126  }
127  // Send through the UTF-8 normaliser
128  // This resolves a few differences between the old preprocessor and the
129  // XML-based one, which doesn't like illegals and converts line endings.
130  // It's done by the MW UI, so it's a reasonably legitimate thing to do.
131  $s = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $s );
132 
133  return $s;
134  }
135 
136  public function makeTitle() {
137  return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
138  }
139 
140  /*
141  public function pickOutputType() {
142  $count = count( $this->outputTypes );
143  return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
144  }*/
145 
146  public function pickEntryPoint() {
147  $count = count( $this->entryPoints );
148 
149  return $this->entryPoints[mt_rand( 0, $count - 1 )];
150  }
151 }
152 
153 class PPFuzzTest {
158  public $templates;
160 
162  private $parent;
164  public $nickname;
166  public $fancySig;
167 
171  public function __construct( $tester ) {
172  global $wgMaxSigChars;
173  $this->parent = $tester;
174  $this->mainText = $tester->makeInputText();
175  $this->title = $tester->makeTitle();
176  // $this->outputType = $tester->pickOutputType();
177  $this->entryPoint = $tester->pickEntryPoint();
178  $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
179  $this->fancySig = (bool)mt_rand( 0, 1 );
180  $this->templates = [];
181  }
182 
187  public function templateHook( $title ) {
188  $titleText = $title->getPrefixedDBkey();
189 
190  if ( !isset( $this->templates[$titleText] ) ) {
191  $finalTitle = $title;
192  if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
193  // Too many templates
194  $text = false;
195  } else {
196  if ( !mt_rand( 0, 1 ) ) {
197  // Redirect
198  $finalTitle = $this->parent->makeTitle();
199  }
200  if ( !mt_rand( 0, 5 ) ) {
201  // Doesn't exist
202  $text = false;
203  } else {
204  $text = $this->parent->makeInputText();
205  }
206  }
207  $this->templates[$titleText] = [
208  'text' => $text,
209  'finalTitle' => $finalTitle ];
210  }
211 
212  return $this->templates[$titleText];
213  }
214 
215  public function execute() {
216  global $wgUser;
217 
218  $user = new PPFuzzUser;
219  $user->mName = 'Fuzz';
220  $user->mFrom = 'name';
221  $user->ppfz_test = $this;
222 
223  $wgUser = $user;
224 
225  $options = ParserOptions::newFromUser( $user );
226  $options->setTemplateCallback( [ $this, 'templateHook' ] );
227  $options->setTimestamp( wfTimestampNow() );
228  $this->output = call_user_func(
229  [ TestingAccessWrapper::newFromObject(
230  MediaWikiServices::getInstance()->getParser()
231  ), $this->entryPoint ],
232  $this->mainText,
233  $this->title,
234  $options
235  );
236 
237  return $this->output;
238  }
239 
240  public function getReport() {
241  $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
242  // "Output type: {$this->outputType}\n" .
243  "Entry point: {$this->entryPoint}\n" .
244  "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
245  ' ' . var_export( $this->nickname, true ) . "\n" .
246  "Main text: " . var_export( $this->mainText, true ) . "\n";
247  foreach ( $this->templates as $titleText => $template ) {
248  $finalTitle = $template['finalTitle'];
249  if ( $finalTitle != $titleText ) {
250  $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
251  } else {
252  $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
253  }
254  }
255  $s .= "Output: " . var_export( $this->output, true ) . "\n";
256 
257  return $s;
258  }
259 }
260 
261 class PPFuzzUser extends User {
263 
264  public function load( $flags = null ) {
265  if ( $this->mDataLoaded ) {
266  return;
267  }
268  $this->mDataLoaded = true;
269  $this->loadDefaults( $this->mName );
270  }
271 
272  public function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
273  if ( $oname === 'fancysig' ) {
274  return $this->ppfz_test->fancySig;
275  } elseif ( $oname === 'nickname' ) {
276  return $this->ppfz_test->nickname;
277  } else {
278  return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
279  }
280  }
281 }
282 
283 ini_set( 'memory_limit', '50M' );
284 if ( isset( $args[0] ) ) {
285  $testText = file_get_contents( $args[0] );
286  if ( !$testText ) {
287  print "File not found\n";
288  exit( 1 );
289  }
290  $test = unserialize( $testText );
291  $result = $test->execute();
292  print "Test passed.\n";
293 } else {
294  $tester = new PPFuzzTester;
295  $tester->verbose = isset( $options['verbose'] );
296  $tester->execute();
297 }
PPFuzzTester\$maxTemplates
$maxTemplates
Definition: preprocessorFuzzTest.php:49
PPFuzzTester\$verbose
$verbose
Definition: preprocessorFuzzTest.php:52
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:329
PPFuzzTest\$mainText
$mainText
Definition: preprocessorFuzzTest.php:159
PPFuzzTester\pickEntryPoint
pickEntryPoint()
Definition: preprocessorFuzzTest.php:146
PPFuzzTest\getReport
getReport()
Definition: preprocessorFuzzTest.php:240
PPFuzzTest\$templates
array[] $templates
-var array<string,array{text:string|false,finalTitle:Title}>
Definition: preprocessorFuzzTest.php:158
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
PPFuzzUser\$mDataLoaded
$mDataLoaded
Definition: preprocessorFuzzTest.php:262
PPFuzzTester\makeTitle
makeTitle()
Definition: preprocessorFuzzTest.php:136
PPFuzzTester\$minLength
$minLength
Definition: preprocessorFuzzTest.php:47
PPFuzzTester
Definition: preprocessorFuzzTest.php:33
PPFuzzUser\getOption
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
Definition: preprocessorFuzzTest.php:272
PPFuzzTest\execute
execute()
Definition: preprocessorFuzzTest.php:215
$s
$s
Definition: mergeMessageFileList.php:185
PPFuzzUser\$ppfz_test
$ppfz_test
Definition: preprocessorFuzzTest.php:262
serialize
serialize()
Definition: ApiMessageTrait.php:138
PPFuzzTester\$hairs
$hairs
Definition: preprocessorFuzzTest.php:34
PPFuzzTest\templateHook
templateHook( $title)
Definition: preprocessorFuzzTest.php:187
PPFuzzTester\$entryPoints
$entryPoints
Definition: preprocessorFuzzTest.php:51
MWException
MediaWiki exception.
Definition: MWException.php:26
PPFuzzTest\$fancySig
bool $fancySig
Definition: preprocessorFuzzTest.php:166
MWException\getText
getText()
Get the text to display when reporting the error on the command line.
Definition: MWException.php:138
PPFuzzTest
Definition: preprocessorFuzzTest.php:153
PPFuzzTest\$parent
PPFuzzTester $parent
Definition: preprocessorFuzzTest.php:162
User\loadDefaults
loadDefaults( $name=false, $actorId=null)
Set cached properties to default.
Definition: User.php:1154
$args
if( $line===false) $args
Definition: mcc.php:124
PPFuzzTest\$nickname
string $nickname
Definition: preprocessorFuzzTest.php:164
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1840
PPFuzzUser
Definition: preprocessorFuzzTest.php:261
$optionsWithoutArgs
$optionsWithoutArgs
Definition: preprocessorFuzzTest.php:28
PPFuzzTester\$maxLength
$maxLength
Definition: preprocessorFuzzTest.php:48
PPFuzzUser\load
load( $flags=null)
Load the user table data for this object from the source given by mFrom.
Definition: preprocessorFuzzTest.php:264
PPFuzzTest\$entryPoint
$entryPoint
Definition: preprocessorFuzzTest.php:159
PPFuzzTester\execute
execute()
Definition: preprocessorFuzzTest.php:59
PPFuzzTester\makeInputText
makeInputText( $max=false)
Definition: preprocessorFuzzTest.php:117
verbose
$tester verbose
Definition: preprocessorFuzzTest.php:295
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
PPFuzzTest\$title
$title
Definition: preprocessorFuzzTest.php:159
PPFuzzTest\__construct
__construct( $tester)
Definition: preprocessorFuzzTest.php:171
$wgMaxSigChars
$wgMaxSigChars
Maximum number of Unicode characters in signature.
Definition: DefaultSettings.php:5206
$t
$t
Definition: testCompression.php:74
$wgHooks
$wgHooks['BeforeParserFetchTemplateAndtitle'][]
Definition: preprocessorFuzzTest.php:31
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:54
PPFuzzTester\$currentTest
static bool PPFuzzTest $currentTest
Definition: preprocessorFuzzTest.php:57
PPFuzzTest\$output
$output
Definition: preprocessorFuzzTest.php:159
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:1088