3 * Tests for UtfNormal::cleanUp() function.
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
27 * Additional tests for UtfNormal::cleanUp() function, inclusion
28 * regression checks for known problems.
33 class CleanUpTest
extends MediaWikiTestCase
{
35 function testAscii() {
36 $text = 'This is plain ASCII text.';
37 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
42 $text = "a \x00 null";
43 $expect = "a \xef\xbf\xbd null";
46 bin2hex( UtfNormal
::cleanUp( $text ) ) );
50 function testLatin() {
51 $text = "L'\xc3\xa9cole";
52 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
56 function testLatinNormal() {
57 $text = "L'e\xcc\x81cole";
58 $expect = "L'\xc3\xa9cole";
59 $this->assertEquals( $expect, UtfNormal
::cleanUp( $text ) );
63 * This test is *very* expensive!
66 function XtestAllChars() {
67 $rep = UTF8_REPLACEMENT
;
68 for ( $i = 0x0; $i < UNICODE_MAX
; $i++
) {
69 $char = codepointToUtf8( $i );
70 $clean = UtfNormal
::cleanUp( $char );
71 $x = sprintf( "%04X", $i );
73 if ( $i %
0x1000 == 0 ) {
80 ( $i > 0x001f && $i < UNICODE_SURROGATE_FIRST
) ||
81 ( $i > UNICODE_SURROGATE_LAST
&& $i < 0xfffe ) ||
82 ( $i > 0xffff && $i <= UNICODE_MAX
)
84 if ( isset( UtfNormal
::$utfCanonicalComp[$char] ) ||
isset( UtfNormal
::$utfCanonicalDecomp[$char] ) ) {
85 $comp = UtfNormal
::NFC( $char );
89 "U+$x should be decomposed" );
94 "U+$x should be intact" );
97 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
102 /** @todo document */
103 function testAllBytes() {
104 $this->doTestBytes( '', '' );
105 $this->doTestBytes( 'x', '' );
106 $this->doTestBytes( '', 'x' );
107 $this->doTestBytes( 'x', 'x' );
110 /** @todo document */
111 function doTestBytes( $head, $tail ) {
112 for ( $i = 0x0; $i < 256; $i++
) {
113 $char = $head . chr( $i ) . $tail;
114 $clean = UtfNormal
::cleanUp( $char );
115 $x = sprintf( "%02X", $i );
120 ( $i > 0x001f && $i < 0x80 )
125 "ASCII byte $x should be intact" );
126 if ( $char != $clean ) {
130 $norm = $head . UTF8_REPLACEMENT
. $tail;
134 "Forbidden byte $x should be rejected" );
135 if ( $norm != $clean ) {
142 /** @todo document */
143 function testDoubleBytes() {
144 $this->doTestDoubleBytes( '', '' );
145 $this->doTestDoubleBytes( 'x', '' );
146 $this->doTestDoubleBytes( '', 'x' );
147 $this->doTestDoubleBytes( 'x', 'x' );
153 function doTestDoubleBytes( $head, $tail ) {
154 for ( $first = 0xc0; $first < 0x100; $first +
= 2 ) {
155 for ( $second = 0x80; $second < 0x100; $second +
= 2 ) {
156 $char = $head . chr( $first ) . chr( $second ) . $tail;
157 $clean = UtfNormal
::cleanUp( $char );
158 $x = sprintf( "%02X,%02X", $first, $second );
159 if ( $first > 0xc1 &&
163 $norm = UtfNormal
::NFC( $char );
167 "Pair $x should be intact" );
168 if ( $norm != $clean ) {
171 } elseif ( $first > 0xfd ||
$second > 0xbf ) {
172 # fe and ff are not legal head bytes -- expect two replacement chars
173 $norm = $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail;
177 "Forbidden pair $x should be rejected" );
178 if ( $norm != $clean ) {
182 $norm = $head . UTF8_REPLACEMENT
. $tail;
186 "Forbidden pair $x should be rejected" );
187 if ( $norm != $clean ) {
195 /** @todo document */
196 function testTripleBytes() {
197 $this->doTestTripleBytes( '', '' );
198 $this->doTestTripleBytes( 'x', '' );
199 $this->doTestTripleBytes( '', 'x' );
200 $this->doTestTripleBytes( 'x', 'x' );
203 /** @todo document */
204 function doTestTripleBytes( $head, $tail ) {
205 for ( $first = 0xc0; $first < 0x100; $first +
= 2 ) {
206 for ( $second = 0x80; $second < 0x100; $second +
= 2 ) {
207 #for( $third = 0x80; $third < 0x100; $third++ ) {
208 for ( $third = 0x80; $third < 0x81; $third++
) {
209 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
210 $clean = UtfNormal
::cleanUp( $char );
211 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
213 if ( $first >= 0xe0 &&
218 if ( $first == 0xe0 && $second < 0xa0 ) {
220 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
222 "Overlong triplet $x should be rejected" );
223 } elseif ( $first == 0xed &&
224 ( chr( $first ) . chr( $second ) . chr( $third ) ) >= UTF8_SURROGATE_FIRST
227 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
229 "Surrogate triplet $x should be rejected" );
232 bin2hex( UtfNormal
::NFC( $char ) ),
234 "Triplet $x should be intact" );
236 } elseif ( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
238 bin2hex( UtfNormal
::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT
. $tail ),
240 "Valid 2-byte $x + broken tail" );
241 } elseif ( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
243 bin2hex( $head . UTF8_REPLACEMENT
. UtfNormal
::NFC( chr( $second ) . chr( $third ) . $tail ) ),
245 "Broken head + valid 2-byte $x" );
246 } elseif ( ( $first > 0xfd ||
$second > 0xfd ) &&
247 ( ( $second > 0xbf && $third > 0xbf ) ||
248 ( $second < 0xc0 && $third < 0xc0 ) ||
249 ( $second > 0xfd ) ||
252 # fe and ff are not legal head bytes -- expect three replacement chars
254 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
256 "Forbidden triplet $x should be rejected" );
257 } elseif ( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
259 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
261 "Forbidden triplet $x should be rejected" );
264 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
266 "Forbidden triplet $x should be rejected" );
273 /** @todo document */
274 function testChunkRegression() {
275 # Check for regression against a chunking bug
276 $text = "\x46\x55\xb8" .
283 $expect = "\x46\x55\xef\xbf\xbd" .
293 bin2hex( UtfNormal
::cleanUp( $text ) ) );
296 /** @todo document */
297 function testInterposeRegression() {
312 $expect = "\x4e\x30" .
328 bin2hex( UtfNormal
::cleanUp( $text ) ) );
331 /** @todo document */
332 function testOverlongRegression() {
334 "\x1a" . # forbidden ascii
336 "\xc1\xa6" . # overlong sequence
338 "\x1c" . # forbidden ascii
353 bin2hex( UtfNormal
::cleanUp( $text ) ) );
356 /** @todo document */
357 function testSurrogateRegression() {
358 $text = "\xed\xb4\x96" . # surrogate 0xDD16
362 $expect = "\xef\xbf\xbd" .
368 bin2hex( UtfNormal
::cleanUp( $text ) ) );
371 /** @todo document */
372 function testBomRegression() {
373 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
377 $expect = "\xef\xbf\xbd" .
383 bin2hex( UtfNormal
::cleanUp( $text ) ) );
386 /** @todo document */
387 function testForbiddenRegression() {
388 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
389 $expect = "\xef\xbf\xbd";
392 bin2hex( UtfNormal
::cleanUp( $text ) ) );
395 /** @todo document */
396 function testHangulRegression() {
397 $text = "\xed\x9c\xaf" . # Hangul char
398 "\xe1\x87\x81"; # followed by another final jamo
399 $expect = $text; # Should *not* change.
402 bin2hex( UtfNormal
::cleanUp( $text ) ) );