This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add macro for determining if UTF-8 is Unicode-strict
[perl5.git] / ext / XS-APItest / t / utf8.t
CommitLineData
fed3ba5d
NC
1#!perl -w
2
3use strict;
4use Test::More;
7dfd8446 5$|=1;
fed3ba5d 6
760c7c2f
KW
7no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
8 # machines, and that is tested elsewhere
9
fed3ba5d
NC
10use XS::APItest;
11
4deba822
KW
12my $pound_sign = chr utf8::unicode_to_native(163);
13
7dfd8446
KW
14sub isASCII { ord "A" == 65 }
15
16sub display_bytes {
9d2d0ecd 17 use bytes;
7dfd8446
KW
18 my $string = shift;
19 return '"'
20 . join("", map { sprintf("\\x%02x", ord $_) } split "", $string)
21 . '"';
22}
23
24# This test file can't use byte_utf8a_to_utf8n() from t/charset_tools.pl
25# because that uses the same functions we are testing here. So UTF-EBCDIC
26# strings are hard-coded as I8 strings in this file instead, and we use array
27# lookup to translate into the appropriate code page.
28
29my @i8_to_native = ( # Only code page 1047 so far.
30# _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F
310x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x15,0x0B,0x0C,0x0D,0x0E,0x0F,
320x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x1C,0x1D,0x1E,0x1F,
330x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,
340xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,
350x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,
360xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xAD,0xE0,0xBD,0x5F,0x6D,
370x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
380x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,
390x20,0x21,0x22,0x23,0x24,0x25,0x06,0x17,0x28,0x29,0x2A,0x2B,0x2C,0x09,0x0A,0x1B,
400x30,0x31,0x1A,0x33,0x34,0x35,0x36,0x08,0x38,0x39,0x3A,0x3B,0x04,0x14,0x3E,0xFF,
410x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x51,0x52,0x53,0x54,0x55,0x56,
420x57,0x58,0x59,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x70,0x71,0x72,0x73,
430x74,0x75,0x76,0x77,0x78,0x80,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x9A,0x9B,0x9C,
440x9D,0x9E,0x9F,0xA0,0xAA,0xAB,0xAC,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,
450xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBF,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xDA,0xDB,
460xDC,0xDD,0xDE,0xDF,0xE1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xFA,0xFB,0xFC,0xFD,0xFE,
47);
48
49*I8_to_native = (isASCII)
50 ? sub { return shift }
51 : sub { return join "", map { chr $i8_to_native[ord $_] }
52 split "", shift };
53
54my $is64bit = length sprintf("%x", ~0) > 8;
55
56
57# Test utf8n_to_uvchr(). These provide essentially complete code coverage.
58# Copied from utf8.h
59my $UTF8_ALLOW_EMPTY = 0x0001;
60my $UTF8_ALLOW_CONTINUATION = 0x0002;
61my $UTF8_ALLOW_NON_CONTINUATION = 0x0004;
62my $UTF8_ALLOW_SHORT = 0x0008;
63my $UTF8_ALLOW_LONG = 0x0010;
64my $UTF8_DISALLOW_SURROGATE = 0x0020;
65my $UTF8_WARN_SURROGATE = 0x0040;
66my $UTF8_DISALLOW_NONCHAR = 0x0080;
67my $UTF8_WARN_NONCHAR = 0x0100;
68my $UTF8_DISALLOW_SUPER = 0x0200;
69my $UTF8_WARN_SUPER = 0x0400;
1d1c12d9
KW
70my $UTF8_DISALLOW_ABOVE_31_BIT = 0x0800;
71my $UTF8_WARN_ABOVE_31_BIT = 0x1000;
7dfd8446
KW
72my $UTF8_CHECK_ONLY = 0x2000;
73
046d01eb
KW
74# Test uvchr_to_utf8().
75my $UNICODE_WARN_SURROGATE = 0x0001;
76my $UNICODE_WARN_NONCHAR = 0x0002;
77my $UNICODE_WARN_SUPER = 0x0004;
78my $UNICODE_WARN_ABOVE_31_BIT = 0x0008;
79my $UNICODE_DISALLOW_SURROGATE = 0x0010;
80my $UNICODE_DISALLOW_NONCHAR = 0x0020;
81my $UNICODE_DISALLOW_SUPER = 0x0040;
82my $UNICODE_DISALLOW_ABOVE_31_BIT = 0x0080;
83
84my $look_for_everything_utf8n_to
85 = $UTF8_DISALLOW_SURROGATE
7dfd8446
KW
86 | $UTF8_WARN_SURROGATE
87 | $UTF8_DISALLOW_NONCHAR
88 | $UTF8_WARN_NONCHAR
89 | $UTF8_DISALLOW_SUPER
90 | $UTF8_WARN_SUPER
1d1c12d9
KW
91 | $UTF8_DISALLOW_ABOVE_31_BIT
92 | $UTF8_WARN_ABOVE_31_BIT;
046d01eb
KW
93my $look_for_everything_uvchr_to
94 = $UNICODE_DISALLOW_SURROGATE
95 | $UNICODE_WARN_SURROGATE
96 | $UNICODE_DISALLOW_NONCHAR
97 | $UNICODE_WARN_NONCHAR
98 | $UNICODE_DISALLOW_SUPER
99 | $UNICODE_WARN_SUPER
100 | $UNICODE_DISALLOW_ABOVE_31_BIT
101 | $UNICODE_WARN_ABOVE_31_BIT;
7dfd8446 102
fed3ba5d
NC
103foreach ([0, '', '', 'empty'],
104 [0, 'N', 'N', '1 char'],
105 [1, 'NN', 'N', '1 char substring'],
106 [-2, 'Perl', 'Rules', 'different'],
4deba822
KW
107 [0, $pound_sign, $pound_sign, 'pound sign'],
108 [1, $pound_sign . 10, $pound_sign . 1, '10 pounds is more than 1 pound'],
109 [1, $pound_sign . $pound_sign, $pound_sign, '2 pound signs are more than 1'],
fed3ba5d
NC
110 [-2, ' $!', " \x{1F42B}!", 'Camels are worth more than 1 dollar'],
111 [-1, '!', "!\x{1F42A}", 'Initial substrings match'],
112 ) {
113 my ($expect, $left, $right, $desc) = @$_;
114 my $copy = $right;
115 utf8::encode($copy);
116 is(bytes_cmp_utf8($left, $copy), $expect, $desc);
117 next if $right =~ tr/\0-\377//c;
118 utf8::encode($left);
119 is(bytes_cmp_utf8($right, $left), -$expect, "$desc reversed");
120}
121
7dfd8446
KW
122# The keys to this hash are Unicode code points, their values are the native
123# UTF-8 representations of them. The code points are chosen because they are
124# "interesting" on either or both ASCII and EBCDIC platforms. First we add
125# boundaries where the number of bytes required to represent them increase, or
126# are adjacent to problematic code points, so we want to make sure they aren't
127# considered problematic.
128my %code_points = (
129 0x0100 => (isASCII) ? "\xc4\x80" : I8_to_native("\xc8\xa0"),
130 0x0400 - 1 => (isASCII) ? "\xcf\xbf" : I8_to_native("\xdf\xbf"),
131 0x0400 => (isASCII) ? "\xd0\x80" : I8_to_native("\xe1\xa0\xa0"),
132 0x0800 - 1 => (isASCII) ? "\xdf\xbf" : I8_to_native("\xe1\xbf\xbf"),
133 0x0800 => (isASCII) ? "\xe0\xa0\x80" : I8_to_native("\xe2\xa0\xa0"),
134 0x4000 - 1 => (isASCII) ? "\xe3\xbf\xbf" : I8_to_native("\xef\xbf\xbf"),
135 0x4000 => (isASCII) ? "\xe4\x80\x80" : I8_to_native("\xf0\xb0\xa0\xa0"),
136 0x8000 - 1 => (isASCII) ? "\xe7\xbf\xbf" : I8_to_native("\xf0\xbf\xbf\xbf"),
137
138 # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
139 # as of this writing, considers potentially problematic on EBCDIC
140 0x8000 => (isASCII) ? "\xe8\x80\x80" : I8_to_native("\xf1\xa0\xa0\xa0"),
141
142 0xD000 - 1 => (isASCII) ? "\xec\xbf\xbf" : I8_to_native("\xf1\xb3\xbf\xbf"),
143
144 # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
145 # as of this writing, considers potentially problematic on ASCII
146 0xD000 => (isASCII) ? "\xed\x80\x80" : I8_to_native("\xf1\xb4\xa0\xa0"),
147
148 # Bracket the surrogates
149 0xD7FF => (isASCII) ? "\xed\x9f\xbf" : I8_to_native("\xf1\xb5\xbf\xbf"),
150 0xE000 => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
151
152 # Bracket the 32 contiguous non characters
153 0xFDCF => (isASCII) ? "\xef\xb7\x8f" : I8_to_native("\xf1\xbf\xae\xaf"),
154 0xFDF0 => (isASCII) ? "\xef\xb7\xb0" : I8_to_native("\xf1\xbf\xaf\xb0"),
155
156 # Mostly bracket non-characters, but some are transitions to longer
157 # strings
158 0xFFFD => (isASCII) ? "\xef\xbf\xbd" : I8_to_native("\xf1\xbf\xbf\xbd"),
159 0x10000 - 1 => (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
160 0x10000 => (isASCII) ? "\xf0\x90\x80\x80" : I8_to_native("\xf2\xa0\xa0\xa0"),
161 0x1FFFD => (isASCII) ? "\xf0\x9f\xbf\xbd" : I8_to_native("\xf3\xbf\xbf\xbd"),
162 0x20000 => (isASCII) ? "\xf0\xa0\x80\x80" : I8_to_native("\xf4\xa0\xa0\xa0"),
163 0x2FFFD => (isASCII) ? "\xf0\xaf\xbf\xbd" : I8_to_native("\xf5\xbf\xbf\xbd"),
164 0x30000 => (isASCII) ? "\xf0\xb0\x80\x80" : I8_to_native("\xf6\xa0\xa0\xa0"),
165 0x3FFFD => (isASCII) ? "\xf0\xbf\xbf\xbd" : I8_to_native("\xf7\xbf\xbf\xbd"),
166 0x40000 - 1 => (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
167 0x40000 => (isASCII) ? "\xf1\x80\x80\x80" : I8_to_native("\xf8\xa8\xa0\xa0\xa0"),
168 0x4FFFD => (isASCII) ? "\xf1\x8f\xbf\xbd" : I8_to_native("\xf8\xa9\xbf\xbf\xbd"),
169 0x50000 => (isASCII) ? "\xf1\x90\x80\x80" : I8_to_native("\xf8\xaa\xa0\xa0\xa0"),
170 0x5FFFD => (isASCII) ? "\xf1\x9f\xbf\xbd" : I8_to_native("\xf8\xab\xbf\xbf\xbd"),
171 0x60000 => (isASCII) ? "\xf1\xa0\x80\x80" : I8_to_native("\xf8\xac\xa0\xa0\xa0"),
172 0x6FFFD => (isASCII) ? "\xf1\xaf\xbf\xbd" : I8_to_native("\xf8\xad\xbf\xbf\xbd"),
173 0x70000 => (isASCII) ? "\xf1\xb0\x80\x80" : I8_to_native("\xf8\xae\xa0\xa0\xa0"),
174 0x7FFFD => (isASCII) ? "\xf1\xbf\xbf\xbd" : I8_to_native("\xf8\xaf\xbf\xbf\xbd"),
175 0x80000 => (isASCII) ? "\xf2\x80\x80\x80" : I8_to_native("\xf8\xb0\xa0\xa0\xa0"),
176 0x8FFFD => (isASCII) ? "\xf2\x8f\xbf\xbd" : I8_to_native("\xf8\xb1\xbf\xbf\xbd"),
177 0x90000 => (isASCII) ? "\xf2\x90\x80\x80" : I8_to_native("\xf8\xb2\xa0\xa0\xa0"),
178 0x9FFFD => (isASCII) ? "\xf2\x9f\xbf\xbd" : I8_to_native("\xf8\xb3\xbf\xbf\xbd"),
179 0xA0000 => (isASCII) ? "\xf2\xa0\x80\x80" : I8_to_native("\xf8\xb4\xa0\xa0\xa0"),
180 0xAFFFD => (isASCII) ? "\xf2\xaf\xbf\xbd" : I8_to_native("\xf8\xb5\xbf\xbf\xbd"),
181 0xB0000 => (isASCII) ? "\xf2\xb0\x80\x80" : I8_to_native("\xf8\xb6\xa0\xa0\xa0"),
182 0xBFFFD => (isASCII) ? "\xf2\xbf\xbf\xbd" : I8_to_native("\xf8\xb7\xbf\xbf\xbd"),
183 0xC0000 => (isASCII) ? "\xf3\x80\x80\x80" : I8_to_native("\xf8\xb8\xa0\xa0\xa0"),
184 0xCFFFD => (isASCII) ? "\xf3\x8f\xbf\xbd" : I8_to_native("\xf8\xb9\xbf\xbf\xbd"),
185 0xD0000 => (isASCII) ? "\xf3\x90\x80\x80" : I8_to_native("\xf8\xba\xa0\xa0\xa0"),
186 0xDFFFD => (isASCII) ? "\xf3\x9f\xbf\xbd" : I8_to_native("\xf8\xbb\xbf\xbf\xbd"),
187 0xE0000 => (isASCII) ? "\xf3\xa0\x80\x80" : I8_to_native("\xf8\xbc\xa0\xa0\xa0"),
188 0xEFFFD => (isASCII) ? "\xf3\xaf\xbf\xbd" : I8_to_native("\xf8\xbd\xbf\xbf\xbd"),
189 0xF0000 => (isASCII) ? "\xf3\xb0\x80\x80" : I8_to_native("\xf8\xbe\xa0\xa0\xa0"),
190 0xFFFFD => (isASCII) ? "\xf3\xbf\xbf\xbd" : I8_to_native("\xf8\xbf\xbf\xbf\xbd"),
191 0x100000 => (isASCII) ? "\xf4\x80\x80\x80" : I8_to_native("\xf9\xa0\xa0\xa0\xa0"),
192 0x10FFFD => (isASCII) ? "\xf4\x8f\xbf\xbd" : I8_to_native("\xf9\xa1\xbf\xbf\xbd"),
193 0x110000 => (isASCII) ? "\xf4\x90\x80\x80" : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
194
195 # Things that would be noncharacters if they were in Unicode, and might be
196 # mistaken, if the C code is bad, to be nonchars
197 0x11FFFE => (isASCII) ? "\xf4\x9f\xbf\xbe" : I8_to_native("\xf9\xa3\xbf\xbf\xbe"),
198 0x11FFFF => (isASCII) ? "\xf4\x9f\xbf\xbf" : I8_to_native("\xf9\xa3\xbf\xbf\xbf"),
199 0x20FFFE => (isASCII) ? "\xf8\x88\x8f\xbf\xbe" : I8_to_native("\xfa\xa1\xbf\xbf\xbe"),
200 0x20FFFF => (isASCII) ? "\xf8\x88\x8f\xbf\xbf" : I8_to_native("\xfa\xa1\xbf\xbf\xbf"),
201
202 0x200000 - 1 => (isASCII) ? "\xf7\xbf\xbf\xbf" : I8_to_native("\xf9\xbf\xbf\xbf\xbf"),
203 0x200000 => (isASCII) ? "\xf8\x88\x80\x80\x80" : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
204 0x400000 - 1 => (isASCII) ? "\xf8\x8f\xbf\xbf\xbf" : I8_to_native("\xfb\xbf\xbf\xbf\xbf"),
205 0x400000 => (isASCII) ? "\xf8\x90\x80\x80\x80" : I8_to_native("\xfc\xa4\xa0\xa0\xa0\xa0"),
206 0x4000000 - 1 => (isASCII) ? "\xfb\xbf\xbf\xbf\xbf" : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
207 0x4000000 => (isASCII) ? "\xfc\x84\x80\x80\x80\x80" : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
208 0x4000000 - 1 => (isASCII) ? "\xfb\xbf\xbf\xbf\xbf" : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
209 0x4000000 => (isASCII) ? "\xfc\x84\x80\x80\x80\x80" : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
210 0x40000000 - 1 => (isASCII) ? "\xfc\xbf\xbf\xbf\xbf\xbf" : I8_to_native("\xfe\xbf\xbf\xbf\xbf\xbf\xbf"),
211 0x40000000 => (isASCII) ? "\xfd\x80\x80\x80\x80\x80" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0"),
212 0x80000000 - 1 => (isASCII) ? "\xfd\xbf\xbf\xbf\xbf\xbf" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf"),
c0236afe
KW
213 0x80000000 => (isASCII) ? "\xfe\x82\x80\x80\x80\x80\x80" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
214 0xFFFFFFFF => (isASCII) ? "\xfe\x83\xbf\xbf\xbf\xbf\xbf" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
7dfd8446 215);
4deba822 216
7dfd8446
KW
217if ($is64bit) {
218 no warnings qw(overflow portable);
9d2d0ecd
KW
219 $code_points{0x100000000} = (isASCII)
220 ? "\xfe\x84\x80\x80\x80\x80\x80"
221 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0");
222 $code_points{0x1000000000 - 1} = (isASCII)
223 ? "\xfe\xbf\xbf\xbf\xbf\xbf\xbf"
224 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
225 $code_points{0x1000000000} = (isASCII)
226 ? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
227 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0");
228 $code_points{0xFFFFFFFFFFFFFFFF} = (isASCII)
229 ? "\xff\x80\x8f\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
230 : I8_to_native("\xff\xaf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
d566bd20
KW
231 if (isASCII) { # These could falsely show as overlongs in a naive implementation
232 $code_points{0x40000000000} = "\xff\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80";
233 $code_points{0x1000000000000} = "\xff\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80";
234 $code_points{0x40000000000000} = "\xff\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80";
235 $code_points{0x1000000000000000} = "\xff\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
236 # overflows
237 #$code_points{0xfoo} = "\xff\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
238 }
239}
240elsif (! isASCII) { # 32-bit EBCDIC. 64-bit is clearer to handle, so doesn't need this test case
241 no warnings qw(overflow portable);
242 $code_points{0x40000000} = I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0");
7dfd8446 243}
eb83ed87 244
7dfd8446
KW
245# Now add in entries for each of code points 0-255, which require special
246# handling on EBCDIC. Remember the keys are Unicode values, and the values
247# are the native UTF-8. For invariants, the bytes are just the native chr.
248
249my $cp = 0;
250while ($cp < ((isASCII) ? 128 : 160)) { # This is from the definition of
251 # invariant
252 $code_points{$cp} = chr utf8::unicode_to_native($cp);
253 $cp++;
254}
255
256# Done with the invariants. Now do the variants. All in this range are 2
257# byte. Again, we can't use the internal functions to generate UTF-8, as
258# those are what we are trying to test. In the loop, we know what range the
259# continuation bytes can be in, and what the lowest start byte can be. So we
260# cycle through them.
261
262my $first_continuation = (isASCII) ? 0x80 : 0xA0;
263my $final_continuation = 0xBF;
264my $start = (isASCII) ? 0xC2 : 0xC5;
265
266my $continuation = $first_continuation - 1;
267
268while ($cp < 255) {
269 if (++$continuation > $final_continuation) {
270
271 # Wrap to the next start byte when we reach the final continuation
272 # byte possible
273 $continuation = $first_continuation;
274 $start++;
275 }
276 $code_points{$cp} = I8_to_native(chr($start) . chr($continuation));
277
278 $cp++;
279}
eb83ed87
KW
280
281my @warnings;
282
283use warnings 'utf8';
284local $SIG{__WARN__} = sub { push @warnings, @_ };
285
7dfd8446
KW
286# This set of tests looks for basic sanity, and lastly tests the bottom level
287# decode routine for the given code point. If the earlier tests for that code
288# point fail, that one probably will too. Malformations are tested in later
289# segments of code.
290for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
291 keys %code_points)
292{
293 my $hex_u = sprintf("0x%02X", $u);
294 my $n = utf8::unicode_to_native($u);
295 my $hex_n = sprintf("0x%02X", $n);
296 my $bytes = $code_points{$u};
297
298 my $offskip_should_be;
299 {
300 no warnings qw(overflow portable);
301 $offskip_should_be = (isASCII)
302 ? ( $u < 0x80 ? 1 :
303 $u < 0x800 ? 2 :
304 $u < 0x10000 ? 3 :
305 $u < 0x200000 ? 4 :
306 $u < 0x4000000 ? 5 :
307 $u < 0x80000000 ? 6 : (($is64bit)
308 ? ($u < 0x1000000000 ? 7 : 13)
309 : 7)
310 )
311 : ($u < 0xA0 ? 1 :
312 $u < 0x400 ? 2 :
313 $u < 0x4000 ? 3 :
314 $u < 0x40000 ? 4 :
315 $u < 0x400000 ? 5 :
c0236afe
KW
316 $u < 0x4000000 ? 6 :
317 $u < 0x40000000 ? 7 : 14 );
7dfd8446
KW
318 }
319
320 # If this test fails, subsequent ones are meaningless.
321 next unless is(test_OFFUNISKIP($u), $offskip_should_be,
322 "Verify OFFUNISKIP($hex_u) is $offskip_should_be");
323 my $invariant = $offskip_should_be == 1;
324 my $display_invariant = $invariant || 0;
325 is(test_OFFUNI_IS_INVARIANT($u), $invariant,
326 "Verify OFFUNI_IS_INVARIANT($hex_u) is $display_invariant");
327
328 my $uvchr_skip_should_be = $offskip_should_be;
329 next unless is(test_UVCHR_SKIP($n), $uvchr_skip_should_be,
330 "Verify UVCHR_SKIP($hex_n) is $uvchr_skip_should_be");
331 is(test_UVCHR_IS_INVARIANT($n), $offskip_should_be == 1,
332 "Verify UVCHR_IS_INVARIANT($hex_n) is $display_invariant");
333
334 my $n_chr = chr $n;
335 utf8::upgrade $n_chr;
336
337 is(test_UTF8_SKIP($n_chr), $uvchr_skip_should_be,
338 "Verify UTF8_SKIP(chr $hex_n) is $uvchr_skip_should_be");
339
340 use bytes;
3d56ecbe
KW
341 my $byte_length = length $n_chr;
342 for (my $j = 0; $j < $byte_length; $j++) {
343 undef @warnings;
344
345 if ($j == $byte_length - 1) {
346 my $ret = test_is_utf8_valid_partial_char_flags($n_chr, $byte_length, 0);
347 is($ret, 0, " Verify is_utf8_valid_partial_char_flags(" . display_bytes($n_chr) . ") returns 0 for full character");
348 }
349 else {
350 my $bytes_so_far = substr($n_chr, 0, $j + 1);
351 my $ret = test_is_utf8_valid_partial_char_flags($bytes_so_far, $j + 1, 0);
352 is($ret, 1, " Verify is_utf8_valid_partial_char_flags(" . display_bytes($bytes_so_far) . ") returns 1");
353 }
354
355 unless (is(scalar @warnings, 0,
356 " Verify is_utf8_valid_partial_char_flags generated no warnings"))
357 {
358 diag "The warnings were: " . join(", ", @warnings);
359 }
360
7dfd8446
KW
361 my $b = substr($n_chr, $j, 1);
362 my $hex_b = sprintf("\"\\x%02x\"", ord $b);
363
364 my $byte_invariant = $j == 0 && $uvchr_skip_should_be == 1;
365 my $display_byte_invariant = $byte_invariant || 0;
366 next unless is(test_UTF8_IS_INVARIANT($b), $byte_invariant,
367 " Verify UTF8_IS_INVARIANT($hex_b) for byte $j "
368 . "is $display_byte_invariant");
369
370 my $is_start = $j == 0 && $uvchr_skip_should_be > 1;
371 my $display_is_start = $is_start || 0;
372 next unless is(test_UTF8_IS_START($b), $is_start,
373 " Verify UTF8_IS_START($hex_b) is $display_is_start");
374
375 my $is_continuation = $j != 0 && $uvchr_skip_should_be > 1;
376 my $display_is_continuation = $is_continuation || 0;
377 next unless is(test_UTF8_IS_CONTINUATION($b), $is_continuation,
378 " Verify UTF8_IS_CONTINUATION($hex_b) is "
379 . "$display_is_continuation");
380
381 my $is_continued = $uvchr_skip_should_be > 1;
382 my $display_is_continued = $is_continued || 0;
383 next unless is(test_UTF8_IS_CONTINUED($b), $is_continued,
384 " Verify UTF8_IS_CONTINUED($hex_b) is "
385 . "$display_is_continued");
386
387 my $is_downgradeable_start = $n < 256
388 && $uvchr_skip_should_be > 1
389 && $j == 0;
390 my $display_is_downgradeable_start = $is_downgradeable_start || 0;
391 next unless is(test_UTF8_IS_DOWNGRADEABLE_START($b),
392 $is_downgradeable_start,
393 " Verify UTF8_IS_DOWNGRADEABLE_START($hex_b) is "
394 . "$display_is_downgradeable_start");
395
396 my $is_above_latin1 = $n > 255 && $j == 0;
397 my $display_is_above_latin1 = $is_above_latin1 || 0;
398 next unless is(test_UTF8_IS_ABOVE_LATIN1($b),
399 $is_above_latin1,
400 " Verify UTF8_IS_ABOVE_LATIN1($hex_b) is "
401 . "$display_is_above_latin1");
402
403 my $is_possibly_problematic = $j == 0
404 && $n >= ((isASCII)
405 ? 0xD000
406 : 0x8000);
407 my $display_is_possibly_problematic = $is_possibly_problematic || 0;
408 next unless is(test_isUTF8_POSSIBLY_PROBLEMATIC($b),
409 $is_possibly_problematic,
410 " Verify isUTF8_POSSIBLY_PROBLEMATIC($hex_b) is "
411 . "$display_is_above_latin1");
412 }
413
414 # We are not trying to look for warnings, etc, so if they should occur, it
415 # is an error. But some of the code points here do cause warnings, so we
416 # check here and turn off the ones that apply to such code points. A
417 # later section of the code tests for these kinds of things.
046d01eb 418 my $this_utf8_flags = $look_for_everything_utf8n_to;
7dfd8446
KW
419 my $len = length $bytes;
420 if ($n > 2 ** 31 - 1) {
1d1c12d9
KW
421 $this_utf8_flags &=
422 ~($UTF8_DISALLOW_ABOVE_31_BIT|$UTF8_WARN_ABOVE_31_BIT);
7dfd8446 423 }
e23e8bc1
KW
424
425 my $valid_under_strict = 1;
7dfd8446
KW
426 if ($n > 0x10FFFF) {
427 $this_utf8_flags &= ~($UTF8_DISALLOW_SUPER|$UTF8_WARN_SUPER);
e23e8bc1 428 $valid_under_strict = 0;
7dfd8446
KW
429 }
430 elsif (($n & 0xFFFE) == 0xFFFE) {
431 $this_utf8_flags &= ~($UTF8_DISALLOW_NONCHAR|$UTF8_WARN_NONCHAR);
e23e8bc1 432 $valid_under_strict = 0;
7dfd8446
KW
433 }
434
435 undef @warnings;
436
437 my $display_flags = sprintf "0x%x", $this_utf8_flags;
7dfd8446 438 my $display_bytes = display_bytes($bytes);
d7874298 439 my $ret_ref = test_utf8n_to_uvchr($bytes, $len, $this_utf8_flags);
7dfd8446 440 is($ret_ref->[0], $n, "Verify utf8n_to_uvchr($display_bytes, $display_flags) returns $hex_n");
9d2d0ecd 441 is($ret_ref->[1], $len, "Verify utf8n_to_uvchr() for $hex_n returns expected length: $len");
7dfd8446
KW
442
443 unless (is(scalar @warnings, 0,
444 "Verify utf8n_to_uvchr() for $hex_n generated no warnings"))
445 {
446 diag "The warnings were: " . join(", ", @warnings);
447 }
046d01eb 448
75ffa578
KW
449 undef @warnings;
450
d7874298 451 my $ret = test_isUTF8_CHAR($bytes, $len);
9d2d0ecd 452 is($ret, $len, "Verify isUTF8_CHAR($display_bytes) returns expected length: $len");
d7874298
KW
453
454 unless (is(scalar @warnings, 0,
455 "Verify isUTF8_CHAR() for $hex_n generated no warnings"))
456 {
457 diag "The warnings were: " . join(", ", @warnings);
458 }
459
460 undef @warnings;
461
462 $ret = test_isUTF8_CHAR($bytes, $len - 1);
463 is($ret, 0, "Verify isUTF8_CHAR() with too short length parameter returns 0");
464
465 unless (is(scalar @warnings, 0,
466 "Verify isUTF8_CHAR() generated no warnings"))
467 {
468 diag "The warnings were: " . join(", ", @warnings);
469 }
470
471 undef @warnings;
472
e23e8bc1
KW
473 $ret = test_isSTRICT_UTF8_CHAR($bytes, $len);
474 my $expected_len = ($valid_under_strict) ? $len : 0;
475 is($ret, $expected_len, "Verify isSTRICT_UTF8_CHAR($display_bytes) returns expected length: $expected_len");
476
477 unless (is(scalar @warnings, 0,
478 "Verify isSTRICT_UTF8_CHAR() for $hex_n generated no warnings"))
479 {
480 diag "The warnings were: " . join(", ", @warnings);
481 }
482
483 undef @warnings;
484
485 $ret = test_isSTRICT_UTF8_CHAR($bytes, $len - 1);
486 is($ret, 0, "Verify isSTRICT_UTF8_CHAR() with too short length parameter returns 0");
487
488 unless (is(scalar @warnings, 0,
489 "Verify isSTRICT_UTF8_CHAR() generated no warnings"))
490 {
491 diag "The warnings were: " . join(", ", @warnings);
492 }
493
494 undef @warnings;
495
75ffa578
KW
496 $ret_ref = test_valid_utf8_to_uvchr($bytes);
497 is($ret_ref->[0], $n, "Verify valid_utf8_to_uvchr($display_bytes) returns $hex_n");
9d2d0ecd 498 is($ret_ref->[1], $len, "Verify valid_utf8_to_uvchr() for $hex_n returns expected length: $len");
75ffa578
KW
499
500 unless (is(scalar @warnings, 0,
501 "Verify valid_utf8_to_uvchr() for $hex_n generated no warnings"))
502 {
503 diag "The warnings were: " . join(", ", @warnings);
504 }
505
046d01eb
KW
506 # Similarly for uvchr_to_utf8
507 my $this_uvchr_flags = $look_for_everything_uvchr_to;
508 if ($n > 2 ** 31 - 1) {
509 $this_uvchr_flags &=
510 ~($UNICODE_DISALLOW_ABOVE_31_BIT|$UNICODE_WARN_ABOVE_31_BIT);
511 }
512 if ($n > 0x10FFFF) {
513 $this_uvchr_flags &= ~($UNICODE_DISALLOW_SUPER|$UNICODE_WARN_SUPER);
514 }
515 elsif (($n & 0xFFFE) == 0xFFFE) {
516 $this_uvchr_flags &= ~($UNICODE_DISALLOW_NONCHAR|$UNICODE_WARN_NONCHAR);
517 }
518 $display_flags = sprintf "0x%x", $this_uvchr_flags;
519
520 undef @warnings;
521
d7874298 522 $ret = test_uvchr_to_utf8_flags($n, $this_uvchr_flags);
046d01eb
KW
523 ok(defined $ret, "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returned success");
524 is($ret, $bytes, "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returns correct bytes");
525
526 unless (is(scalar @warnings, 0,
527 "Verify uvchr_to_utf8_flags($hex_n, $display_flags) for $hex_n generated no warnings"))
528 {
529 diag "The warnings were: " . join(", ", @warnings);
530 }
7dfd8446
KW
531}
532
533my $REPLACEMENT = 0xFFFD;
534
535# Now test the malformations. All these raise category utf8 warnings.
536my $c = (isASCII) ? "\x80" : "\xa0"; # A continuation byte
537my @malformations = (
eb83ed87
KW
538 [ "zero length string malformation", "", 0,
539 $UTF8_ALLOW_EMPTY, 0, 0,
540 qr/empty string/
541 ],
7dfd8446
KW
542 [ "orphan continuation byte malformation", I8_to_native("${c}a"),
543 2,
eb83ed87
KW
544 $UTF8_ALLOW_CONTINUATION, $REPLACEMENT, 1,
545 qr/unexpected continuation byte/
546 ],
7dfd8446 547 [ "premature next character malformation (immediate)",
9d2d0ecd
KW
548 (isASCII) ? "\xc2\xc2\x80" : I8_to_native("\xc5\xc5\xa0"),
549 3,
eb83ed87
KW
550 $UTF8_ALLOW_NON_CONTINUATION, $REPLACEMENT, 1,
551 qr/unexpected non-continuation byte.*immediately after start byte/
552 ],
7dfd8446
KW
553 [ "premature next character malformation (non-immediate)",
554 I8_to_native("\xf0${c}a"),
555 3,
eb83ed87
KW
556 $UTF8_ALLOW_NON_CONTINUATION, $REPLACEMENT, 2,
557 qr/unexpected non-continuation byte .* 2 bytes after start byte/
558 ],
7dfd8446 559 [ "too short malformation", I8_to_native("\xf0${c}a"), 2,
eb83ed87
KW
560 # Having the 'a' after this, but saying there are only 2 bytes also
561 # tests that we pay attention to the passed in length
562 $UTF8_ALLOW_SHORT, $REPLACEMENT, 2,
563 qr/2 bytes, need 4/
564 ],
d566bd20
KW
565 [ "overlong malformation, lowest 2-byte",
566 (isASCII) ? "\xc0\x80" : I8_to_native("\xc0\xa0"),
567 2,
7dfd8446
KW
568 $UTF8_ALLOW_LONG,
569 0, # NUL
570 2,
eb83ed87 571 qr/2 bytes, need 1/
c0236afe 572 ],
d566bd20
KW
573 [ "overlong malformation, highest 2-byte",
574 (isASCII) ? "\xc1\xbf" : I8_to_native("\xc4\xbf"),
575 2,
576 $UTF8_ALLOW_LONG,
577 (isASCII) ? 0x7F : utf8::unicode_to_native(0xBF),
578 2,
579 qr/2 bytes, need 1/
580 ],
581 [ "overlong malformation, lowest 3-byte",
582 (isASCII) ? "\xe0\x80\x80" : I8_to_native("\xe0\xa0\xa0"),
583 3,
584 $UTF8_ALLOW_LONG,
585 0, # NUL
586 3,
587 qr/3 bytes, need 1/
588 ],
589 [ "overlong malformation, highest 3-byte",
590 (isASCII) ? "\xe0\x9f\xbf" : I8_to_native("\xe0\xbf\xbf"),
591 3,
592 $UTF8_ALLOW_LONG,
593 (isASCII) ? 0x7FF : 0x3FF,
594 3,
595 qr/3 bytes, need 2/
596 ],
597 [ "overlong malformation, lowest 4-byte",
598 (isASCII) ? "\xf0\x80\x80\x80" : I8_to_native("\xf0\xa0\xa0\xa0"),
599 4,
600 $UTF8_ALLOW_LONG,
601 0, # NUL
602 4,
603 qr/4 bytes, need 1/
604 ],
605 [ "overlong malformation, highest 4-byte",
606 (isASCII) ? "\xf0\x8F\xbf\xbf" : I8_to_native("\xf0\xaf\xbf\xbf"),
607 4,
608 $UTF8_ALLOW_LONG,
609 (isASCII) ? 0xFFFF : 0x3FFF,
610 4,
611 qr/4 bytes, need 3/
612 ],
613 [ "overlong malformation, lowest 5-byte",
614 (isASCII)
615 ? "\xf8\x80\x80\x80\x80"
616 : I8_to_native("\xf8\xa0\xa0\xa0\xa0"),
617 5,
618 $UTF8_ALLOW_LONG,
619 0, # NUL
620 5,
621 qr/5 bytes, need 1/
622 ],
623 [ "overlong malformation, highest 5-byte",
624 (isASCII)
625 ? "\xf8\x87\xbf\xbf\xbf"
626 : I8_to_native("\xf8\xa7\xbf\xbf\xbf"),
627 5,
628 $UTF8_ALLOW_LONG,
629 (isASCII) ? 0x1FFFFF : 0x3FFFF,
630 5,
631 qr/5 bytes, need 4/
632 ],
633 [ "overlong malformation, lowest 6-byte",
634 (isASCII)
635 ? "\xfc\x80\x80\x80\x80\x80"
636 : I8_to_native("\xfc\xa0\xa0\xa0\xa0\xa0"),
637 6,
638 $UTF8_ALLOW_LONG,
639 0, # NUL
640 6,
641 qr/6 bytes, need 1/
642 ],
643 [ "overlong malformation, highest 6-byte",
644 (isASCII)
645 ? "\xfc\x83\xbf\xbf\xbf\xbf"
646 : I8_to_native("\xfc\xa3\xbf\xbf\xbf\xbf"),
647 6,
648 $UTF8_ALLOW_LONG,
649 (isASCII) ? 0x3FFFFFF : 0x3FFFFF,
650 6,
651 qr/6 bytes, need 5/
652 ],
653 [ "overlong malformation, lowest 7-byte",
654 (isASCII)
655 ? "\xfe\x80\x80\x80\x80\x80\x80"
656 : I8_to_native("\xfe\xa0\xa0\xa0\xa0\xa0\xa0"),
657 7,
658 $UTF8_ALLOW_LONG,
659 0, # NUL
660 7,
661 qr/7 bytes, need 1/
662 ],
663 [ "overlong malformation, highest 7-byte",
664 (isASCII)
665 ? "\xfe\x81\xbf\xbf\xbf\xbf\xbf"
666 : I8_to_native("\xfe\xa1\xbf\xbf\xbf\xbf\xbf"),
667 7,
668 $UTF8_ALLOW_LONG,
669 (isASCII) ? 0x7FFFFFFF : 0x3FFFFFF,
670 7,
671 qr/7 bytes, need 6/
c0236afe
KW
672 ],
673);
7dfd8446 674
d566bd20
KW
675if (isASCII && ! $is64bit) { # 32-bit ASCII platform
676 no warnings 'portable';
677 push @malformations,
678 [ "overflow malformation",
679 "\xfe\x84\x80\x80\x80\x80\x80", # Represents 2**32
680 7,
681 0, # There is no way to allow this malformation
682 $REPLACEMENT,
683 7,
684 qr/overflow/
685 ],
686 [ "overflow malformation, can tell on first byte",
687 "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
688 13,
689 0, # There is no way to allow this malformation
690 $REPLACEMENT,
691 13,
692 qr/overflow/
693 ];
694}
695else {
696 # On EBCDIC platforms, another overlong test is needed even on 32-bit
697 # systems, whereas it doesn't happen on ASCII except on 64-bit ones.
698
699 no warnings 'portable';
700 no warnings 'overflow'; # Doesn't run on 32-bit systems, but compiles
701 push @malformations,
702 [ "overlong malformation, lowest max-byte",
703 (isASCII)
704 ? "\xff\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
705 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
706 (isASCII) ? 13 : 14,
707 $UTF8_ALLOW_LONG,
708 0, # NUL
709 (isASCII) ? 13 : 14,
710 qr/1[34] bytes, need 1/, # 1[34] to work on either ASCII or EBCDIC
711 ],
712 [ "overlong malformation, highest max-byte",
713 (isASCII) # 2**36-1 on ASCII; 2**30-1 on EBCDIC
714 ? "\xff\x80\x80\x80\x80\x80\x80\xbf\xbf\xbf\xbf\xbf\xbf"
715 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xbf\xbf\xbf\xbf\xbf\xbf"),
716 (isASCII) ? 13 : 14,
717 $UTF8_ALLOW_LONG,
718 (isASCII) ? 0xFFFFFFFFF : 0x3FFFFFFF,
719 (isASCII) ? 13 : 14,
720 qr/1[34] bytes, need 7/,
721 ];
722
723 if (! $is64bit) { # 32-bit EBCDIC
724 push @malformations,
725 [ "overflow malformation",
726 I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"),
727 14,
728 0, # There is no way to allow this malformation
729 $REPLACEMENT,
730 14,
731 qr/overflow/
732 ];
733 }
734 else { # 64-bit
735 push @malformations,
736 [ "overflow malformation",
737 (isASCII)
738 ? "\xff\x80\x90\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"
739 : I8_to_native("\xff\xb0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
740 (isASCII) ? 13 : 14,
741 0, # There is no way to allow this malformation
742 $REPLACEMENT,
743 (isASCII) ? 13 : 14,
744 qr/overflow/
745 ];
746 }
747}
748
7dfd8446 749foreach my $test (@malformations) {
eb83ed87
KW
750 my ($testname, $bytes, $length, $allow_flags, $allowed_uv, $expected_len, $message ) = @$test;
751
752 next if ! ok(length($bytes) >= $length, "$testname: Make sure won't read beyond buffer: " . length($bytes) . " >= $length");
753
d7874298
KW
754 undef @warnings;
755
756 my $ret = test_isUTF8_CHAR($bytes, $length);
757 is($ret, 0, "$testname: isUTF8_CHAR returns 0");
758 unless (is(scalar @warnings, 0,
759 "$testname: isUTF8_CHAR() generated no warnings"))
760 {
761 diag "The warnings were: " . join(", ", @warnings);
762 }
763
e23e8bc1
KW
764 $ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
765 is($ret, 0, "$testname: isSTRICT_UTF8_CHAR returns 0");
766 unless (is(scalar @warnings, 0,
767 "$testname: isSTRICT_UTF8_CHAR() generated no warnings"))
768 {
769 diag "The warnings were: " . join(", ", @warnings);
770 }
771
3d56ecbe
KW
772 for my $j (1 .. $length - 1) {
773 my $partial = substr($bytes, 0, $j);
774
775 undef @warnings;
776
777 $ret = test_is_utf8_valid_partial_char_flags($bytes, $j, 0);
778 my $ret_should_be = 0;
779 my $comment = "";
780 if ($testname =~ /premature|short/ && $j < 2) {
781 $ret_should_be = 1;
782 $comment = ", but need 2 bytes to discern:";
783 }
784 elsif ($testname =~ /overlong/ && $length > 2) {
785 if ($length <= 7 && $j < 2) {
786 $ret_should_be = 1;
787 $comment = ", but need 2 bytes to discern:";
788 }
789 elsif ($length > 7 && $j < 7) {
790 $ret_should_be = 1;
791 $comment = ", but need 7 bytes to discern:";
792 }
793 }
794 elsif ($testname =~ /overflow/ && $testname !~ /first byte/) {
795 if (isASCII) {
796 if ($j < (($is64bit) ? 3 : 2)) {
797 $comment = ", but need $j bytes to discern:";
798 $ret_should_be = 1;
799 }
800 }
801 else {
802 if ($j < (($is64bit) ? 2 : 8)) {
803 $comment = ", but need $j bytes to discern:";
804 $ret_should_be = 1;
805 }
806 }
807 }
808 is($ret, $ret_should_be, "$testname: is_utf8_valid_partial_char_flags("
809 . display_bytes($partial)
810 . ")$comment returns $ret_should_be");
811 unless (is(scalar @warnings, 0,
812 "$testname: is_utf8_valid_partial_char_flags() generated no warnings"))
813 {
814 diag "The warnings were: " . join(", ", @warnings);
815 }
816 }
817
d7874298 818
eb83ed87
KW
819 # Test what happens when this malformation is not allowed
820 undef @warnings;
bd70aaaf 821 my $ret_ref = test_utf8n_to_uvchr($bytes, $length, 0);
eb83ed87 822 is($ret_ref->[0], 0, "$testname: disallowed: Returns 0");
9d2d0ecd 823 is($ret_ref->[1], $expected_len, "$testname: utf8n_to_uvchr(), disallowed: Returns expected length: $expected_len");
eb83ed87
KW
824 if (is(scalar @warnings, 1, "$testname: disallowed: Got a single warning ")) {
825 like($warnings[0], $message, "$testname: disallowed: Got expected warning");
826 }
827 else {
828 if (scalar @warnings) {
7dfd8446 829 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
830 }
831 }
832
833 { # Next test when disallowed, and warnings are off.
834 undef @warnings;
835 no warnings 'utf8';
bd70aaaf 836 my $ret_ref = test_utf8n_to_uvchr($bytes, $length, 0);
9d2d0ecd
KW
837 is($ret_ref->[0], 0, "$testname: utf8n_to_uvchr(), disallowed: no warnings 'utf8': Returns 0");
838 is($ret_ref->[1], $expected_len, "$testname: utf8n_to_uvchr(), disallowed: no warnings 'utf8': Returns expected length: $expected_len");
839 if (!is(scalar @warnings, 0, "$testname: utf8n_to_uvchr(), disallowed: no warnings 'utf8': no warnings generated")) {
7dfd8446 840 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
841 }
842 }
843
844 # Test with CHECK_ONLY
845 undef @warnings;
bd70aaaf 846 $ret_ref = test_utf8n_to_uvchr($bytes, $length, $UTF8_CHECK_ONLY);
eb83ed87 847 is($ret_ref->[0], 0, "$testname: CHECK_ONLY: Returns 0");
9d2d0ecd 848 is($ret_ref->[1], -1, "$testname: CHECK_ONLY: returns -1 for length");
eb83ed87 849 if (! is(scalar @warnings, 0, "$testname: CHECK_ONLY: no warnings generated")) {
7dfd8446 850 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
851 }
852
853 next if $allow_flags == 0; # Skip if can't allow this malformation
854
855 # Test when the malformation is allowed
856 undef @warnings;
bd70aaaf 857 $ret_ref = test_utf8n_to_uvchr($bytes, $length, $allow_flags);
9d2d0ecd
KW
858 is($ret_ref->[0], $allowed_uv, "$testname: utf8n_to_uvchr(), allowed: Returns expected uv: " . sprintf("0x%04X", $allowed_uv));
859 is($ret_ref->[1], $expected_len, "$testname: utf8n_to_uvchr(), allowed: Returns expected length: $expected_len");
860 if (!is(scalar @warnings, 0, "$testname: utf8n_to_uvchr(), allowed: no warnings generated"))
eb83ed87 861 {
7dfd8446 862 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
863 }
864}
865
eb83ed87
KW
866# Now test the cases where a legal code point is generated, but may or may not
867# be allowed/warned on.
2f8f112e 868my @tests = (
7dfd8446
KW
869 [ "lowest surrogate",
870 (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
871 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE,
872 'surrogate', 0xD800,
873 (isASCII) ? 3 : 4,
874 qr/surrogate/
875 ],
876 [ "a middle surrogate",
877 (isASCII) ? "\xed\xa4\x8d" : I8_to_native("\xf1\xb6\xa8\xad"),
878 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE,
879 'surrogate', 0xD90D,
880 (isASCII) ? 3 : 4,
eb83ed87
KW
881 qr/surrogate/
882 ],
7dfd8446
KW
883 [ "highest surrogate",
884 (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
885 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE,
886 'surrogate', 0xDFFF,
887 (isASCII) ? 3 : 4,
888 qr/surrogate/
889 ],
890 [ "first non_unicode",
891 (isASCII) ? "\xf4\x90\x80\x80" : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
892 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER,
893 'non_unicode', 0x110000,
894 (isASCII) ? 4 : 5,
895 qr/not Unicode.* may not be portable/
896 ],
d566bd20
KW
897 [ "non_unicode whose first byte tells that",
898 (isASCII) ? "\xf5\x80\x80\x80" : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
899 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER,
900 'non_unicode',
901 (isASCII) ? 0x140000 : 0x200000,
902 (isASCII) ? 4 : 5,
903 qr/not Unicode.* may not be portable/
904 ],
7dfd8446
KW
905 [ "first of 32 consecutive non-character code points",
906 (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
907 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
908 'nonchar', 0xFDD0,
909 (isASCII) ? 3 : 4,
910 qr/Unicode non-character.*is not recommended for open interchange/
911 ],
912 [ "a mid non-character code point of the 32 consecutive ones",
913 (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
914 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
915 'nonchar', 0xFDE0,
916 (isASCII) ? 3 : 4,
917 qr/Unicode non-character.*is not recommended for open interchange/
918 ],
919 [ "final of 32 consecutive non-character code points",
920 (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
921 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
922 'nonchar', 0xFDEF,
923 (isASCII) ? 3 : 4,
924 qr/Unicode non-character.*is not recommended for open interchange/
925 ],
926 [ "non-character code point U+FFFE",
927 (isASCII) ? "\xef\xbf\xbe" : I8_to_native("\xf1\xbf\xbf\xbe"),
928 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
929 'nonchar', 0xFFFE,
930 (isASCII) ? 3 : 4,
931 qr/Unicode non-character.*is not recommended for open interchange/
932 ],
933 [ "non-character code point U+FFFF",
934 (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
935 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
936 'nonchar', 0xFFFF,
937 (isASCII) ? 3 : 4,
938 qr/Unicode non-character.*is not recommended for open interchange/
939 ],
940 [ "non-character code point U+1FFFE",
941 (isASCII) ? "\xf0\x9f\xbf\xbe" : I8_to_native("\xf3\xbf\xbf\xbe"),
942 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
943 'nonchar', 0x1FFFE, 4,
944 qr/Unicode non-character.*is not recommended for open interchange/
945 ],
946 [ "non-character code point U+1FFFF",
947 (isASCII) ? "\xf0\x9f\xbf\xbf" : I8_to_native("\xf3\xbf\xbf\xbf"),
948 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
949 'nonchar', 0x1FFFF, 4,
950 qr/Unicode non-character.*is not recommended for open interchange/
951 ],
952 [ "non-character code point U+2FFFE",
953 (isASCII) ? "\xf0\xaf\xbf\xbe" : I8_to_native("\xf5\xbf\xbf\xbe"),
954 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
955 'nonchar', 0x2FFFE, 4,
956 qr/Unicode non-character.*is not recommended for open interchange/
957 ],
958 [ "non-character code point U+2FFFF",
959 (isASCII) ? "\xf0\xaf\xbf\xbf" : I8_to_native("\xf5\xbf\xbf\xbf"),
960 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
961 'nonchar', 0x2FFFF, 4,
962 qr/Unicode non-character.*is not recommended for open interchange/
963 ],
964 [ "non-character code point U+3FFFE",
965 (isASCII) ? "\xf0\xbf\xbf\xbe" : I8_to_native("\xf7\xbf\xbf\xbe"),
966 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
967 'nonchar', 0x3FFFE, 4,
968 qr/Unicode non-character.*is not recommended for open interchange/
969 ],
970 [ "non-character code point U+3FFFF",
971 (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
972 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
973 'nonchar', 0x3FFFF, 4,
974 qr/Unicode non-character.*is not recommended for open interchange/
975 ],
976 [ "non-character code point U+4FFFE",
977 (isASCII) ? "\xf1\x8f\xbf\xbe" : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
978 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
979 'nonchar', 0x4FFFE,
980 (isASCII) ? 4 : 5,
981 qr/Unicode non-character.*is not recommended for open interchange/
982 ],
983 [ "non-character code point U+4FFFF",
984 (isASCII) ? "\xf1\x8f\xbf\xbf" : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
985 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
986 'nonchar', 0x4FFFF,
987 (isASCII) ? 4 : 5,
988 qr/Unicode non-character.*is not recommended for open interchange/
989 ],
990 [ "non-character code point U+5FFFE",
991 (isASCII) ? "\xf1\x9f\xbf\xbe" : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
992 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
993 'nonchar', 0x5FFFE,
994 (isASCII) ? 4 : 5,
995 qr/Unicode non-character.*is not recommended for open interchange/
996 ],
997 [ "non-character code point U+5FFFF",
998 (isASCII) ? "\xf1\x9f\xbf\xbf" : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
999 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1000 'nonchar', 0x5FFFF,
1001 (isASCII) ? 4 : 5,
1002 qr/Unicode non-character.*is not recommended for open interchange/
1003 ],
1004 [ "non-character code point U+6FFFE",
1005 (isASCII) ? "\xf1\xaf\xbf\xbe" : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
1006 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1007 'nonchar', 0x6FFFE,
1008 (isASCII) ? 4 : 5,
1009 qr/Unicode non-character.*is not recommended for open interchange/
1010 ],
1011 [ "non-character code point U+6FFFF",
1012 (isASCII) ? "\xf1\xaf\xbf\xbf" : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
1013 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1014 'nonchar', 0x6FFFF,
1015 (isASCII) ? 4 : 5,
1016 qr/Unicode non-character.*is not recommended for open interchange/
1017 ],
1018 [ "non-character code point U+7FFFE",
1019 (isASCII) ? "\xf1\xbf\xbf\xbe" : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
1020 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1021 'nonchar', 0x7FFFE,
1022 (isASCII) ? 4 : 5,
1023 qr/Unicode non-character.*is not recommended for open interchange/
1024 ],
1025 [ "non-character code point U+7FFFF",
1026 (isASCII) ? "\xf1\xbf\xbf\xbf" : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
1027 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1028 'nonchar', 0x7FFFF,
1029 (isASCII) ? 4 : 5,
1030 qr/Unicode non-character.*is not recommended for open interchange/
1031 ],
1032 [ "non-character code point U+8FFFE",
1033 (isASCII) ? "\xf2\x8f\xbf\xbe" : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
1034 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1035 'nonchar', 0x8FFFE,
1036 (isASCII) ? 4 : 5,
1037 qr/Unicode non-character.*is not recommended for open interchange/
1038 ],
1039 [ "non-character code point U+8FFFF",
1040 (isASCII) ? "\xf2\x8f\xbf\xbf" : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
1041 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1042 'nonchar', 0x8FFFF,
1043 (isASCII) ? 4 : 5,
1044 qr/Unicode non-character.*is not recommended for open interchange/
1045 ],
1046 [ "non-character code point U+9FFFE",
1047 (isASCII) ? "\xf2\x9f\xbf\xbe" : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
1048 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1049 'nonchar', 0x9FFFE,
1050 (isASCII) ? 4 : 5,
1051 qr/Unicode non-character.*is not recommended for open interchange/
1052 ],
1053 [ "non-character code point U+9FFFF",
1054 (isASCII) ? "\xf2\x9f\xbf\xbf" : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
1055 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1056 'nonchar', 0x9FFFF,
1057 (isASCII) ? 4 : 5,
1058 qr/Unicode non-character.*is not recommended for open interchange/
1059 ],
1060 [ "non-character code point U+AFFFE",
1061 (isASCII) ? "\xf2\xaf\xbf\xbe" : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
1062 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1063 'nonchar', 0xAFFFE,
1064 (isASCII) ? 4 : 5,
1065 qr/Unicode non-character.*is not recommended for open interchange/
1066 ],
1067 [ "non-character code point U+AFFFF",
1068 (isASCII) ? "\xf2\xaf\xbf\xbf" : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
1069 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1070 'nonchar', 0xAFFFF,
1071 (isASCII) ? 4 : 5,
1072 qr/Unicode non-character.*is not recommended for open interchange/
1073 ],
1074 [ "non-character code point U+BFFFE",
1075 (isASCII) ? "\xf2\xbf\xbf\xbe" : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
1076 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1077 'nonchar', 0xBFFFE,
1078 (isASCII) ? 4 : 5,
1079 qr/Unicode non-character.*is not recommended for open interchange/
1080 ],
1081 [ "non-character code point U+BFFFF",
1082 (isASCII) ? "\xf2\xbf\xbf\xbf" : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
1083 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1084 'nonchar', 0xBFFFF,
1085 (isASCII) ? 4 : 5,
1086 qr/Unicode non-character.*is not recommended for open interchange/
1087 ],
1088 [ "non-character code point U+CFFFE",
1089 (isASCII) ? "\xf3\x8f\xbf\xbe" : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
1090 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1091 'nonchar', 0xCFFFE,
1092 (isASCII) ? 4 : 5,
1093 qr/Unicode non-character.*is not recommended for open interchange/
1094 ],
1095 [ "non-character code point U+CFFFF",
1096 (isASCII) ? "\xf3\x8f\xbf\xbf" : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
1097 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1098 'nonchar', 0xCFFFF,
1099 (isASCII) ? 4 : 5,
1100 qr/Unicode non-character.*is not recommended for open interchange/
1101 ],
1102 [ "non-character code point U+DFFFE",
1103 (isASCII) ? "\xf3\x9f\xbf\xbe" : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
1104 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1105 'nonchar', 0xDFFFE,
1106 (isASCII) ? 4 : 5,
1107 qr/Unicode non-character.*is not recommended for open interchange/
1108 ],
1109 [ "non-character code point U+DFFFF",
1110 (isASCII) ? "\xf3\x9f\xbf\xbf" : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
1111 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1112 'nonchar', 0xDFFFF,
1113 (isASCII) ? 4 : 5,
1114 qr/Unicode non-character.*is not recommended for open interchange/
1115 ],
1116 [ "non-character code point U+EFFFE",
1117 (isASCII) ? "\xf3\xaf\xbf\xbe" : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
1118 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1119 'nonchar', 0xEFFFE,
1120 (isASCII) ? 4 : 5,
1121 qr/Unicode non-character.*is not recommended for open interchange/
1122 ],
1123 [ "non-character code point U+EFFFF",
1124 (isASCII) ? "\xf3\xaf\xbf\xbf" : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
1125 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1126 'nonchar', 0xEFFFF,
1127 (isASCII) ? 4 : 5,
1128 qr/Unicode non-character.*is not recommended for open interchange/
1129 ],
1130 [ "non-character code point U+FFFFE",
1131 (isASCII) ? "\xf3\xbf\xbf\xbe" : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
1132 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1133 'nonchar', 0xFFFFE,
1134 (isASCII) ? 4 : 5,
1135 qr/Unicode non-character.*is not recommended for open interchange/
1136 ],
1137 [ "non-character code point U+FFFFF",
1138 (isASCII) ? "\xf3\xbf\xbf\xbf" : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
1139 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1140 'nonchar', 0xFFFFF,
1141 (isASCII) ? 4 : 5,
1142 qr/Unicode non-character.*is not recommended for open interchange/
1143 ],
1144 [ "non-character code point U+10FFFE",
1145 (isASCII) ? "\xf4\x8f\xbf\xbe" : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
1146 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1147 'nonchar', 0x10FFFE,
1148 (isASCII) ? 4 : 5,
1149 qr/Unicode non-character.*is not recommended for open interchange/
eb83ed87 1150 ],
7dfd8446
KW
1151 [ "non-character code point U+10FFFF",
1152 (isASCII) ? "\xf4\x8f\xbf\xbf" : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
1153 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1154 'nonchar', 0x10FFFF,
1155 (isASCII) ? 4 : 5,
ba707cdc 1156 qr/Unicode non-character.*is not recommended for open interchange/
eb83ed87 1157 ],
1d1c12d9 1158 [ "requires at least 32 bits",
c0236afe
KW
1159 (isASCII)
1160 ? "\xfe\x82\x80\x80\x80\x80\x80"
1161 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
eb83ed87 1162 # This code point is chosen so that it is representable in a UV on
2f8f112e 1163 # 32-bit machines
1d1c12d9 1164 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
c0236afe 1165 'utf8', 0x80000000, (isASCII) ? 7 :14,
7dfd8446
KW
1166 qr/Code point 0x80000000 is not Unicode, and not portable/
1167 ],
1d1c12d9 1168 [ "requires at least 32 bits, and use SUPER-type flags, instead of ABOVE_31_BIT",
c0236afe
KW
1169 (isASCII)
1170 ? "\xfe\x82\x80\x80\x80\x80\x80"
1171 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
7dfd8446 1172 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER,
c0236afe 1173 'utf8', 0x80000000, (isASCII) ? 7 :14,
ea5ced44 1174 qr/Code point 0x80000000 is not Unicode, and not portable/
eb83ed87 1175 ],
1d1c12d9
KW
1176 [ "overflow with warnings/disallow for more than 31 bits",
1177 # This tests the interaction of WARN_ABOVE_31_BIT/DISALLOW_ABOVE_31_BIT
1178 # with overflow. The overflow malformation is never allowed, so
1179 # preventing it takes precedence if the ABOVE_31_BIT options would
c0236afe 1180 # otherwise allow in an overflowing value. The ASCII code points (1
1d1c12d9
KW
1181 # for 32-bits; 1 for 64) were chosen because the old overflow
1182 # detection algorithm did not catch them; this means this test also
c0236afe
KW
1183 # checks for that fix. The EBCDIC are arbitrary overflowing ones
1184 # since we have no reports of failures with it.
1185 (($is64bit)
1186 ? ((isASCII)
9d2d0ecd 1187 ? "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
c0236afe
KW
1188 : I8_to_native("\xff\xB0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"))
1189 : ((isASCII)
9d2d0ecd 1190 ? "\xfe\x86\x80\x80\x80\x80\x80"
c0236afe 1191 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"))),
ea5ced44 1192
1d1c12d9
KW
1193 # We include both warning categories to make sure the ABOVE_31_BIT one
1194 # has precedence
1195 "$UTF8_WARN_ABOVE_31_BIT|$UTF8_WARN_SUPER",
1196 "$UTF8_DISALLOW_ABOVE_31_BIT",
7dfd8446 1197 'utf8', 0,
c0236afe 1198 (! isASCII) ? 14 : ($is64bit) ? 13 : 7,
ea5ced44 1199 qr/overflow at byte .*, after start byte 0xf/
eb83ed87 1200 ],
c0236afe 1201);
2f8f112e 1202
c0236afe
KW
1203if ($is64bit) {
1204 no warnings qw{portable overflow};
2f8f112e 1205 push @tests,
c0236afe
KW
1206 [ "More than 32 bits",
1207 (isASCII)
9d2d0ecd 1208 ? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
c0236afe 1209 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1d1c12d9 1210 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
c0236afe 1211 'utf8', 0x1000000000, (isASCII) ? 13 : 14,
ea5ced44 1212 qr/Code point 0x.* is not Unicode, and not portable/
2f8f112e 1213 ];
83dc0f42
KW
1214 if (! isASCII) {
1215 push @tests, # These could falsely show wrongly in a naive implementation
1216 [ "requires at least 32 bits",
1217 I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1218 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1219 'utf8', 0x800000000, 14,
1220 qr/Code point 0x800000000 is not Unicode, and not portable/
1221 ],
1222 [ "requires at least 32 bits",
1223 I8_to_native("\xff\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1224 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1225 'utf8', 0x10000000000, 14,
1226 qr/Code point 0x10000000000 is not Unicode, and not portable/
1227 ],
1228 [ "requires at least 32 bits",
1229 I8_to_native("\xff\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1230 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1231 'utf8', 0x200000000000, 14,
1232 qr/Code point 0x200000000000 is not Unicode, and not portable/
1233 ],
1234 [ "requires at least 32 bits",
1235 I8_to_native("\xff\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1236 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1237 'utf8', 0x4000000000000, 14,
1238 qr/Code point 0x4000000000000 is not Unicode, and not portable/
1239 ],
1240 [ "requires at least 32 bits",
1241 I8_to_native("\xff\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1242 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1243 'utf8', 0x80000000000000, 14,
1244 qr/Code point 0x80000000000000 is not Unicode, and not portable/
1245 ],
1246 [ "requires at least 32 bits",
1247 I8_to_native("\xff\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1248 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1249 'utf8', 0x1000000000000000, 14,
1250 qr/Code point 0x1000000000000000 is not Unicode, and not portable/
1251 ];
1252 }
2f8f112e
KW
1253}
1254
1255foreach my $test (@tests) {
eb83ed87
KW
1256 my ($testname, $bytes, $warn_flags, $disallow_flags, $category, $allowed_uv, $expected_len, $message ) = @$test;
1257
1258 my $length = length $bytes;
2f8f112e 1259 my $will_overflow = $testname =~ /overflow/;
eb83ed87 1260
d7874298
KW
1261 {
1262 use warnings;
1263 undef @warnings;
1264 my $ret = test_isUTF8_CHAR($bytes, $length);
1265 if ($will_overflow) {
1266 is($ret, 0, "isUTF8_CHAR() $testname: returns 0");
1267 }
1268 else {
1269 is($ret, $length,
1270 "isUTF8_CHAR() $testname: returns expected length: $length");
1271 }
1272 unless (is(scalar @warnings, 0,
1273 "isUTF8_CHAR() $testname: generated no warnings"))
1274 {
1275 diag "The warnings were: " . join(", ", @warnings);
1276 }
3d56ecbe 1277
e23e8bc1
KW
1278 undef @warnings;
1279 $ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
1280 if ($will_overflow) {
1281 is($ret, 0, "isSTRICT_UTF8_CHAR() $testname: returns 0");
1282 }
1283 else {
1284 my $expected_ret = ( $testname =~ /surrogate|non-character/
1285 || $allowed_uv > 0x10FFFF)
1286 ? 0
1287 : $length;
1288 is($ret, $expected_ret,
1289 "isSTRICT_UTF8_CHAR() $testname: returns expected length: $expected_ret");
1290 }
1291 unless (is(scalar @warnings, 0,
1292 "isSTRICT_UTF8_CHAR() $testname: generated no warnings"))
1293 {
1294 diag "The warnings were: " . join(", ", @warnings);
1295 }
1296
3d56ecbe
KW
1297 # Test partial character handling, for each byte not a full character
1298 for my $j (1.. $length - 1) {
1299
1300 # Skip the test for the interaction between overflow and above-31
1301 # bit. It is really testing other things than the partial
1302 # character tests, for which other tests in this file are
1303 # sufficient
1304 last if $testname =~ /overflow/;
1305
1306 foreach my $disallow_flag (0, $disallow_flags) {
1307 my $partial = substr($bytes, 0, $j);
1308 my $ret_should_be;
1309 my $comment;
1310 if ($disallow_flag) {
1311 $ret_should_be = 0;
1312 $comment = "disallowed";
1313 }
1314 else {
1315 $ret_should_be = 1;
1316 $comment = "allowed";
1317 }
1318
1319 if ($disallow_flag) {
1320 if ($testname =~ /non-character/) {
1321 $ret_should_be = 1;
1322 $comment .= ", but but need full char to discern";
1323 }
1324 elsif ($testname =~ /surrogate/) {
1325 if ($j < 2) {
1326 $ret_should_be = 1;
1327 $comment .= ", but need 2 bytes to discern";
1328 }
1329 }
1330 elsif ($testname =~ /first non_unicode/ && $j < 2) {
1331 $ret_should_be = 1;
1332 $comment .= ", but need 2 bytes to discern";
1333 }
1334 }
1335
1336 undef @warnings;
1337
1338 $ret = test_is_utf8_valid_partial_char_flags($partial, $j, $disallow_flag);
1339 is($ret, $ret_should_be, "$testname: is_utf8_valid_partial_char_flags("
1340 . display_bytes($partial)
1341 . "), $comment: returns $ret_should_be");
1342 unless (is(scalar @warnings, 0,
1343 "$testname: is_utf8_valid_partial_char_flags() generated no warnings"))
1344 {
1345 diag "The warnings were: " . join(", ", @warnings);
1346 }
1347 }
1348 }
d7874298
KW
1349 }
1350
eb83ed87
KW
1351 # This is more complicated than the malformations tested earlier, as there
1352 # are several orthogonal variables involved. We test all the subclasses
1353 # of utf8 warnings to verify they work with and without the utf8 class,
1354 # and don't have effects on other sublass warnings
54f4afef 1355 foreach my $warning ('utf8', 'surrogate', 'nonchar', 'non_unicode') {
eb83ed87
KW
1356 foreach my $warn_flag (0, $warn_flags) {
1357 foreach my $disallow_flag (0, $disallow_flags) {
54f4afef 1358 foreach my $do_warning (0, 1) {
eb83ed87 1359
13d7a909
KW
1360 my $eval_warn = $do_warning
1361 ? "use warnings '$warning'"
1362 : $warning eq "utf8"
7dfd8446
KW
1363 ? "no warnings 'utf8'"
1364 : "use warnings 'utf8'; no warnings '$warning'";
2f8f112e 1365
13d7a909
KW
1366 # is effectively disallowed if will overflow, even if the
1367 # flag indicates it is allowed, fix up test name to
1368 # indicate this as well
1369 my $disallowed = $disallow_flag || $will_overflow;
2f8f112e 1370
046d01eb 1371 my $this_name = "utf8n_to_uvchr() $testname: " . (($disallow_flag)
13d7a909
KW
1372 ? 'disallowed'
1373 : ($disallowed)
1d1c12d9 1374 ? 'ABOVE_31_BIT allowed'
13d7a909
KW
1375 : 'allowed');
1376 $this_name .= ", $eval_warn";
1377 $this_name .= ", " . (($warn_flag)
1378 ? 'with warning flag'
1379 : 'no warning flag');
eb83ed87 1380
13d7a909
KW
1381 undef @warnings;
1382 my $ret_ref;
7dfd8446
KW
1383 my $display_bytes = display_bytes($bytes);
1384 my $call = "Call was: $eval_warn; \$ret_ref = test_utf8n_to_uvchr('$display_bytes', $length, $warn_flag|$disallow_flag)";
1385 my $eval_text = "$eval_warn; \$ret_ref = test_utf8n_to_uvchr('$bytes', $length, $warn_flag|$disallow_flag)";
13d7a909
KW
1386 eval "$eval_text";
1387 if (! ok ("$@ eq ''", "$this_name: eval succeeded")) {
7dfd8446 1388 diag "\$!='$!'; eval'd=\"$call\"";
13d7a909 1389 next;
54f4afef 1390 }
13d7a909 1391 if ($disallowed) {
7dfd8446
KW
1392 unless (is($ret_ref->[0], 0, "$this_name: Returns 0"))
1393 {
1394 diag $call;
1395 }
2f8f112e
KW
1396 }
1397 else {
7dfd8446 1398 unless (is($ret_ref->[0], $allowed_uv,
9d2d0ecd
KW
1399 "$this_name: Returns expected uv: "
1400 . sprintf("0x%04X", $allowed_uv)))
7dfd8446
KW
1401 {
1402 diag $call;
1403 }
1404 }
1405 unless (is($ret_ref->[1], $expected_len,
9d2d0ecd 1406 "$this_name: Returns expected length: $expected_len"))
7dfd8446
KW
1407 {
1408 diag $call;
13d7a909 1409 }
13d7a909
KW
1410
1411 if (! $do_warning
1412 && ($warning eq 'utf8' || $warning eq $category))
1413 {
1414 if (!is(scalar @warnings, 0,
1415 "$this_name: No warnings generated"))
1416 {
7dfd8446
KW
1417 diag $call;
1418 diag "The warnings were: " . join(", ", @warnings);
2f8f112e
KW
1419 }
1420 }
13d7a909
KW
1421 elsif ($will_overflow
1422 && ! $disallow_flag
1423 && $warning eq 'utf8')
1424 {
1425
1426 # Will get the overflow message instead of the expected
1427 # message under these circumstances, as they would
1428 # otherwise accept an overflowed value, which the code
1429 # should not allow, so falls back to overflow.
1430 if (is(scalar @warnings, 1,
1431 "$this_name: Got a single warning "))
1432 {
7dfd8446
KW
1433 unless (like($warnings[0], qr/overflow/,
1434 "$this_name: Got overflow warning"))
1435 {
1436 diag $call;
1437 }
13d7a909
KW
1438 }
1439 else {
7dfd8446 1440 diag $call;
13d7a909 1441 if (scalar @warnings) {
7dfd8446 1442 diag "The warnings were: "
13d7a909
KW
1443 . join(", ", @warnings);
1444 }
1445 }
eb83ed87 1446 }
13d7a909
KW
1447 elsif ($warn_flag
1448 && ($warning eq 'utf8' || $warning eq $category))
1449 {
1450 if (is(scalar @warnings, 1,
1451 "$this_name: Got a single warning "))
1452 {
7dfd8446
KW
1453 unless (like($warnings[0], $message,
1454 "$this_name: Got expected warning"))
1455 {
1456 diag $call;
1457 }
13d7a909
KW
1458 }
1459 else {
7dfd8446 1460 diag $call;
13d7a909 1461 if (scalar @warnings) {
7dfd8446 1462 diag "The warnings were: "
13d7a909
KW
1463 . join(", ", @warnings);
1464 }
eb83ed87
KW
1465 }
1466 }
eb83ed87 1467
13d7a909
KW
1468 # Check CHECK_ONLY results when the input is disallowed. Do
1469 # this when actually disallowed, not just when the
1470 # $disallow_flag is set
1471 if ($disallowed) {
1472 undef @warnings;
1473 $ret_ref = test_utf8n_to_uvchr($bytes, $length,
1474 $disallow_flag|$UTF8_CHECK_ONLY);
7dfd8446
KW
1475 unless (is($ret_ref->[0], 0, "$this_name, CHECK_ONLY: Returns 0")) {
1476 diag $call;
1477 }
1478 unless (is($ret_ref->[1], -1,
9d2d0ecd 1479 "$this_name: CHECK_ONLY: returns -1 for length"))
7dfd8446
KW
1480 {
1481 diag $call;
1482 }
13d7a909
KW
1483 if (! is(scalar @warnings, 0,
1484 "$this_name, CHECK_ONLY: no warnings generated"))
1485 {
7dfd8446
KW
1486 diag $call;
1487 diag "The warnings were: " . join(", ", @warnings);
13d7a909 1488 }
eb83ed87 1489 }
046d01eb
KW
1490
1491 # Now repeat some of the above, but for
1492 # uvchr_to_utf8_flags(). Since this comes from an
1493 # existing code point, it hasn't overflowed.
1494 next if $will_overflow;
1495
1496 # The warning and disallow flags passed in are for
1497 # utf8n_to_uvchr(). Convert them for
1498 # uvchr_to_utf8_flags().
1499 my $uvchr_warn_flag = 0;
1500 my $uvchr_disallow_flag = 0;
1501 if ($warn_flag) {
1502 if ($warn_flag == $UTF8_WARN_SURROGATE) {
1503 $uvchr_warn_flag = $UNICODE_WARN_SURROGATE
1504 }
1505 elsif ($warn_flag == $UTF8_WARN_NONCHAR) {
1506 $uvchr_warn_flag = $UNICODE_WARN_NONCHAR
1507 }
1508 elsif ($warn_flag == $UTF8_WARN_SUPER) {
1509 $uvchr_warn_flag = $UNICODE_WARN_SUPER
1510 }
1d1c12d9 1511 elsif ($warn_flag == $UTF8_WARN_ABOVE_31_BIT) {
046d01eb
KW
1512 $uvchr_warn_flag = $UNICODE_WARN_ABOVE_31_BIT;
1513 }
1514 else {
1515 fail(sprintf "Unexpected warn flag: %x",
1516 $warn_flag);
1517 next;
1518 }
1519 }
1520 if ($disallow_flag) {
1521 if ($disallow_flag == $UTF8_DISALLOW_SURROGATE) {
1522 $uvchr_disallow_flag = $UNICODE_DISALLOW_SURROGATE
1523 }
1524 elsif ($disallow_flag == $UTF8_DISALLOW_NONCHAR) {
1525 $uvchr_disallow_flag = $UNICODE_DISALLOW_NONCHAR
1526 }
1527 elsif ($disallow_flag == $UTF8_DISALLOW_SUPER) {
1528 $uvchr_disallow_flag = $UNICODE_DISALLOW_SUPER
1529 }
1d1c12d9 1530 elsif ($disallow_flag == $UTF8_DISALLOW_ABOVE_31_BIT) {
046d01eb
KW
1531 $uvchr_disallow_flag =
1532 $UNICODE_DISALLOW_ABOVE_31_BIT;
1533 }
1534 else {
1535 fail(sprintf "Unexpected disallow flag: %x",
1536 $disallow_flag);
1537 next;
1538 }
1539 }
1540
1541 $disallowed = $uvchr_disallow_flag;
1542
1543 $this_name = "uvchr_to_utf8_flags() $testname: "
1544 . (($uvchr_disallow_flag)
1545 ? 'disallowed'
1546 : ($disallowed)
1547 ? 'ABOVE_31_BIT allowed'
1548 : 'allowed');
1549 $this_name .= ", $eval_warn";
1550 $this_name .= ", " . (($uvchr_warn_flag)
1551 ? 'with warning flag'
1552 : 'no warning flag');
1553
1554 undef @warnings;
1555 my $ret;
1556 my $warn_flag = sprintf "0x%x", $uvchr_warn_flag;
1557 my $disallow_flag = sprintf "0x%x", $uvchr_disallow_flag;
1558 $call = sprintf "call was: $eval_warn; \$ret = test_uvchr_to_utf8_flags(0x%x, $warn_flag|$disallow_flag)", $allowed_uv;
1559 $eval_text = "$eval_warn; \$ret = test_uvchr_to_utf8_flags($allowed_uv, $warn_flag|$disallow_flag)";
1560 eval "$eval_text";
1561 if (! ok ("$@ eq ''", "$this_name: eval succeeded")) {
1562 diag "\$!='$!'; eval'd=\"$eval_text\"";
1563 next;
1564 }
1565 if ($disallowed) {
1566 unless (is($ret, undef, "$this_name: Returns undef")) {
1567 diag $call;
1568 }
1569 }
1570 else {
1571 unless (is($ret, $bytes, "$this_name: Returns expected string")) {
1572 diag $call;
1573 }
1574 }
1575 if (! $do_warning
1576 && ($warning eq 'utf8' || $warning eq $category))
1577 {
1578 if (!is(scalar @warnings, 0,
1579 "$this_name: No warnings generated"))
1580 {
1581 diag $call;
1582 diag "The warnings were: " . join(", ", @warnings);
1583 }
1584 }
1585 elsif ($uvchr_warn_flag
1586 && ($warning eq 'utf8' || $warning eq $category))
1587 {
1588 if (is(scalar @warnings, 1,
1589 "$this_name: Got a single warning "))
1590 {
046d01eb
KW
1591 unless (like($warnings[0], $message,
1592 "$this_name: Got expected warning"))
1593 {
1594 diag $call;
1595 }
046d01eb
KW
1596 }
1597 else {
1598 diag $call;
1599 if (scalar @warnings) {
1600 diag "The warnings were: "
1601 . join(", ", @warnings);
1602 }
1603 }
1604 }
eb83ed87
KW
1605 }
1606 }
1607 }
1608 }
1609}
6e3d6c02 1610
fed3ba5d 1611done_testing;