Postcard from linuxland.
[lhc/web/wiklou.git] / maintenance / preprocessorFuzzTest.php
1 <?php
2
3 require_once( dirname( __FILE__ ). '/../maintenance/commandLine.inc' );
4
5 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
6
7 class PPFuzzTester {
8 var $hairs = array(
9 '[[', ']]', '{{', '}}', '{{{', '}}}',
10 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
11 '<!--' , '-->',
12 "\n==", "==\n",
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',
17
18 // extensions
19 //'<ref>', '</ref>', '<references/>',
20 );
21 var $minLength = 0;
22 var $maxLength = 20;
23 var $maxTemplates = 5;
24 //var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
25 var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
26 static $currentTest = false;
27
28 function execute() {
29 if ( !file_exists( 'results' ) ) {
30 mkdir( 'results' );
31 }
32 if ( !is_dir( 'results' ) ) {
33 echo "Unable to create 'results' directory\n";
34 exit( 1 );
35 }
36 for ( $i = 0; true; $i++ ) {
37 try {
38 self::$currentTest = new PPFuzzTest( $this );
39 self::$currentTest->execute();
40 } catch ( MWException $e ) {
41 $testReport = self::$currentTest->getReport();
42 $exceptionReport = $e->getText();
43 $hash = md5( $testReport );
44 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
45 file_put_contents( "results/ppft-$hash.fail",
46 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
47 print "Test $hash failed\n";
48 }
49 if ( $i % 1000 == 0 ) {
50 print "$i tests done\n";
51 /*
52 $testReport = self::$currentTest->getReport();
53 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
54 file_put_contents( $filename, "Input:\n$testReport\n" );*/
55 }
56 }
57 }
58
59 function makeInputText() {
60 $length = mt_rand( $this->minLength, $this->maxLength );
61 $s = '';
62 for ( $i = 0; $i < $length; $i++ ) {
63 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
64 $s .= $this->hairs[$hairIndex];
65 }
66 // Send through the UTF-8 normaliser
67 // This resolves a few differences between the old preprocessor and the
68 // XML-based one, which doesn't like illegals and converts line endings.
69 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
70 $s = UtfNormal::cleanUp( $s );
71 return $s;
72 }
73
74 function makeTitle() {
75 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
76 }
77
78 /*
79 function pickOutputType() {
80 $count = count( $this->outputTypes );
81 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
82 }*/
83
84 function pickEntryPoint() {
85 $count = count( $this->entryPoints );
86 return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
87 }
88 }
89
90 class PPFuzzTest {
91 var $templates, $mainText, $title, $entryPoint;
92
93 function __construct( $tester ) {
94 $this->parent = $tester;
95 $this->mainText = $tester->makeInputText();
96 $this->title = $tester->makeTitle();
97 //$this->outputType = $tester->pickOutputType();
98 $this->entryPoint = $tester->pickEntryPoint();
99 $this->nickname = $tester->makeInputText();
100 $this->fancySig = (bool)mt_rand( 0, 1 );
101 $this->templates = array();
102 }
103
104 function templateHook( $title ) {
105 $titleText = $title->getPrefixedDBkey();
106
107 if ( !isset( $this->templates[$titleText] ) ) {
108 $finalTitle = $title;
109 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
110 // Too many templates
111 $text = false;
112 } else {
113 if ( !mt_rand( 0, 1 ) ) {
114 // Redirect
115 $finalTitle = $this->parent->makeTitle();
116 }
117 if ( !mt_rand( 0, 5 ) ) {
118 // Doesn't exist
119 $text = false;
120 } else {
121 $text = $this->parent->makeInputText();
122 }
123 }
124 $this->templates[$titleText] = array(
125 'text' => $text,
126 'finalTitle' => $finalTitle );
127 }
128 return $this->templates[$titleText];
129 }
130
131 function execute() {
132 global $wgParser, $wgUser;
133
134 $wgUser = new PPFuzzUser;
135 $wgUser->mName = 'Fuzz';
136 $wgUser->mFrom = 'name';
137 $wgUser->ppfz_test = $this;
138
139 $options = new ParserOptions;
140 $options->setTemplateCallback( array( $this, 'templateHook' ) );
141 //$wgParser->startExternalParse( $this->title, $options, constant( $this->outputType ) );
142 return call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title, $options );
143 }
144
145 function getReport() {
146 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
147 // "Output type: {$this->outputType}\n" .
148 "Entry point: {$this->entryPoint}\n" .
149 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
150 "Main text: " . var_export( $this->mainText, true ) . "\n";
151 foreach ( $this->templates as $titleText => $template ) {
152 $finalTitle = $template['finalTitle'];
153 if ( $finalTitle != $titleText ) {
154 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
155 } else {
156 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
157 }
158 }
159 return $s;
160 }
161 }
162
163 class PPFuzzUser extends User {
164 var $ppfz_test;
165
166 function getOption( $option, $defaultOverride = '' ) {
167 if ( $option === 'fancysig' ) {
168 return $this->ppfz_test->fancySig;
169 } elseif ( $option === 'nickname' ) {
170 return $this->ppfz_test->nickname;
171 } else {
172 return parent::getOption( $option, $defaultOverride );
173 }
174 }
175 }
176
177 ini_set( 'memory_limit', '50M' );
178 if ( isset( $args[0] ) ) {
179 $testText = file_get_contents( $args[0] );
180 if ( !$testText ) {
181 print "File not found\n";
182 exit( 1 );
183 }
184 $test = unserialize( $testText );
185 print $test->getReport();
186 $result = $test->execute();
187 print "Test passed.\nResult: $result\n";
188 } else {
189 $tester = new PPFuzzTester;
190 $tester->execute();
191 }