MediaWiki  REL1_22
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 
00040     function escaped( $string ) {
00041         $escaped = '';
00042         $length = strlen( $string );
00043         for ( $i = 0; $i < $length; $i++ ) {
00044             $char = $string[$i];
00045             $val = ord( $char );
00046             if ( $val > 127 ) {
00047                 $escaped .= '\x' . dechex( $val );
00048             } else {
00049                 $escaped .= $char;
00050             }
00051         }
00052 
00053         return $escaped;
00054     }
00055 
00061     public static function provideStringsForIsUtf8Check() {
00062         // Expected return values for StringUtils::isUtf8()
00063         $PASS = true;
00064         $FAIL = false;
00065 
00066         return array(
00067             'some ASCII' => array( $PASS, 'Some ASCII' ),
00068             'euro sign' => array( $PASS, "Euro sign €" ),
00069 
00070             'first possible sequence 1 byte' => array( $PASS, "\x00" ),
00071             'first possible sequence 2 bytes' => array( $PASS, "\xc2\x80" ),
00072             'first possible sequence 3 bytes' => array( $PASS, "\xe0\xa0\x80" ),
00073             'first possible sequence 4 bytes' => array( $PASS, "\xf0\x90\x80\x80" ),
00074             'first possible sequence 5 bytes' => array( $FAIL, "\xf8\x88\x80\x80\x80" ),
00075             'first possible sequence 6 bytes' => array( $FAIL, "\xfc\x84\x80\x80\x80\x80" ),
00076 
00077             'last possible sequence 1 byte' => array( $PASS, "\x7f" ),
00078             'last possible sequence 2 bytes' => array( $PASS, "\xdf\xbf" ),
00079             'last possible sequence 3 bytes' => array( $PASS, "\xef\xbf\xbf" ),
00080             'last possible sequence 4 bytes (U+1FFFFF)' => array( $FAIL, "\xf7\xbf\xbf\xbf" ),
00081             'last possible sequence 5 bytes' => array( $FAIL, "\xfb\xbf\xbf\xbf\xbf" ),
00082             'last possible sequence 6 bytes' => array( $FAIL, "\xfd\xbf\xbf\xbf\xbf\xbf" ),
00083 
00084             'boundary 1' => array( $PASS, "\xed\x9f\xbf" ),
00085             'boundary 2' => array( $PASS, "\xee\x80\x80" ),
00086             'boundary 3' => array( $PASS, "\xef\xbf\xbd" ),
00087             'boundary 4' => array( $PASS, "\xf2\x80\x80\x80" ),
00088             'boundary 5 (U+FFFFF)' => array( $PASS, "\xf3\xbf\xbf\xbf" ),
00089             'boundary 6 (U+100000)' => array( $PASS, "\xf4\x80\x80\x80" ),
00090             'boundary 7 (U+10FFFF)' => array( $PASS, "\xf4\x8f\xbf\xbf" ),
00091             'boundary 8 (U+110000)' => array( $FAIL, "\xf4\x90\x80\x80" ),
00092 
00093             'malformed 1' => array( $FAIL, "\x80" ),
00094             'malformed 2' => array( $FAIL, "\xbf" ),
00095             'malformed 3' => array( $FAIL, "\x80\xbf" ),
00096             'malformed 4' => array( $FAIL, "\x80\xbf\x80" ),
00097             'malformed 5' => array( $FAIL, "\x80\xbf\x80\xbf" ),
00098             'malformed 6' => array( $FAIL, "\x80\xbf\x80\xbf\x80" ),
00099             'malformed 7' => array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf" ),
00100             'malformed 8' => array( $FAIL, "\x80\xbf\x80\xbf\x80\xbf\x80" ),
00101 
00102             'last byte missing 1' => array( $FAIL, "\xc0" ),
00103             'last byte missing 2' => array( $FAIL, "\xe0\x80" ),
00104             'last byte missing 3' => array( $FAIL, "\xf0\x80\x80" ),
00105             'last byte missing 4' => array( $FAIL, "\xf8\x80\x80\x80" ),
00106             'last byte missing 5' => array( $FAIL, "\xfc\x80\x80\x80\x80" ),
00107             'last byte missing 6' => array( $FAIL, "\xdf" ),
00108             'last byte missing 7' => array( $FAIL, "\xef\xbf" ),
00109             'last byte missing 8' => array( $FAIL, "\xf7\xbf\xbf" ),
00110             'last byte missing 9' => array( $FAIL, "\xfb\xbf\xbf\xbf" ),
00111             'last byte missing 10' => array( $FAIL, "\xfd\xbf\xbf\xbf\xbf" ),
00112 
00113             'extra continuation byte 1' => array( $FAIL, "e\xaf" ),
00114             'extra continuation byte 2' => array( $FAIL, "\xc3\x89\xaf" ),
00115             'extra continuation byte 3' => array( $FAIL, "\xef\xbc\xa5\xaf" ),
00116             'extra continuation byte 4' => array( $FAIL, "\xf0\x9d\x99\xb4\xaf" ),
00117 
00118             'impossible bytes 1' => array( $FAIL, "\xfe" ),
00119             'impossible bytes 2' => array( $FAIL, "\xff" ),
00120             'impossible bytes 3' => array( $FAIL, "\xfe\xfe\xff\xff" ),
00121 
00122             'overlong sequences 1' => array( $FAIL, "\xc0\xaf" ),
00123             'overlong sequences 2' => array( $FAIL, "\xc1\xaf" ),
00124             'overlong sequences 3' => array( $FAIL, "\xe0\x80\xaf" ),
00125             'overlong sequences 4' => array( $FAIL, "\xf0\x80\x80\xaf" ),
00126             'overlong sequences 5' => array( $FAIL, "\xf8\x80\x80\x80\xaf" ),
00127             'overlong sequences 6' => array( $FAIL, "\xfc\x80\x80\x80\x80\xaf" ),
00128 
00129             'maximum overlong sequences 1' => array( $FAIL, "\xc1\xbf" ),
00130             'maximum overlong sequences 2' => array( $FAIL, "\xe0\x9f\xbf" ),
00131             'maximum overlong sequences 3' => array( $FAIL, "\xf0\x8f\xbf\xbf" ),
00132             'maximum overlong sequences 4' => array( $FAIL, "\xf8\x87\xbf\xbf" ),
00133             'maximum overlong sequences 5' => array( $FAIL, "\xfc\x83\xbf\xbf\xbf\xbf" ),
00134 
00135             'surrogates 1 (U+D799)' => array( $PASS, "\xed\x9f\xbf" ),
00136             'surrogates 2 (U+E000)' => array( $PASS, "\xee\x80\x80" ),
00137             'surrogates 3 (U+D800)' => array( $FAIL, "\xed\xa0\x80" ),
00138             'surrogates 4 (U+DBFF)' => array( $FAIL, "\xed\xaf\xbf" ),
00139             'surrogates 5 (U+DC00)' => array( $FAIL, "\xed\xb0\x80" ),
00140             'surrogates 6 (U+DFFF)' => array( $FAIL, "\xed\xbf\xbf" ),
00141             'surrogates 7 (U+D800 U+DC00)' => array( $FAIL, "\xed\xa0\x80\xed\xb0\x80" ),
00142 
00143             'noncharacters 1' => array( $PASS, "\xef\xbf\xbe" ),
00144             'noncharacters 2' => array( $PASS, "\xef\xbf\xbf" ),
00145         );
00146     }
00147 }