test: Clean up data providers that should be static
[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 * https://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 * @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
36 *
37 * We ignore code coverage for this test suite until they are rewritten
38 * to use data providers (bug 46561).
39 * @codeCoverageIgnore
40 */
41 class CleanUpTest extends MediaWikiTestCase {
42 /** @todo document */
43 public function testAscii() {
44 $text = 'This is plain ASCII text.';
45 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
46 }
47
48 /** @todo document */
49 public function testNull() {
50 $text = "a \x00 null";
51 $expect = "a \xef\xbf\xbd null";
52 $this->assertEquals(
53 bin2hex( $expect ),
54 bin2hex( UtfNormal::cleanUp( $text ) ) );
55 }
56
57 /** @todo document */
58 public function testLatin() {
59 $text = "L'\xc3\xa9cole";
60 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
61 }
62
63 /** @todo document */
64 public function testLatinNormal() {
65 $text = "L'e\xcc\x81cole";
66 $expect = "L'\xc3\xa9cole";
67 $this->assertEquals( $expect, UtfNormal::cleanUp( $text ) );
68 }
69
70 /**
71 * This test is *very* expensive!
72 * @todo document
73 */
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 );
80
81 if ( $i % 0x1000 == 0 ) {
82 echo "U+$x\n";
83 }
84
85 if ( $i == 0x0009 ||
86 $i == 0x000a ||
87 $i == 0x000d ||
88 ( $i > 0x001f && $i < UNICODE_SURROGATE_FIRST ) ||
89 ( $i > UNICODE_SURROGATE_LAST && $i < 0xfffe ) ||
90 ( $i > 0xffff && $i <= UNICODE_MAX )
91 ) {
92 if ( isset( UtfNormal::$utfCanonicalComp[$char] )
93 || isset( UtfNormal::$utfCanonicalDecomp[$char] )
94 ) {
95 $comp = UtfNormal::NFC( $char );
96 $this->assertEquals(
97 bin2hex( $comp ),
98 bin2hex( $clean ),
99 "U+$x should be decomposed" );
100 } else {
101 $this->assertEquals(
102 bin2hex( $char ),
103 bin2hex( $clean ),
104 "U+$x should be intact" );
105 }
106 } else {
107 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
108 }
109 }
110 }
111
112 /** @todo document */
113 public static function provideAllBytes() {
114 return array(
115 array( '', '' ),
116 array( 'x', '' ),
117 array( '', 'x' ),
118 array( 'x', 'x' ),
119 );
120 }
121
122 /**
123 * @dataProvider provideAllBytes
124 * @todo document
125 */
126 function testBytes( $head, $tail ) {
127 for ( $i = 0x0; $i < 256; $i++ ) {
128 $char = $head . chr( $i ) . $tail;
129 $clean = UtfNormal::cleanUp( $char );
130 $x = sprintf( "%02X", $i );
131
132 if ( $i == 0x0009 ||
133 $i == 0x000a ||
134 $i == 0x000d ||
135 ( $i > 0x001f && $i < 0x80 )
136 ) {
137 $this->assertEquals(
138 bin2hex( $char ),
139 bin2hex( $clean ),
140 "ASCII byte $x should be intact" );
141 if ( $char != $clean ) {
142 return;
143 }
144 } else {
145 $norm = $head . UTF8_REPLACEMENT . $tail;
146 $this->assertEquals(
147 bin2hex( $norm ),
148 bin2hex( $clean ),
149 "Forbidden byte $x should be rejected" );
150 if ( $norm != $clean ) {
151 return;
152 }
153 }
154 }
155 }
156
157 /**
158 * @dataProvider provideAllBytes
159 * @todo document
160 */
161 function testDoubleBytes( $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 &&
168 $first < 0xe0 &&
169 $second < 0xc0
170 ) {
171 $norm = UtfNormal::NFC( $char );
172 $this->assertEquals(
173 bin2hex( $norm ),
174 bin2hex( $clean ),
175 "Pair $x should be intact" );
176 if ( $norm != $clean ) {
177 return;
178 }
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;
182 $this->assertEquals(
183 bin2hex( $norm ),
184 bin2hex( $clean ),
185 "Forbidden pair $x should be rejected" );
186 if ( $norm != $clean ) {
187 return;
188 }
189 } else {
190 $norm = $head . UTF8_REPLACEMENT . $tail;
191 $this->assertEquals(
192 bin2hex( $norm ),
193 bin2hex( $clean ),
194 "Forbidden pair $x should be rejected" );
195 if ( $norm != $clean ) {
196 return;
197 }
198 }
199 }
200 }
201 }
202
203 /**
204 * @dataProvider provideAllBytes
205 * @todo document
206 */
207 function testTripleBytes( $head, $tail ) {
208 for ( $first = 0xc0; $first < 0x100; $first += 2 ) {
209 for ( $second = 0x80; $second < 0x100; $second += 2 ) {
210 #for( $third = 0x80; $third < 0x100; $third++ ) {
211 for ( $third = 0x80; $third < 0x81; $third++ ) {
212 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
213 $clean = UtfNormal::cleanUp( $char );
214 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
215
216 if ( $first >= 0xe0 &&
217 $first < 0xf0 &&
218 $second < 0xc0 &&
219 $third < 0xc0
220 ) {
221 if ( $first == 0xe0 && $second < 0xa0 ) {
222 $this->assertEquals(
223 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
224 bin2hex( $clean ),
225 "Overlong triplet $x should be rejected" );
226 } elseif ( $first == 0xed &&
227 ( chr( $first ) . chr( $second ) . chr( $third ) ) >= UTF8_SURROGATE_FIRST
228 ) {
229 $this->assertEquals(
230 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
231 bin2hex( $clean ),
232 "Surrogate triplet $x should be rejected" );
233 } else {
234 $this->assertEquals(
235 bin2hex( UtfNormal::NFC( $char ) ),
236 bin2hex( $clean ),
237 "Triplet $x should be intact" );
238 }
239 } elseif ( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
240 $this->assertEquals(
241 bin2hex( UtfNormal::NFC( $head . chr( $first ) .
242 chr( $second ) ) . UTF8_REPLACEMENT . $tail ),
243 bin2hex( $clean ),
244 "Valid 2-byte $x + broken tail" );
245 } elseif ( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
246 $this->assertEquals(
247 bin2hex( $head . UTF8_REPLACEMENT .
248 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 public 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 public 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 public 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 public 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 public 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 public 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 public 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 }