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;
48 static $currentTest = false;
51 if ( !file_exists( 'results' ) ) {
54 if ( !is_dir( 'results' ) ) {
55 echo "Unable to create 'results' directory\n";
58 $overallStart = microtime( true );
59 $reportInterval = 1000;
60 for ( $i = 1; true; $i++
) {
61 $t = -microtime( true );
63 self
::$currentTest = new PPFuzzTest( $this );
64 self
::$currentTest->execute();
66 } catch ( MWException
$e ) {
67 $testReport = self
::$currentTest->getReport();
68 $exceptionReport = $e->getText();
69 $hash = md5( $testReport );
70 file_put_contents( "results/ppft-$hash.in", serialize( self
::$currentTest ) );
71 file_put_contents( "results/ppft-$hash.fail",
72 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
73 print "Test $hash failed\n";
76 $t +
= microtime( true );
78 if ( $this->verbose
) {
79 printf( "Test $passed in %.3f seconds\n", $t );
80 print self
::$currentTest->getReport();
83 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
84 if ( $reportMetric > 25 ) {
85 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
90 } elseif ( $reportMetric < 4 ) {
91 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
97 if ( $i %
$reportInterval == 0 ) {
98 print "$i tests done\n";
100 $testReport = self::$currentTest->getReport();
101 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
102 file_put_contents( $filename, "Input:\n$testReport\n" );*/
107 function makeInputText( $max = false ) {
108 if ( $max === false ) {
109 $max = $this->maxLength
;
111 $length = mt_rand( $this->minLength
, $max );
113 for ( $i = 0; $i < $length; $i++
) {
114 $hairIndex = mt_rand( 0, count( $this->hairs
) - 1 );
115 $s .= $this->hairs
[$hairIndex];
117 // Send through the UTF-8 normaliser
118 // This resolves a few differences between the old preprocessor and the
119 // XML-based one, which doesn't like illegals and converts line endings.
120 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
122 $s = $wgContLang->normalize( $s );
126 function makeTitle() {
127 return Title
::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
131 function pickOutputType() {
132 $count = count( $this->outputTypes );
133 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
136 function pickEntryPoint() {
137 $count = count( $this->entryPoints
);
138 return $this->entryPoints
[ mt_rand( 0, $count - 1 ) ];
143 public $templates, $mainText, $title, $entryPoint, $output;
145 function __construct( $tester ) {
146 global $wgMaxSigChars;
147 $this->parent
= $tester;
148 $this->mainText
= $tester->makeInputText();
149 $this->title
= $tester->makeTitle();
150 // $this->outputType = $tester->pickOutputType();
151 $this->entryPoint
= $tester->pickEntryPoint();
152 $this->nickname
= $tester->makeInputText( $wgMaxSigChars +
10 );
153 $this->fancySig
= (bool)mt_rand( 0, 1 );
154 $this->templates
= array();
158 * @param $title Title
160 function templateHook( $title ) {
161 $titleText = $title->getPrefixedDBkey();
163 if ( !isset( $this->templates
[$titleText] ) ) {
164 $finalTitle = $title;
165 if ( count( $this->templates
) >= $this->parent
->maxTemplates
) {
166 // Too many templates
169 if ( !mt_rand( 0, 1 ) ) {
171 $finalTitle = $this->parent
->makeTitle();
173 if ( !mt_rand( 0, 5 ) ) {
177 $text = $this->parent
->makeInputText();
180 $this->templates
[$titleText] = array(
182 'finalTitle' => $finalTitle );
184 return $this->templates
[$titleText];
188 global $wgParser, $wgUser;
190 $wgUser = new PPFuzzUser
;
191 $wgUser->mName
= 'Fuzz';
192 $wgUser->mFrom
= 'name';
193 $wgUser->ppfz_test
= $this;
195 $options = ParserOptions
::newFromUser( $wgUser );
196 $options->setTemplateCallback( array( $this, 'templateHook' ) );
197 $options->setTimestamp( wfTimestampNow() );
198 $this->output
= call_user_func( array( $wgParser, $this->entryPoint
), $this->mainText
, $this->title
, $options );
199 return $this->output
;
202 function getReport() {
203 $s = "Title: " . $this->title
->getPrefixedDBkey() . "\n" .
204 // "Output type: {$this->outputType}\n" .
205 "Entry point: {$this->entryPoint}\n" .
206 "User: " . ( $this->fancySig ?
'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname
, true ) . "\n" .
207 "Main text: " . var_export( $this->mainText
, true ) . "\n";
208 foreach ( $this->templates
as $titleText => $template ) {
209 $finalTitle = $template['finalTitle'];
210 if ( $finalTitle != $titleText ) {
211 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
213 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
216 $s .= "Output: " . var_export( $this->output
, true ) . "\n";
221 class PPFuzzUser
extends User
{
222 public $ppfz_test, $mDataLoaded;
225 if ( $this->mDataLoaded
) {
228 $this->mDataLoaded
= true;
229 $this->loadDefaults( $this->mName
);
232 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
233 if ( $oname === 'fancysig' ) {
234 return $this->ppfz_test
->fancySig
;
235 } elseif ( $oname === 'nickname' ) {
236 return $this->ppfz_test
->nickname
;
238 return parent
::getOption( $oname, $defaultOverride, $ignoreHidden );
243 ini_set( 'memory_limit', '50M' );
244 if ( isset( $args[0] ) ) {
245 $testText = file_get_contents( $args[0] );
247 print "File not found\n";
250 $test = unserialize( $testText );
251 $result = $test->execute();
252 print "Test passed.\n";
254 $tester = new PPFuzzTester
;
255 $tester->verbose
= isset( $options['verbose'] );