219cc575e0817fb924b6424cabe29ebf2a68cbdd
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Additional tests for UtfNormal::cleanUp() function, inclusion
22 * regression checks for known problems.
30 if( php_sapi_name() != 'cli' ) {
31 die( "Run me from the command line please.\n" );
35 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
36 dl( 'php_utfnormal.so' );
39 #ini_set( 'memory_limit', '40M' );
41 require_once( 'PHPUnit.php' );
42 require_once( 'UtfNormal.php' );
44 class CleanUpTest
extends PHPUnit_TestCase
{
45 function CleanUpTest( $name ) {
46 $this->PHPUnit_TestCase( $name );
55 function testAscii() {
56 $text = 'This is plain ASCII text.';
57 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
61 $text = "a \x00 null";
62 $expect = "a \xef\xbf\xbd null";
65 bin2hex( UtfNormal
::cleanUp( $text ) ) );
68 function testLatin() {
69 $text = "L'\xc3\xa9cole";
70 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
73 function testLatinNormal() {
74 $text = "L'e\xcc\x81cole";
75 $expect = "L'\xc3\xa9cole";
76 $this->assertEquals( $expect, UtfNormal
::cleanUp( $text ) );
79 # This test is *very* expensive!
80 function XtestAllChars() {
81 $rep = UTF8_REPLACEMENT
;
82 global $utfCanonicalComp, $utfCanonicalDecomp;
83 for( $i = 0x0; $i < UNICODE_MAX
; $i++
) {
84 $char = codepointToUtf8( $i );
85 $clean = UtfNormal
::cleanUp( $char );
86 $x = sprintf( "%04X", $i );
87 if( $i %
0x1000 == 0 ) echo "U+$x\n";
91 ($i > 0x001f && $i < UNICODE_SURROGATE_FIRST
) ||
92 ($i > UNICODE_SURROGATE_LAST
&& $i < 0xfffe ) ||
93 ($i > 0xffff && $i <= UNICODE_MAX
) ) {
94 if( isset( $utfCanonicalComp[$char] ) ||
isset( $utfCanonicalDecomp[$char] ) ) {
95 $comp = UtfNormal
::NFC( $char );
99 "U+$x should be decomposed" );
104 "U+$x should be intact" );
107 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
112 function testAllBytes() {
113 $this->doTestBytes( '', '' );
114 $this->doTestBytes( 'x', '' );
115 $this->doTestBytes( '', 'x' );
116 $this->doTestBytes( 'x', 'x' );
119 function doTestBytes( $head, $tail ) {
120 for( $i = 0x0; $i < 256; $i++
) {
121 $char = $head . chr( $i ) . $tail;
122 $clean = UtfNormal
::cleanUp( $char );
123 $x = sprintf( "%02X", $i );
127 ($i > 0x001f && $i < 0x80) ) {
131 "ASCII byte $x should be intact" );
132 if( $char != $clean ) return;
134 $norm = $head . UTF8_REPLACEMENT
. $tail;
138 "Forbidden byte $x should be rejected" );
139 if( $norm != $clean ) return;
144 function testDoubleBytes() {
145 $this->doTestDoubleBytes( '', '' );
146 $this->doTestDoubleBytes( 'x', '' );
147 $this->doTestDoubleBytes( '', 'x' );
148 $this->doTestDoubleBytes( 'x', 'x' );
151 function doTestDoubleBytes( $head, $tail ) {
152 for( $first = 0xc0; $first < 0x100; $first++
) {
153 for( $second = 0x80; $second < 0x100; $second++
) {
154 $char = $head . chr( $first ) . chr( $second ) . $tail;
155 $clean = UtfNormal
::cleanUp( $char );
156 $x = sprintf( "%02X,%02X", $first, $second );
160 $norm = UtfNormal
::NFC( $char );
164 "Pair $x should be intact" );
165 if( $norm != $clean ) return;
166 } elseif( $first > 0xfd ||
$second > 0xbf ) {
167 # fe and ff are not legal head bytes -- expect two replacement chars
168 $norm = $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail;
172 "Forbidden pair $x should be rejected" );
173 if( $norm != $clean ) return;
175 $norm = $head . UTF8_REPLACEMENT
. $tail;
179 "Forbidden pair $x should be rejected" );
180 if( $norm != $clean ) return;
186 function testTripleBytes() {
187 $this->doTestTripleBytes( '', '' );
188 $this->doTestTripleBytes( 'x', '' );
189 $this->doTestTripleBytes( '', 'x' );
190 $this->doTestTripleBytes( 'x', 'x' );
193 function doTestTripleBytes( $head, $tail ) {
194 for( $first = 0xc0; $first < 0x100; $first++
) {
195 for( $second = 0x80; $second < 0x100; $second++
) {
196 #for( $third = 0x80; $third < 0x100; $third++ ) {
197 for( $third = 0x80; $third < 0x81; $third++
) {
198 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
199 $clean = UtfNormal
::cleanUp( $char );
200 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
201 if( $first >= 0xe0 &&
205 if( $first == 0xe0 && $second < 0xa0 ) {
207 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
209 "Overlong triplet $x should be rejected" );
210 } elseif( $first == 0xed &&
211 ( chr( $first ) . chr( $second ) . chr( $third )) >= UTF8_SURROGATE_FIRST
) {
213 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
215 "Surrogate triplet $x should be rejected" );
218 bin2hex( UtfNormal
::NFC( $char ) ),
220 "Triplet $x should be intact" );
222 } elseif( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
224 bin2hex( UtfNormal
::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT
. $tail ),
226 "Valid 2-byte $x + broken tail" );
227 } elseif( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
229 bin2hex( $head . UTF8_REPLACEMENT
. UtfNormal
::NFC( chr( $second ) . chr( $third ) . $tail ) ),
231 "Broken head + valid 2-byte $x" );
232 } elseif( ( $first > 0xfd ||
$second > 0xfd ) &&
233 ( ( $second > 0xbf && $third > 0xbf ) ||
234 ( $second < 0xc0 && $third < 0xc0 ) ||
235 ( $second > 0xfd ) ||
236 ( $third > 0xfd ) ) ) {
237 # fe and ff are not legal head bytes -- expect three replacement chars
239 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
241 "Forbidden triplet $x should be rejected" );
242 } elseif( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
244 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
246 "Forbidden triplet $x should be rejected" );
249 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
251 "Forbidden triplet $x should be rejected" );
258 function testChunkRegression() {
259 # Check for regression against a chunking bug
260 $text = "\x46\x55\xb8" .
267 $expect = "\x46\x55\xef\xbf\xbd" .
277 bin2hex( UtfNormal
::cleanUp( $text ) ) );
280 function testInterposeRegression() {
295 $expect = "\x4e\x30" .
311 bin2hex( UtfNormal
::cleanUp( $text ) ) );
314 function testOverlongRegression() {
316 "\x1a" . # forbidden ascii
318 "\xc1\xa6" . # overlong sequence
320 "\x1c" . # forbidden ascii
335 bin2hex( UtfNormal
::cleanUp( $text ) ) );
338 function testSurrogateRegression() {
339 $text = "\xed\xb4\x96" . # surrogate 0xDD16
343 $expect = "\xef\xbf\xbd" .
349 bin2hex( UtfNormal
::cleanUp( $text ) ) );
352 function testBomRegression() {
353 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
357 $expect = "\xef\xbf\xbd" .
363 bin2hex( UtfNormal
::cleanUp( $text ) ) );
366 function testForbiddenRegression() {
367 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
368 $expect = "\xef\xbf\xbd";
371 bin2hex( UtfNormal
::cleanUp( $text ) ) );
374 function testHangulRegression() {
375 $text = "\xed\x9c\xaf" . # Hangul char
376 "\xe1\x87\x81"; # followed by another final jamo
377 $expect = $text; # Should *not* change.
380 bin2hex( UtfNormal
::cleanUp( $text ) ) );
385 $suite =& new PHPUnit_TestSuite( 'CleanUpTest' );
386 $result = PHPUnit
::run( $suite );
387 echo $result->toString();
389 if( !$result->wasSuccessful() ) {