2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Additional tests for UtfNormal::cleanUp() function, inclusion
22 * regression checks for known problems.
30 if( php_sapi_name() != 'cli' ) {
31 die( "Run me from the command line please.\n" );
35 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
36 dl( 'php_utfnormal.so' );
39 #ini_set( 'memory_limit', '40M' );
41 require_once 'PHPUnit/Framework.php';
42 require_once 'PHPUnit/TextUI/TestRunner.php';
44 require_once 'UtfNormal.php';
49 class CleanUpTest
extends PHPUnit_Framework_TestCase
{
59 function testAscii() {
60 $text = 'This is plain ASCII text.';
61 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
66 $text = "a \x00 null";
67 $expect = "a \xef\xbf\xbd null";
70 bin2hex( UtfNormal
::cleanUp( $text ) ) );
74 function testLatin() {
75 $text = "L'\xc3\xa9cole";
76 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
80 function testLatinNormal() {
81 $text = "L'e\xcc\x81cole";
82 $expect = "L'\xc3\xa9cole";
83 $this->assertEquals( $expect, UtfNormal
::cleanUp( $text ) );
87 * This test is *very* expensive!
90 function XtestAllChars() {
91 $rep = UTF8_REPLACEMENT
;
92 global $utfCanonicalComp, $utfCanonicalDecomp;
93 for( $i = 0x0; $i < UNICODE_MAX
; $i++
) {
94 $char = codepointToUtf8( $i );
95 $clean = UtfNormal
::cleanUp( $char );
96 $x = sprintf( "%04X", $i );
97 if( $i %
0x1000 == 0 ) echo "U+$x\n";
101 ($i > 0x001f && $i < UNICODE_SURROGATE_FIRST
) ||
102 ($i > UNICODE_SURROGATE_LAST
&& $i < 0xfffe ) ||
103 ($i > 0xffff && $i <= UNICODE_MAX
) ) {
104 if( isset( $utfCanonicalComp[$char] ) ||
isset( $utfCanonicalDecomp[$char] ) ) {
105 $comp = UtfNormal
::NFC( $char );
109 "U+$x should be decomposed" );
114 "U+$x should be intact" );
117 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
122 /** @todo document */
123 function testAllBytes() {
124 $this->doTestBytes( '', '' );
125 $this->doTestBytes( 'x', '' );
126 $this->doTestBytes( '', 'x' );
127 $this->doTestBytes( 'x', 'x' );
130 /** @todo document */
131 function doTestBytes( $head, $tail ) {
132 for( $i = 0x0; $i < 256; $i++
) {
133 $char = $head . chr( $i ) . $tail;
134 $clean = UtfNormal
::cleanUp( $char );
135 $x = sprintf( "%02X", $i );
139 ($i > 0x001f && $i < 0x80) ) {
143 "ASCII byte $x should be intact" );
144 if( $char != $clean ) return;
146 $norm = $head . UTF8_REPLACEMENT
. $tail;
150 "Forbidden byte $x should be rejected" );
151 if( $norm != $clean ) return;
156 /** @todo document */
157 function testDoubleBytes() {
158 $this->doTestDoubleBytes( '', '' );
159 $this->doTestDoubleBytes( 'x', '' );
160 $this->doTestDoubleBytes( '', 'x' );
161 $this->doTestDoubleBytes( 'x', 'x' );
167 function doTestDoubleBytes( $head, $tail ) {
168 for( $first = 0xc0; $first < 0x100; $first++
) {
169 for( $second = 0x80; $second < 0x100; $second++
) {
170 $char = $head . chr( $first ) . chr( $second ) . $tail;
171 $clean = UtfNormal
::cleanUp( $char );
172 $x = sprintf( "%02X,%02X", $first, $second );
176 $norm = UtfNormal
::NFC( $char );
180 "Pair $x should be intact" );
181 if( $norm != $clean ) return;
182 } elseif( $first > 0xfd ||
$second > 0xbf ) {
183 # fe and ff are not legal head bytes -- expect two replacement chars
184 $norm = $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail;
188 "Forbidden pair $x should be rejected" );
189 if( $norm != $clean ) return;
191 $norm = $head . UTF8_REPLACEMENT
. $tail;
195 "Forbidden pair $x should be rejected" );
196 if( $norm != $clean ) return;
202 /** @todo document */
203 function testTripleBytes() {
204 $this->doTestTripleBytes( '', '' );
205 $this->doTestTripleBytes( 'x', '' );
206 $this->doTestTripleBytes( '', 'x' );
207 $this->doTestTripleBytes( 'x', 'x' );
210 /** @todo document */
211 function doTestTripleBytes( $head, $tail ) {
212 for( $first = 0xc0; $first < 0x100; $first++
) {
213 for( $second = 0x80; $second < 0x100; $second++
) {
214 #for( $third = 0x80; $third < 0x100; $third++ ) {
215 for( $third = 0x80; $third < 0x81; $third++
) {
216 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
217 $clean = UtfNormal
::cleanUp( $char );
218 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
219 if( $first >= 0xe0 &&
223 if( $first == 0xe0 && $second < 0xa0 ) {
225 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
227 "Overlong triplet $x should be rejected" );
228 } elseif( $first == 0xed &&
229 ( chr( $first ) . chr( $second ) . chr( $third )) >= UTF8_SURROGATE_FIRST
) {
231 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
233 "Surrogate triplet $x should be rejected" );
236 bin2hex( UtfNormal
::NFC( $char ) ),
238 "Triplet $x should be intact" );
240 } elseif( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
242 bin2hex( UtfNormal
::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT
. $tail ),
244 "Valid 2-byte $x + broken tail" );
245 } elseif( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
247 bin2hex( $head . UTF8_REPLACEMENT
. UtfNormal
::NFC( chr( $second ) . chr( $third ) . $tail ) ),
249 "Broken head + valid 2-byte $x" );
250 } elseif( ( $first > 0xfd ||
$second > 0xfd ) &&
251 ( ( $second > 0xbf && $third > 0xbf ) ||
252 ( $second < 0xc0 && $third < 0xc0 ) ||
253 ( $second > 0xfd ) ||
254 ( $third > 0xfd ) ) ) {
255 # fe and ff are not legal head bytes -- expect three replacement chars
257 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
259 "Forbidden triplet $x should be rejected" );
260 } elseif( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
262 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
264 "Forbidden triplet $x should be rejected" );
267 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
269 "Forbidden triplet $x should be rejected" );
276 /** @todo document */
277 function testChunkRegression() {
278 # Check for regression against a chunking bug
279 $text = "\x46\x55\xb8" .
286 $expect = "\x46\x55\xef\xbf\xbd" .
296 bin2hex( UtfNormal
::cleanUp( $text ) ) );
299 /** @todo document */
300 function testInterposeRegression() {
315 $expect = "\x4e\x30" .
331 bin2hex( UtfNormal
::cleanUp( $text ) ) );
334 /** @todo document */
335 function testOverlongRegression() {
337 "\x1a" . # forbidden ascii
339 "\xc1\xa6" . # overlong sequence
341 "\x1c" . # forbidden ascii
356 bin2hex( UtfNormal
::cleanUp( $text ) ) );
359 /** @todo document */
360 function testSurrogateRegression() {
361 $text = "\xed\xb4\x96" . # surrogate 0xDD16
365 $expect = "\xef\xbf\xbd" .
371 bin2hex( UtfNormal
::cleanUp( $text ) ) );
374 /** @todo document */
375 function testBomRegression() {
376 $text = "\xef\xbf\xbe" . # U+FFFE, illegal char
380 $expect = "\xef\xbf\xbd" .
386 bin2hex( UtfNormal
::cleanUp( $text ) ) );
389 /** @todo document */
390 function testForbiddenRegression() {
391 $text = "\xef\xbf\xbf"; # U+FFFF, illegal char
392 $expect = "\xef\xbf\xbd";
395 bin2hex( UtfNormal
::cleanUp( $text ) ) );
398 /** @todo document */
399 function testHangulRegression() {
400 $text = "\xed\x9c\xaf" . # Hangul char
401 "\xe1\x87\x81"; # followed by another final jamo
402 $expect = $text; # Should *not* change.
405 bin2hex( UtfNormal
::cleanUp( $text ) ) );
410 $suite = new PHPUnit_Framework_TestSuite( 'CleanUpTest' );
411 $result = PHPUnit_TextUI_TestRunner
::run( $suite );
413 if( !$result->wasSuccessful() ) {