00001 <?php
00007 require_once( dirname( __FILE__ ). '/../maintenance/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 $s = UtfNormal::cleanUp( $s );
00106 return $s;
00107 }
00108
00109 function makeTitle() {
00110 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 function pickEntryPoint() {
00120 $count = count( $this->entryPoints );
00121 return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
00122 }
00123 }
00124
00125 class PPFuzzTest {
00126 var $templates, $mainText, $title, $entryPoint, $output;
00127
00128 function __construct( $tester ) {
00129 global $wgMaxSigChars;
00130 $this->parent = $tester;
00131 $this->mainText = $tester->makeInputText();
00132 $this->title = $tester->makeTitle();
00133
00134 $this->entryPoint = $tester->pickEntryPoint();
00135 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10);
00136 $this->fancySig = (bool)mt_rand( 0, 1 );
00137 $this->templates = array();
00138 }
00139
00140 function templateHook( $title ) {
00141 $titleText = $title->getPrefixedDBkey();
00142
00143 if ( !isset( $this->templates[$titleText] ) ) {
00144 $finalTitle = $title;
00145 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
00146
00147 $text = false;
00148 } else {
00149 if ( !mt_rand( 0, 1 ) ) {
00150
00151 $finalTitle = $this->parent->makeTitle();
00152 }
00153 if ( !mt_rand( 0, 5 ) ) {
00154
00155 $text = false;
00156 } else {
00157 $text = $this->parent->makeInputText();
00158 }
00159 }
00160 $this->templates[$titleText] = array(
00161 'text' => $text,
00162 'finalTitle' => $finalTitle );
00163 }
00164 return $this->templates[$titleText];
00165 }
00166
00167 function execute() {
00168 global $wgParser, $wgUser;
00169
00170 $wgUser = new PPFuzzUser;
00171 $wgUser->mName = 'Fuzz';
00172 $wgUser->mFrom = 'name';
00173 $wgUser->ppfz_test = $this;
00174
00175 $options = new ParserOptions;
00176 $options->setTemplateCallback( array( $this, 'templateHook' ) );
00177 $options->setTimestamp( wfTimestampNow() );
00178 $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title->getPrefixedText(), $options );
00179 return $this->output;
00180 }
00181
00182 function getReport() {
00183 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
00184
00185 "Entry point: {$this->entryPoint}\n" .
00186 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
00187 "Main text: " . var_export( $this->mainText, true ) . "\n";
00188 foreach ( $this->templates as $titleText => $template ) {
00189 $finalTitle = $template['finalTitle'];
00190 if ( $finalTitle != $titleText ) {
00191 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
00192 } else {
00193 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
00194 }
00195 }
00196 $s .= "Output: " . var_export( $this->output, true ) . "\n";
00197 return $s;
00198 }
00199 }
00200
00201 class PPFuzzUser extends User {
00202 var $ppfz_test;
00203
00204 function load() {
00205 if ( $this->mDataLoaded ) {
00206 return;
00207 }
00208 $this->mDataLoaded = true;
00209 $this->loadDefaults( $this->mName );
00210 }
00211
00212 function getOption( $option, $defaultOverride = '' ) {
00213 if ( $option === 'fancysig' ) {
00214 return $this->ppfz_test->fancySig;
00215 } elseif ( $option === 'nickname' ) {
00216 return $this->ppfz_test->nickname;
00217 } else {
00218 return parent::getOption( $option, $defaultOverride );
00219 }
00220 }
00221 }
00222
00223 ini_set( 'memory_limit', '50M' );
00224 if ( isset( $args[0] ) ) {
00225 $testText = file_get_contents( $args[0] );
00226 if ( !$testText ) {
00227 print "File not found\n";
00228 exit( 1 );
00229 }
00230 $test = unserialize( $testText );
00231 $result = $test->execute();
00232 print "Test passed.\n";
00233 } else {
00234 $tester = new PPFuzzTester;
00235 $tester->verbose = isset( $options['verbose'] );
00236 $tester->execute();
00237 }