https://www.mediawiki.org/wiki/Special:Code/MediaWiki/115499

Revision: 115499
Author:   maxsem
Date:     2012-06-11 18:50:31 +0000 (Mon, 11 Jun 2012)
Log Message:
-----------
Moar tests

Modified Paths:
--------------
    trunk/debs/squid/test-redirector.php

Modified: trunk/debs/squid/test-redirector.php
===================================================================
--- trunk/debs/squid/test-redirector.php        2012-06-11 14:33:12 UTC (rev 
115498)
+++ trunk/debs/squid/test-redirector.php        2012-06-11 18:50:31 UTC (rev 
115499)
@@ -7,19 +7,19 @@
 );
 
 $pipes = array();
-$proc = proc_open( './redirector', $descriptorspec, $pipes );
+$proc = proc_open( './a.out', $descriptorspec, $pipes );
 list( $stdin, $stdout, $stderr ) = $pipes;
 
 function closeall() {
-       global $stdin, $stdout, $stderr;
+       global $stdin, $stdout, $stderr, $proc;
        fclose( $stdin );
        fclose( $stdout );
        fclose( $stderr );
+       proc_close( $proc );
 }
 
 function dieout( $err ) {
        global $proc;
-       proc_close( $proc );
        closeall();
        fwrite( STDERR, "{$err}\n" );
        die( 1 );
@@ -45,6 +45,51 @@
        return $result;
 }
 
+$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+$chars .= strtolower( $chars ) . '0123456789';
+$chars = str_split( $chars );
+
+function randomElement( $array ) {
+       return $array[array_rand( $array )];
+}
+
+function randomString( $length ) {
+       global $chars;
+       $s = '';
+       for ( $i = 0; $i < $length; $i++ ) {
+               $s .= $chars[array_rand( $chars )];
+       }
+       return $s;
+}
+
+function generateUrl() {
+       switch ( mt_rand( 0, 1 ) ) {
+               case 0: // wiki URL
+                       $url = randomElement( array( 'en', 'ru', 'fr', 'de', 
'sq' ) )
+                               . '.' . randomElement( array( 'wikipedia', 
'wiktionary', 'wikinews', 'wikisource' ) )
+                               . '.org/';
+                       if ( !mt_rand( 0, 1 ) ) {
+                               $url .= 'wiki/' . randomString( mt_rand( 1, 255 
) );
+                       } else {
+                               $url .= 'w/index.php?';
+                               $count = mt_rand( 0, 10 );
+                               for ( $i = 0; $i < $count; $i++ ) {
+                                       if ( $i > 0 ) {
+                                               $url .= '&';
+                                       }
+                                       $url .= randomString( mt_rand( 0, 20 ) 
) . '=' . randomString( mt_rand( 0, 300 ) );
+                               }
+                       }
+                       break;
+               case 1:
+                       $url = randomString( mt_rand( 3, 50 ) ) . '.'
+                               . randomElement( array( 'org', 'com', 'net', 
'ru' ) )
+                               . '/' . randomString( mt_rand( 0, 3000 ) );
+                       break;
+       }
+       return "http://$url";;
+}
+
 $longUrl = 'http://en.wikipedia.org/I_am_a_ridiculous_URL_' . str_repeat( 'X', 
5000 );
 $anotherLongUrl = 'http://en.wikipedia.org/wiki/I_am_a_ridiculous_URL_' . 
str_repeat( 'X', 5000 );
 $overflow = 'http://sq.wiktionary.org/w/index.php?foo=' . str_repeat( 'X', 
100500 );
@@ -53,10 +98,20 @@
        'http://en.wikipedia.org' => 'http://en.wikipedia.org',
        'http://en.wikipedia.org/w/' => 'http://en.wikipedia.org/w/',
        'http://en.wikipedia.org/w/index.php?foo=bar' => 
'http://en.wikipedia.org/w/index.php?foo=bar',
+       'http://en.wikipedia.org/wiki/Foo' => 
'302:http://en.m.wikipedia.org/wiki/Foo',
+       'http://ru.m.wikipedia.org/wiki/Foo' => 
'http://ru.m.wikipedia.org/wiki/Foo',
+       'http://en.wiktionary.org/wiki/Foo' => 
'302:http://en.m.wiktionary.org/wiki/Foo',
+       'http://en.wikinews.org/wiki/Foo' => 
'302:http://en.m.wikinews.org/wiki/Foo',
+       'http://en.wikisource.org/wiki/Foo' => 
'302:http://en.m.wikisource.org/wiki/Foo',
+       'http://en.wikicrap.org/wiki/Foo' => 'http://en.wikicrap.org/wiki/Foo',
        $anotherLongUrl => 
"302:http://en.m.wikipedia.org/wiki/I_am_a_ridiculous_URL_"; . str_repeat( 'X', 
5000 ),
-       $overflow => substr( $overflow, 0, 8251 ),
+       'http://someothersite.com' => 'http://someothersite.com',
+       'http://someothersite.com/?foo=bar' => 
'http://someothersite.com/?foo=bar',
+       'http://someothersite.com/wiki/Foo' => 
'http://someothersite.com/wiki/Foo',
+       //$overflow => substr( $overflow, 0, 8251 ),
 );
 
+echo "Testing with preset cases...\n";
 foreach( $testCases as $input => $expected ) {
        $result = io( $input );
        if ( $result != $expected ) {
@@ -64,7 +119,18 @@
        }
 }
 
+echo "Performing fuzz testing...\n";
+
+$time = microtime( true );
+for ( $i = 0; $i < 10000; $i++ ) {
+       if ( $i % 100 == 0 ) {
+               echo "$i\n";
+       }
+       $url = generateUrl();
+       io( $url );
+}
+printf( "   done in %1.fs\n", microtime( true) - $time );
+
 closeall();
-proc_close( $proc );
 
 echo "No errors found!\n";


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to