3 require_once( dirname( __FILE__
). '/../maintenance/commandLine.inc' );
5 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
9 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
10 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
13 '|', '=', "\n", ' ', "\t", "\x7f",
14 '~~', '~~~', '~~~~', 'subst:',
15 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
16 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
19 //'<ref>', '</ref>', '<references/>',
23 var $maxTemplates = 5;
24 //var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
25 var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
27 static $currentTest = false;
30 if ( !file_exists( 'results' ) ) {
33 if ( !is_dir( 'results' ) ) {
34 echo "Unable to create 'results' directory\n";
37 $overallStart = microtime( true );
38 $reportInterval = 1000;
39 for ( $i = 1; true; $i++
) {
40 $t = -microtime( true );
42 self
::$currentTest = new PPFuzzTest( $this );
43 self
::$currentTest->execute();
45 } catch ( MWException
$e ) {
46 $testReport = self
::$currentTest->getReport();
47 $exceptionReport = $e->getText();
48 $hash = md5( $testReport );
49 file_put_contents( "results/ppft-$hash.in", serialize( self
::$currentTest ) );
50 file_put_contents( "results/ppft-$hash.fail",
51 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
52 print "Test $hash failed\n";
55 $t +
= microtime( true );
57 if ( $this->verbose
) {
58 printf( "Test $passed in %.3f seconds\n", $t );
59 print self
::$currentTest->getReport();
62 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
63 if ( $reportMetric > 25 ) {
64 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
69 } elseif ( $reportMetric < 4 ) {
70 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
76 if ( $i %
$reportInterval == 0 ) {
77 print "$i tests done\n";
79 $testReport = self::$currentTest->getReport();
80 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
81 file_put_contents( $filename, "Input:\n$testReport\n" );*/
87 function makeInputText( $max = false ) {
88 if ( $max === false ) {
89 $max = $this->maxLength
;
91 $length = mt_rand( $this->minLength
, $max );
93 for ( $i = 0; $i < $length; $i++
) {
94 $hairIndex = mt_rand( 0, count( $this->hairs
) - 1 );
95 $s .= $this->hairs
[$hairIndex];
97 // Send through the UTF-8 normaliser
98 // This resolves a few differences between the old preprocessor and the
99 // XML-based one, which doesn't like illegals and converts line endings.
100 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
101 $s = UtfNormal
::cleanUp( $s );
105 function makeTitle() {
106 return Title
::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
110 function pickOutputType() {
111 $count = count( $this->outputTypes );
112 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
115 function pickEntryPoint() {
116 $count = count( $this->entryPoints
);
117 return $this->entryPoints
[ mt_rand( 0, $count - 1 ) ];
122 var $templates, $mainText, $title, $entryPoint, $output;
124 function __construct( $tester ) {
125 global $wgMaxSigChars;
126 $this->parent
= $tester;
127 $this->mainText
= $tester->makeInputText();
128 $this->title
= $tester->makeTitle();
129 //$this->outputType = $tester->pickOutputType();
130 $this->entryPoint
= $tester->pickEntryPoint();
131 $this->nickname
= $tester->makeInputText( $wgMaxSigChars +
10);
132 $this->fancySig
= (bool)mt_rand( 0, 1 );
133 $this->templates
= array();
136 function templateHook( $title ) {
137 $titleText = $title->getPrefixedDBkey();
139 if ( !isset( $this->templates
[$titleText] ) ) {
140 $finalTitle = $title;
141 if ( count( $this->templates
) >= $this->parent
->maxTemplates
) {
142 // Too many templates
145 if ( !mt_rand( 0, 1 ) ) {
147 $finalTitle = $this->parent
->makeTitle();
149 if ( !mt_rand( 0, 5 ) ) {
153 $text = $this->parent
->makeInputText();
156 $this->templates
[$titleText] = array(
158 'finalTitle' => $finalTitle );
160 return $this->templates
[$titleText];
164 global $wgParser, $wgUser;
166 $wgUser = new PPFuzzUser
;
167 $wgUser->mName
= 'Fuzz';
168 $wgUser->mFrom
= 'name';
169 $wgUser->ppfz_test
= $this;
171 $options = new ParserOptions
;
172 $options->setTemplateCallback( array( $this, 'templateHook' ) );
173 $options->setTimestamp( wfTimestampNow() );
174 $this->output
= call_user_func( array( $wgParser, $this->entryPoint
), $this->mainText
, $this->title
->getPrefixedText(), $options );
175 return $this->output
;
178 function getReport() {
179 $s = "Title: " . $this->title
->getPrefixedDBkey() . "\n" .
180 // "Output type: {$this->outputType}\n" .
181 "Entry point: {$this->entryPoint}\n" .
182 "User: " . ( $this->fancySig ?
'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname
, true ) . "\n" .
183 "Main text: " . var_export( $this->mainText
, true ) . "\n";
184 foreach ( $this->templates
as $titleText => $template ) {
185 $finalTitle = $template['finalTitle'];
186 if ( $finalTitle != $titleText ) {
187 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
189 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
192 $s .= "Output: " . var_export( $this->output
, true ) . "\n";
197 class PPFuzzUser
extends User
{
201 if ( $this->mDataLoaded
) {
204 $this->mDataLoaded
= true;
205 $this->loadDefaults( $this->mName
);
208 function getOption( $option, $defaultOverride = '' ) {
209 if ( $option === 'fancysig' ) {
210 return $this->ppfz_test
->fancySig
;
211 } elseif ( $option === 'nickname' ) {
212 return $this->ppfz_test
->nickname
;
214 return parent
::getOption( $option, $defaultOverride );
219 ini_set( 'memory_limit', '50M' );
220 if ( isset( $args[0] ) ) {
221 $testText = file_get_contents( $args[0] );
223 print "File not found\n";
226 $test = unserialize( $testText );
227 $result = $test->execute();
228 print "Test passed.\n";
230 $tester = new PPFuzzTester
;
231 $tester->verbose
= isset( $options['verbose'] );