219cc575e0817fb924b6424cabe29ebf2a68cbdd
[lhc/web/wiklou.git] / includes / normal / CleanUpTest.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Additional tests for UtfNormal::cleanUp() function, inclusion
22 * regression checks for known problems.
23 *
24 * Requires PHPUnit.
25 *
26 * @package UtfNormal
27 * @access private
28 */
29
30 if( php_sapi_name() != 'cli' ) {
31 die( "Run me from the command line please.\n" );
32 }
33
34 /** */
35 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
36 dl( 'php_utfnormal.so' );
37 }
38
39 #ini_set( 'memory_limit', '40M' );
40
41 require_once( 'PHPUnit.php' );
42 require_once( 'UtfNormal.php' );
43
44 class CleanUpTest extends PHPUnit_TestCase {
45 function CleanUpTest( $name ) {
46 $this->PHPUnit_TestCase( $name );
47 }
48
49 function setUp() {
50 }
51
52 function tearDown() {
53 }
54
55 function testAscii() {
56 $text = 'This is plain ASCII text.';
57 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
58 }
59
60 function testNull() {
61 $text = "a \x00 null";
62 $expect = "a \xef\xbf\xbd null";
63 $this->assertEquals(
64 bin2hex( $expect ),
65 bin2hex( UtfNormal::cleanUp( $text ) ) );
66 }
67
68 function testLatin() {
69 $text = "L'\xc3\xa9cole";
70 $this->assertEquals( $text, UtfNormal::cleanUp( $text ) );
71 }
72
73 function testLatinNormal() {
74 $text = "L'e\xcc\x81cole";
75 $expect = "L'\xc3\xa9cole";
76 $this->assertEquals( $expect, UtfNormal::cleanUp( $text ) );
77 }
78
79 # This test is *very* expensive!
80 function XtestAllChars() {
81 $rep = UTF8_REPLACEMENT;
82 global $utfCanonicalComp, $utfCanonicalDecomp;
83 for( $i = 0x0; $i < UNICODE_MAX; $i++ ) {
84 $char = codepointToUtf8( $i );
85 $clean = UtfNormal::cleanUp( $char );
86 $x = sprintf( "%04X", $i );
87 if( $i % 0x1000 == 0 ) echo "U+$x\n";
88 if( $i == 0x0009 ||
89 $i == 0x000a ||
90 $i == 0x000d ||
91 ($i > 0x001f && $i < UNICODE_SURROGATE_FIRST) ||
92 ($i > UNICODE_SURROGATE_LAST && $i < 0xfffe ) ||
93 ($i > 0xffff && $i <= UNICODE_MAX ) ) {
94 if( isset( $utfCanonicalComp[$char] ) || isset( $utfCanonicalDecomp[$char] ) ) {
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 function testAllBytes() {
113 $this->doTestBytes( '', '' );
114 $this->doTestBytes( 'x', '' );
115 $this->doTestBytes( '', 'x' );
116 $this->doTestBytes( 'x', 'x' );
117 }
118
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 );
124 if( $i == 0x0009 ||
125 $i == 0x000a ||
126 $i == 0x000d ||
127 ($i > 0x001f && $i < 0x80) ) {
128 $this->assertEquals(
129 bin2hex( $char ),
130 bin2hex( $clean ),
131 "ASCII byte $x should be intact" );
132 if( $char != $clean ) return;
133 } else {
134 $norm = $head . UTF8_REPLACEMENT . $tail;
135 $this->assertEquals(
136 bin2hex( $norm ),
137 bin2hex( $clean ),
138 "Forbidden byte $x should be rejected" );
139 if( $norm != $clean ) return;
140 }
141 }
142 }
143
144 function testDoubleBytes() {
145 $this->doTestDoubleBytes( '', '' );
146 $this->doTestDoubleBytes( 'x', '' );
147 $this->doTestDoubleBytes( '', 'x' );
148 $this->doTestDoubleBytes( 'x', 'x' );
149 }
150
151 function doTestDoubleBytes( $head, $tail ) {
152 for( $first = 0xc0; $first < 0x100; $first++ ) {
153 for( $second = 0x80; $second < 0x100; $second++ ) {
154 $char = $head . chr( $first ) . chr( $second ) . $tail;
155 $clean = UtfNormal::cleanUp( $char );
156 $x = sprintf( "%02X,%02X", $first, $second );
157 if( $first > 0xc1 &&
158 $first < 0xe0 &&
159 $second < 0xc0 ) {
160 $norm = UtfNormal::NFC( $char );
161 $this->assertEquals(
162 bin2hex( $norm ),
163 bin2hex( $clean ),
164 "Pair $x should be intact" );
165 if( $norm != $clean ) return;
166 } elseif( $first > 0xfd || $second > 0xbf ) {
167 # fe and ff are not legal head bytes -- expect two replacement chars
168 $norm = $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail;
169 $this->assertEquals(
170 bin2hex( $norm ),
171 bin2hex( $clean ),
172 "Forbidden pair $x should be rejected" );
173 if( $norm != $clean ) return;
174 } else {
175 $norm = $head . UTF8_REPLACEMENT . $tail;
176 $this->assertEquals(
177 bin2hex( $norm ),
178 bin2hex( $clean ),
179 "Forbidden pair $x should be rejected" );
180 if( $norm != $clean ) return;
181 }
182 }
183 }
184 }
185
186 function testTripleBytes() {
187 $this->doTestTripleBytes( '', '' );
188 $this->doTestTripleBytes( 'x', '' );
189 $this->doTestTripleBytes( '', 'x' );
190 $this->doTestTripleBytes( 'x', 'x' );
191 }
192
193 function doTestTripleBytes( $head, $tail ) {
194 for( $first = 0xc0; $first < 0x100; $first++ ) {
195 for( $second = 0x80; $second < 0x100; $second++ ) {
196 #for( $third = 0x80; $third < 0x100; $third++ ) {
197 for( $third = 0x80; $third < 0x81; $third++ ) {
198 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
199 $clean = UtfNormal::cleanUp( $char );
200 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
201 if( $first >= 0xe0 &&
202 $first < 0xf0 &&
203 $second < 0xc0 &&
204 $third < 0xc0 ) {
205 if( $first == 0xe0 && $second < 0xa0 ) {
206 $this->assertEquals(
207 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
208 bin2hex( $clean ),
209 "Overlong triplet $x should be rejected" );
210 } elseif( $first == 0xed &&
211 ( chr( $first ) . chr( $second ) . chr( $third )) >= UTF8_SURROGATE_FIRST ) {
212 $this->assertEquals(
213 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
214 bin2hex( $clean ),
215 "Surrogate triplet $x should be rejected" );
216 } else {
217 $this->assertEquals(
218 bin2hex( UtfNormal::NFC( $char ) ),
219 bin2hex( $clean ),
220 "Triplet $x should be intact" );
221 }
222 } elseif( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
223 $this->assertEquals(
224 bin2hex( UtfNormal::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT . $tail ),
225 bin2hex( $clean ),
226 "Valid 2-byte $x + broken tail" );
227 } elseif( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
228 $this->assertEquals(
229 bin2hex( $head . UTF8_REPLACEMENT . UtfNormal::NFC( chr( $second ) . chr( $third ) . $tail ) ),
230 bin2hex( $clean ),
231 "Broken head + valid 2-byte $x" );
232 } elseif( ( $first > 0xfd || $second > 0xfd ) &&
233 ( ( $second > 0xbf && $third > 0xbf ) ||
234 ( $second < 0xc0 && $third < 0xc0 ) ||
235 ( $second > 0xfd ) ||
236 ( $third > 0xfd ) ) ) {
237 # fe and ff are not legal head bytes -- expect three replacement chars
238 $this->assertEquals(
239 bin2hex( $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail ),
240 bin2hex( $clean ),
241 "Forbidden triplet $x should be rejected" );
242 } elseif( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
243 $this->assertEquals(
244 bin2hex( $head . UTF8_REPLACEMENT . $tail ),
245 bin2hex( $clean ),
246 "Forbidden triplet $x should be rejected" );
247 } else {
248 $this->assertEquals(
249 bin2hex( $head . UTF8_REPLACEMENT . UTF8_REPLACEMENT . $tail ),
250 bin2hex( $clean ),
251 "Forbidden triplet $x should be rejected" );
252 }
253 }
254 }
255 }
256 }
257
258 function testChunkRegression() {
259 # Check for regression against a chunking bug
260 $text = "\x46\x55\xb8" .
261 "\xdc\x96" .
262 "\xee" .
263 "\xe7" .
264 "\x44" .
265 "\xaa" .
266 "\x2f\x25";
267 $expect = "\x46\x55\xef\xbf\xbd" .
268 "\xdc\x96" .
269 "\xef\xbf\xbd" .
270 "\xef\xbf\xbd" .
271 "\x44" .
272 "\xef\xbf\xbd" .
273 "\x2f\x25";
274
275 $this->assertEquals(
276 bin2hex( $expect ),
277 bin2hex( UtfNormal::cleanUp( $text ) ) );
278 }
279
280 function testInterposeRegression() {
281 $text = "\x4e\x30" .
282 "\xb1" . # bad tail
283 "\x3a" .
284 "\x92" . # bad tail
285 "\x62\x3a" .
286 "\x84" . # bad tail
287 "\x43" .
288 "\xc6" . # bad head
289 "\x3f" .
290 "\x92" . # bad tail
291 "\xad" . # bad tail
292 "\x7d" .
293 "\xd9\x95";
294
295 $expect = "\x4e\x30" .
296 "\xef\xbf\xbd" .
297 "\x3a" .
298 "\xef\xbf\xbd" .
299 "\x62\x3a" .
300 "\xef\xbf\xbd" .
301 "\x43" .
302 "\xef\xbf\xbd" .
303 "\x3f" .
304 "\xef\xbf\xbd" .
305 "\xef\xbf\xbd" .
306 "\x7d" .
307 "\xd9\x95";
308
309 $this->assertEquals(
310 bin2hex( $expect ),
311 bin2hex( UtfNormal::cleanUp( $text ) ) );
312 }
313
314 function testOverlongRegression() {
315 $text = "\x67" .
316 "\x1a" . # forbidden ascii
317 "\xea" . # bad head
318 "\xc1\xa6" . # overlong sequence
319 "\xad" . # bad tail
320 "\x1c" . # forbidden ascii
321 "\xb0" . # bad tail
322 "\x3c" .
323 "\x9e"; # bad tail
324 $expect = "\x67" .
325 "\xef\xbf\xbd" .
326 "\xef\xbf\xbd" .
327 "\xef\xbf\xbd" .
328 "\xef\xbf\xbd" .
329 "\xef\xbf\xbd" .
330 "\xef\xbf\xbd" .
331 "\x3c" .
332 "\xef\xbf\xbd";
333 $this->assertEquals(
334 bin2hex( $expect ),
335 bin2hex( UtfNormal::cleanUp( $text ) ) );
336 }
337
338 function testSurrogateRegression() {
339 $text = "\xed\xb4\x96" . # surrogate 0xDD16
340 "\x83" . # bad tail
341 "\xb4" . # bad tail
342 "\xac"; # bad head
343 $expect = "\xef\xbf\xbd" .
344 "\xef\xbf\xbd" .
345 "\xef\xbf\xbd" .
346 "\xef\xbf\xbd";
347 $this->assertEquals(
348 bin2hex( $expect ),
349 bin2hex( UtfNormal::cleanUp( $text ) ) );
350 }
351
352 function testBomRegression() {
353 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
354 "\xb2" . # bad tail
355 "\xef" . # bad head
356 "\x59";
357 $expect = "\xef\xbf\xbd" .
358 "\xef\xbf\xbd" .
359 "\xef\xbf\xbd" .
360 "\x59";
361 $this->assertEquals(
362 bin2hex( $expect ),
363 bin2hex( UtfNormal::cleanUp( $text ) ) );
364 }
365
366 function testForbiddenRegression() {
367 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
368 $expect = "\xef\xbf\xbd";
369 $this->assertEquals(
370 bin2hex( $expect ),
371 bin2hex( UtfNormal::cleanUp( $text ) ) );
372 }
373
374 function testHangulRegression() {
375 $text = "\xed\x9c\xaf" . # Hangul char
376 "\xe1\x87\x81"; # followed by another final jamo
377 $expect = $text; # Should *not* change.
378 $this->assertEquals(
379 bin2hex( $expect ),
380 bin2hex( UtfNormal::cleanUp( $text ) ) );
381 }
382 }
383
384
385 $suite =& new PHPUnit_TestSuite( 'CleanUpTest' );
386 $result = PHPUnit::run( $suite );
387 echo $result->toString();
388
389 if( !$result->wasSuccessful() ) {
390 exit( -1 );
391 }
392 exit( 0 );
393 ?>