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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/commandLine.inc';
26 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
29 public $hairs = array(
30 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
31 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
34 '|', '=', "\n", ' ', "\t", "\x7f",
35 '~~', '~~~', '~~~~', 'subst:',
36 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
37 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
40 // '<ref>', '</ref>', '<references/>',
42 public $minLength = 0;
43 public $maxLength = 20;
44 public $maxTemplates = 5;
45 // public $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
46 public $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
47 public $verbose = false;
49 private static $currentTest = false;
52 if ( !file_exists( 'results' ) ) {
55 if ( !is_dir( 'results' ) ) {
56 echo "Unable to create 'results' directory\n";
59 $overallStart = microtime( true );
60 $reportInterval = 1000;
61 for ( $i = 1; true; $i++
) {
62 $t = -microtime( true );
64 self
::$currentTest = new PPFuzzTest( $this );
65 self
::$currentTest->execute();
67 } catch ( MWException
$e ) {
68 $testReport = self
::$currentTest->getReport();
69 $exceptionReport = $e->getText();
70 $hash = md5( $testReport );
71 file_put_contents( "results/ppft-$hash.in", serialize( self
::$currentTest ) );
72 file_put_contents( "results/ppft-$hash.fail",
73 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
74 print "Test $hash failed\n";
77 $t +
= microtime( true );
79 if ( $this->verbose
) {
80 printf( "Test $passed in %.3f seconds\n", $t );
81 print self
::$currentTest->getReport();
84 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
85 if ( $reportMetric > 25 ) {
86 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
91 } elseif ( $reportMetric < 4 ) {
92 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
98 if ( $i %
$reportInterval == 0 ) {
99 print "$i tests done\n";
101 $testReport = self::$currentTest->getReport();
102 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
103 file_put_contents( $filename, "Input:\n$testReport\n" );*/
108 function makeInputText( $max = false ) {
109 if ( $max === false ) {
110 $max = $this->maxLength
;
112 $length = mt_rand( $this->minLength
, $max );
114 for ( $i = 0; $i < $length; $i++
) {
115 $hairIndex = mt_rand( 0, count( $this->hairs
) - 1 );
116 $s .= $this->hairs
[$hairIndex];
118 // Send through the UTF-8 normaliser
119 // This resolves a few differences between the old preprocessor and the
120 // XML-based one, which doesn't like illegals and converts line endings.
121 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
123 $s = $wgContLang->normalize( $s );
128 function makeTitle() {
129 return Title
::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
133 function pickOutputType() {
134 $count = count( $this->outputTypes );
135 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
138 function pickEntryPoint() {
139 $count = count( $this->entryPoints
);
141 return $this->entryPoints
[mt_rand( 0, $count - 1 )];
146 public $templates, $mainText, $title, $entryPoint, $output;
148 function __construct( $tester ) {
149 global $wgMaxSigChars;
150 $this->parent
= $tester;
151 $this->mainText
= $tester->makeInputText();
152 $this->title
= $tester->makeTitle();
153 // $this->outputType = $tester->pickOutputType();
154 $this->entryPoint
= $tester->pickEntryPoint();
155 $this->nickname
= $tester->makeInputText( $wgMaxSigChars +
10 );
156 $this->fancySig
= (bool)mt_rand( 0, 1 );
157 $this->templates
= array();
161 * @param Title $title
163 function templateHook( $title ) {
164 $titleText = $title->getPrefixedDBkey();
166 if ( !isset( $this->templates
[$titleText] ) ) {
167 $finalTitle = $title;
168 if ( count( $this->templates
) >= $this->parent
->maxTemplates
) {
169 // Too many templates
172 if ( !mt_rand( 0, 1 ) ) {
174 $finalTitle = $this->parent
->makeTitle();
176 if ( !mt_rand( 0, 5 ) ) {
180 $text = $this->parent
->makeInputText();
183 $this->templates
[$titleText] = array(
185 'finalTitle' => $finalTitle );
188 return $this->templates
[$titleText];
192 global $wgParser, $wgUser;
194 $wgUser = new PPFuzzUser
;
195 $wgUser->mName
= 'Fuzz';
196 $wgUser->mFrom
= 'name';
197 $wgUser->ppfz_test
= $this;
199 $options = ParserOptions
::newFromUser( $wgUser );
200 $options->setTemplateCallback( array( $this, 'templateHook' ) );
201 $options->setTimestamp( wfTimestampNow() );
202 $this->output
= call_user_func(
203 array( $wgParser, $this->entryPoint
),
209 return $this->output
;
212 function getReport() {
213 $s = "Title: " . $this->title
->getPrefixedDBkey() . "\n" .
214 // "Output type: {$this->outputType}\n" .
215 "Entry point: {$this->entryPoint}\n" .
216 "User: " . ( $this->fancySig ?
'fancy' : 'no-fancy' ) .
217 ' ' . var_export( $this->nickname
, true ) . "\n" .
218 "Main text: " . var_export( $this->mainText
, true ) . "\n";
219 foreach ( $this->templates
as $titleText => $template ) {
220 $finalTitle = $template['finalTitle'];
221 if ( $finalTitle != $titleText ) {
222 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
224 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
227 $s .= "Output: " . var_export( $this->output
, true ) . "\n";
233 class PPFuzzUser
extends User
{
234 public $ppfz_test, $mDataLoaded;
237 if ( $this->mDataLoaded
) {
240 $this->mDataLoaded
= true;
241 $this->loadDefaults( $this->mName
);
244 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
245 if ( $oname === 'fancysig' ) {
246 return $this->ppfz_test
->fancySig
;
247 } elseif ( $oname === 'nickname' ) {
248 return $this->ppfz_test
->nickname
;
250 return parent
::getOption( $oname, $defaultOverride, $ignoreHidden );
255 ini_set( 'memory_limit', '50M' );
256 if ( isset( $args[0] ) ) {
257 $testText = file_get_contents( $args[0] );
259 print "File not found\n";
262 $test = unserialize( $testText );
263 $result = $test->execute();
264 print "Test passed.\n";
266 $tester = new PPFuzzTester
;
267 $tester->verbose
= isset( $options['verbose'] );