MediaWiki  master
preprocessorFuzzTest.php
Go to the documentation of this file.
1 <?php
25 
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  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  public 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  public function makeTitle() {
136  return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
137  }
138 
139  /*
140  public function pickOutputType() {
141  $count = count( $this->outputTypes );
142  return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
143  }*/
144 
145  public function pickEntryPoint() {
146  $count = count( $this->entryPoints );
147 
148  return $this->entryPoints[mt_rand( 0, $count - 1 )];
149  }
150 }
151 
152 class PPFuzzTest {
157  public $templates;
158  public $mainText, $title, $entryPoint, $output;
159 
161  private $parent;
163  public $nickname;
165  public $fancySig;
166 
170  public function __construct( $tester ) {
171  global $wgMaxSigChars;
172  $this->parent = $tester;
173  $this->mainText = $tester->makeInputText();
174  $this->title = $tester->makeTitle();
175  // $this->outputType = $tester->pickOutputType();
176  $this->entryPoint = $tester->pickEntryPoint();
177  $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
178  $this->fancySig = (bool)mt_rand( 0, 1 );
179  $this->templates = [];
180  }
181 
186  public function templateHook( $title ) {
187  $titleText = $title->getPrefixedDBkey();
188 
189  if ( !isset( $this->templates[$titleText] ) ) {
190  $finalTitle = $title;
191  if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
192  // Too many templates
193  $text = false;
194  } else {
195  if ( !mt_rand( 0, 1 ) ) {
196  // Redirect
197  $finalTitle = $this->parent->makeTitle();
198  }
199  if ( !mt_rand( 0, 5 ) ) {
200  // Doesn't exist
201  $text = false;
202  } else {
203  $text = $this->parent->makeInputText();
204  }
205  }
206  $this->templates[$titleText] = [
207  'text' => $text,
208  'finalTitle' => $finalTitle ];
209  }
210 
211  return $this->templates[$titleText];
212  }
213 
214  public function execute() {
215  global $wgUser;
216 
217  $wgUser = new PPFuzzUser;
218  $wgUser->mName = 'Fuzz';
219  $wgUser->mFrom = 'name';
220  $wgUser->ppfz_test = $this;
221 
222  $options = ParserOptions::newFromUser( $wgUser );
223  $options->setTemplateCallback( [ $this, 'templateHook' ] );
224  $options->setTimestamp( wfTimestampNow() );
225  $this->output = call_user_func(
226  [ TestingAccessWrapper::newFromObject(
227  MediaWikiServices::getInstance()->getParser()
228  ), $this->entryPoint ],
229  $this->mainText,
230  $this->title,
231  $options
232  );
233 
234  return $this->output;
235  }
236 
237  public function getReport() {
238  $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
239 // "Output type: {$this->outputType}\n" .
240  "Entry point: {$this->entryPoint}\n" .
241  "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
242  ' ' . var_export( $this->nickname, true ) . "\n" .
243  "Main text: " . var_export( $this->mainText, true ) . "\n";
244  foreach ( $this->templates as $titleText => $template ) {
245  $finalTitle = $template['finalTitle'];
246  if ( $finalTitle != $titleText ) {
247  $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
248  } else {
249  $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
250  }
251  }
252  $s .= "Output: " . var_export( $this->output, true ) . "\n";
253 
254  return $s;
255  }
256 }
257 
258 class PPFuzzUser extends User {
259  public $ppfz_test, $mDataLoaded;
260 
261  public function load( $flags = null ) {
262  if ( $this->mDataLoaded ) {
263  return;
264  }
265  $this->mDataLoaded = true;
266  $this->loadDefaults( $this->mName );
267  }
268 
269  public function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
270  if ( $oname === 'fancysig' ) {
271  return $this->ppfz_test->fancySig;
272  } elseif ( $oname === 'nickname' ) {
273  return $this->ppfz_test->nickname;
274  } else {
275  return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
276  }
277  }
278 }
279 
280 ini_set( 'memory_limit', '50M' );
281 if ( isset( $args[0] ) ) {
282  $testText = file_get_contents( $args[0] );
283  if ( !$testText ) {
284  print "File not found\n";
285  exit( 1 );
286  }
287  $test = unserialize( $testText );
288  $result = $test->execute();
289  print "Test passed.\n";
290 } else {
291  $tester = new PPFuzzTester;
292  $tester->verbose = isset( $options['verbose'] );
293  $tester->execute();
294 }
load( $flags=null)
serialize()
$optionsWithoutArgs
$wgMaxSigChars
Maximum number of Unicode characters in signature.
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
if( $line===false) $args
Definition: mcc.php:124
array [] $templates
-var array<string,array{text:string|false,finalTitle:Title}>
static bool PPFuzzTest $currentTest
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
unserialize( $serialized)
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
static newFromUser( $user)
Get a ParserOptions object from a given user.
$wgHooks['BeforeParserFetchTemplateAndtitle'][]
$tester verbose
makeInputText( $max=false)
PPFuzzTester $parent
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317