test: ignore code coverage for CleanUpTest
[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 * We ignore code coverage for this test suite until they are rewritten
35 * to use data providers (bug 46561).
36 * @codeCoverageIgnore
37 */
38 class CleanUpTest extends MediaWikiTestCase {
39 /** @todo document */
40 function testAscii() {
41 $text = 'This is plain ASCII text.';
42 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
43 }
44
45 /** @todo document */
46 function testNull() {
47 $text = "a \x00 null";
48 $expect = "a \xef\xbf\xbd null";
49 $this->assertEquals(
50 bin2hex( $expect ),
51 bin2hex( UtfNormal::cleanUp( $text ) ) );
52 }
53
54 /** @todo document */
55 function testLatin() {
56 $text = "L'\xc3\xa9cole";
57 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
58 }
59
60 /** @todo document */
61 function testLatinNormal() {
62 $text = "L'e\xcc\x81cole";
63 $expect = "L'\xc3\xa9cole";
64 $this->assertEquals( $expect, UtfNormal::cleanUp( $text ) );
65 }
66
67 /**
68 * This test is *very* expensive!
69 * @todo document
70 */
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 );
77
78 if ( $i % 0x1000 == 0 ) {
79 echo "U+$x\n";
80 }
81
82 if ( $i == 0x0009 ||
83 $i == 0x000a ||
84 $i == 0x000d ||
85 ( $i > 0x001f && $i < UNICODE_SURROGATE_FIRST ) ||
86 ( $i > UNICODE_SURROGATE_LAST && $i < 0xfffe ) ||
87 ( $i > 0xffff && $i <= UNICODE_MAX )
88 ) {
89 if ( isset( UtfNormal::$utfCanonicalComp[$char] ) || isset( UtfNormal::$utfCanonicalDecomp[$char] ) ) {
90 $comp = UtfNormal::NFC( $char );
91 $this->assertEquals(
92 bin2hex( $comp ),
93 bin2hex( $clean ),
94 "U+$x should be decomposed" );
95 } else {
96 $this->assertEquals(
97 bin2hex( $char ),
98 bin2hex( $clean ),
99 "U+$x should be intact" );
100 }
101 } else {
102 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
103 }
104 }
105 }
106
107 /** @todo document */
108 function testAllBytes() {
109 $this->doTestBytes( '', '' );
110 $this->doTestBytes( 'x', '' );
111 $this->doTestBytes( '', 'x' );
112 $this->doTestBytes( 'x', 'x' );
113 }
114
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 );
121
122 if ( $i == 0x0009 ||
123 $i == 0x000a ||
124 $i == 0x000d ||
125 ( $i > 0x001f && $i < 0x80 )
126 ) {
127 $this->assertEquals(
128 bin2hex( $char ),
129 bin2hex( $clean ),
130 "ASCII byte $x should be intact" );
131 if ( $char != $clean ) {
132 return;
133 }
134 } else {
135 $norm = $head . UTF8_REPLACEMENT . $tail;
136 $this->assertEquals(
137 bin2hex( $norm ),
138 bin2hex( $clean ),
139 "Forbidden byte $x should be rejected" );
140 if ( $norm != $clean ) {
141 return;
142 }
143 }
144 }
145 }
146
147 /** @todo document */
148 function testDoubleBytes() {
149 $this->doTestDoubleBytes( '', '' );
150 $this->doTestDoubleBytes( 'x', '' );
151 $this->doTestDoubleBytes( '', 'x' );
152 $this->doTestDoubleBytes( 'x', 'x' );
153 }
154
155 /**
156 * @todo document
157 */
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 &&
165 $first < 0xe0 &&
166 $second < 0xc0
167 ) {
168 $norm = UtfNormal::NFC( $char );
169 $this->assertEquals(
170 bin2hex( $norm ),
171 bin2hex( $clean ),
172 "Pair $x should be intact" );
173 if ( $norm != $clean ) {
174 return;
175 }
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;
179 $this->assertEquals(
180 bin2hex( $norm ),
181 bin2hex( $clean ),
182 "Forbidden pair $x should be rejected" );
183 if ( $norm != $clean ) {
184 return;
185 }
186 } else {
187 $norm = $head . UTF8_REPLACEMENT . $tail;
188 $this->assertEquals(
189 bin2hex( $norm ),
190 bin2hex( $clean ),
191 "Forbidden pair $x should be rejected" );
192 if ( $norm != $clean ) {
193 return;
194 }
195 }
196 }
197 }
198 }
199
200 /** @todo document */
201 function testTripleBytes() {
202 $this->doTestTripleBytes( '', '' );
203 $this->doTestTripleBytes( 'x', '' );
204 $this->doTestTripleBytes( '', 'x' );
205 $this->doTestTripleBytes( 'x', 'x' );
206 }
207
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 );
217
218 if ( $first >= 0xe0 &&
219 $first < 0xf0 &&
220 $second < 0xc0 &&
221 $third < 0xc0
222 ) {
223 if ( $first == 0xe0 && $second < 0xa0 ) {
224 $this->assertEquals(
225 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
226 bin2hex( $clean ),
227 "Overlong triplet $x should be rejected" );
228 } elseif ( $first == 0xed &&
229 ( chr( $first ) . chr( $second ) . chr( $third ) ) >= UTF8_SURROGATE_FIRST
230 ) {
231 $this->assertEquals(
232 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
233 bin2hex( $clean ),
234 "Surrogate triplet $x should be rejected" );
235 } else {
236 $this->assertEquals(
237 bin2hex( UtfNormal::NFC( $char ) ),
238 bin2hex( $clean ),
239 "Triplet $x should be intact" );
240 }
241 } elseif ( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
242 $this->assertEquals(
243 bin2hex( UtfNormal::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT . $tail ),
244 bin2hex( $clean ),
245 "Valid 2-byte $x + broken tail" );
246 } elseif ( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
247 $this->assertEquals(
248 bin2hex( $head . UTF8_REPLACEMENT . UtfNormal::NFC( chr( $second ) . chr( $third ) . $tail ) ),
249 bin2hex( $clean ),
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 ) ||
255 ( $third > 0xfd ) )
256 ) {
257 # fe and ff are not legal head bytes -- expect three replacement chars
258 $this->assertEquals(
259 bin2hex( $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail ),
260 bin2hex( $clean ),
261 "Forbidden triplet $x should be rejected" );
262 } elseif ( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
263 $this->assertEquals(
264 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
265 bin2hex( $clean ),
266 "Forbidden triplet $x should be rejected" );
267 } else {
268 $this->assertEquals(
269 bin2hex( $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail ),
270 bin2hex( $clean ),
271 "Forbidden triplet $x should be rejected" );
272 }
273 }
274 }
275 }
276 }
277
278 /** @todo document */
279 function testChunkRegression() {
280 # Check for regression against a chunking bug
281 $text = "\x46\x55\xb8" .
282 "\xdc\x96" .
283 "\xee" .
284 "\xe7" .
285 "\x44" .
286 "\xaa" .
287 "\x2f\x25";
288 $expect = "\x46\x55\xef\xbf\xbd" .
289 "\xdc\x96" .
290 "\xef\xbf\xbd" .
291 "\xef\xbf\xbd" .
292 "\x44" .
293 "\xef\xbf\xbd" .
294 "\x2f\x25";
295
296 $this->assertEquals(
297 bin2hex( $expect ),
298 bin2hex( UtfNormal::cleanUp( $text ) ) );
299 }
300
301 /** @todo document */
302 function testInterposeRegression() {
303 $text = "\x4e\x30" .
304 "\xb1" . # bad tail
305 "\x3a" .
306 "\x92" . # bad tail
307 "\x62\x3a" .
308 "\x84" . # bad tail
309 "\x43" .
310 "\xc6" . # bad head
311 "\x3f" .
312 "\x92" . # bad tail
313 "\xad" . # bad tail
314 "\x7d" .
315 "\xd9\x95";
316
317 $expect = "\x4e\x30" .
318 "\xef\xbf\xbd" .
319 "\x3a" .
320 "\xef\xbf\xbd" .
321 "\x62\x3a" .
322 "\xef\xbf\xbd" .
323 "\x43" .
324 "\xef\xbf\xbd" .
325 "\x3f" .
326 "\xef\xbf\xbd" .
327 "\xef\xbf\xbd" .
328 "\x7d" .
329 "\xd9\x95";
330
331 $this->assertEquals(
332 bin2hex( $expect ),
333 bin2hex( UtfNormal::cleanUp( $text ) ) );
334 }
335
336 /** @todo document */
337 function testOverlongRegression() {
338 $text = "\x67" .
339 "\x1a" . # forbidden ascii
340 "\xea" . # bad head
341 "\xc1\xa6" . # overlong sequence
342 "\xad" . # bad tail
343 "\x1c" . # forbidden ascii
344 "\xb0" . # bad tail
345 "\x3c" .
346 "\x9e"; # bad tail
347 $expect = "\x67" .
348 "\xef\xbf\xbd" .
349 "\xef\xbf\xbd" .
350 "\xef\xbf\xbd" .
351 "\xef\xbf\xbd" .
352 "\xef\xbf\xbd" .
353 "\xef\xbf\xbd" .
354 "\x3c" .
355 "\xef\xbf\xbd";
356 $this->assertEquals(
357 bin2hex( $expect ),
358 bin2hex( UtfNormal::cleanUp( $text ) ) );
359 }
360
361 /** @todo document */
362 function testSurrogateRegression() {
363 $text = "\xed\xb4\x96" . # surrogate 0xDD16
364 "\x83" . # bad tail
365 "\xb4" . # bad tail
366 "\xac"; # bad head
367 $expect = "\xef\xbf\xbd" .
368 "\xef\xbf\xbd" .
369 "\xef\xbf\xbd" .
370 "\xef\xbf\xbd";
371 $this->assertEquals(
372 bin2hex( $expect ),
373 bin2hex( UtfNormal::cleanUp( $text ) ) );
374 }
375
376 /** @todo document */
377 function testBomRegression() {
378 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
379 "\xb2" . # bad tail
380 "\xef" . # bad head
381 "\x59";
382 $expect = "\xef\xbf\xbd" .
383 "\xef\xbf\xbd" .
384 "\xef\xbf\xbd" .
385 "\x59";
386 $this->assertEquals(
387 bin2hex( $expect ),
388 bin2hex( UtfNormal::cleanUp( $text ) ) );
389 }
390
391 /** @todo document */
392 function testForbiddenRegression() {
393 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
394 $expect = "\xef\xbf\xbd";
395 $this->assertEquals(
396 bin2hex( $expect ),
397 bin2hex( UtfNormal::cleanUp( $text ) ) );
398 }
399
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.
405 $this->assertEquals(
406 bin2hex( $expect ),
407 bin2hex( UtfNormal::cleanUp( $text ) ) );
408 }
409 }