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 * @todo covers tags, will be UtfNormal::cleanUp once the below is resolved
35 * @todo split me into test methods and providers per the below comment
37 * We ignore code coverage for this test suite until they are rewritten
38 * to use data providers (bug 46561).
41 class CleanUpTest
extends MediaWikiTestCase
{
43 public function testAscii() {
44 $text = 'This is plain ASCII text.';
45 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
49 public function testNull() {
50 $text = "a \x00 null";
51 $expect = "a \xef\xbf\xbd null";
54 bin2hex( UtfNormal
::cleanUp( $text ) ) );
58 public function testLatin() {
59 $text = "L'\xc3\xa9cole";
60 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
64 public function testLatinNormal() {
65 $text = "L'e\xcc\x81cole";
66 $expect = "L'\xc3\xa9cole";
67 $this->assertEquals( $expect, UtfNormal
::cleanUp( $text ) );
71 * This test is *very* expensive!
74 function XtestAllChars() {
75 $rep = UTF8_REPLACEMENT
;
76 for ( $i = 0x0; $i < UNICODE_MAX
; $i++
) {
77 $char = codepointToUtf8( $i );
78 $clean = UtfNormal
::cleanUp( $char );
79 $x = sprintf( "%04X", $i );
81 if ( $i %
0x1000 == 0 ) {
88 ( $i > 0x001f && $i < UNICODE_SURROGATE_FIRST
) ||
89 ( $i > UNICODE_SURROGATE_LAST
&& $i < 0xfffe ) ||
90 ( $i > 0xffff && $i <= UNICODE_MAX
)
92 if ( isset( UtfNormal
::$utfCanonicalComp[$char] ) ||
isset( UtfNormal
::$utfCanonicalDecomp[$char] ) ) {
93 $comp = UtfNormal
::NFC( $char );
97 "U+$x should be decomposed" );
102 "U+$x should be intact" );
105 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
110 /** @todo document */
111 public function testAllBytes() {
112 $this->doTestBytes( '', '' );
113 $this->doTestBytes( 'x', '' );
114 $this->doTestBytes( '', 'x' );
115 $this->doTestBytes( 'x', 'x' );
118 /** @todo document */
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 );
128 ( $i > 0x001f && $i < 0x80 )
133 "ASCII byte $x should be intact" );
134 if ( $char != $clean ) {
138 $norm = $head . UTF8_REPLACEMENT
. $tail;
142 "Forbidden byte $x should be rejected" );
143 if ( $norm != $clean ) {
150 /** @todo document */
151 public function testDoubleBytes() {
152 $this->doTestDoubleBytes( '', '' );
153 $this->doTestDoubleBytes( 'x', '' );
154 $this->doTestDoubleBytes( '', 'x' );
155 $this->doTestDoubleBytes( 'x', 'x' );
161 function doTestDoubleBytes( $head, $tail ) {
162 for ( $first = 0xc0; $first < 0x100; $first +
= 2 ) {
163 for ( $second = 0x80; $second < 0x100; $second +
= 2 ) {
164 $char = $head . chr( $first ) . chr( $second ) . $tail;
165 $clean = UtfNormal
::cleanUp( $char );
166 $x = sprintf( "%02X,%02X", $first, $second );
167 if ( $first > 0xc1 &&
171 $norm = UtfNormal
::NFC( $char );
175 "Pair $x should be intact" );
176 if ( $norm != $clean ) {
179 } elseif ( $first > 0xfd ||
$second > 0xbf ) {
180 # fe and ff are not legal head bytes -- expect two replacement chars
181 $norm = $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail;
185 "Forbidden pair $x should be rejected" );
186 if ( $norm != $clean ) {
190 $norm = $head . UTF8_REPLACEMENT
. $tail;
194 "Forbidden pair $x should be rejected" );
195 if ( $norm != $clean ) {
203 /** @todo document */
204 public function testTripleBytes() {
205 $this->doTestTripleBytes( '', '' );
206 $this->doTestTripleBytes( 'x', '' );
207 $this->doTestTripleBytes( '', 'x' );
208 $this->doTestTripleBytes( 'x', 'x' );
211 /** @todo document */
212 function doTestTripleBytes( $head, $tail ) {
213 for ( $first = 0xc0; $first < 0x100; $first +
= 2 ) {
214 for ( $second = 0x80; $second < 0x100; $second +
= 2 ) {
215 #for( $third = 0x80; $third < 0x100; $third++ ) {
216 for ( $third = 0x80; $third < 0x81; $third++
) {
217 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
218 $clean = UtfNormal
::cleanUp( $char );
219 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
221 if ( $first >= 0xe0 &&
226 if ( $first == 0xe0 && $second < 0xa0 ) {
228 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
230 "Overlong triplet $x should be rejected" );
231 } elseif ( $first == 0xed &&
232 ( chr( $first ) . chr( $second ) . chr( $third ) ) >= UTF8_SURROGATE_FIRST
235 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
237 "Surrogate triplet $x should be rejected" );
240 bin2hex( UtfNormal
::NFC( $char ) ),
242 "Triplet $x should be intact" );
244 } elseif ( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
246 bin2hex( UtfNormal
::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT
. $tail ),
248 "Valid 2-byte $x + broken tail" );
249 } elseif ( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
251 bin2hex( $head . UTF8_REPLACEMENT
. UtfNormal
::NFC( chr( $second ) . chr( $third ) . $tail ) ),
253 "Broken head + valid 2-byte $x" );
254 } elseif ( ( $first > 0xfd ||
$second > 0xfd ) &&
255 ( ( $second > 0xbf && $third > 0xbf ) ||
256 ( $second < 0xc0 && $third < 0xc0 ) ||
257 ( $second > 0xfd ) ||
260 # fe and ff are not legal head bytes -- expect three replacement chars
262 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
264 "Forbidden triplet $x should be rejected" );
265 } elseif ( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
267 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
269 "Forbidden triplet $x should be rejected" );
272 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
274 "Forbidden triplet $x should be rejected" );
281 /** @todo document */
282 public function testChunkRegression() {
283 # Check for regression against a chunking bug
284 $text = "\x46\x55\xb8" .
291 $expect = "\x46\x55\xef\xbf\xbd" .
301 bin2hex( UtfNormal
::cleanUp( $text ) ) );
304 /** @todo document */
305 public function testInterposeRegression() {
320 $expect = "\x4e\x30" .
336 bin2hex( UtfNormal
::cleanUp( $text ) ) );
339 /** @todo document */
340 public function testOverlongRegression() {
342 "\x1a" . # forbidden ascii
344 "\xc1\xa6" . # overlong sequence
346 "\x1c" . # forbidden ascii
361 bin2hex( UtfNormal
::cleanUp( $text ) ) );
364 /** @todo document */
365 public function testSurrogateRegression() {
366 $text = "\xed\xb4\x96" . # surrogate 0xDD16
370 $expect = "\xef\xbf\xbd" .
376 bin2hex( UtfNormal
::cleanUp( $text ) ) );
379 /** @todo document */
380 public function testBomRegression() {
381 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
385 $expect = "\xef\xbf\xbd" .
391 bin2hex( UtfNormal
::cleanUp( $text ) ) );
394 /** @todo document */
395 public function testForbiddenRegression() {
396 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
397 $expect = "\xef\xbf\xbd";
400 bin2hex( UtfNormal
::cleanUp( $text ) ) );
403 /** @todo document */
404 public function testHangulRegression() {
405 $text = "\xed\x9c\xaf" . # Hangul char
406 "\xe1\x87\x81"; # followed by another final jamo
407 $expect = $text; # Should *not* change.
410 bin2hex( UtfNormal
::cleanUp( $text ) ) );