00001 <?php
00007 require_once( dirname(__FILE__) . '/commandLine.inc' );
00008
00009 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
00010
00011 class PPFuzzTester {
00012 var $hairs = array(
00013 '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
00014 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
00015 '<!--' , '-->',
00016 "\n==", "==\n",
00017 '|', '=', "\n", ' ', "\t", "\x7f",
00018 '~~', '~~~', '~~~~', 'subst:',
00019 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
00020 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
00021
00022
00023
00024 );
00025 var $minLength = 0;
00026 var $maxLength = 20;
00027 var $maxTemplates = 5;
00028
00029 var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
00030 var $verbose = false;
00031 static $currentTest = false;
00032
00033 function execute() {
00034 if ( !file_exists( 'results' ) ) {
00035 mkdir( 'results' );
00036 }
00037 if ( !is_dir( 'results' ) ) {
00038 echo "Unable to create 'results' directory\n";
00039 exit( 1 );
00040 }
00041 $overallStart = microtime( true );
00042 $reportInterval = 1000;
00043 for ( $i = 1; true; $i++ ) {
00044 $t = -microtime( true );
00045 try {
00046 self::$currentTest = new PPFuzzTest( $this );
00047 self::$currentTest->execute();
00048 $passed = 'passed';
00049 } catch ( MWException $e ) {
00050 $testReport = self::$currentTest->getReport();
00051 $exceptionReport = $e->getText();
00052 $hash = md5( $testReport );
00053 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
00054 file_put_contents( "results/ppft-$hash.fail",
00055 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
00056 print "Test $hash failed\n";
00057 $passed = 'failed';
00058 }
00059 $t += microtime( true );
00060
00061 if ( $this->verbose ) {
00062 printf( "Test $passed in %.3f seconds\n", $t );
00063 print self::$currentTest->getReport();
00064 }
00065
00066 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
00067 if ( $reportMetric > 25 ) {
00068 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
00069 $reportInterval /= 2;
00070 } else {
00071 $reportInterval /= 5;
00072 }
00073 } elseif ( $reportMetric < 4 ) {
00074 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
00075 $reportInterval *= 5;
00076 } else {
00077 $reportInterval *= 2;
00078 }
00079 }
00080 if ( $i % $reportInterval == 0 ) {
00081 print "$i tests done\n";
00082
00083
00084
00085
00086 }
00087 }
00088 wfLogProfilingData();
00089 }
00090
00091 function makeInputText( $max = false ) {
00092 if ( $max === false ) {
00093 $max = $this->maxLength;
00094 }
00095 $length = mt_rand( $this->minLength, $max );
00096 $s = '';
00097 for ( $i = 0; $i < $length; $i++ ) {
00098 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
00099 $s .= $this->hairs[$hairIndex];
00100 }
00101
00102
00103
00104
00105 global $wgContLang;
00106 $s = $wgContLang->normalize( $s );
00107 return $s;
00108 }
00109
00110 function makeTitle() {
00111 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 function pickEntryPoint() {
00121 $count = count( $this->entryPoints );
00122 return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
00123 }
00124 }
00125
00126 class PPFuzzTest {
00127 var $templates, $mainText, $title, $entryPoint, $output;
00128
00129 function __construct( $tester ) {
00130 global $wgMaxSigChars;
00131 $this->parent = $tester;
00132 $this->mainText = $tester->makeInputText();
00133 $this->title = $tester->makeTitle();
00134
00135 $this->entryPoint = $tester->pickEntryPoint();
00136 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10);
00137 $this->fancySig = (bool)mt_rand( 0, 1 );
00138 $this->templates = array();
00139 }
00140
00141 function templateHook( $title ) {
00142 $titleText = $title->getPrefixedDBkey();
00143
00144 if ( !isset( $this->templates[$titleText] ) ) {
00145 $finalTitle = $title;
00146 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
00147
00148 $text = false;
00149 } else {
00150 if ( !mt_rand( 0, 1 ) ) {
00151
00152 $finalTitle = $this->parent->makeTitle();
00153 }
00154 if ( !mt_rand( 0, 5 ) ) {
00155
00156 $text = false;
00157 } else {
00158 $text = $this->parent->makeInputText();
00159 }
00160 }
00161 $this->templates[$titleText] = array(
00162 'text' => $text,
00163 'finalTitle' => $finalTitle );
00164 }
00165 return $this->templates[$titleText];
00166 }
00167
00168 function execute() {
00169 global $wgParser, $wgUser;
00170
00171 $wgUser = new PPFuzzUser;
00172 $wgUser->mName = 'Fuzz';
00173 $wgUser->mFrom = 'name';
00174 $wgUser->ppfz_test = $this;
00175
00176 $options = new ParserOptions;
00177 $options->setTemplateCallback( array( $this, 'templateHook' ) );
00178 $options->setTimestamp( wfTimestampNow() );
00179 $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title->getPrefixedText(), $options );
00180 return $this->output;
00181 }
00182
00183 function getReport() {
00184 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
00185
00186 "Entry point: {$this->entryPoint}\n" .
00187 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
00188 "Main text: " . var_export( $this->mainText, true ) . "\n";
00189 foreach ( $this->templates as $titleText => $template ) {
00190 $finalTitle = $template['finalTitle'];
00191 if ( $finalTitle != $titleText ) {
00192 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
00193 } else {
00194 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
00195 }
00196 }
00197 $s .= "Output: " . var_export( $this->output, true ) . "\n";
00198 return $s;
00199 }
00200 }
00201
00202 class PPFuzzUser extends User {
00203 var $ppfz_test;
00204
00205 function load() {
00206 if ( $this->mDataLoaded ) {
00207 return;
00208 }
00209 $this->mDataLoaded = true;
00210 $this->loadDefaults( $this->mName );
00211 }
00212
00213 function getOption( $option, $defaultOverride = '' ) {
00214 if ( $option === 'fancysig' ) {
00215 return $this->ppfz_test->fancySig;
00216 } elseif ( $option === 'nickname' ) {
00217 return $this->ppfz_test->nickname;
00218 } else {
00219 return parent::getOption( $option, $defaultOverride );
00220 }
00221 }
00222 }
00223
00224 ini_set( 'memory_limit', '50M' );
00225 if ( isset( $args[0] ) ) {
00226 $testText = file_get_contents( $args[0] );
00227 if ( !$testText ) {
00228 print "File not found\n";
00229 exit( 1 );
00230 }
00231 $test = unserialize( $testText );
00232 $result = $test->execute();
00233 print "Test passed.\n";
00234 } else {
00235 $tester = new PPFuzzTester;
00236 $tester->verbose = isset( $options['verbose'] );
00237 $tester->execute();
00238 }