7 require_once( dirname( __FILE__
). '/../maintenance/commandLine.inc' );
9 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
13 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
14 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
17 '|', '=', "\n", ' ', "\t", "\x7f",
18 '~~', '~~~', '~~~~', 'subst:',
19 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
20 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
23 //'<ref>', '</ref>', '<references/>',
27 var $maxTemplates = 5;
28 //var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
29 var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
31 static $currentTest = false;
34 if ( !file_exists( 'results' ) ) {
37 if ( !is_dir( 'results' ) ) {
38 echo "Unable to create 'results' directory\n";
41 $overallStart = microtime( true );
42 $reportInterval = 1000;
43 for ( $i = 1; true; $i++
) {
44 $t = -microtime( true );
46 self
::$currentTest = new PPFuzzTest( $this );
47 self
::$currentTest->execute();
49 } catch ( MWException
$e ) {
50 $testReport = self
::$currentTest->getReport();
51 $exceptionReport = $e->getText();
52 $hash = md5( $testReport );
53 file_put_contents( "results/ppft-$hash.in", serialize( self
::$currentTest ) );
54 file_put_contents( "results/ppft-$hash.fail",
55 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
56 print "Test $hash failed\n";
59 $t +
= microtime( true );
61 if ( $this->verbose
) {
62 printf( "Test $passed in %.3f seconds\n", $t );
63 print self
::$currentTest->getReport();
66 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
67 if ( $reportMetric > 25 ) {
68 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
73 } elseif ( $reportMetric < 4 ) {
74 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
80 if ( $i %
$reportInterval == 0 ) {
81 print "$i tests done\n";
83 $testReport = self::$currentTest->getReport();
84 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
85 file_put_contents( $filename, "Input:\n$testReport\n" );*/
91 function makeInputText( $max = false ) {
92 if ( $max === false ) {
93 $max = $this->maxLength
;
95 $length = mt_rand( $this->minLength
, $max );
97 for ( $i = 0; $i < $length; $i++
) {
98 $hairIndex = mt_rand( 0, count( $this->hairs
) - 1 );
99 $s .= $this->hairs
[$hairIndex];
101 // Send through the UTF-8 normaliser
102 // This resolves a few differences between the old preprocessor and the
103 // XML-based one, which doesn't like illegals and converts line endings.
104 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
105 $s = UtfNormal
::cleanUp( $s );
109 function makeTitle() {
110 return Title
::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
114 function pickOutputType() {
115 $count = count( $this->outputTypes );
116 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
119 function pickEntryPoint() {
120 $count = count( $this->entryPoints
);
121 return $this->entryPoints
[ mt_rand( 0, $count - 1 ) ];
126 var $templates, $mainText, $title, $entryPoint, $output;
128 function __construct( $tester ) {
129 global $wgMaxSigChars;
130 $this->parent
= $tester;
131 $this->mainText
= $tester->makeInputText();
132 $this->title
= $tester->makeTitle();
133 //$this->outputType = $tester->pickOutputType();
134 $this->entryPoint
= $tester->pickEntryPoint();
135 $this->nickname
= $tester->makeInputText( $wgMaxSigChars +
10);
136 $this->fancySig
= (bool)mt_rand( 0, 1 );
137 $this->templates
= array();
140 function templateHook( $title ) {
141 $titleText = $title->getPrefixedDBkey();
143 if ( !isset( $this->templates
[$titleText] ) ) {
144 $finalTitle = $title;
145 if ( count( $this->templates
) >= $this->parent
->maxTemplates
) {
146 // Too many templates
149 if ( !mt_rand( 0, 1 ) ) {
151 $finalTitle = $this->parent
->makeTitle();
153 if ( !mt_rand( 0, 5 ) ) {
157 $text = $this->parent
->makeInputText();
160 $this->templates
[$titleText] = array(
162 'finalTitle' => $finalTitle );
164 return $this->templates
[$titleText];
168 global $wgParser, $wgUser;
170 $wgUser = new PPFuzzUser
;
171 $wgUser->mName
= 'Fuzz';
172 $wgUser->mFrom
= 'name';
173 $wgUser->ppfz_test
= $this;
175 $options = new ParserOptions
;
176 $options->setTemplateCallback( array( $this, 'templateHook' ) );
177 $options->setTimestamp( wfTimestampNow() );
178 $this->output
= call_user_func( array( $wgParser, $this->entryPoint
), $this->mainText
, $this->title
->getPrefixedText(), $options );
179 return $this->output
;
182 function getReport() {
183 $s = "Title: " . $this->title
->getPrefixedDBkey() . "\n" .
184 // "Output type: {$this->outputType}\n" .
185 "Entry point: {$this->entryPoint}\n" .
186 "User: " . ( $this->fancySig ?
'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname
, true ) . "\n" .
187 "Main text: " . var_export( $this->mainText
, true ) . "\n";
188 foreach ( $this->templates
as $titleText => $template ) {
189 $finalTitle = $template['finalTitle'];
190 if ( $finalTitle != $titleText ) {
191 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
193 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
196 $s .= "Output: " . var_export( $this->output
, true ) . "\n";
201 class PPFuzzUser
extends User
{
205 if ( $this->mDataLoaded
) {
208 $this->mDataLoaded
= true;
209 $this->loadDefaults( $this->mName
);
212 function getOption( $option, $defaultOverride = '' ) {
213 if ( $option === 'fancysig' ) {
214 return $this->ppfz_test
->fancySig
;
215 } elseif ( $option === 'nickname' ) {
216 return $this->ppfz_test
->nickname
;
218 return parent
::getOption( $option, $defaultOverride );
223 ini_set( 'memory_limit', '50M' );
224 if ( isset( $args[0] ) ) {
225 $testText = file_get_contents( $args[0] );
227 print "File not found\n";
230 $test = unserialize( $testText );
231 $result = $test->execute();
232 print "Test passed.\n";
234 $tester = new PPFuzzTester
;
235 $tester->verbose
= isset( $options['verbose'] );