Merge "Fix default value of the SpecialWatchlistFilters hook"
[lhc/web/wiklou.git] / tests / phpunit / includes / normal / CleanUpTest.php
1 <?php
2 /**
3 * Tests for UtfNormal::cleanUp() function.
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 /**
27 * Additional tests for UtfNormal::cleanUp() function, inclusion
28 * regression checks for known problems.
29 * Requires PHPUnit.
30 *
31 * @ingroup UtfNormal
32 * @group Large
33 */
34 class CleanUpTest extends MediaWikiTestCase {
35 /** @todo document */
36 function testAscii() {
37 $text = 'This is plain ASCII text.';
38 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
39 }
40
41 /** @todo document */
42 function testNull() {
43 $text = "a \x00 null";
44 $expect = "a \xef\xbf\xbd null";
45 $this->assertEquals(
46 bin2hex( $expect ),
47 bin2hex( UtfNormal::cleanUp( $text ) ) );
48 }
49
50 /** @todo document */
51 function testLatin() {
52 $text = "L'\xc3\xa9cole";
53 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
54 }
55
56 /** @todo document */
57 function testLatinNormal() {
58 $text = "L'e\xcc\x81cole";
59 $expect = "L'\xc3\xa9cole";
60 $this->assertEquals( $expect, UtfNormal::cleanUp( $text ) );
61 }
62
63 /**
64 * This test is *very* expensive!
65 * @todo document
66 */
67 function XtestAllChars() {
68 $rep = UTF8_REPLACEMENT;
69 for ( $i = 0x0; $i < UNICODE_MAX; $i++ ) {
70 $char = codepointToUtf8( $i );
71 $clean = UtfNormal::cleanUp( $char );
72 $x = sprintf( "%04X", $i );
73
74 if ( $i % 0x1000 == 0 ) {
75 echo "U+$x\n";
76 }
77
78 if ( $i == 0x0009 ||
79 $i == 0x000a ||
80 $i == 0x000d ||
81 ( $i > 0x001f && $i < UNICODE_SURROGATE_FIRST ) ||
82 ( $i > UNICODE_SURROGATE_LAST && $i < 0xfffe ) ||
83 ( $i > 0xffff && $i <= UNICODE_MAX )
84 ) {
85 if ( isset( UtfNormal::$utfCanonicalComp[$char] ) || isset( UtfNormal::$utfCanonicalDecomp[$char] ) ) {
86 $comp = UtfNormal::NFC( $char );
87 $this->assertEquals(
88 bin2hex( $comp ),
89 bin2hex( $clean ),
90 "U+$x should be decomposed" );
91 } else {
92 $this->assertEquals(
93 bin2hex( $char ),
94 bin2hex( $clean ),
95 "U+$x should be intact" );
96 }
97 } else {
98 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
99 }
100 }
101 }
102
103 /** @todo document */
104 function testAllBytes() {
105 $this->doTestBytes( '', '' );
106 $this->doTestBytes( 'x', '' );
107 $this->doTestBytes( '', 'x' );
108 $this->doTestBytes( 'x', 'x' );
109 }
110
111 /** @todo document */
112 function doTestBytes( $head, $tail ) {
113 for ( $i = 0x0; $i < 256; $i++ ) {
114 $char = $head . chr( $i ) . $tail;
115 $clean = UtfNormal::cleanUp( $char );
116 $x = sprintf( "%02X", $i );
117
118 if ( $i == 0x0009 ||
119 $i == 0x000a ||
120 $i == 0x000d ||
121 ( $i > 0x001f && $i < 0x80 )
122 ) {
123 $this->assertEquals(
124 bin2hex( $char ),
125 bin2hex( $clean ),
126 "ASCII byte $x should be intact" );
127 if ( $char != $clean ) {
128 return;
129 }
130 } else {
131 $norm = $head . UTF8_REPLACEMENT . $tail;
132 $this->assertEquals(
133 bin2hex( $norm ),
134 bin2hex( $clean ),
135 "Forbidden byte $x should be rejected" );
136 if ( $norm != $clean ) {
137 return;
138 }
139 }
140 }
141 }
142
143 /** @todo document */
144 function testDoubleBytes() {
145 $this->doTestDoubleBytes( '', '' );
146 $this->doTestDoubleBytes( 'x', '' );
147 $this->doTestDoubleBytes( '', 'x' );
148 $this->doTestDoubleBytes( 'x', 'x' );
149 }
150
151 /**
152 * @todo document
153 */
154 function doTestDoubleBytes( $head, $tail ) {
155 for ( $first = 0xc0; $first < 0x100; $first += 2 ) {
156 for ( $second = 0x80; $second < 0x100; $second += 2 ) {
157 $char = $head . chr( $first ) . chr( $second ) . $tail;
158 $clean = UtfNormal::cleanUp( $char );
159 $x = sprintf( "%02X,%02X", $first, $second );
160 if ( $first > 0xc1 &&
161 $first < 0xe0 &&
162 $second < 0xc0
163 ) {
164 $norm = UtfNormal::NFC( $char );
165 $this->assertEquals(
166 bin2hex( $norm ),
167 bin2hex( $clean ),
168 "Pair $x should be intact" );
169 if ( $norm != $clean ) {
170 return;
171 }
172 } elseif ( $first > 0xfd || $second > 0xbf ) {
173 # fe and ff are not legal head bytes -- expect two replacement chars
174 $norm = $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail;
175 $this->assertEquals(
176 bin2hex( $norm ),
177 bin2hex( $clean ),
178 "Forbidden pair $x should be rejected" );
179 if ( $norm != $clean ) {
180 return;
181 }
182 } else {
183 $norm = $head . UTF8_REPLACEMENT . $tail;
184 $this->assertEquals(
185 bin2hex( $norm ),
186 bin2hex( $clean ),
187 "Forbidden pair $x should be rejected" );
188 if ( $norm != $clean ) {
189 return;
190 }
191 }
192 }
193 }
194 }
195
196 /** @todo document */
197 function testTripleBytes() {
198 $this->doTestTripleBytes( '', '' );
199 $this->doTestTripleBytes( 'x', '' );
200 $this->doTestTripleBytes( '', 'x' );
201 $this->doTestTripleBytes( 'x', 'x' );
202 }
203
204 /** @todo document */
205 function doTestTripleBytes( $head, $tail ) {
206 for ( $first = 0xc0; $first < 0x100; $first += 2 ) {
207 for ( $second = 0x80; $second < 0x100; $second += 2 ) {
208 #for( $third = 0x80; $third < 0x100; $third++ ) {
209 for ( $third = 0x80; $third < 0x81; $third++ ) {
210 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
211 $clean = UtfNormal::cleanUp( $char );
212 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
213
214 if ( $first >= 0xe0 &&
215 $first < 0xf0 &&
216 $second < 0xc0 &&
217 $third < 0xc0
218 ) {
219 if ( $first == 0xe0 && $second < 0xa0 ) {
220 $this->assertEquals(
221 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
222 bin2hex( $clean ),
223 "Overlong triplet $x should be rejected" );
224 } elseif ( $first == 0xed &&
225 ( chr( $first ) . chr( $second ) . chr( $third ) ) >= UTF8_SURROGATE_FIRST
226 ) {
227 $this->assertEquals(
228 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
229 bin2hex( $clean ),
230 "Surrogate triplet $x should be rejected" );
231 } else {
232 $this->assertEquals(
233 bin2hex( UtfNormal::NFC( $char ) ),
234 bin2hex( $clean ),
235 "Triplet $x should be intact" );
236 }
237 } elseif ( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
238 $this->assertEquals(
239 bin2hex( UtfNormal::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT . $tail ),
240 bin2hex( $clean ),
241 "Valid 2-byte $x + broken tail" );
242 } elseif ( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
243 $this->assertEquals(
244 bin2hex( $head . UTF8_REPLACEMENT . UtfNormal::NFC( chr( $second ) . chr( $third ) . $tail ) ),
245 bin2hex( $clean ),
246 "Broken head + valid 2-byte $x" );
247 } elseif ( ( $first > 0xfd || $second > 0xfd ) &&
248 ( ( $second > 0xbf && $third > 0xbf ) ||
249 ( $second < 0xc0 && $third < 0xc0 ) ||
250 ( $second > 0xfd ) ||
251 ( $third > 0xfd ) )
252 ) {
253 # fe and ff are not legal head bytes -- expect three replacement chars
254 $this->assertEquals(
255 bin2hex( $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail ),
256 bin2hex( $clean ),
257 "Forbidden triplet $x should be rejected" );
258 } elseif ( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
259 $this->assertEquals(
260 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
261 bin2hex( $clean ),
262 "Forbidden triplet $x should be rejected" );
263 } else {
264 $this->assertEquals(
265 bin2hex( $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail ),
266 bin2hex( $clean ),
267 "Forbidden triplet $x should be rejected" );
268 }
269 }
270 }
271 }
272 }
273
274 /** @todo document */
275 function testChunkRegression() {
276 # Check for regression against a chunking bug
277 $text = "\x46\x55\xb8" .
278 "\xdc\x96" .
279 "\xee" .
280 "\xe7" .
281 "\x44" .
282 "\xaa" .
283 "\x2f\x25";
284 $expect = "\x46\x55\xef\xbf\xbd" .
285 "\xdc\x96" .
286 "\xef\xbf\xbd" .
287 "\xef\xbf\xbd" .
288 "\x44" .
289 "\xef\xbf\xbd" .
290 "\x2f\x25";
291
292 $this->assertEquals(
293 bin2hex( $expect ),
294 bin2hex( UtfNormal::cleanUp( $text ) ) );
295 }
296
297 /** @todo document */
298 function testInterposeRegression() {
299 $text = "\x4e\x30" .
300 "\xb1" . # bad tail
301 "\x3a" .
302 "\x92" . # bad tail
303 "\x62\x3a" .
304 "\x84" . # bad tail
305 "\x43" .
306 "\xc6" . # bad head
307 "\x3f" .
308 "\x92" . # bad tail
309 "\xad" . # bad tail
310 "\x7d" .
311 "\xd9\x95";
312
313 $expect = "\x4e\x30" .
314 "\xef\xbf\xbd" .
315 "\x3a" .
316 "\xef\xbf\xbd" .
317 "\x62\x3a" .
318 "\xef\xbf\xbd" .
319 "\x43" .
320 "\xef\xbf\xbd" .
321 "\x3f" .
322 "\xef\xbf\xbd" .
323 "\xef\xbf\xbd" .
324 "\x7d" .
325 "\xd9\x95";
326
327 $this->assertEquals(
328 bin2hex( $expect ),
329 bin2hex( UtfNormal::cleanUp( $text ) ) );
330 }
331
332 /** @todo document */
333 function testOverlongRegression() {
334 $text = "\x67" .
335 "\x1a" . # forbidden ascii
336 "\xea" . # bad head
337 "\xc1\xa6" . # overlong sequence
338 "\xad" . # bad tail
339 "\x1c" . # forbidden ascii
340 "\xb0" . # bad tail
341 "\x3c" .
342 "\x9e"; # bad tail
343 $expect = "\x67" .
344 "\xef\xbf\xbd" .
345 "\xef\xbf\xbd" .
346 "\xef\xbf\xbd" .
347 "\xef\xbf\xbd" .
348 "\xef\xbf\xbd" .
349 "\xef\xbf\xbd" .
350 "\x3c" .
351 "\xef\xbf\xbd";
352 $this->assertEquals(
353 bin2hex( $expect ),
354 bin2hex( UtfNormal::cleanUp( $text ) ) );
355 }
356
357 /** @todo document */
358 function testSurrogateRegression() {
359 $text = "\xed\xb4\x96" . # surrogate 0xDD16
360 "\x83" . # bad tail
361 "\xb4" . # bad tail
362 "\xac"; # bad head
363 $expect = "\xef\xbf\xbd" .
364 "\xef\xbf\xbd" .
365 "\xef\xbf\xbd" .
366 "\xef\xbf\xbd";
367 $this->assertEquals(
368 bin2hex( $expect ),
369 bin2hex( UtfNormal::cleanUp( $text ) ) );
370 }
371
372 /** @todo document */
373 function testBomRegression() {
374 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
375 "\xb2" . # bad tail
376 "\xef" . # bad head
377 "\x59";
378 $expect = "\xef\xbf\xbd" .
379 "\xef\xbf\xbd" .
380 "\xef\xbf\xbd" .
381 "\x59";
382 $this->assertEquals(
383 bin2hex( $expect ),
384 bin2hex( UtfNormal::cleanUp( $text ) ) );
385 }
386
387 /** @todo document */
388 function testForbiddenRegression() {
389 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
390 $expect = "\xef\xbf\xbd";
391 $this->assertEquals(
392 bin2hex( $expect ),
393 bin2hex( UtfNormal::cleanUp( $text ) ) );
394 }
395
396 /** @todo document */
397 function testHangulRegression() {
398 $text = "\xed\x9c\xaf" . # Hangul char
399 "\xe1\x87\x81"; # followed by another final jamo
400 $expect = $text; # Should *not* change.
401 $this->assertEquals(
402 bin2hex( $expect ),
403 bin2hex( UtfNormal::cleanUp( $text ) ) );
404 }
405 }