This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #125540] handle already being at EOF while not finding a heredoc terminator
[perl5.git] / t / op / utf8decode.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8
9 $|=1;
10
11 {
12     my $wide = v256;
13     use bytes;
14     my $ordwide = ord($wide);
15     printf "# under use bytes ord(v256) = 0x%02x\n", $ordwide;
16     skip_all('UTF-8-centric tests (not valid for UTF-EBCDIC)') if $ordwide == 140;
17
18     if ($ordwide != 196) {
19         printf "# v256 starts with 0x%02x\n", $ordwide;
20     }
21 }
22
23 no utf8;
24
25 foreach (<DATA>) {
26     if (/^(?:\d+(?:\.\d+)?)\s/ || /^#/) {
27         # print "# $_\n";
28     } elsif (my ($id, $okay, $Unicode, $byteslen, $hex, $charslen, $experr)
29              = /^(\d+\.\d+\.\d+[bu]?)   # ID
30                 \s+(y|n|N-?\d+)         # expect to pass or fail
31                 \s+([0-9a-f]{1,8}(?:,[0-9a-f]{1,8})*|-) # Unicode characters
32                 \s+(\d+)                # number of octets
33                 \s+([0-9a-f]{2}(?::[0-9a-f]{2})*)       # octets in hex
34                 \s+(\d+|-)              # number of characters
35                 (?:\s+(.+))?            # expected error (or comment)
36                 $/x) {
37         my @hex = split(/:/, $hex);
38         is(scalar @hex, $byteslen, 'Amount of hex tallies with byteslen');
39         my $octets = join '', map {chr hex $_} @hex;
40         is(length $octets, $byteslen, 'Number of octets tallies with byteslen');
41         if ($okay eq 'y') {
42             my @chars = map {hex $_} split ',', $Unicode;
43             is(scalar @chars, $charslen, 'Amount of hex tallies with charslen');
44             my @got;
45             warning_is(sub {@got = unpack 'C0U*', $octets}, undef,
46                        "No warnings expected for $id");
47             is("@got", "@chars", 'Got expected Unicode characters');
48         } elsif ($okay eq 'n') {
49             isnt($experr, '', "Expected warning for $id provided");
50             warnings_like(sub {unpack 'C0U*', $octets}, [qr/$experr/],
51                          "Only expected warning for $id");
52         } elsif ($okay !~ /^N(-?\d+)/) {
53             is($okay, 'n', "Confused test description for $id");
54         } else {
55             my $expect = $1;
56             my @warnings;
57
58             {
59                 local $SIG{__WARN__} = sub {
60                     print "# $id: @_";
61                     push @warnings, "@_";
62                 };
63                 unpack 'C0U*', $octets;
64             }
65
66             isnt($experr, '', "Expected first warning for $id provided");
67             like($warnings[0], qr/$experr/, "Expected first warning for $id seen");
68             local $::TODO;
69             if ($expect < 0) {
70                 $expect = -$expect;
71                 $::TODO = "Markus Kuhn states that $expect invalid sequences should be signalled";
72             }
73             unless (is(scalar @warnings, $expect, "Expected number of warnings for $id seen")) {
74                 note(join "", "Got:\n", @warnings);
75             }
76
77         }
78     } else {
79         fail("unknown format '$_'");
80     }
81 }
82
83 done_testing();
84
85 # This table is based on Markus Kuhn's UTF-8 Decode Stress Tester,
86 # http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt,
87 # version dated 2015-08-28.
88
89 __DATA__
90 1       Correct UTF-8
91 1.1.1 y 3ba,1f79,3c3,3bc,3b5    11      ce:ba:e1:bd:b9:cf:83:ce:bc:ce:b5        5
92 2       Boundary conditions
93 2.1     First possible sequence of certain length
94 2.1.1 y 0               1       00      1
95 2.1.2 y 80              2       c2:80   1
96 2.1.3 y 800             3       e0:a0:80        1
97 2.1.4 y 10000           4       f0:90:80:80     1
98 2.1.5 y 200000          5       f8:88:80:80:80  1
99 2.1.6 y 4000000         6       fc:84:80:80:80:80       1
100 2.2     Last possible sequence of certain length
101 2.2.1 y 7f              1       7f      1
102 2.2.2 y 7ff             2       df:bf   1
103 # The ffff is legal by default since 872c91ae155f6880
104 2.2.3 y ffff            3       ef:bf:bf        1       character 0xffff
105 2.2.4 y 1fffff          4       f7:bf:bf:bf     1
106 2.2.5 y 3ffffff         5       fb:bf:bf:bf:bf  1
107 2.2.6 y 7fffffff        6       fd:bf:bf:bf:bf:bf       1
108 2.3     Other boundary conditions
109 2.3.1 y d7ff            3       ed:9f:bf        1
110 2.3.2 y e000            3       ee:80:80        1
111 2.3.3 y fffd            3       ef:bf:bd        1
112 2.3.4 y 10ffff          4       f4:8f:bf:bf     1
113 2.3.5 y 110000          4       f4:90:80:80     1
114 3       Malformed sequences
115 3.1     Unexpected continuation bytes
116 3.1.1 n -               1       80      -       unexpected continuation byte 0x80
117 3.1.2 n -               1       bf      -       unexpected continuation byte 0xbf
118 3.1.3 N2 -              2       80:bf   -       unexpected continuation byte 0x80
119 3.1.4 N3 -              3       80:bf:80        -       unexpected continuation byte 0x80
120 3.1.5 N4 -              4       80:bf:80:bf     -       unexpected continuation byte 0x80
121 3.1.6 N5 -              5       80:bf:80:bf:80  -       unexpected continuation byte 0x80
122 3.1.7 N6 -              6       80:bf:80:bf:80:bf       -       unexpected continuation byte 0x80
123 3.1.8 N7 -              7       80:bf:80:bf:80:bf:80    -       unexpected continuation byte 0x80
124 3.1.9 N64 -     64      80:81:82:83:84:85:86:87:88:89:8a:8b:8c:8d:8e:8f:90:91:92:93:94:95:96:97:98:99:9a:9b:9c:9d:9e:9f:a0:a1:a2:a3:a4:a5:a6:a7:a8:a9:aa:ab:ac:ad:ae:af:b0:b1:b2:b3:b4:b5:b6:b7:b8:b9:ba:bb:bc:bd:be:bf -       unexpected continuation byte 0x80
125 3.2     Lonely start characters
126 3.2.1 N32 -     64      c0:20:c1:20:c2:20:c3:20:c4:20:c5:20:c6:20:c7:20:c8:20:c9:20:ca:20:cb:20:cc:20:cd:20:ce:20:cf:20:d0:20:d1:20:d2:20:d3:20:d4:20:d5:20:d6:20:d7:20:d8:20:d9:20:da:20:db:20:dc:20:dd:20:de:20:df:20 -       unexpected non-continuation byte 0x20, immediately after start byte 0xc0
127 3.2.2 N16 -     32      e0:20:e1:20:e2:20:e3:20:e4:20:e5:20:e6:20:e7:20:e8:20:e9:20:ea:20:eb:20:ec:20:ed:20:ee:20:ef:20 -       unexpected non-continuation byte 0x20, immediately after start byte 0xe0
128 3.2.3 N8 -      16      f0:20:f1:20:f2:20:f3:20:f4:20:f5:20:f6:20:f7:20 -       unexpected non-continuation byte 0x20, immediately after start byte 0xf0
129 3.2.4 N4 -      8       f8:20:f9:20:fa:20:fb:20 -       unexpected non-continuation byte 0x20, immediately after start byte 0xf8
130 3.2.5 N2 -      4       fc:20:fd:20     -       unexpected non-continuation byte 0x20, immediately after start byte 0xfc
131 3.3     Sequences with last continuation byte missing
132 3.3.1 n -       1       c0      -       1 byte, need 2
133 3.3.2 n -       2       e0:80   -       2 bytes, need 3
134 3.3.3 n -       3       f0:80:80        -       3 bytes, need 4
135 3.3.4 n -       4       f8:80:80:80     -       4 bytes, need 5
136 3.3.5 n -       5       fc:80:80:80:80  -       5 bytes, need 6
137 3.3.6 n -       1       df      -       1 byte, need 2
138 3.3.7 n -       2       ef:bf   -       2 bytes, need 3
139 3.3.8 n -       3       f7:bf:bf        -       3 bytes, need 4
140 3.3.9 n -       4       fb:bf:bf:bf     -       4 bytes, need 5
141 3.3.10 n -      5       fd:bf:bf:bf:bf  -       5 bytes, need 6
142 3.4     Concatenation of incomplete sequences
143 3.4.1 N10 -     30      c0:e0:80:f0:80:80:f8:80:80:80:fc:80:80:80:80:df:ef:bf:f7:bf:bf:fb:bf:bf:bf:fd:bf:bf:bf:bf       -       unexpected non-continuation byte 0xe0, immediately after start byte 0xc0
144 3.5     Impossible bytes
145 3.5.1 n -       1       fe      -       byte 0xfe
146 3.5.2 n -       1       ff      -       byte 0xff
147 3.5.3 N4 -      4       fe:fe:ff:ff     -       byte 0xfe
148 4       Overlong sequences
149 4.1     Examples of an overlong ASCII character
150 4.1.1 n -       2       c0:af   -       2 bytes, need 1
151 4.1.2 n -       3       e0:80:af        -       3 bytes, need 1
152 4.1.3 n -       4       f0:80:80:af     -       4 bytes, need 1
153 4.1.4 n -       5       f8:80:80:80:af  -       5 bytes, need 1
154 4.1.5 n -       6       fc:80:80:80:80:af       -       6 bytes, need 1
155 4.2     Maximum overlong sequences
156 4.2.1 n -       2       c1:bf   -       2 bytes, need 1
157 4.2.2 n -       3       e0:9f:bf        -       3 bytes, need 2
158 4.2.3 n -       4       f0:8f:bf:bf     -       4 bytes, need 3
159 4.2.4 n -       5       f8:87:bf:bf:bf  -       5 bytes, need 4
160 4.2.5 n -       6       fc:83:bf:bf:bf:bf       -       6 bytes, need 5
161 4.3     Overlong representation of the NUL character
162 4.3.1 n -       2       c0:80   -       2 bytes, need 1
163 4.3.2 n -       3       e0:80:80        -       3 bytes, need 1
164 4.3.3 n -       4       f0:80:80:80     -       4 bytes, need 1
165 4.3.4 n -       5       f8:80:80:80:80  -       5 bytes, need 1
166 4.3.5 n -       6       fc:80:80:80:80:80       -       6 bytes, need 1
167 5       Illegal code positions
168 5.1     Single UTF-16 surrogates
169 5.1.1 y d800    3       ed:a0:80        1       UTF-16 surrogate 0xd800
170 5.1.2 y db7f    3       ed:ad:bf        1       UTF-16 surrogate 0xdb7f
171 5.1.3 y db80    3       ed:ae:80        1       UTF-16 surrogate 0xdb80
172 5.1.4 y dbff    3       ed:af:bf        1       UTF-16 surrogate 0xdbff
173 5.1.5 y dc00    3       ed:b0:80        1       UTF-16 surrogate 0xdc00
174 5.1.6 y df80    3       ed:be:80        1       UTF-16 surrogate 0xdf80
175 5.1.7 y dfff    3       ed:bf:bf        1       UTF-16 surrogate 0xdfff
176 5.2     Paired UTF-16 surrogates
177 5.2.1 y d800,dc00       6       ed:a0:80:ed:b0:80       2       UTF-16 surrogates 0xd800, dc00
178 5.2.2 y d800,dfff       6       ed:a0:80:ed:bf:bf       2       UTF-16 surrogates 0xd800, dfff
179 5.2.3 y db7f,dc00       6       ed:ad:bf:ed:b0:80       2       UTF-16 surrogates 0xdb7f, dc00
180 5.2.4 y db7f,dfff       6       ed:ad:bf:ed:bf:bf       2       UTF-16 surrogates 0xdb7f, dfff
181 5.2.5 y db80,dc00       6       ed:ae:80:ed:b0:80       2       UTF-16 surrogates 0xdb80, dc00
182 5.2.6 y db80,dfff       6       ed:ae:80:ed:bf:bf       2       UTF-16 surrogates 0xdb80, dfff
183 5.2.7 y dbff,dc00       6       ed:af:bf:ed:b0:80       2       UTF-16 surrogates 0xdbff, dc00
184 5.2.8 y dbff,dfff       6       ed:af:bf:ed:bf:bf       2       UTF-16 surrogates 0xdbff, dfff
185 5.3     Other illegal code positions
186 5.3.1 y fffe    3       ef:bf:be        1       byte order mark 0xfffe
187 # The ffff is legal by default since 872c91ae155f6880
188 5.3.2 y ffff    3       ef:bf:bf        1       non-character 0xffff
189 5.3.3 y fdd0    3       ef:b7:90        1       non-character 0xfdd0
190 5.3.3 y fdd1    3       ef:b7:91        1       non-character 0xfdd1
191 5.3.3 y fdd2    3       ef:b7:92        1       non-character 0xfdd2
192 5.3.3 y fdd3    3       ef:b7:93        1       non-character 0xfdd3
193 5.3.3 y fdd4    3       ef:b7:94        1       non-character 0xfdd4
194 5.3.3 y fdd5    3       ef:b7:95        1       non-character 0xfdd5
195 5.3.3 y fdd6    3       ef:b7:96        1       non-character 0xfdd6
196 5.3.3 y fdd7    3       ef:b7:97        1       non-character 0xfdd7
197 5.3.3 y fdd8    3       ef:b7:98        1       non-character 0xfdd8
198 5.3.3 y fdd9    3       ef:b7:99        1       non-character 0xfdd9
199 5.3.3 y fdda    3       ef:b7:9a        1       non-character 0xfdda
200 5.3.3 y fddb    3       ef:b7:9b        1       non-character 0xfddb
201 5.3.3 y fddc    3       ef:b7:9c        1       non-character 0xfddc
202 5.3.3 y fddd    3       ef:b7:9d        1       non-character 0xfddd
203 5.3.3 y fdde    3       ef:b7:9e        1       non-character 0xfdde
204 5.3.3 y fddf    3       ef:b7:9f        1       non-character 0xfddf
205 5.3.3 y fde0    3       ef:b7:a0        1       non-character 0xfde0
206 5.3.3 y fde1    3       ef:b7:a1        1       non-character 0xfde1
207 5.3.3 y fde2    3       ef:b7:a2        1       non-character 0xfde2
208 5.3.3 y fde3    3       ef:b7:a3        1       non-character 0xfde3
209 5.3.3 y fde4    3       ef:b7:a4        1       non-character 0xfde4
210 5.3.3 y fde5    3       ef:b7:a5        1       non-character 0xfde5
211 5.3.3 y fde6    3       ef:b7:a6        1       non-character 0xfde6
212 5.3.3 y fde7    3       ef:b7:a7        1       non-character 0xfde7
213 5.3.3 y fde8    3       ef:b7:a8        1       non-character 0xfde8
214 5.3.3 y fde9    3       ef:b7:a9        1       non-character 0xfde9
215 5.3.3 y fdea    3       ef:b7:aa        1       non-character 0xfdea
216 5.3.3 y fdeb    3       ef:b7:ab        1       non-character 0xfdeb
217 5.3.3 y fdec    3       ef:b7:ac        1       non-character 0xfdec
218 5.3.3 y fded    3       ef:b7:ad        1       non-character 0xfded
219 5.3.3 y fdee    3       ef:b7:ae        1       non-character 0xfdee
220 5.3.3 y fdef    3       ef:b7:af        1       non-character 0xfdef
221 5.3.4 y 1fffe   4       f0:9f:bf:be     1       non-character 0x1fffe
222 5.3.4 y 1ffff   4       f0:9f:bf:bf     1       non-character 0x1ffff
223 5.3.4 y 2fffe   4       f0:af:bf:be     1       non-character 0x2fffe
224 5.3.4 y 2ffff   4       f0:af:bf:bf     1       non-character 0x2ffff
225 5.3.4 y 3fffe   4       f0:bf:bf:be     1       non-character 0x3fffe
226 5.3.4 y 3ffff   4       f0:bf:bf:bf     1       non-character 0x3ffff
227 5.3.4 y 4fffe   4       f1:8f:bf:be     1       non-character 0x4fffe
228 5.3.4 y 4ffff   4       f1:8f:bf:bf     1       non-character 0x4ffff
229 5.3.4 y 5fffe   4       f1:9f:bf:be     1       non-character 0x5fffe
230 5.3.4 y 5ffff   4       f1:9f:bf:bf     1       non-character 0x5ffff
231 5.3.4 y 6fffe   4       f1:af:bf:be     1       non-character 0x6fffe
232 5.3.4 y 6ffff   4       f1:af:bf:bf     1       non-character 0x6ffff
233 5.3.4 y 7fffe   4       f1:bf:bf:be     1       non-character 0x7fffe
234 5.3.4 y 7ffff   4       f1:bf:bf:bf     1       non-character 0x7ffff
235 5.3.4 y 8fffe   4       f2:8f:bf:be     1       non-character 0x8fffe
236 5.3.4 y 8ffff   4       f2:8f:bf:bf     1       non-character 0x8ffff
237 5.3.4 y 9fffe   4       f2:9f:bf:be     1       non-character 0x9fffe
238 5.3.4 y 9ffff   4       f2:9f:bf:bf     1       non-character 0x9ffff
239 5.3.4 y afffe   4       f2:af:bf:be     1       non-character 0xafffe
240 5.3.4 y affff   4       f2:af:bf:bf     1       non-character 0xaffff
241 5.3.4 y bfffe   4       f2:bf:bf:be     1       non-character 0xbfffe
242 5.3.4 y bffff   4       f2:bf:bf:bf     1       non-character 0xbffff
243 5.3.4 y cfffe   4       f3:8f:bf:be     1       non-character 0xcfffe
244 5.3.4 y cffff   4       f3:8f:bf:bf     1       non-character 0xcffff
245 5.3.4 y dfffe   4       f3:9f:bf:be     1       non-character 0xdfffe
246 5.3.4 y dffff   4       f3:9f:bf:bf     1       non-character 0xdffff
247 5.3.4 y efffe   4       f3:af:bf:be     1       non-character 0xefffe
248 5.3.4 y effff   4       f3:af:bf:bf     1       non-character 0xeffff
249 5.3.4 y ffffe   4       f3:bf:bf:be     1       non-character 0xffffe
250 5.3.4 y fffff   4       f3:bf:bf:bf     1       non-character 0xfffff
251 5.3.4 y 10fffe  4       f4:8f:bf:be     1       non-character 0x10fffe
252 5.3.4 y 10ffff  4       f4:8f:bf:bf     1       non-character 0x10ffff