MediaWiki  REL1_24
StringUtilsTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class StringUtilsTest extends MediaWikiTestCase {
00004 
00012     public function testIsUtf8WithMbstring( $expected, $string ) {
00013         if ( !function_exists( 'mb_check_encoding' ) ) {
00014             $this->markTestSkipped( 'Test requires the mbstring PHP extension' );
00015         }
00016         $this->assertEquals( $expected,
00017             StringUtils::isUtf8( $string ),
00018             'Testing string "' . $this->escaped( $string ) . '" with mb_check_encoding'
00019         );
00020     }
00021 
00030     public function testIsUtf8WithPhpFallbackImplementation( $expected, $string ) {
00031         $this->assertEquals( $expected,
00032             StringUtils::isUtf8( $string, true ),
00033             'Testing string "' . $this->escaped( $string ) . '" with pure PHP implementation'
00034         );
00035     }
00036 
00042     function escaped( $string ) {
00043         $escaped = '';
00044         $length = strlen( $string );
00045         for ( $i = 0; $i < $length; $i++ ) {
00046             $char = $string[$i];
00047             $val = ord( $char );
00048             if ( $val > 127 ) {
00049                 $escaped .= '\x' . dechex( $val );
00050             } else {
00051                 $escaped .= $char;
00052             }
00053         }
00054 
00055         return $escaped;
00056     }
00057 
00063     public static function provideStringsForIsUtf8Check() {
00064         // Expected return values for StringUtils::isUtf8()
00065         $PASS = true;
00066         $FAIL = false;
00067 
00068         return array(
00069             'some ASCII' => array( $PASS, 'Some ASCII' ),
00070             'euro sign' => array( $PASS, "Euro sign €" ),
00071 
00072             'first possible sequence 1 byte' => array( $PASS, "\x00" ),
00073             'first possible sequence 2 bytes' => array( $PASS, "\xc2\x80" ),
00074             'first possible sequence 3 bytes' => array( $PASS, "\xe0\xa0\x80" ),
00075             'first possible sequence 4 bytes' => array( $PASS, "\xf0\x90\x80\x80" ),
00076             'first possible sequence 5 bytes' => array( $FAIL, "\xf8\x88\x80\x80\x80" ),
00077             'first possible sequence 6 bytes' => array( $FAIL, "\xfc\x84\x80\x80\x80\x80" ),
00078 
00079             'last possible sequence 1 byte' => array( $PASS, "\x7f" ),
00080             'last possible sequence 2 bytes' => array( $PASS, "\xdf\xbf" ),
00081             'last possible sequence 3 bytes' => array( $PASS, "\xef\xbf\xbf" ),
00082             'last possible sequence 4 bytes (U+1FFFFF)' => array( $FAIL, "\xf7\xbf\xbf\xbf" ),
00083             'last possible sequence 5 bytes' => array( $FAIL, "\xfb\xbf\xbf\xbf\xbf" ),
00084             'last possible sequence 6 bytes' => array( $FAIL, "\xfd\xbf\xbf\xbf\xbf\xbf" ),
00085 
00086             'boundary 1' => array( $PASS, "\xed\x9f\xbf" ),
00087             'boundary 2' => array( $PASS, "\xee\x80\x80" ),
00088             'boundary 3' => array( $PASS, "\xef\xbf\xbd" ),
00089             'boundary 4' => array( $PASS, "\xf2\x80\x80\x80" ),
00090             'boundary 5 (U+FFFFF)' => array( $PASS, "\xf3\xbf\xbf\xbf" ),
00091             'boundary 6 (U+100000)' => array( $PASS, "\xf4\x80\x80\x80" ),
00092             'boundary 7 (U+10FFFF)' => array( $PASS, "\xf4\x8f\xbf\xbf" ),
00093             'boundary 8 (U+110000)' => array( $FAIL, "\xf4\x90\x80\x80" ),
00094 
00095             'malformed 1' => array( $FAIL, "\x80" ),
00096             'malformed 2' => array( $FAIL, "\xbf" ),
00097             'malformed 3' => array( $FAIL, "\x80\xbf" ),
00098             'malformed 4' => array( $FAIL, "\x80\xbf\x80" ),
00099             'malformed 5' => array( $FAIL, "\x80\xbf\x80\xbf" ),
00100             'malformed 6' => array( $FAIL, "\x80\xbf\x80\xbf\x80" ),
00101             'malformed 7' => array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf" ),
00102             'malformed 8' => array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf\x80" ),
00103 
00104             'last byte missing 1' => array( $FAIL, "\xc0" ),
00105             'last byte missing 2' => array( $FAIL, "\xe0\x80" ),
00106             'last byte missing 3' => array( $FAIL, "\xf0\x80\x80" ),
00107             'last byte missing 4' => array( $FAIL, "\xf8\x80\x80\x80" ),
00108             'last byte missing 5' => array( $FAIL, "\xfc\x80\x80\x80\x80" ),
00109             'last byte missing 6' => array( $FAIL, "\xdf" ),
00110             'last byte missing 7' => array( $FAIL, "\xef\xbf" ),
00111             'last byte missing 8' => array( $FAIL, "\xf7\xbf\xbf" ),
00112             'last byte missing 9' => array( $FAIL, "\xfb\xbf\xbf\xbf" ),
00113             'last byte missing 10' => array( $FAIL, "\xfd\xbf\xbf\xbf\xbf" ),
00114 
00115             'extra continuation byte 1' => array( $FAIL, "e\xaf" ),
00116             'extra continuation byte 2' => array( $FAIL, "\xc3\x89\xaf" ),
00117             'extra continuation byte 3' => array( $FAIL, "\xef\xbc\xa5\xaf" ),
00118             'extra continuation byte 4' => array( $FAIL, "\xf0\x9d\x99\xb4\xaf" ),
00119 
00120             'impossible bytes 1' => array( $FAIL, "\xfe" ),
00121             'impossible bytes 2' => array( $FAIL, "\xff" ),
00122             'impossible bytes 3' => array( $FAIL, "\xfe\xfe\xff\xff" ),
00123 
00124             'overlong sequences 1' => array( $FAIL, "\xc0\xaf" ),
00125             'overlong sequences 2' => array( $FAIL, "\xc1\xaf" ),
00126             'overlong sequences 3' => array( $FAIL, "\xe0\x80\xaf" ),
00127             'overlong sequences 4' => array( $FAIL, "\xf0\x80\x80\xaf" ),
00128             'overlong sequences 5' => array( $FAIL, "\xf8\x80\x80\x80\xaf" ),
00129             'overlong sequences 6' => array( $FAIL, "\xfc\x80\x80\x80\x80\xaf" ),
00130 
00131             'maximum overlong sequences 1' => array( $FAIL, "\xc1\xbf" ),
00132             'maximum overlong sequences 2' => array( $FAIL, "\xe0\x9f\xbf" ),
00133             'maximum overlong sequences 3' => array( $FAIL, "\xf0\x8f\xbf\xbf" ),
00134             'maximum overlong sequences 4' => array( $FAIL, "\xf8\x87\xbf\xbf" ),
00135             'maximum overlong sequences 5' => array( $FAIL, "\xfc\x83\xbf\xbf\xbf\xbf" ),
00136 
00137             'surrogates 1 (U+D799)' => array( $PASS, "\xed\x9f\xbf" ),
00138             'surrogates 2 (U+E000)' => array( $PASS, "\xee\x80\x80" ),
00139             'surrogates 3 (U+D800)' => array( $FAIL, "\xed\xa0\x80" ),
00140             'surrogates 4 (U+DBFF)' => array( $FAIL, "\xed\xaf\xbf" ),
00141             'surrogates 5 (U+DC00)' => array( $FAIL, "\xed\xb0\x80" ),
00142             'surrogates 6 (U+DFFF)' => array( $FAIL, "\xed\xbf\xbf" ),
00143             'surrogates 7 (U+D800 U+DC00)' => array( $FAIL, "\xed\xa0\x80\xed\xb0\x80" ),
00144 
00145             'noncharacters 1' => array( $PASS, "\xef\xbf\xbe" ),
00146             'noncharacters 2' => array( $PASS, "\xef\xbf\xbf" ),
00147         );
00148     }
00149 }