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.
34 * We ignore code coverage for this test suite until they are rewritten
35 * to use data providers (bug 46561).
38 class CleanUpTest
extends MediaWikiTestCase
{
40 function testAscii() {
41 $text = 'This is plain ASCII text.';
42 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
47 $text = "a \x00 null";
48 $expect = "a \xef\xbf\xbd null";
51 bin2hex( UtfNormal
::cleanUp( $text ) ) );
55 function testLatin() {
56 $text = "L'\xc3\xa9cole";
57 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
61 function testLatinNormal() {
62 $text = "L'e\xcc\x81cole";
63 $expect = "L'\xc3\xa9cole";
64 $this->assertEquals( $expect, UtfNormal
::cleanUp( $text ) );
68 * This test is *very* expensive!
71 function XtestAllChars() {
72 $rep = UTF8_REPLACEMENT
;
73 for ( $i = 0x0; $i < UNICODE_MAX
; $i++
) {
74 $char = codepointToUtf8( $i );
75 $clean = UtfNormal
::cleanUp( $char );
76 $x = sprintf( "%04X", $i );
78 if ( $i %
0x1000 == 0 ) {
85 ( $i > 0x001f && $i < UNICODE_SURROGATE_FIRST
) ||
86 ( $i > UNICODE_SURROGATE_LAST
&& $i < 0xfffe ) ||
87 ( $i > 0xffff && $i <= UNICODE_MAX
)
89 if ( isset( UtfNormal
::$utfCanonicalComp[$char] ) ||
isset( UtfNormal
::$utfCanonicalDecomp[$char] ) ) {
90 $comp = UtfNormal
::NFC( $char );
94 "U+$x should be decomposed" );
99 "U+$x should be intact" );
102 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
107 /** @todo document */
108 function testAllBytes() {
109 $this->doTestBytes( '', '' );
110 $this->doTestBytes( 'x', '' );
111 $this->doTestBytes( '', 'x' );
112 $this->doTestBytes( 'x', 'x' );
115 /** @todo document */
116 function doTestBytes( $head, $tail ) {
117 for ( $i = 0x0; $i < 256; $i++
) {
118 $char = $head . chr( $i ) . $tail;
119 $clean = UtfNormal
::cleanUp( $char );
120 $x = sprintf( "%02X", $i );
125 ( $i > 0x001f && $i < 0x80 )
130 "ASCII byte $x should be intact" );
131 if ( $char != $clean ) {
135 $norm = $head . UTF8_REPLACEMENT
. $tail;
139 "Forbidden byte $x should be rejected" );
140 if ( $norm != $clean ) {
147 /** @todo document */
148 function testDoubleBytes() {
149 $this->doTestDoubleBytes( '', '' );
150 $this->doTestDoubleBytes( 'x', '' );
151 $this->doTestDoubleBytes( '', 'x' );
152 $this->doTestDoubleBytes( 'x', 'x' );
158 function doTestDoubleBytes( $head, $tail ) {
159 for ( $first = 0xc0; $first < 0x100; $first +
= 2 ) {
160 for ( $second = 0x80; $second < 0x100; $second +
= 2 ) {
161 $char = $head . chr( $first ) . chr( $second ) . $tail;
162 $clean = UtfNormal
::cleanUp( $char );
163 $x = sprintf( "%02X,%02X", $first, $second );
164 if ( $first > 0xc1 &&
168 $norm = UtfNormal
::NFC( $char );
172 "Pair $x should be intact" );
173 if ( $norm != $clean ) {
176 } elseif ( $first > 0xfd ||
$second > 0xbf ) {
177 # fe and ff are not legal head bytes -- expect two replacement chars
178 $norm = $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail;
182 "Forbidden pair $x should be rejected" );
183 if ( $norm != $clean ) {
187 $norm = $head . UTF8_REPLACEMENT
. $tail;
191 "Forbidden pair $x should be rejected" );
192 if ( $norm != $clean ) {
200 /** @todo document */
201 function testTripleBytes() {
202 $this->doTestTripleBytes( '', '' );
203 $this->doTestTripleBytes( 'x', '' );
204 $this->doTestTripleBytes( '', 'x' );
205 $this->doTestTripleBytes( 'x', 'x' );
208 /** @todo document */
209 function doTestTripleBytes( $head, $tail ) {
210 for ( $first = 0xc0; $first < 0x100; $first +
= 2 ) {
211 for ( $second = 0x80; $second < 0x100; $second +
= 2 ) {
212 #for( $third = 0x80; $third < 0x100; $third++ ) {
213 for ( $third = 0x80; $third < 0x81; $third++
) {
214 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
215 $clean = UtfNormal
::cleanUp( $char );
216 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
218 if ( $first >= 0xe0 &&
223 if ( $first == 0xe0 && $second < 0xa0 ) {
225 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
227 "Overlong triplet $x should be rejected" );
228 } elseif ( $first == 0xed &&
229 ( chr( $first ) . chr( $second ) . chr( $third ) ) >= UTF8_SURROGATE_FIRST
232 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
234 "Surrogate triplet $x should be rejected" );
237 bin2hex( UtfNormal
::NFC( $char ) ),
239 "Triplet $x should be intact" );
241 } elseif ( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
243 bin2hex( UtfNormal
::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT
. $tail ),
245 "Valid 2-byte $x + broken tail" );
246 } elseif ( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
248 bin2hex( $head . UTF8_REPLACEMENT
. UtfNormal
::NFC( chr( $second ) . chr( $third ) . $tail ) ),
250 "Broken head + valid 2-byte $x" );
251 } elseif ( ( $first > 0xfd ||
$second > 0xfd ) &&
252 ( ( $second > 0xbf && $third > 0xbf ) ||
253 ( $second < 0xc0 && $third < 0xc0 ) ||
254 ( $second > 0xfd ) ||
257 # fe and ff are not legal head bytes -- expect three replacement chars
259 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
261 "Forbidden triplet $x should be rejected" );
262 } elseif ( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
264 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
266 "Forbidden triplet $x should be rejected" );
269 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
271 "Forbidden triplet $x should be rejected" );
278 /** @todo document */
279 function testChunkRegression() {
280 # Check for regression against a chunking bug
281 $text = "\x46\x55\xb8" .
288 $expect = "\x46\x55\xef\xbf\xbd" .
298 bin2hex( UtfNormal
::cleanUp( $text ) ) );
301 /** @todo document */
302 function testInterposeRegression() {
317 $expect = "\x4e\x30" .
333 bin2hex( UtfNormal
::cleanUp( $text ) ) );
336 /** @todo document */
337 function testOverlongRegression() {
339 "\x1a" . # forbidden ascii
341 "\xc1\xa6" . # overlong sequence
343 "\x1c" . # forbidden ascii
358 bin2hex( UtfNormal
::cleanUp( $text ) ) );
361 /** @todo document */
362 function testSurrogateRegression() {
363 $text = "\xed\xb4\x96" . # surrogate 0xDD16
367 $expect = "\xef\xbf\xbd" .
373 bin2hex( UtfNormal
::cleanUp( $text ) ) );
376 /** @todo document */
377 function testBomRegression() {
378 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
382 $expect = "\xef\xbf\xbd" .
388 bin2hex( UtfNormal
::cleanUp( $text ) ) );
391 /** @todo document */
392 function testForbiddenRegression() {
393 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
394 $expect = "\xef\xbf\xbd";
397 bin2hex( UtfNormal
::cleanUp( $text ) ) );
400 /** @todo document */
401 function testHangulRegression() {
402 $text = "\xed\x9c\xaf" . # Hangul char
403 "\xe1\x87\x81"; # followed by another final jamo
404 $expect = $text; # Should *not* change.
407 bin2hex( UtfNormal
::cleanUp( $text ) ) );