7 no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
8 # machines, and that is tested elsewhere
12 my $pound_sign = chr utf8::unicode_to_native(163);
14 sub isASCII { ord "A" == 65 }
20 . join("", map { sprintf("\\x%02x", ord $_) } split "", $string)
24 sub output_warnings(@) {
25 diag "The warnings were:\n" . join("", @_);
28 # This test file can't use byte_utf8a_to_utf8n() from t/charset_tools.pl
29 # because that uses the same functions we are testing here. So UTF-EBCDIC
30 # strings are hard-coded as I8 strings in this file instead, and we use array
31 # lookup to translate into the appropriate code page.
33 my @i8_to_native = ( # Only code page 1047 so far.
34 # _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F
35 0x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x15,0x0B,0x0C,0x0D,0x0E,0x0F,
36 0x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x1C,0x1D,0x1E,0x1F,
37 0x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,
38 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,
39 0x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,
40 0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xAD,0xE0,0xBD,0x5F,0x6D,
41 0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
42 0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,
43 0x20,0x21,0x22,0x23,0x24,0x25,0x06,0x17,0x28,0x29,0x2A,0x2B,0x2C,0x09,0x0A,0x1B,
44 0x30,0x31,0x1A,0x33,0x34,0x35,0x36,0x08,0x38,0x39,0x3A,0x3B,0x04,0x14,0x3E,0xFF,
45 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x51,0x52,0x53,0x54,0x55,0x56,
46 0x57,0x58,0x59,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x70,0x71,0x72,0x73,
47 0x74,0x75,0x76,0x77,0x78,0x80,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x9A,0x9B,0x9C,
48 0x9D,0x9E,0x9F,0xA0,0xAA,0xAB,0xAC,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,
49 0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBF,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xDA,0xDB,
50 0xDC,0xDD,0xDE,0xDF,0xE1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xFA,0xFB,0xFC,0xFD,0xFE,
54 for (my $i = 0; $i < 256; $i++) {
55 $native_to_i8[$i8_to_native[$i]] = $i;
58 *I8_to_native = (isASCII)
59 ? sub { return shift }
60 : sub { return join "", map { chr $i8_to_native[ord $_] }
62 *native_to_I8 = (isASCII)
63 ? sub { return shift }
64 : sub { return join "", map { chr $native_to_i8[ord $_] }
66 sub start_byte_to_cont($) {
68 # Extract the code point information from the input UTF-8 start byte, and
69 # return a continuation byte containing the same information. This is
70 # used in constructing an overlong malformation from valid input.
73 my $len = test_UTF8_SKIP($byte);
75 die "start_byte_to_cont() is expecting a UTF-8 variant";
78 $byte = ord native_to_I8($byte);
80 # Copied from utf8.h. This gets rid of the leading 1 bits.
81 $byte &= ((($len) >= 7) ? 0x00 : (0x1F >> (($len)-2)));
83 $byte |= (isASCII) ? 0x80 : 0xA0;
84 return I8_to_native(chr $byte);
87 my $is64bit = length sprintf("%x", ~0) > 8;
90 # Test utf8n_to_uvchr_error(). These provide essentially complete code
91 # coverage. Copied from utf8.h
92 my $UTF8_ALLOW_EMPTY = 0x0001;
93 my $UTF8_GOT_EMPTY = $UTF8_ALLOW_EMPTY;
94 my $UTF8_ALLOW_CONTINUATION = 0x0002;
95 my $UTF8_GOT_CONTINUATION = $UTF8_ALLOW_CONTINUATION;
96 my $UTF8_ALLOW_NON_CONTINUATION = 0x0004;
97 my $UTF8_GOT_NON_CONTINUATION = $UTF8_ALLOW_NON_CONTINUATION;
98 my $UTF8_ALLOW_SHORT = 0x0008;
99 my $UTF8_GOT_SHORT = $UTF8_ALLOW_SHORT;
100 my $UTF8_ALLOW_LONG = 0x0010;
101 my $UTF8_GOT_LONG = $UTF8_ALLOW_LONG;
102 my $UTF8_GOT_OVERFLOW = 0x0080;
103 my $UTF8_DISALLOW_SURROGATE = 0x0100;
104 my $UTF8_GOT_SURROGATE = $UTF8_DISALLOW_SURROGATE;
105 my $UTF8_WARN_SURROGATE = 0x0200;
106 my $UTF8_DISALLOW_NONCHAR = 0x0400;
107 my $UTF8_GOT_NONCHAR = $UTF8_DISALLOW_NONCHAR;
108 my $UTF8_WARN_NONCHAR = 0x0800;
109 my $UTF8_DISALLOW_SUPER = 0x1000;
110 my $UTF8_GOT_SUPER = $UTF8_DISALLOW_SUPER;
111 my $UTF8_WARN_SUPER = 0x2000;
112 my $UTF8_DISALLOW_ABOVE_31_BIT = 0x4000;
113 my $UTF8_GOT_ABOVE_31_BIT = $UTF8_DISALLOW_ABOVE_31_BIT;
114 my $UTF8_WARN_ABOVE_31_BIT = 0x8000;
115 my $UTF8_CHECK_ONLY = 0x10000;
116 my $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE
117 = $UTF8_DISALLOW_SUPER|$UTF8_DISALLOW_SURROGATE;
118 my $UTF8_DISALLOW_ILLEGAL_INTERCHANGE
119 = $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE|$UTF8_DISALLOW_NONCHAR;
120 my $UTF8_WARN_ILLEGAL_C9_INTERCHANGE
121 = $UTF8_WARN_SUPER|$UTF8_WARN_SURROGATE;
122 my $UTF8_WARN_ILLEGAL_INTERCHANGE
123 = $UTF8_WARN_ILLEGAL_C9_INTERCHANGE|$UTF8_WARN_NONCHAR;
125 # Test uvchr_to_utf8().
126 my $UNICODE_WARN_SURROGATE = 0x0001;
127 my $UNICODE_WARN_NONCHAR = 0x0002;
128 my $UNICODE_WARN_SUPER = 0x0004;
129 my $UNICODE_WARN_ABOVE_31_BIT = 0x0008;
130 my $UNICODE_DISALLOW_SURROGATE = 0x0010;
131 my $UNICODE_DISALLOW_NONCHAR = 0x0020;
132 my $UNICODE_DISALLOW_SUPER = 0x0040;
133 my $UNICODE_DISALLOW_ABOVE_31_BIT = 0x0080;
135 my $look_for_everything_utf8n_to
136 = $UTF8_DISALLOW_SURROGATE
137 | $UTF8_WARN_SURROGATE
138 | $UTF8_DISALLOW_NONCHAR
140 | $UTF8_DISALLOW_SUPER
142 | $UTF8_DISALLOW_ABOVE_31_BIT
143 | $UTF8_WARN_ABOVE_31_BIT;
144 my $look_for_everything_uvchr_to
145 = $UNICODE_DISALLOW_SURROGATE
146 | $UNICODE_WARN_SURROGATE
147 | $UNICODE_DISALLOW_NONCHAR
148 | $UNICODE_WARN_NONCHAR
149 | $UNICODE_DISALLOW_SUPER
150 | $UNICODE_WARN_SUPER
151 | $UNICODE_DISALLOW_ABOVE_31_BIT
152 | $UNICODE_WARN_ABOVE_31_BIT;
154 foreach ([0, '', '', 'empty'],
155 [0, 'N', 'N', '1 char'],
156 [1, 'NN', 'N', '1 char substring'],
157 [-2, 'Perl', 'Rules', 'different'],
158 [0, $pound_sign, $pound_sign, 'pound sign'],
159 [1, $pound_sign . 10, $pound_sign . 1,
160 '10 pounds is more than 1 pound'],
161 [1, $pound_sign . $pound_sign, $pound_sign,
162 '2 pound signs are more than 1'],
163 [-2, ' $!', " \x{1F42B}!", 'Camels are worth more than 1 dollar'],
164 [-1, '!', "!\x{1F42A}", 'Initial substrings match'],
166 my ($expect, $left, $right, $desc) = @$_;
169 is(bytes_cmp_utf8($left, $copy), $expect, $desc);
170 next if $right =~ tr/\0-\377//c;
172 is(bytes_cmp_utf8($right, $left), -$expect, "$desc reversed");
175 # The keys to this hash are Unicode code points, their values are the native
176 # UTF-8 representations of them. The code points are chosen because they are
177 # "interesting" on either or both ASCII and EBCDIC platforms. First we add
178 # boundaries where the number of bytes required to represent them increase, or
179 # are adjacent to problematic code points, so we want to make sure they aren't
180 # considered problematic.
182 0x0100 => (isASCII) ? "\xc4\x80" : I8_to_native("\xc8\xa0"),
183 0x0400 - 1 => (isASCII) ? "\xcf\xbf" : I8_to_native("\xdf\xbf"),
184 0x0400 => (isASCII) ? "\xd0\x80" : I8_to_native("\xe1\xa0\xa0"),
185 0x0800 - 1 => (isASCII) ? "\xdf\xbf" : I8_to_native("\xe1\xbf\xbf"),
186 0x0800 => (isASCII) ? "\xe0\xa0\x80" : I8_to_native("\xe2\xa0\xa0"),
187 0x4000 - 1 => (isASCII) ? "\xe3\xbf\xbf" : I8_to_native("\xef\xbf\xbf"),
188 0x4000 => (isASCII) ? "\xe4\x80\x80" : I8_to_native("\xf0\xb0\xa0\xa0"),
189 0x8000 - 1 => (isASCII) ? "\xe7\xbf\xbf" : I8_to_native("\xf0\xbf\xbf\xbf"),
191 # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
192 # as of this writing, considers potentially problematic on EBCDIC
193 0x8000 => (isASCII) ? "\xe8\x80\x80" : I8_to_native("\xf1\xa0\xa0\xa0"),
195 0xD000 - 1 => (isASCII) ? "\xec\xbf\xbf" : I8_to_native("\xf1\xb3\xbf\xbf"),
197 # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
198 # as of this writing, considers potentially problematic on ASCII
199 0xD000 => (isASCII) ? "\xed\x80\x80" : I8_to_native("\xf1\xb4\xa0\xa0"),
201 # Bracket the surrogates, and include several surrogates
202 0xD7FF => (isASCII) ? "\xed\x9f\xbf" : I8_to_native("\xf1\xb5\xbf\xbf"),
203 0xD800 => (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
204 0xDC00 => (isASCII) ? "\xed\xb0\x80" : I8_to_native("\xf1\xb7\xa0\xa0"),
205 0xDFFF => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
206 0xDFFF => (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
207 0xE000 => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
209 # Include the 32 contiguous non characters, and surrounding code points
210 0xFDCF => (isASCII) ? "\xef\xb7\x8f" : I8_to_native("\xf1\xbf\xae\xaf"),
211 0xFDD0 => (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
212 0xFDD1 => (isASCII) ? "\xef\xb7\x91" : I8_to_native("\xf1\xbf\xae\xb1"),
213 0xFDD2 => (isASCII) ? "\xef\xb7\x92" : I8_to_native("\xf1\xbf\xae\xb2"),
214 0xFDD3 => (isASCII) ? "\xef\xb7\x93" : I8_to_native("\xf1\xbf\xae\xb3"),
215 0xFDD4 => (isASCII) ? "\xef\xb7\x94" : I8_to_native("\xf1\xbf\xae\xb4"),
216 0xFDD5 => (isASCII) ? "\xef\xb7\x95" : I8_to_native("\xf1\xbf\xae\xb5"),
217 0xFDD6 => (isASCII) ? "\xef\xb7\x96" : I8_to_native("\xf1\xbf\xae\xb6"),
218 0xFDD7 => (isASCII) ? "\xef\xb7\x97" : I8_to_native("\xf1\xbf\xae\xb7"),
219 0xFDD8 => (isASCII) ? "\xef\xb7\x98" : I8_to_native("\xf1\xbf\xae\xb8"),
220 0xFDD9 => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xae\xb9"),
221 0xFDDA => (isASCII) ? "\xef\xb7\x9a" : I8_to_native("\xf1\xbf\xae\xba"),
222 0xFDDB => (isASCII) ? "\xef\xb7\x9b" : I8_to_native("\xf1\xbf\xae\xbb"),
223 0xFDDC => (isASCII) ? "\xef\xb7\x9c" : I8_to_native("\xf1\xbf\xae\xbc"),
224 0xFDDD => (isASCII) ? "\xef\xb7\x9d" : I8_to_native("\xf1\xbf\xae\xbd"),
225 0xFDDE => (isASCII) ? "\xef\xb7\x9e" : I8_to_native("\xf1\xbf\xae\xbe"),
226 0xFDDF => (isASCII) ? "\xef\xb7\x9f" : I8_to_native("\xf1\xbf\xae\xbf"),
227 0xFDE0 => (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
228 0xFDE1 => (isASCII) ? "\xef\xb7\xa1" : I8_to_native("\xf1\xbf\xaf\xa1"),
229 0xFDE2 => (isASCII) ? "\xef\xb7\xa2" : I8_to_native("\xf1\xbf\xaf\xa2"),
230 0xFDE3 => (isASCII) ? "\xef\xb7\xa3" : I8_to_native("\xf1\xbf\xaf\xa3"),
231 0xFDE4 => (isASCII) ? "\xef\xb7\xa4" : I8_to_native("\xf1\xbf\xaf\xa4"),
232 0xFDE5 => (isASCII) ? "\xef\xb7\xa5" : I8_to_native("\xf1\xbf\xaf\xa5"),
233 0xFDE6 => (isASCII) ? "\xef\xb7\xa6" : I8_to_native("\xf1\xbf\xaf\xa6"),
234 0xFDE7 => (isASCII) ? "\xef\xb7\xa7" : I8_to_native("\xf1\xbf\xaf\xa7"),
235 0xFDE8 => (isASCII) ? "\xef\xb7\xa8" : I8_to_native("\xf1\xbf\xaf\xa8"),
236 0xFDEa => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xaf\xa9"),
237 0xFDEA => (isASCII) ? "\xef\xb7\xaa" : I8_to_native("\xf1\xbf\xaf\xaa"),
238 0xFDEB => (isASCII) ? "\xef\xb7\xab" : I8_to_native("\xf1\xbf\xaf\xab"),
239 0xFDEC => (isASCII) ? "\xef\xb7\xac" : I8_to_native("\xf1\xbf\xaf\xac"),
240 0xFDED => (isASCII) ? "\xef\xb7\xad" : I8_to_native("\xf1\xbf\xaf\xad"),
241 0xFDEE => (isASCII) ? "\xef\xb7\xae" : I8_to_native("\xf1\xbf\xaf\xae"),
242 0xFDEF => (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
243 0xFDF0 => (isASCII) ? "\xef\xb7\xb0" : I8_to_native("\xf1\xbf\xaf\xb0"),
245 # Mostly around non-characters, but some are transitions to longer strings
246 0xFFFD => (isASCII) ? "\xef\xbf\xbd" : I8_to_native("\xf1\xbf\xbf\xbd"),
247 0x10000 - 1 => (isASCII)
249 : I8_to_native("\xf1\xbf\xbf\xbf"),
252 : I8_to_native("\xf2\xa0\xa0\xa0"),
255 : I8_to_native("\xf3\xbf\xbf\xbd"),
258 : I8_to_native("\xf3\xbf\xbf\xbe"),
261 : I8_to_native("\xf3\xbf\xbf\xbf"),
264 : I8_to_native("\xf4\xa0\xa0\xa0"),
267 : I8_to_native("\xf5\xbf\xbf\xbd"),
270 : I8_to_native("\xf5\xbf\xbf\xbe"),
273 : I8_to_native("\xf5\xbf\xbf\xbf"),
276 : I8_to_native("\xf6\xa0\xa0\xa0"),
279 : I8_to_native("\xf7\xbf\xbf\xbd"),
282 : I8_to_native("\xf7\xbf\xbf\xbe"),
283 0x40000 - 1 => (isASCII)
285 : I8_to_native("\xf7\xbf\xbf\xbf"),
288 : I8_to_native("\xf8\xa8\xa0\xa0\xa0"),
291 : I8_to_native("\xf8\xa9\xbf\xbf\xbd"),
294 : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
297 : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
300 : I8_to_native("\xf8\xaa\xa0\xa0\xa0"),
303 : I8_to_native("\xf8\xab\xbf\xbf\xbd"),
306 : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
309 : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
312 : I8_to_native("\xf8\xac\xa0\xa0\xa0"),
315 : I8_to_native("\xf8\xad\xbf\xbf\xbd"),
318 : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
321 : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
324 : I8_to_native("\xf8\xae\xa0\xa0\xa0"),
327 : I8_to_native("\xf8\xaf\xbf\xbf\xbd"),
330 : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
333 : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
336 : I8_to_native("\xf8\xb0\xa0\xa0\xa0"),
339 : I8_to_native("\xf8\xb1\xbf\xbf\xbd"),
342 : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
345 : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
348 : I8_to_native("\xf8\xb2\xa0\xa0\xa0"),
351 : I8_to_native("\xf8\xb3\xbf\xbf\xbd"),
354 : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
357 : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
360 : I8_to_native("\xf8\xb4\xa0\xa0\xa0"),
363 : I8_to_native("\xf8\xb5\xbf\xbf\xbd"),
366 : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
369 : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
372 : I8_to_native("\xf8\xb6\xa0\xa0\xa0"),
375 : I8_to_native("\xf8\xb7\xbf\xbf\xbd"),
378 : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
381 : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
384 : I8_to_native("\xf8\xb8\xa0\xa0\xa0"),
387 : I8_to_native("\xf8\xb9\xbf\xbf\xbd"),
390 : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
393 : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
396 : I8_to_native("\xf8\xba\xa0\xa0\xa0"),
399 : I8_to_native("\xf8\xbb\xbf\xbf\xbd"),
402 : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
405 : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
408 : I8_to_native("\xf8\xbc\xa0\xa0\xa0"),
411 : I8_to_native("\xf8\xbd\xbf\xbf\xbd"),
414 : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
417 : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
420 : I8_to_native("\xf8\xbe\xa0\xa0\xa0"),
423 : I8_to_native("\xf8\xbf\xbf\xbf\xbd"),
426 : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
429 : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
430 0x100000 => (isASCII)
432 : I8_to_native("\xf9\xa0\xa0\xa0\xa0"),
433 0x10FFFD => (isASCII)
435 : I8_to_native("\xf9\xa1\xbf\xbf\xbd"),
436 0x10FFFE => (isASCII)
438 : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
439 0x10FFFF => (isASCII)
441 : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
442 0x110000 => (isASCII)
444 : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
446 # Things that would be noncharacters if they were in Unicode, and might be
447 # mistaken, if the C code is bad, to be nonchars
448 0x11FFFE => (isASCII)
450 : I8_to_native("\xf9\xa3\xbf\xbf\xbe"),
451 0x11FFFF => (isASCII)
453 : I8_to_native("\xf9\xa3\xbf\xbf\xbf"),
454 0x20FFFE => (isASCII)
455 ? "\xf8\x88\x8f\xbf\xbe"
456 : I8_to_native("\xfa\xa1\xbf\xbf\xbe"),
457 0x20FFFF => (isASCII)
458 ? "\xf8\x88\x8f\xbf\xbf"
459 : I8_to_native("\xfa\xa1\xbf\xbf\xbf"),
461 0x200000 - 1 => (isASCII)
463 : I8_to_native("\xf9\xbf\xbf\xbf\xbf"),
464 0x200000 => (isASCII)
465 ? "\xf8\x88\x80\x80\x80"
466 : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
467 0x400000 - 1 => (isASCII)
468 ? "\xf8\x8f\xbf\xbf\xbf"
469 : I8_to_native("\xfb\xbf\xbf\xbf\xbf"),
470 0x400000 => (isASCII)
471 ? "\xf8\x90\x80\x80\x80"
472 : I8_to_native("\xfc\xa4\xa0\xa0\xa0\xa0"),
473 0x4000000 - 1 => (isASCII)
474 ? "\xfb\xbf\xbf\xbf\xbf"
475 : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
476 0x4000000 => (isASCII)
477 ? "\xfc\x84\x80\x80\x80\x80"
478 : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
479 0x4000000 - 1 => (isASCII)
480 ? "\xfb\xbf\xbf\xbf\xbf"
481 : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
482 0x4000000 => (isASCII)
483 ? "\xfc\x84\x80\x80\x80\x80"
484 : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
485 0x40000000 - 1 => (isASCII)
486 ? "\xfc\xbf\xbf\xbf\xbf\xbf"
487 : I8_to_native("\xfe\xbf\xbf\xbf\xbf\xbf\xbf"),
489 (isASCII) ? "\xfd\x80\x80\x80\x80\x80"
490 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0"),
492 (isASCII) ? "\xfd\xbf\xbf\xbf\xbf\xbf"
493 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf"),
495 (isASCII) ? "\xfe\x82\x80\x80\x80\x80\x80"
496 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
498 (isASCII) ? "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
499 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
503 no warnings qw(overflow portable);
504 $code_points{0x100000000}
506 ? "\xfe\x84\x80\x80\x80\x80\x80"
507 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0");
508 $code_points{0x1000000000 - 1}
510 ? "\xfe\xbf\xbf\xbf\xbf\xbf\xbf"
511 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
512 $code_points{0x1000000000}
514 ? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
515 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0");
516 $code_points{0xFFFFFFFFFFFFFFFF}
518 ? "\xff\x80\x8f\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
519 : I8_to_native("\xff\xaf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
520 if (isASCII) { # These could falsely show as overlongs in a naive
522 $code_points{0x40000000000}
523 = "\xff\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80";
524 $code_points{0x1000000000000}
525 = "\xff\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80";
526 $code_points{0x40000000000000}
527 = "\xff\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80";
528 $code_points{0x1000000000000000}
529 = "\xff\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
532 # = "\xff\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
535 elsif (! isASCII) { # 32-bit EBCDIC. 64-bit is clearer to handle, so doesn't
536 # need this test case
537 no warnings qw(overflow portable);
538 $code_points{0x40000000} = I8_to_native(
539 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0");
542 # Now add in entries for each of code points 0-255, which require special
543 # handling on EBCDIC. Remember the keys are Unicode values, and the values
544 # are the native UTF-8. For invariants, the bytes are just the native chr.
547 while ($cp < ((isASCII) ? 128 : 160)) { # This is from the definition of
549 $code_points{$cp} = chr utf8::unicode_to_native($cp);
553 # Done with the invariants. Now do the variants. All in this range are 2
554 # byte. Again, we can't use the internal functions to generate UTF-8, as
555 # those are what we are trying to test. In the loop, we know what range the
556 # continuation bytes can be in, and what the lowest start byte can be. So we
557 # cycle through them.
559 my $first_continuation = (isASCII) ? 0x80 : 0xA0;
560 my $final_continuation = 0xBF;
561 my $start = (isASCII) ? 0xC2 : 0xC5;
563 my $max_bytes = (isASCII) ? 13 : 14; # Max number of bytes in a UTF-8 sequence
564 # representing a single code point
566 my $continuation = $first_continuation - 1;
569 if (++$continuation > $final_continuation) {
571 # Wrap to the next start byte when we reach the final continuation
573 $continuation = $first_continuation;
576 $code_points{$cp} = I8_to_native(chr($start) . chr($continuation));
584 local $SIG{__WARN__} = sub { push @warnings, @_ };
586 my %restriction_types;
588 $restriction_types{""}{'valid_strings'} = "";
589 $restriction_types{"c9strict"}{'valid_strings'} = "";
590 $restriction_types{"strict"}{'valid_strings'} = "";
591 $restriction_types{"fits_in_31_bits"}{'valid_strings'} = "";
593 # This set of tests looks for basic sanity, and lastly tests various routines
594 # for the given code point. If the earlier tests for that code point fail,
595 # the later ones probably will too. Malformations are tested in later
597 for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
600 my $hex_u = sprintf("0x%02X", $u);
601 my $n = utf8::unicode_to_native($u);
602 my $hex_n = sprintf("0x%02X", $n);
603 my $bytes = $code_points{$u};
605 my $offskip_should_be;
607 no warnings qw(overflow portable);
608 $offskip_should_be = (isASCII)
614 $u < 0x80000000 ? 6 : (($is64bit)
615 ? ($u < 0x1000000000 ? 7 : $max_bytes)
624 $u < 0x40000000 ? 7 : $max_bytes );
627 # If this test fails, subsequent ones are meaningless.
628 next unless is(test_OFFUNISKIP($u), $offskip_should_be,
629 "Verify OFFUNISKIP($hex_u) is $offskip_should_be");
630 my $invariant = $offskip_should_be == 1;
631 my $display_invariant = $invariant || 0;
632 is(test_OFFUNI_IS_INVARIANT($u), $invariant,
633 "Verify OFFUNI_IS_INVARIANT($hex_u) is $display_invariant");
635 my $uvchr_skip_should_be = $offskip_should_be;
636 next unless is(test_UVCHR_SKIP($n), $uvchr_skip_should_be,
637 "Verify UVCHR_SKIP($hex_n) is $uvchr_skip_should_be");
638 is(test_UVCHR_IS_INVARIANT($n), $offskip_should_be == 1,
639 "Verify UVCHR_IS_INVARIANT($hex_n) is $display_invariant");
642 utf8::upgrade $n_chr;
644 is(test_UTF8_SKIP($n_chr), $uvchr_skip_should_be,
645 "Verify UTF8_SKIP(chr $hex_n) is $uvchr_skip_should_be");
648 my $byte_length = length $n_chr;
649 for (my $j = 0; $j < $byte_length; $j++) {
652 if ($j == $byte_length - 1) {
654 = test_is_utf8_valid_partial_char_flags($n_chr, $byte_length, 0);
655 is($ret, 0, " Verify is_utf8_valid_partial_char_flags("
656 . display_bytes($n_chr)
657 . ") returns 0 for full character");
660 my $bytes_so_far = substr($n_chr, 0, $j + 1);
662 = test_is_utf8_valid_partial_char_flags($bytes_so_far, $j + 1, 0);
663 is($ret, 1, " Verify is_utf8_valid_partial_char_flags("
664 . display_bytes($bytes_so_far)
668 is(scalar @warnings, 0, " Verify is_utf8_valid_partial_char_flags"
669 . " generated no warnings")
670 or output_warnings(@warnings);
672 my $b = substr($n_chr, $j, 1);
673 my $hex_b = sprintf("\"\\x%02x\"", ord $b);
675 my $byte_invariant = $j == 0 && $uvchr_skip_should_be == 1;
676 my $display_byte_invariant = $byte_invariant || 0;
677 next unless is(test_UTF8_IS_INVARIANT($b), $byte_invariant,
678 " Verify UTF8_IS_INVARIANT($hex_b) for byte $j "
679 . "is $display_byte_invariant");
681 my $is_start = $j == 0 && $uvchr_skip_should_be > 1;
682 my $display_is_start = $is_start || 0;
683 next unless is(test_UTF8_IS_START($b), $is_start,
684 " Verify UTF8_IS_START($hex_b) is $display_is_start");
686 my $is_continuation = $j != 0 && $uvchr_skip_should_be > 1;
687 my $display_is_continuation = $is_continuation || 0;
688 next unless is(test_UTF8_IS_CONTINUATION($b), $is_continuation,
689 " Verify UTF8_IS_CONTINUATION($hex_b) is "
690 . "$display_is_continuation");
692 my $is_continued = $uvchr_skip_should_be > 1;
693 my $display_is_continued = $is_continued || 0;
694 next unless is(test_UTF8_IS_CONTINUED($b), $is_continued,
695 " Verify UTF8_IS_CONTINUED($hex_b) is "
696 . "$display_is_continued");
698 my $is_downgradeable_start = $n < 256
699 && $uvchr_skip_should_be > 1
701 my $display_is_downgradeable_start = $is_downgradeable_start || 0;
702 next unless is(test_UTF8_IS_DOWNGRADEABLE_START($b),
703 $is_downgradeable_start,
704 " Verify UTF8_IS_DOWNGRADEABLE_START($hex_b) is "
705 . "$display_is_downgradeable_start");
707 my $is_above_latin1 = $n > 255 && $j == 0;
708 my $display_is_above_latin1 = $is_above_latin1 || 0;
709 next unless is(test_UTF8_IS_ABOVE_LATIN1($b),
711 " Verify UTF8_IS_ABOVE_LATIN1($hex_b) is "
712 . "$display_is_above_latin1");
714 my $is_possibly_problematic = $j == 0
718 my $display_is_possibly_problematic = $is_possibly_problematic || 0;
719 next unless is(test_isUTF8_POSSIBLY_PROBLEMATIC($b),
720 $is_possibly_problematic,
721 " Verify isUTF8_POSSIBLY_PROBLEMATIC($hex_b) is "
722 . "$display_is_above_latin1");
725 # We are not trying to look for warnings, etc, so if they should occur, it
726 # is an error. But some of the code points here do cause warnings, so we
727 # check here and turn off the ones that apply to such code points. A
728 # later section of the code tests for these kinds of things.
729 my $this_utf8_flags = $look_for_everything_utf8n_to;
730 my $len = length $bytes;
732 my $valid_under_strict = 1;
733 my $valid_under_c9strict = 1;
734 my $valid_for_fits_in_31_bits = 1;
736 $this_utf8_flags &= ~($UTF8_DISALLOW_SUPER|$UTF8_WARN_SUPER);
737 $valid_under_strict = 0;
738 $valid_under_c9strict = 0;
739 if ($n > 2 ** 31 - 1) {
741 ~($UTF8_DISALLOW_ABOVE_31_BIT|$UTF8_WARN_ABOVE_31_BIT);
742 $valid_for_fits_in_31_bits = 0;
745 elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
746 $this_utf8_flags &= ~($UTF8_DISALLOW_NONCHAR|$UTF8_WARN_NONCHAR);
747 $valid_under_strict = 0;
749 elsif ($n >= 0xD800 && $n <= 0xDFFF) {
750 $this_utf8_flags &= ~($UTF8_DISALLOW_SURROGATE|$UTF8_WARN_SURROGATE);
751 $valid_under_c9strict = 0;
752 $valid_under_strict = 0;
757 my $display_flags = sprintf "0x%x", $this_utf8_flags;
758 my $display_bytes = display_bytes($bytes);
759 my $ret_ref = test_utf8n_to_uvchr_error($bytes, $len, $this_utf8_flags);
761 # Rest of tests likely meaningless if it gets the wrong code point.
762 next unless is($ret_ref->[0], $n,
763 "Verify utf8n_to_uvchr_error($display_bytes, $display_flags)"
765 is($ret_ref->[1], $len,
766 "Verify utf8n_to_uvchr_error() for $hex_n returns expected length:"
769 unless (is(scalar @warnings, 0,
770 "Verify utf8n_to_uvchr_error() for $hex_n generated no warnings"))
772 output_warnings(@warnings);
775 "Verify utf8n_to_uvchr_error() returned no error bits");
779 my $ret = test_isUTF8_CHAR($bytes, $len);
781 "Verify isUTF8_CHAR($display_bytes) returns expected length: $len");
783 unless (is(scalar @warnings, 0,
784 "Verify isUTF8_CHAR() for $hex_n generated no warnings"))
786 output_warnings(@warnings);
791 $ret = test_isUTF8_CHAR($bytes, $len - 1);
793 "Verify isUTF8_CHAR() with too short length parameter returns 0");
795 is(scalar @warnings, 0, "Verify isUTF8_CHAR() generated no warnings")
796 or output_warnings(@warnings);
800 $ret = test_isUTF8_CHAR_flags($bytes, $len, 0);
801 is($ret, $len, "Verify isUTF8_CHAR_flags($display_bytes, 0)"
802 . " returns expected length: $len");
804 is(scalar @warnings, 0,
805 "Verify isUTF8_CHAR_flags() for $hex_n generated no warnings")
806 or output_warnings(@warnings);
810 $ret = test_isUTF8_CHAR_flags($bytes, $len - 1, 0);
812 "Verify isUTF8_CHAR_flags() with too short length parameter returns 0");
814 is(scalar @warnings, 0, "Verify isUTF8_CHAR_flags() generated no warnings")
815 or output_warnings(@warnings);
819 $ret = test_isSTRICT_UTF8_CHAR($bytes, $len);
820 my $expected_len = ($valid_under_strict) ? $len : 0;
821 is($ret, $expected_len, "Verify isSTRICT_UTF8_CHAR($display_bytes)"
822 . " returns expected length: $expected_len");
824 is(scalar @warnings, 0,
825 "Verify isSTRICT_UTF8_CHAR() for $hex_n generated no warnings")
826 or output_warnings(@warnings);
830 $ret = test_isSTRICT_UTF8_CHAR($bytes, $len - 1);
832 "Verify isSTRICT_UTF8_CHAR() with too short length parameter returns 0");
834 is(scalar @warnings, 0, "Verify isSTRICT_UTF8_CHAR() generated no warnings")
835 or output_warnings(@warnings);
839 $ret = test_isUTF8_CHAR_flags($bytes, $len,
840 $UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
841 is($ret, $expected_len,
842 "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
843 . " acts like isSTRICT_UTF8_CHAR");
845 is(scalar @warnings, 0,
846 "Verify isUTF8_CHAR() for $hex_n generated no warnings")
847 or output_warnings(@warnings);
851 $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len);
852 $expected_len = ($valid_under_c9strict) ? $len : 0;
853 is($ret, $expected_len, "Verify isC9_STRICT_UTF8_CHAR($display_bytes)"
854 . " returns expected length: $len");
856 is(scalar @warnings, 0,
857 "Verify isC9_STRICT_UTF8_CHAR() for $hex_n generated no warnings")
858 or output_warnings(@warnings);
862 $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len - 1);
864 "Verify isC9_STRICT_UTF8_CHAR() with too short length parameter returns 0");
866 is(scalar @warnings, 0,
867 "Verify isC9_STRICT_UTF8_CHAR() generated no warnings")
868 or output_warnings(@warnings);
872 $ret = test_isUTF8_CHAR_flags($bytes, $len,
873 $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
874 is($ret, $expected_len,
875 "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
876 ." acts like isC9_STRICT_UTF8_CHAR");
878 is(scalar @warnings, 0,
879 "Verify isUTF8_CHAR() for $hex_n generated no warnings")
880 or output_warnings(@warnings);
884 $ret_ref = test_valid_utf8_to_uvchr($bytes);
885 is($ret_ref->[0], $n,
886 "Verify valid_utf8_to_uvchr($display_bytes) returns $hex_n");
887 is($ret_ref->[1], $len,
888 "Verify valid_utf8_to_uvchr() for $hex_n returns expected length: $len");
890 is(scalar @warnings, 0,
891 "Verify valid_utf8_to_uvchr() for $hex_n generated no warnings")
892 or output_warnings(@warnings);
894 # Similarly for uvchr_to_utf8
895 my $this_uvchr_flags = $look_for_everything_uvchr_to;
896 if ($n > 2 ** 31 - 1) {
898 ~($UNICODE_DISALLOW_ABOVE_31_BIT|$UNICODE_WARN_ABOVE_31_BIT);
901 $this_uvchr_flags &= ~($UNICODE_DISALLOW_SUPER|$UNICODE_WARN_SUPER);
903 elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
904 $this_uvchr_flags &= ~($UNICODE_DISALLOW_NONCHAR|$UNICODE_WARN_NONCHAR);
906 elsif ($n >= 0xD800 && $n <= 0xDFFF) {
908 &= ~($UNICODE_DISALLOW_SURROGATE|$UNICODE_WARN_SURROGATE);
910 $display_flags = sprintf "0x%x", $this_uvchr_flags;
914 $ret = test_uvchr_to_utf8_flags($n, $this_uvchr_flags);
916 "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returned success");
918 "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returns correct bytes");
920 is(scalar @warnings, 0,
921 "Verify uvchr_to_utf8_flags($hex_n, $display_flags) for $hex_n"
922 . " generated no warnings")
923 or output_warnings(@warnings);
925 # Now append this code point to a string that we will test various
926 # versions of is_foo_utf8_string_bar on, and keep a count of how many code
927 # points are in it. All the code points in this loop are valid in Perl's
928 # extended UTF-8, but some are not valid under various restrictions. A
929 # string and count is kept separately that is entirely valid for each
930 # restriction. And, for each restriction, we note the first occurrence in
931 # the unrestricted string where we find something not in the restricted
933 $restriction_types{""}{'valid_strings'} .= $bytes;
934 $restriction_types{""}{'valid_counts'}++;
936 if ($valid_under_c9strict) {
937 $restriction_types{"c9strict"}{'valid_strings'} .= $bytes;
938 $restriction_types{"c9strict"}{'valid_counts'}++;
940 elsif (! exists $restriction_types{"c9strict"}{'first_invalid_offset'}) {
941 $restriction_types{"c9strict"}{'first_invalid_offset'}
942 = length $restriction_types{"c9strict"}{'valid_strings'};
943 $restriction_types{"c9strict"}{'first_invalid_count'}
944 = $restriction_types{"c9strict"}{'valid_counts'};
947 if ($valid_under_strict) {
948 $restriction_types{"strict"}{'valid_strings'} .= $bytes;
949 $restriction_types{"strict"}{'valid_counts'}++;
951 elsif (! exists $restriction_types{"strict"}{'first_invalid_offset'}) {
952 $restriction_types{"strict"}{'first_invalid_offset'}
953 = length $restriction_types{"strict"}{'valid_strings'};
954 $restriction_types{"strict"}{'first_invalid_count'}
955 = $restriction_types{"strict"}{'valid_counts'};
958 if ($valid_for_fits_in_31_bits) {
959 $restriction_types{"fits_in_31_bits"}{'valid_strings'} .= $bytes;
960 $restriction_types{"fits_in_31_bits"}{'valid_counts'}++;
963 $restriction_types{"fits_in_31_bits"}{'first_invalid_offset'})
965 $restriction_types{"fits_in_31_bits"}{'first_invalid_offset'}
966 = length $restriction_types{"fits_in_31_bits"}{'valid_strings'};
967 $restriction_types{"fits_in_31_bits"}{'first_invalid_count'}
968 = $restriction_types{"fits_in_31_bits"}{'valid_counts'};
972 my $I8c = (isASCII) ? "\x80" : "\xa0"; # A continuation byte
973 my $cont_byte = I8_to_native($I8c);
974 my $p = (isASCII) ? "\xe1\x80" : I8_to_native("\xE4\xA0"); # partial
976 # The loop above tested the single or partial character functions/macros,
977 # while building up strings to test the string functions, which we do now.
979 for my $restriction (sort keys %restriction_types) {
982 for my $use_flags ("", "_flags") {
984 # For each restriction, we test it in both the is_foo_flags functions
985 # and the specially named foo function. But not if there isn't such a
986 # specially named function. Currently, this is the only tested
987 # restriction that doesn't have a specially named function
988 next if $use_flags eq "" && $restriction eq "fits_in_31_bits";
990 # Start building up the name of the function we will test.
991 my $base_name = "is_";
993 if (! $use_flags && $restriction ne "") {
994 $base_name .= $restriction . "_";
997 # We test both "is_utf8_string_foo" and "is_fixed_width_buf" functions
998 foreach my $operand ('string', 'fixed_width_buf') {
1000 # Currently, the only fixed_width_buf functions have the '_flags'
1002 next if $operand eq 'fixed_width_buf' && $use_flags eq "";
1004 my $name = "${base_name}utf8_$operand";
1006 # We test each version of the function
1007 for my $function ("_loclen", "_loc", "") {
1009 # We test each function against
1011 # b) invalid input created by appending an out-of-place
1012 # continuation character to the valid string
1013 # c) input created by appending a partial character. This
1014 # is valid in the 'fixed_width' functions, but invalid in
1016 # d) invalid input created by calling a function that is
1017 # expecting a restricted form of the input using the string
1018 # that's valid when unrestricted
1019 for my $error_type (0, $cont_byte, $p, $restriction) {
1020 #diag "restriction=$restriction, use_flags=$use_flags, function=$function, error_type=" . display_bytes($error_type);
1022 # If there is no restriction, the error type will be "",
1023 # which is redundant with 0.
1024 next if $error_type eq "";
1026 my $this_name = "$name$function$use_flags";
1028 = $restriction_types{$restriction}{'valid_strings'};
1029 my $expected_offset = length $bytes;
1031 = $restriction_types{$restriction}{'valid_counts'};
1032 my $test_name_suffix = "";
1034 my $this_error_type = $error_type;
1035 if ($this_error_type) {
1037 # Appending a bare continuation byte or a partial
1038 # character doesn't change the character count or
1039 # offset. But in the other cases, we have saved where
1040 # the failures should occur, so use those. Appending
1041 # a continuation byte makes it invalid; appending a
1042 # partial character makes the 'string' form invalid,
1043 # but not the 'fixed_width_buf' form.
1044 if ( $this_error_type eq $cont_byte
1045 || $this_error_type eq $p)
1047 $bytes .= $this_error_type;
1048 if ($this_error_type eq $cont_byte) {
1050 = " for an unexpected continuation";
1054 = " if ends with a partial character";
1056 = 0 if $operand eq "fixed_width_buf";
1061 = " if contains forbidden code points";
1062 if ($this_error_type eq "c9strict") {
1063 $bytes = $restriction_types{""}{'valid_strings'};
1065 = $restriction_types{"c9strict"}
1066 {'first_invalid_offset'};
1068 = $restriction_types{"c9strict"}
1069 {'first_invalid_count'};
1071 elsif ($this_error_type eq "strict") {
1072 $bytes = $restriction_types{""}{'valid_strings'};
1074 = $restriction_types{"strict"}
1075 {'first_invalid_offset'};
1077 = $restriction_types{"strict"}
1078 {'first_invalid_count'};
1081 elsif ($this_error_type eq "fits_in_31_bits") {
1082 $bytes = $restriction_types{""}{'valid_strings'};
1084 = $restriction_types{"fits_in_31_bits"}
1085 {'first_invalid_offset'};
1087 = $restriction_types{"fits_in_31_bits"}
1088 {'first_invalid_count'};
1091 fail("Internal test error: Unknown error type "
1092 . "'$this_error_type'");
1098 my $length = length $bytes;
1101 my $test = "\$ret_ref = test_$this_name(\$bytes, $length";
1103 # If using the _flags functions, we have to figure out what
1104 # flags to pass. This is done to match the restriction.
1105 if ($use_flags eq "_flags") {
1106 if (! $restriction) {
1107 $test .= ", 0"; # The flag
1109 # Indicate the kind of flag in the test name.
1110 $this_name .= "(0)";
1113 $this_name .= "($restriction)";
1114 if ($restriction eq "c9strict") {
1116 .= ", $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE";
1118 elsif ($restriction eq "strict") {
1119 $test .= ", $UTF8_DISALLOW_ILLEGAL_INTERCHANGE";
1121 elsif ($restriction eq "fits_in_31_bits") {
1122 $test .= ", $UTF8_DISALLOW_ABOVE_31_BIT";
1125 fail("Internal test error: Unknown restriction "
1126 . "'$restriction'");
1133 # Actually run the test
1145 if ($function eq "") {
1146 $ret = $ret_ref; # For plain function, there's only a
1147 # single return value
1149 else { # Otherwise, the multiple values come in an array.
1150 $ret = shift @$ret_ref ;
1151 $error_offset = shift @$ret_ref;
1152 $cp_count = shift@$ret_ref if $function eq "_loclen";
1155 if ($this_error_type) {
1157 "Verify $this_name is FALSE$test_name_suffix");
1161 "Verify $this_name is TRUE for valid input"
1162 . "$test_name_suffix"))
1164 diag(" The bytes starting at offset"
1165 . " $error_offset are"
1166 . display_bytes(substr(
1167 $restriction_types{$restriction}
1174 if ($function ne "") {
1175 unless (is($error_offset, $expected_offset,
1176 "\tAnd returns the correct offset"))
1178 my $min = ($error_offset < $expected_offset)
1181 diag(" The bytes starting at offset" . $min
1182 . " are " . display_bytes(substr($bytes, $min)));
1185 if ($function eq '_loclen') {
1186 is($cp_count, $expected_count,
1187 "\tAnd returns the correct character count");
1196 my $REPLACEMENT = 0xFFFD;
1198 # Now test the malformations. All these raise category utf8 warnings.
1199 my @malformations = (
1200 # ($testname, $bytes, $length, $allow_flags, $expected_error_flags,
1201 # $allowed_uv, $expected_len, $needed_to_discern_len, $message )
1202 [ "zero length string malformation", "", 0,
1203 $UTF8_ALLOW_EMPTY, $UTF8_GOT_EMPTY, 0, 0, 0,
1206 [ "orphan continuation byte malformation", I8_to_native("${I8c}a"), 2,
1207 $UTF8_ALLOW_CONTINUATION, $UTF8_GOT_CONTINUATION, $REPLACEMENT,
1209 qr/unexpected continuation byte/
1211 [ "premature next character malformation (immediate)",
1212 (isASCII) ? "\xc2\xc2\x80" : I8_to_native("\xc5\xc5\xa0"),
1214 $UTF8_ALLOW_NON_CONTINUATION, $UTF8_GOT_NON_CONTINUATION, $REPLACEMENT,
1216 qr/unexpected non-continuation byte.*immediately after start byte/
1218 [ "premature next character malformation (non-immediate)",
1219 I8_to_native("\xef${I8c}a"), 3,
1220 $UTF8_ALLOW_NON_CONTINUATION, $UTF8_GOT_NON_CONTINUATION, $REPLACEMENT,
1222 qr/unexpected non-continuation byte .* 2 bytes after start byte/
1224 [ "too short malformation", I8_to_native("\xf1${I8c}a"), 2,
1225 # Having the 'a' after this, but saying there are only 2 bytes also
1226 # tests that we pay attention to the passed in length
1227 $UTF8_ALLOW_SHORT, $UTF8_GOT_SHORT, $REPLACEMENT,
1229 qr/2 bytes available, need 4/
1231 [ "overlong malformation, lowest 2-byte",
1232 (isASCII) ? "\xc0\x80" : I8_to_native("\xc0\xa0"),
1234 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1239 [ "overlong malformation, highest 2-byte",
1240 (isASCII) ? "\xc1\xbf" : I8_to_native("\xc4\xbf"),
1242 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1243 (isASCII) ? 0x7F : utf8::unicode_to_native(0x9F),
1247 [ "overlong malformation, lowest 3-byte",
1248 (isASCII) ? "\xe0\x80\x80" : I8_to_native("\xe0\xa0\xa0"),
1250 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1252 3, (isASCII) ? 2 : 1,
1255 [ "overlong malformation, highest 3-byte",
1256 (isASCII) ? "\xe0\x9f\xbf" : I8_to_native("\xe0\xbf\xbf"),
1258 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1259 (isASCII) ? 0x7FF : 0x3FF,
1260 3, (isASCII) ? 2 : 1,
1263 [ "overlong malformation, lowest 4-byte",
1264 (isASCII) ? "\xf0\x80\x80\x80" : I8_to_native("\xf0\xa0\xa0\xa0"),
1266 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1271 [ "overlong malformation, highest 4-byte",
1272 (isASCII) ? "\xf0\x8F\xbf\xbf" : I8_to_native("\xf0\xaf\xbf\xbf"),
1274 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1275 (isASCII) ? 0xFFFF : 0x3FFF,
1279 [ "overlong malformation, lowest 5-byte",
1281 ? "\xf8\x80\x80\x80\x80"
1282 : I8_to_native("\xf8\xa0\xa0\xa0\xa0"),
1284 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1289 [ "overlong malformation, highest 5-byte",
1291 ? "\xf8\x87\xbf\xbf\xbf"
1292 : I8_to_native("\xf8\xa7\xbf\xbf\xbf"),
1294 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1295 (isASCII) ? 0x1FFFFF : 0x3FFFF,
1299 [ "overlong malformation, lowest 6-byte",
1301 ? "\xfc\x80\x80\x80\x80\x80"
1302 : I8_to_native("\xfc\xa0\xa0\xa0\xa0\xa0"),
1304 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1309 [ "overlong malformation, highest 6-byte",
1311 ? "\xfc\x83\xbf\xbf\xbf\xbf"
1312 : I8_to_native("\xfc\xa3\xbf\xbf\xbf\xbf"),
1314 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1315 (isASCII) ? 0x3FFFFFF : 0x3FFFFF,
1319 [ "overlong malformation, lowest 7-byte",
1321 ? "\xfe\x80\x80\x80\x80\x80\x80"
1322 : I8_to_native("\xfe\xa0\xa0\xa0\xa0\xa0\xa0"),
1324 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1329 [ "overlong malformation, highest 7-byte",
1331 ? "\xfe\x81\xbf\xbf\xbf\xbf\xbf"
1332 : I8_to_native("\xfe\xa1\xbf\xbf\xbf\xbf\xbf"),
1334 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1335 (isASCII) ? 0x7FFFFFFF : 0x3FFFFFF,
1341 if (isASCII && ! $is64bit) { # 32-bit ASCII platform
1342 no warnings 'portable';
1343 push @malformations,
1344 [ "overflow malformation",
1345 "\xfe\x84\x80\x80\x80\x80\x80", # Represents 2**32
1347 0, # There is no way to allow this malformation
1353 [ "overflow malformation",
1354 "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
1356 0, # There is no way to allow this malformation
1363 else { # 64-bit ASCII, or EBCDIC of any size.
1364 # On EBCDIC platforms, another overlong test is needed even on 32-bit
1365 # systems, whereas it doesn't happen on ASCII except on 64-bit ones.
1367 no warnings 'portable';
1368 no warnings 'overflow'; # Doesn't run on 32-bit systems, but compiles
1369 push @malformations,
1370 [ "overlong malformation, lowest max-byte",
1372 ? "\xff\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
1374 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1376 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1378 $max_bytes, (isASCII) ? 7 : 8,
1381 [ "overlong malformation, highest max-byte",
1382 (isASCII) # 2**36-1 on ASCII; 2**30-1 on EBCDIC
1383 ? "\xff\x80\x80\x80\x80\x80\x80\xbf\xbf\xbf\xbf\xbf\xbf"
1385 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xbf\xbf\xbf\xbf\xbf\xbf"),
1387 $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1388 (isASCII) ? 0xFFFFFFFFF : 0x3FFFFFFF,
1389 $max_bytes, (isASCII) ? 7 : 8,
1393 if (! $is64bit) { # 32-bit EBCDIC
1394 push @malformations,
1395 [ "overflow malformation",
1397 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"),
1399 0, # There is no way to allow this malformation
1406 else { # 64-bit, either ASCII or EBCDIC
1407 push @malformations,
1408 [ "overflow malformation",
1410 ? "\xff\x80\x90\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"
1412 "\xff\xb0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1414 0, # There is no way to allow this malformation
1417 $max_bytes, (isASCII) ? 3 : 2,
1423 foreach my $test (@malformations) {
1424 my ($testname, $bytes, $length, $allow_flags, $expected_error_flags,
1425 $allowed_uv, $expected_len, $needed_to_discern_len, $message ) = @$test;
1427 if (length($bytes) < $length) {
1428 fail("Internal test error: actual buffer length (" . length($bytes)
1429 . ") must be at least as high as how far we are allowed to read"
1430 . " into it ($length)");
1437 my $ret = test_isUTF8_CHAR($bytes, $length);
1438 is($ret, 0, "$testname: isUTF8_CHAR returns 0");
1439 is(scalar @warnings, 0, "$testname: isUTF8_CHAR() generated no warnings")
1440 or output_warnings(@warnings);
1444 $ret = test_isUTF8_CHAR_flags($bytes, $length, 0);
1445 is($ret, 0, "$testname: isUTF8_CHAR_flags returns 0");
1446 is(scalar @warnings, 0, "$testname: isUTF8_CHAR_flags() generated no"
1448 or output_warnings(@warnings);
1450 $ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
1451 is($ret, 0, "$testname: isSTRICT_UTF8_CHAR returns 0");
1452 is(scalar @warnings, 0,
1453 "$testname: isSTRICT_UTF8_CHAR() generated no warnings")
1454 or output_warnings(@warnings);
1456 $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
1457 is($ret, 0, "$testname: isC9_STRICT_UTF8_CHAR returns 0");
1458 is(scalar @warnings, 0,
1459 "$testname: isC9_STRICT_UTF8_CHAR() generated no warnings")
1460 or output_warnings(@warnings);
1462 for my $j (1 .. $length - 1) {
1463 my $partial = substr($bytes, 0, $j);
1467 $ret = test_is_utf8_valid_partial_char_flags($bytes, $j, 0);
1469 my $ret_should_be = 0;
1471 if ($j < $needed_to_discern_len) {
1473 $comment = ", but need $needed_to_discern_len bytes to discern:";
1476 is($ret, $ret_should_be, "$testname: is_utf8_valid_partial_char_flags("
1477 . display_bytes($partial)
1478 . ")$comment returns $ret_should_be");
1479 is(scalar @warnings, 0,
1480 "$testname: is_utf8_valid_partial_char_flags() generated"
1482 or output_warnings(@warnings);
1486 # Test what happens when this malformation is not allowed
1488 my $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, 0);
1489 is($ret_ref->[0], 0, "$testname: disallowed: Returns 0");
1490 is($ret_ref->[1], $expected_len,
1491 "$testname: utf8n_to_uvchr_error(), disallowed: Returns expected"
1492 . " length: $expected_len");
1493 if (is(scalar @warnings, 1,
1494 "$testname: disallowed: Got a single warning "))
1496 like($warnings[0], $message,
1497 "$testname: disallowed: Got expected warning");
1500 if (scalar @warnings) {
1501 output_warnings(@warnings);
1504 is($ret_ref->[2], $expected_error_flags,
1505 "$testname: utf8n_to_uvchr_error(), disallowed:"
1506 . " Returns expected error");
1508 { # Next test when disallowed, and warnings are off.
1511 my $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, 0);
1512 is($ret_ref->[0], 0,
1513 "$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
1515 is($ret_ref->[1], $expected_len,
1516 "$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
1517 . " Returns expected length: $expected_len");
1518 if (!is(scalar @warnings, 0,
1519 "$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
1520 . " no warnings generated"))
1522 output_warnings(@warnings);
1524 is($ret_ref->[2], $expected_error_flags,
1525 "$testname: utf8n_to_uvchr_error(), disallowed: Returns"
1526 . " expected error");
1529 # Test with CHECK_ONLY
1531 $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, $UTF8_CHECK_ONLY);
1532 is($ret_ref->[0], 0, "$testname: CHECK_ONLY: Returns 0");
1533 is($ret_ref->[1], -1, "$testname: CHECK_ONLY: returns -1 for length");
1534 if (! is(scalar @warnings, 0,
1535 "$testname: CHECK_ONLY: no warnings generated"))
1537 output_warnings(@warnings);
1539 is($ret_ref->[2], $expected_error_flags,
1540 "$testname: utf8n_to_uvchr_error(), disallowed: Returns expected"
1543 next if $allow_flags == 0; # Skip if can't allow this malformation
1545 # Test when the malformation is allowed
1547 $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, $allow_flags);
1548 is($ret_ref->[0], $allowed_uv,
1549 "$testname: utf8n_to_uvchr_error(), allowed: Returns expected uv: "
1550 . sprintf("0x%04X", $allowed_uv));
1551 is($ret_ref->[1], $expected_len,
1552 "$testname: utf8n_to_uvchr_error(), allowed: Returns expected length:"
1553 . " $expected_len");
1554 if (!is(scalar @warnings, 0,
1555 "$testname: utf8n_to_uvchr_error(), allowed: no warnings"
1558 output_warnings(@warnings);
1560 is($ret_ref->[2], $expected_error_flags,
1561 "$testname: utf8n_to_uvchr_error(), disallowed: Returns"
1562 . " expected error");
1565 sub nonportable_regex ($) {
1567 # Returns a pattern that matches the non-portable message raised either
1568 # for the specific input code point, or the one generated when there
1569 # is some malformation that precludes the message containing the specific
1572 my $code_point = shift;
1574 my $string = sprintf '(Code point 0x%X is not Unicode, and'
1575 . '|Any UTF-8 sequence that starts with'
1576 . ' "(\\\x[[:xdigit:]]{2})+" is for a'
1577 . ' non-Unicode code point, and is) not portable',
1582 # Now test the cases where a legal code point is generated, but may or may not
1583 # be allowed/warned on.
1585 # ($testname, $bytes, $warn_flags, $disallow_flags, $expected_error_flags,
1586 # $category, $allowed_uv, $expected_len, $needed_to_discern_len, $message )
1587 [ "lowest surrogate",
1588 (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
1589 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
1590 'surrogate', 0xD800,
1595 [ "a middle surrogate",
1596 (isASCII) ? "\xed\xa4\x8d" : I8_to_native("\xf1\xb6\xa8\xad"),
1597 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
1598 'surrogate', 0xD90D,
1603 [ "highest surrogate",
1604 (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
1605 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
1606 'surrogate', 0xDFFF,
1611 [ "first non_unicode",
1612 (isASCII) ? "\xf4\x90\x80\x80" : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
1613 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
1614 'non_unicode', 0x110000,
1617 qr/(not Unicode|for a non-Unicode code point).* may not be portable/
1619 [ "non_unicode whose first byte tells that",
1620 (isASCII) ? "\xf5\x80\x80\x80" : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
1621 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
1623 (isASCII) ? 0x140000 : 0x200000,
1626 qr/(not Unicode|for a non-Unicode code point).* may not be portable/
1628 [ "first of 32 consecutive non-character code points",
1629 (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
1630 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1634 qr/Unicode non-character.*is not recommended for open interchange/
1636 [ "a mid non-character code point of the 32 consecutive ones",
1637 (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
1638 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1642 qr/Unicode non-character.*is not recommended for open interchange/
1644 [ "final of 32 consecutive non-character code points",
1645 (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
1646 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1650 qr/Unicode non-character.*is not recommended for open interchange/
1652 [ "non-character code point U+FFFE",
1653 (isASCII) ? "\xef\xbf\xbe" : I8_to_native("\xf1\xbf\xbf\xbe"),
1654 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1658 qr/Unicode non-character.*is not recommended for open interchange/
1660 [ "non-character code point U+FFFF",
1661 (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
1662 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1666 qr/Unicode non-character.*is not recommended for open interchange/
1668 [ "non-character code point U+1FFFE",
1669 (isASCII) ? "\xf0\x9f\xbf\xbe" : I8_to_native("\xf3\xbf\xbf\xbe"),
1670 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1673 qr/Unicode non-character.*is not recommended for open interchange/
1675 [ "non-character code point U+1FFFF",
1676 (isASCII) ? "\xf0\x9f\xbf\xbf" : I8_to_native("\xf3\xbf\xbf\xbf"),
1677 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1680 qr/Unicode non-character.*is not recommended for open interchange/
1682 [ "non-character code point U+2FFFE",
1683 (isASCII) ? "\xf0\xaf\xbf\xbe" : I8_to_native("\xf5\xbf\xbf\xbe"),
1684 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1687 qr/Unicode non-character.*is not recommended for open interchange/
1689 [ "non-character code point U+2FFFF",
1690 (isASCII) ? "\xf0\xaf\xbf\xbf" : I8_to_native("\xf5\xbf\xbf\xbf"),
1691 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1694 qr/Unicode non-character.*is not recommended for open interchange/
1696 [ "non-character code point U+3FFFE",
1697 (isASCII) ? "\xf0\xbf\xbf\xbe" : I8_to_native("\xf7\xbf\xbf\xbe"),
1698 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1701 qr/Unicode non-character.*is not recommended for open interchange/
1703 [ "non-character code point U+3FFFF",
1704 (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
1705 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1708 qr/Unicode non-character.*is not recommended for open interchange/
1710 [ "non-character code point U+4FFFE",
1711 (isASCII) ? "\xf1\x8f\xbf\xbe" : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
1712 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1716 qr/Unicode non-character.*is not recommended for open interchange/
1718 [ "non-character code point U+4FFFF",
1719 (isASCII) ? "\xf1\x8f\xbf\xbf" : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
1720 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1724 qr/Unicode non-character.*is not recommended for open interchange/
1726 [ "non-character code point U+5FFFE",
1727 (isASCII) ? "\xf1\x9f\xbf\xbe" : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
1728 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1732 qr/Unicode non-character.*is not recommended for open interchange/
1734 [ "non-character code point U+5FFFF",
1735 (isASCII) ? "\xf1\x9f\xbf\xbf" : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
1736 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1740 qr/Unicode non-character.*is not recommended for open interchange/
1742 [ "non-character code point U+6FFFE",
1743 (isASCII) ? "\xf1\xaf\xbf\xbe" : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
1744 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1748 qr/Unicode non-character.*is not recommended for open interchange/
1750 [ "non-character code point U+6FFFF",
1751 (isASCII) ? "\xf1\xaf\xbf\xbf" : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
1752 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1756 qr/Unicode non-character.*is not recommended for open interchange/
1758 [ "non-character code point U+7FFFE",
1759 (isASCII) ? "\xf1\xbf\xbf\xbe" : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
1760 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1764 qr/Unicode non-character.*is not recommended for open interchange/
1766 [ "non-character code point U+7FFFF",
1767 (isASCII) ? "\xf1\xbf\xbf\xbf" : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
1768 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1772 qr/Unicode non-character.*is not recommended for open interchange/
1774 [ "non-character code point U+8FFFE",
1775 (isASCII) ? "\xf2\x8f\xbf\xbe" : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
1776 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1780 qr/Unicode non-character.*is not recommended for open interchange/
1782 [ "non-character code point U+8FFFF",
1783 (isASCII) ? "\xf2\x8f\xbf\xbf" : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
1784 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1788 qr/Unicode non-character.*is not recommended for open interchange/
1790 [ "non-character code point U+9FFFE",
1791 (isASCII) ? "\xf2\x9f\xbf\xbe" : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
1792 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1796 qr/Unicode non-character.*is not recommended for open interchange/
1798 [ "non-character code point U+9FFFF",
1799 (isASCII) ? "\xf2\x9f\xbf\xbf" : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
1800 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1804 qr/Unicode non-character.*is not recommended for open interchange/
1806 [ "non-character code point U+AFFFE",
1807 (isASCII) ? "\xf2\xaf\xbf\xbe" : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
1808 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1812 qr/Unicode non-character.*is not recommended for open interchange/
1814 [ "non-character code point U+AFFFF",
1815 (isASCII) ? "\xf2\xaf\xbf\xbf" : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
1816 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1820 qr/Unicode non-character.*is not recommended for open interchange/
1822 [ "non-character code point U+BFFFE",
1823 (isASCII) ? "\xf2\xbf\xbf\xbe" : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
1824 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1828 qr/Unicode non-character.*is not recommended for open interchange/
1830 [ "non-character code point U+BFFFF",
1831 (isASCII) ? "\xf2\xbf\xbf\xbf" : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
1832 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1836 qr/Unicode non-character.*is not recommended for open interchange/
1838 [ "non-character code point U+CFFFE",
1839 (isASCII) ? "\xf3\x8f\xbf\xbe" : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
1840 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1844 qr/Unicode non-character.*is not recommended for open interchange/
1846 [ "non-character code point U+CFFFF",
1847 (isASCII) ? "\xf3\x8f\xbf\xbf" : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
1848 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1852 qr/Unicode non-character.*is not recommended for open interchange/
1854 [ "non-character code point U+DFFFE",
1855 (isASCII) ? "\xf3\x9f\xbf\xbe" : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
1856 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1860 qr/Unicode non-character.*is not recommended for open interchange/
1862 [ "non-character code point U+DFFFF",
1863 (isASCII) ? "\xf3\x9f\xbf\xbf" : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
1864 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1868 qr/Unicode non-character.*is not recommended for open interchange/
1870 [ "non-character code point U+EFFFE",
1871 (isASCII) ? "\xf3\xaf\xbf\xbe" : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
1872 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1876 qr/Unicode non-character.*is not recommended for open interchange/
1878 [ "non-character code point U+EFFFF",
1879 (isASCII) ? "\xf3\xaf\xbf\xbf" : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
1880 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1884 qr/Unicode non-character.*is not recommended for open interchange/
1886 [ "non-character code point U+FFFFE",
1887 (isASCII) ? "\xf3\xbf\xbf\xbe" : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
1888 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1892 qr/Unicode non-character.*is not recommended for open interchange/
1894 [ "non-character code point U+FFFFF",
1895 (isASCII) ? "\xf3\xbf\xbf\xbf" : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
1896 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1900 qr/Unicode non-character.*is not recommended for open interchange/
1902 [ "non-character code point U+10FFFE",
1903 (isASCII) ? "\xf4\x8f\xbf\xbe" : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
1904 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1905 'nonchar', 0x10FFFE,
1908 qr/Unicode non-character.*is not recommended for open interchange/
1910 [ "non-character code point U+10FFFF",
1911 (isASCII) ? "\xf4\x8f\xbf\xbf" : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
1912 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1913 'nonchar', 0x10FFFF,
1916 qr/Unicode non-character.*is not recommended for open interchange/
1918 [ "requires at least 32 bits",
1920 ? "\xfe\x82\x80\x80\x80\x80\x80"
1922 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
1923 # This code point is chosen so that it is representable in a UV on
1925 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
1926 $UTF8_GOT_ABOVE_31_BIT,
1928 (isASCII) ? 7 : $max_bytes,
1930 nonportable_regex(0x80000000)
1932 [ "highest 32 bit code point",
1934 ? "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
1936 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
1937 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
1938 $UTF8_GOT_ABOVE_31_BIT,
1940 (isASCII) ? 7 : $max_bytes,
1942 nonportable_regex(0xffffffff)
1944 [ "requires at least 32 bits, and use SUPER-type flags, instead of"
1947 ? "\xfe\x82\x80\x80\x80\x80\x80"
1949 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
1950 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
1952 (isASCII) ? 7 : $max_bytes,
1954 nonportable_regex(0x80000000)
1956 [ "overflow with warnings/disallow for more than 31 bits",
1957 # This tests the interaction of WARN_ABOVE_31_BIT/DISALLOW_ABOVE_31_BIT
1958 # with overflow. The overflow malformation is never allowed, so
1959 # preventing it takes precedence if the ABOVE_31_BIT options would
1960 # otherwise allow in an overflowing value. The ASCII code points (1
1961 # for 32-bits; 1 for 64) were chosen because the old overflow
1962 # detection algorithm did not catch them; this means this test also
1963 # checks for that fix. The EBCDIC are arbitrary overflowing ones
1964 # since we have no reports of failures with it.
1967 ? "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
1969 "\xff\xB0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"))
1971 ? "\xfe\x86\x80\x80\x80\x80\x80"
1973 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"))),
1974 $UTF8_WARN_ABOVE_31_BIT,
1975 $UTF8_DISALLOW_ABOVE_31_BIT,
1976 $UTF8_GOT_ABOVE_31_BIT,
1978 (! isASCII || $is64bit) ? $max_bytes : 7,
1979 (isASCII || $is64bit) ? 2 : 8,
1986 no warnings qw{portable overflow};
1988 [ "Lowest 33 bit code point: overflow",
1989 "\xFE\x84\x80\x80\x80\x80\x80",
1990 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
1991 $UTF8_GOT_ABOVE_31_BIT,
1992 'utf8', 0x100000000,
1994 qr/and( is)? not portable/
1999 no warnings qw{portable overflow};
2001 [ "More than 32 bits",
2003 ? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
2005 "\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2006 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
2007 $UTF8_GOT_ABOVE_31_BIT,
2008 'utf8', 0x1000000000,
2009 $max_bytes, (isASCII) ? 1 : 7,
2010 qr/and( is)? not portable/
2013 push @tests, # These could falsely show wrongly in a naive
2015 [ "requires at least 32 bits",
2017 "\xff\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2018 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2019 $UTF8_GOT_ABOVE_31_BIT,
2020 'utf8', 0x800000000,
2022 nonportable_regex(0x80000000)
2024 [ "requires at least 32 bits",
2026 "\xff\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2027 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2028 $UTF8_GOT_ABOVE_31_BIT,
2029 'utf8', 0x10000000000,
2031 nonportable_regex(0x10000000000)
2033 [ "requires at least 32 bits",
2035 "\xff\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2036 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2037 $UTF8_GOT_ABOVE_31_BIT,
2038 'utf8', 0x200000000000,
2040 nonportable_regex(0x20000000000)
2042 [ "requires at least 32 bits",
2044 "\xff\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2045 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2046 $UTF8_GOT_ABOVE_31_BIT,
2047 'utf8', 0x4000000000000,
2049 nonportable_regex(0x4000000000000)
2051 [ "requires at least 32 bits",
2053 "\xff\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2054 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2055 $UTF8_GOT_ABOVE_31_BIT,
2056 'utf8', 0x80000000000000,
2058 nonportable_regex(0x80000000000000)
2060 [ "requires at least 32 bits",
2062 "\xff\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2063 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2064 $UTF8_GOT_ABOVE_31_BIT,
2065 'utf8', 0x1000000000000000,
2067 nonportable_regex(0x1000000000000000)
2072 foreach my $test (@tests) {
2073 my ($testname, $bytes, $warn_flags, $disallow_flags, $expected_error_flags,
2074 $category, $allowed_uv, $expected_len, $needed_to_discern_len, $message
2077 my $length = length $bytes;
2078 my $will_overflow = $testname =~ /overflow/ ? 'overflow' : "";
2083 my $ret = test_isUTF8_CHAR($bytes, $length);
2084 my $ret_flags = test_isUTF8_CHAR_flags($bytes, $length, 0);
2085 if ($will_overflow) {
2086 is($ret, 0, "isUTF8_CHAR() $testname: returns 0");
2087 is($ret_flags, 0, "isUTF8_CHAR_flags() $testname: returns 0");
2091 "isUTF8_CHAR() $testname: returns expected length: $length");
2092 is($ret_flags, $length, "isUTF8_CHAR_flags(...,0) $testname:"
2093 . " returns expected length: $length");
2095 is(scalar @warnings, 0,
2096 "isUTF8_CHAR() and isUTF8_CHAR()_flags $testname: generated"
2098 or output_warnings(@warnings);
2101 $ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
2102 if ($will_overflow) {
2103 is($ret, 0, "isSTRICT_UTF8_CHAR() $testname: returns 0");
2106 my $expected_ret = ( $testname =~ /surrogate|non-character/
2107 || $allowed_uv > 0x10FFFF)
2110 is($ret, $expected_ret, "isSTRICT_UTF8_CHAR() $testname: returns"
2111 . " expected length: $expected_ret");
2112 $ret = test_isUTF8_CHAR_flags($bytes, $length,
2113 $UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
2114 is($ret, $expected_ret,
2115 "isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
2116 . " acts like isSTRICT_UTF8_CHAR");
2118 is(scalar @warnings, 0,
2119 "isSTRICT_UTF8_CHAR() and isUTF8_CHAR_flags $testname:"
2120 . " generated no warnings")
2121 or output_warnings(@warnings);
2124 $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
2125 if ($will_overflow) {
2126 is($ret, 0, "isC9_STRICT_UTF8_CHAR() $testname: returns 0");
2129 my $expected_ret = ( $testname =~ /surrogate/
2130 || $allowed_uv > 0x10FFFF)
2133 is($ret, $expected_ret, "isC9_STRICT_UTF8_CHAR() $testname:"
2134 ." returns expected length: $expected_ret");
2135 $ret = test_isUTF8_CHAR_flags($bytes, $length,
2136 $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
2137 is($ret, $expected_ret,
2138 "isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
2139 . " acts like isC9_STRICT_UTF8_CHAR");
2141 is(scalar @warnings, 0,
2142 "isC9_STRICT_UTF8_CHAR() and isUTF8_CHAR_flags $testname:"
2143 . " generated no warnings")
2144 or output_warnings(@warnings);
2146 # Test partial character handling, for each byte not a full character
2147 for my $j (1.. $length - 1) {
2149 # Skip the test for the interaction between overflow and above-31
2150 # bit. It is really testing other things than the partial
2151 # character tests, for which other tests in this file are
2153 last if $testname =~ /overflow/;
2155 foreach my $disallow_flag (0, $disallow_flags) {
2156 my $partial = substr($bytes, 0, $j);
2159 if ($disallow_flag) {
2161 $comment = "disallowed";
2162 if ($j < $needed_to_discern_len) {
2164 $comment .= ", but need $needed_to_discern_len bytes"
2170 $comment = "allowed";
2175 $ret = test_is_utf8_valid_partial_char_flags($partial, $j,
2177 is($ret, $ret_should_be,
2178 "$testname: is_utf8_valid_partial_char_flags("
2179 . display_bytes($partial)
2180 . "), $comment: returns $ret_should_be");
2181 is(scalar @warnings, 0,
2182 "$testname: is_utf8_valid_partial_char_flags()"
2183 . " generated no warnings")
2184 or output_warnings(@warnings);
2189 # This is more complicated than the malformations tested earlier, as there
2190 # are several orthogonal variables involved. We test all the subclasses
2191 # of utf8 warnings to verify they work with and without the utf8 class,
2192 # and don't have effects on other sublass warnings
2193 foreach my $warning ('utf8', 'surrogate', 'nonchar', 'non_unicode') {
2194 foreach my $warn_flag (0, $warn_flags) {
2195 foreach my $disallow_flag (0, $disallow_flags) {
2196 foreach my $do_warning (0, 1) {
2198 # We try each of the above with various combinations of
2199 # malformations that can occur on the same input sequence.
2200 foreach my $short ("", "short") {
2201 foreach my $unexpected_noncont ("",
2202 "unexpected non-continuation")
2204 foreach my $overlong ("", "overlong") {
2206 # If we're already at the longest possible, we
2207 # can't create an overlong (which would be longer)
2208 # can't handle anything larger.
2209 next if $overlong && $expected_len >= $max_bytes;
2212 my @expected_errors;
2213 push @malformations, $short if $short;
2214 push @malformations, $unexpected_noncont
2215 if $unexpected_noncont;
2216 push @malformations, $overlong if $overlong;
2218 # The overflow malformation test in the input
2219 # array is coerced into being treated like one of
2221 if ($will_overflow) {
2222 push @malformations, 'overflow';
2223 push @expected_errors, $UTF8_GOT_OVERFLOW;
2226 my $malformations_name = join "/", @malformations;
2227 $malformations_name .= " malformation"
2228 if $malformations_name;
2229 $malformations_name .= "s" if @malformations > 1;
2230 my $this_bytes = $bytes;
2231 my $this_length = $length;
2232 my $expected_uv = $allowed_uv;
2233 my $this_expected_len = $expected_len;
2234 my $this_needed_to_discern_len = $needed_to_discern_len;
2235 if ($malformations_name) {
2238 # Coerce the input into the desired
2240 if ($malformations_name =~ /overlong/) {
2242 # For an overlong, we convert the original
2243 # start byte into a continuation byte with
2244 # the same data bits as originally. ...
2245 substr($this_bytes, 0, 1)
2246 = start_byte_to_cont(substr($this_bytes,
2249 # ... Then we prepend it with a known
2250 # overlong sequence. This should evaluate
2251 # to the exact same code point as the
2254 = I8_to_native("\xff")
2255 . (I8_to_native(chr $first_continuation)
2256 x ( $max_bytes - 1 - length($this_bytes)))
2258 $this_length = length($this_bytes);
2259 $this_needed_to_discern_len
2260 = $max_bytes - ($this_expected_len
2261 - $this_needed_to_discern_len);
2262 $this_expected_len = $max_bytes;
2263 push @expected_errors, $UTF8_GOT_LONG;
2265 if ($malformations_name =~ /short/) {
2267 # Just tell the test to not look far
2268 # enough into the input.
2270 $this_expected_len--;
2271 push @expected_errors, $UTF8_GOT_SHORT;
2273 if ($malformations_name
2274 =~ /non-continuation/)
2276 # Change the final continuation byte into
2278 my $pos = ($short) ? -2 : -1;
2279 substr($this_bytes, $pos, 1) = '?';
2280 $this_expected_len--;
2281 push @expected_errors,
2282 $UTF8_GOT_NON_CONTINUATION;
2286 my $eval_warn = $do_warning
2287 ? "use warnings '$warning'"
2288 : $warning eq "utf8"
2289 ? "no warnings 'utf8'"
2290 : ( "use warnings 'utf8';"
2291 . " no warnings '$warning'");
2293 # Is effectively disallowed if we've set up a
2294 # malformation, even if the flag indicates it is
2295 # allowed. Fix up test name to indicate this as
2297 my $disallowed = $disallow_flag
2298 || $malformations_name;
2299 my $this_name = "utf8n_to_uvchr_error() $testname: "
2305 $this_name .= ", $eval_warn";
2306 $this_name .= ", " . (($warn_flag)
2307 ? 'with warning flag'
2308 : 'no warning flag');
2312 my $display_bytes = display_bytes($this_bytes);
2313 my $call = " Call was: $eval_warn; \$ret_ref"
2314 . " = test_utf8n_to_uvchr_error("
2315 . "'$display_bytes', $this_length,"
2317 . "|$disallow_flag)";
2318 my $eval_text = "$eval_warn; \$ret_ref"
2319 . " = test_utf8n_to_uvchr_error("
2321 . " $this_length, $warn_flag"
2322 . "|$disallow_flag)";
2324 if (! ok ("$@ eq ''",
2325 "$this_name: eval succeeded"))
2327 diag "\$!='$!'; eval'd=\"$call\"";
2331 is($ret_ref->[0], 0, "$this_name: Returns 0")
2335 is($ret_ref->[0], $expected_uv,
2336 "$this_name: Returns expected uv: "
2337 . sprintf("0x%04X", $expected_uv))
2340 is($ret_ref->[1], $this_expected_len,
2341 "$this_name: Returns expected length:"
2342 . " $this_expected_len")
2345 my $errors = $ret_ref->[2];
2347 for (my $i = @expected_errors - 1; $i >= 0; $i--) {
2348 if (ok($expected_errors[$i] & $errors,
2349 "Expected and got error bit return"
2350 . " for $malformations[$i] malformation"))
2352 $errors &= ~$expected_errors[$i];
2354 splice @expected_errors, $i, 1;
2356 is(scalar @expected_errors, 0,
2357 "Got all the expected malformation errors")
2358 or diag Dumper \@expected_errors;
2360 if ( $this_expected_len >= $this_needed_to_discern_len
2361 && ($warn_flag || $disallow_flag))
2363 is($errors, $expected_error_flags,
2364 "Got the correct error flag")
2368 is($errors, 0, "Got no other error flag");
2371 if (@malformations) {
2372 if (! $do_warning && $warning eq 'utf8') {
2373 goto no_warnings_expected;
2376 # Check that each malformation generates a
2377 # warning, removing that warning if found
2379 foreach my $malformation (@malformations) {
2380 foreach (my $i = 0; $i < @warnings; $i++) {
2381 if ($warnings[$i] =~ /$malformation/) {
2382 pass("Expected and got"
2383 . "'$malformation' warning");
2384 splice @warnings, $i, 1;
2388 fail("Expected '$malformation' warning"
2389 . " but didn't get it");
2394 # Any overflow will override any super or above-31
2396 goto no_warnings_expected
2397 if $will_overflow || $this_expected_len
2398 < $this_needed_to_discern_len;
2401 && ( $warning eq 'utf8'
2402 || $warning eq $category))
2404 goto no_warnings_expected;
2406 elsif ($warn_flag) {
2407 if (is(scalar @warnings, 1,
2408 "$this_name: Got a single warning "))
2410 like($warnings[0], $message,
2411 "$this_name: Got expected warning")
2416 if (scalar @warnings) {
2417 output_warnings(@warnings);
2422 no_warnings_expected:
2423 unless (is(scalar @warnings, 0,
2424 "$this_name: Got no warnings"))
2427 output_warnings(@warnings);
2431 # Check CHECK_ONLY results when the input is
2432 # disallowed. Do this when actually disallowed,
2433 # not just when the $disallow_flag is set
2436 $ret_ref = test_utf8n_to_uvchr_error(
2437 $this_bytes, $this_length,
2438 $disallow_flag|$UTF8_CHECK_ONLY);
2439 is($ret_ref->[0], 0,
2440 "$this_name, CHECK_ONLY: Returns 0")
2442 is($ret_ref->[1], -1,
2443 "$this_name: CHECK_ONLY: returns -1 for length")
2445 if (! is(scalar @warnings, 0,
2446 "$this_name, CHECK_ONLY: no warnings"
2450 output_warnings(@warnings);
2454 # Now repeat some of the above, but for
2455 # uvchr_to_utf8_flags(). Since this comes from an
2456 # existing code point, it hasn't overflowed, and
2458 next if @malformations;
2460 # The warning and disallow flags passed in are for
2461 # utf8n_to_uvchr_error(). Convert them for
2462 # uvchr_to_utf8_flags().
2463 my $uvchr_warn_flag = 0;
2464 my $uvchr_disallow_flag = 0;
2466 if ($warn_flag == $UTF8_WARN_SURROGATE) {
2467 $uvchr_warn_flag = $UNICODE_WARN_SURROGATE
2469 elsif ($warn_flag == $UTF8_WARN_NONCHAR) {
2470 $uvchr_warn_flag = $UNICODE_WARN_NONCHAR
2472 elsif ($warn_flag == $UTF8_WARN_SUPER) {
2473 $uvchr_warn_flag = $UNICODE_WARN_SUPER
2475 elsif ($warn_flag == $UTF8_WARN_ABOVE_31_BIT) {
2477 = $UNICODE_WARN_ABOVE_31_BIT;
2480 fail(sprintf "Unexpected warn flag: %x",
2485 if ($disallow_flag) {
2486 if ($disallow_flag == $UTF8_DISALLOW_SURROGATE)
2488 $uvchr_disallow_flag
2489 = $UNICODE_DISALLOW_SURROGATE;
2491 elsif ($disallow_flag == $UTF8_DISALLOW_NONCHAR)
2493 $uvchr_disallow_flag
2494 = $UNICODE_DISALLOW_NONCHAR;
2496 elsif ($disallow_flag == $UTF8_DISALLOW_SUPER) {
2497 $uvchr_disallow_flag
2498 = $UNICODE_DISALLOW_SUPER;
2500 elsif ($disallow_flag
2501 == $UTF8_DISALLOW_ABOVE_31_BIT)
2503 $uvchr_disallow_flag =
2504 $UNICODE_DISALLOW_ABOVE_31_BIT;
2507 fail(sprintf "Unexpected disallow flag: %x",
2513 $disallowed = $uvchr_disallow_flag;
2515 $this_name = "uvchr_to_utf8_flags() $testname: "
2516 . (($uvchr_disallow_flag)
2519 ? 'ABOVE_31_BIT allowed'
2521 $this_name .= ", $eval_warn";
2522 $this_name .= ", " . (($uvchr_warn_flag)
2523 ? 'with warning flag'
2524 : 'no warning flag');
2528 my $warn_flag = sprintf "0x%x", $uvchr_warn_flag;
2529 my $disallow_flag = sprintf "0x%x",
2530 $uvchr_disallow_flag;
2531 $call = sprintf(" Call was: $eval_warn; \$ret"
2532 . " = test_uvchr_to_utf8_flags("
2533 . " 0x%x, $warn_flag|$disallow_flag)",
2535 $eval_text = "$eval_warn; \$ret ="
2536 . " test_uvchr_to_utf8_flags("
2537 . "$allowed_uv, $warn_flag|"
2538 . "$disallow_flag)";
2540 if (! ok ("$@ eq ''", "$this_name: eval succeeded"))
2542 diag "\$!='$!'; eval'd=\"$eval_text\"";
2546 is($ret, undef, "$this_name: Returns undef")
2550 is($ret, $bytes, "$this_name: Returns expected string")
2554 && ($warning eq 'utf8' || $warning eq $category))
2556 if (!is(scalar @warnings, 0,
2557 "$this_name: No warnings generated"))
2560 output_warnings(@warnings);
2563 elsif ( $uvchr_warn_flag
2564 && ( $warning eq 'utf8'
2565 || $warning eq $category))
2567 if (is(scalar @warnings, 1,
2568 "$this_name: Got a single warning "))
2570 like($warnings[0], $message,
2571 "$this_name: Got expected warning")
2576 output_warnings(@warnings)
2577 if scalar @warnings;
2592 or skip "These tests probably break on non-ASCII", 1;
2593 my $simple = join "", "A" .. "J";
2594 my $utf_ch = "\x{7fffffff}";
2595 utf8::encode($utf_ch);
2596 my $utf_ch_len = length $utf_ch;
2597 note "utf_ch_len $utf_ch_len";
2598 my $utf = $utf_ch x 10;
2599 my $bad_start = substr($utf, 1);
2600 # $bad_end ends with a start byte and a single continuation
2601 my $bad_end = substr($utf, 0, length($utf)-$utf_ch_len+2);
2603 # WARNING: all offsets are *byte* offsets
2606 # string s off expected name
2607 [ $simple, 0, 5, 5, "simple in range, forward" ],
2608 [ $simple, 10, -5, 5, "simple in range, backward" ],
2609 [ $simple, 5, 10, 10, "simple out of range, forward" ],
2610 [ $simple, 5, -10, 0, "simple out of range, backward" ],
2611 [ $utf, $utf_ch_len * 5, 5, length($utf), "utf in range, forward" ],
2612 [ $utf, $utf_ch_len * 5, -5, 0, "utf in range, backward" ],
2613 [ $utf, $utf_ch_len * 5, 4, $utf_ch_len * 9, "utf in range b, forward" ],
2614 [ $utf, $utf_ch_len * 5, -4, $utf_ch_len, "utf in range b, backward" ],
2615 [ $utf, $utf_ch_len * 5, 6, length($utf), "utf out of range, forward" ],
2616 [ $utf, $utf_ch_len * 5, -6, 0, "utf out of range, backward" ],
2617 [ $bad_start, 0, 1, 1, "bad start, forward 1 from 0" ],
2618 [ $bad_start, 0, $utf_ch_len-1, $utf_ch_len-1, "bad start, forward ch_len-1 from 0" ],
2619 [ $bad_start, 0, $utf_ch_len, $utf_ch_len*2-1, "bad start, forward ch_len from 0" ],
2620 [ $bad_start, $utf_ch_len-1, -1, 0, "bad start, back 1 from first start byte" ],
2621 [ $bad_start, $utf_ch_len-2, -1, 0, "bad start, back 1 from before first start byte" ],
2622 [ $bad_start, 0, -1, 0, "bad start, back 1 from 0" ],
2623 [ $bad_start, length $bad_start, -10, 0, "bad start, back 10 from end" ],
2624 [ $bad_end, 0, 10, length $bad_end, "bad end, forward 10 from 0" ],
2625 [ $bad_end, length($bad_end)-1, 10, length $bad_end, "bad end, forward 1 from end-1" ],
2628 for my $test (@hop_tests) {
2629 my ($str, $s_off, $off, $want, $name) = @$test;
2630 my $result = test_utf8_hop_safe($str, $s_off, $off);
2631 is($result, $want, "utf8_hop_safe: $name");