2# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
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.
21if( php_sapi_name() !=
'cli' ) {
22 die(
"Run me from the command line please.\n" );
27 dl(
'php_utfnormal.so' );
30#ini_set( 'memory_limit', '40M' );
32require_once
'PHPUnit/Framework.php';
33require_once
'PHPUnit/TextUI/TestRunner.php';
35require_once
'UtfNormal.php';
56 $text =
'This is plain ASCII text.';
62 $text =
"a \x00 null";
63 $expect =
"a \xef\xbf\xbd null";
71 $text =
"L'\xc3\xa9cole";
77 $text =
"L'e\xcc\x81cole";
78 $expect =
"L'\xc3\xa9cole";
92 $x = sprintf(
"%04X", $i );
93 if( $i % 0x1000 == 0 ) echo
"U+$x\n";
105 "U+$x should be decomposed" );
110 "U+$x should be intact" );
113 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ),
$x );
128 for( $i = 0x0; $i < 256; $i++ ) {
129 $char = $head . chr( $i ) . $tail;
131 $x = sprintf(
"%02X", $i );
135 ($i > 0x001f && $i < 0x80) ) {
139 "ASCII byte $x should be intact" );
140 if( $char != $clean )
return;
146 "Forbidden byte $x should be rejected" );
147 if( $norm != $clean )
return;
164 for( $first = 0xc0; $first < 0x100; $first++ ) {
165 for( $second = 0x80; $second < 0x100; $second++ ) {
166 $char = $head . chr( $first ) . chr( $second ) . $tail;
168 $x = sprintf(
"%02X,%02X", $first, $second );
176 "Pair $x should be intact" );
177 if( $norm != $clean )
return;
178 } elseif( $first > 0xfd || $second > 0xbf ) {
179 # fe and ff are not legal head bytes -- expect two replacement chars
184 "Forbidden pair $x should be rejected" );
185 if( $norm != $clean )
return;
191 "Forbidden pair $x should be rejected" );
192 if( $norm != $clean )
return;
208 for( $first = 0xc0; $first < 0x100; $first++ ) {
209 for( $second = 0x80; $second < 0x100; $second++ ) {
210 #for( $third = 0x80; $third < 0x100; $third++ ) {
211 for( $third = 0x80; $third < 0x81; $third++ ) {
212 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
214 $x = sprintf(
"%02X,%02X,%02X", $first, $second, $third );
215 if( $first >= 0xe0 &&
219 if( $first == 0xe0 && $second < 0xa0 ) {
223 "Overlong triplet $x should be rejected" );
224 } elseif( $first == 0xed &&
229 "Surrogate triplet $x should be rejected" );
234 "Triplet $x should be intact" );
236 } elseif( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
240 "Valid 2-byte $x + broken tail" );
241 } elseif( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
245 "Broken head + valid 2-byte $x" );
246 } elseif( ( $first > 0xfd || $second > 0xfd ) &&
247 ( ( $second > 0xbf && $third > 0xbf ) ||
248 ( $second < 0xc0 && $third < 0xc0 ) ||
249 ( $second > 0xfd ) ||
250 ( $third > 0xfd ) ) ) {
251 # fe and ff are not legal head bytes -- expect three replacement chars
255 "Forbidden triplet $x should be rejected" );
256 } elseif( $first > 0xc2 && $second < 0xc0 && $third < 0xc0 ) {
260 "Forbidden triplet $x should be rejected" );
265 "Forbidden triplet $x should be rejected" );
274 # Check for regression against a chunking bug
275 $text =
"\x46\x55\xb8" .
282 $expect =
"\x46\x55\xef\xbf\xbd" .
311 $expect =
"\x4e\x30" .
333 "\x1a" . # forbidden ascii
335 "\xc1\xa6" . # overlong sequence
337 "\x1c" . # forbidden ascii
357 $text =
"\xed\xb4\x96" . # surrogate 0xDD16
361 $expect =
"\xef\xbf\xbd" .
372 $text =
"\xef\xbf\xbe" . # U+FFFE, illegal
char
376 $expect =
"\xef\xbf\xbd" .
387 $text =
"\xef\xbf\xbf"; # U+FFFF, illegal
char
388 $expect =
"\xef\xbf\xbd";
396 $text =
"\xed\x9c\xaf" . # Hangul
char
397 "\xe1\x87\x81"; # followed by another
final jamo
398 $expect =
$text; # Should *not* change.
409if( !
$result->wasSuccessful() ) {
XtestAllChars()
This test is very expensive!
testSurrogateRegression()
testForbiddenRegression()
testInterposeRegression()
doTestBytes( $head, $tail)
doTestDoubleBytes( $head, $tail)
doTestTripleBytes( $head, $tail)
cleanUp( $string)
The ultimate convenience function! Clean up invalid UTF-8 sequences, and convert to normal form C,...
codepointToUtf8( $codepoint)
Return UTF-8 sequence for a given Unicode code point.
const UNICODE_SURROGATE_FIRST
const UTF8_SURROGATE_FIRST
global $utfCanonicalDecomp
const UNICODE_SURROGATE_LAST
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']