This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Split off the UTF-8 decoder tests, make them to check also
[perl5.git] / t / op / utf8decode.t
CommitLineData
a9917092
JH
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8print "1..78\n";
9
10my $test = 1;
11
12# This table is based on Markus Kuhn's UTF-8 Decode Stress Tester,
13# http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt,
14# version dated 2000-09-02.
15
16# Note the \0 instead of a raw zero byte in 2.1.1: for example
17# GNU patch v2.1 has "issues" with raw zero bytes.
18
19my @MK = split(/\n/, <<__EOMK__);
201 Correct UTF-8
211.1.1 y "κόσμε" - 11 ce:ba:e1:bd:b9:cf:83:ce:bc:ce:b5 5
222 Boundary conditions
232.1 First possible sequence of certain length
242.1.1 y "\0" 0 1 00 1
252.1.2 y "\80" 80 2 c2:80 1
262.1.3 y "ࠀ" 800 3 e0:a0:80 1
272.1.4 y "𐀀" 10000 4 f0:90:80:80 1
282.1.5 y "" 200000 5 f8:88:80:80:80 1
292.1.6 y "" 4000000 6 fc:84:80:80:80:80 1
302.2 Last possible sequence of certain length
312.2.1 y "\7f" 7f 1 7f 1
322.2.2 y "߿" 7ff 2 df:bf 1
33# The ffff is illegal unless UTF8_ALLOW_FFFF
342.2.3 n "￿" ffff 3 ef:bf:bf 1 character 0xffff
352.2.4 y "" 1fffff 4 f7:bf:bf:bf 1
362.2.5 y "" 3ffffff 5 fb:bf:bf:bf:bf 1
372.2.6 y "" 7fffffff 6 fd:bf:bf:bf:bf:bf 1
382.3 Other boundary conditions
392.3.1 y "퟿" d7ff 3 ed:9f:bf 1
402.3.2 y "" e000 3 ee:80:80 1
412.3.3 y "�" fffd 3 ef:bf:bd 1
422.3.4 y "􏿿" 10ffff 4 f4:8f:bf:bf 1
432.3.5 y "" 110000 4 f4:90:80:80 1
443 Malformed sequences
453.1 Unexpected continuation bytes
463.1.1 n "\80" - 1 80 - unexpected continuation byte 0x80
473.1.2 n "¿" - 1 bf - unexpected continuation byte 0xbf
483.1.3 n "\80¿" - 2 80:bf - unexpected continuation byte 0x80
493.1.4 n "\80¿\80" - 3 80:bf:80 - unexpected continuation byte 0x80
503.1.5 n "\80¿\80¿" - 4 80:bf:80:bf - unexpected continuation byte 0x80
513.1.6 n "\80¿\80¿\80" - 5 80:bf:80:bf:80 - unexpected continuation byte 0x80
523.1.7 n "\80¿\80¿\80¿" - 6 80:bf:80:bf:80:bf - unexpected continuation byte 0x80
533.1.8 n "\80¿\80¿\80¿\80" - 7 80:bf:80:bf:80:bf:80 - unexpected continuation byte 0x80
543.1.9 n "\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 ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿" - 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
553.2 Lonely start characters
563.2.1 n "À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß " - 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 after byte 0xc0
573.2.2 n "à á â ã ä å æ ç è é ê ë ì í î ï " - 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 after byte 0xe0
583.2.3 n "ð ñ ò ó ô õ ö ÷ " - 16 f0:20:f1:20:f2:20:f3:20:f4:20:f5:20:f6:20:f7:20 - unexpected non-continuation byte 0x20 after byte 0xf0
593.2.4 n "ø ù ú û " - 8 f8:20:f9:20:fa:20:fb:20 - unexpected non-continuation byte 0x20 after byte 0xf8
603.2.5 n "ü ý " - 4 fc:20:fd:20 - unexpected non-continuation byte 0x20 after byte 0xfc
613.3 Sequences with last continuation byte missing
623.3.1 n "À" - 1 c0 - 1 byte, need 2
633.3.2 n "à\80" - 2 e0:80 - 2 bytes, need 3
643.3.3 n "ð\80\80" - 3 f0:80:80 - 3 bytes, need 4
653.3.4 n "ø\80\80\80" - 4 f8:80:80:80 - 4 bytes, need 5
663.3.5 n "ü\80\80\80\80" - 5 fc:80:80:80:80 - 5 bytes, need 6
673.3.6 n "ß" - 1 df - 1 byte, need 2
683.3.7 n "ï¿" - 2 ef:bf - 2 bytes, need 3
693.3.8 n "÷¿¿" - 3 f7:bf:bf - 3 bytes, need 4
703.3.9 n "û¿¿¿" - 4 fb:bf:bf:bf - 4 bytes, need 5
713.3.10 n "ý¿¿¿¿" - 5 fd:bf:bf:bf:bf - 5 bytes, need 6
723.4 Concatenation of incomplete sequences
733.4.1 n "Àà\80ð\80\80ø\80\80\80ü\80\80\80\80ßï¿÷¿¿û¿¿¿ý¿¿¿¿" - 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 continuation byte 0xe0
743.5 Impossible bytes
753.5.1 n "þ" - 1 fe - byte 0xfe
763.5.2 n "ÿ" - 1 ff - byte 0xff
773.5.3 n "þþÿÿ" - 4 fe:fe:ff:ff - byte 0xfe
784 Overlong sequences
794.1 Examples of an overlong ASCII character
804.1.1 n "À¯" - 2 c0:af - 2 bytes, need 1
814.1.2 n "à\80¯" - 3 e0:80:af - 3 bytes, need 1
824.1.3 n "ð\80\80¯" - 4 f0:80:80:af - 4 bytes, need 1
834.1.4 n "ø\80\80\80¯" - 5 f8:80:80:80:af - 5 bytes, need 1
844.1.5 n "ü\80\80\80\80¯" - 6 fc:80:80:80:80:af - 6 bytes, need 1
854.2 Maximum overlong sequences
864.2.1 n "Á¿" - 2 c1:bf - 2 bytes, need 1
874.2.2 n "à\9f¿" - 3 e0:9f:bf - 3 bytes, need 2
884.2.3 n "ð\8f¿¿" - 4 f0:8f:bf:bf - 4 bytes, need 3
894.2.4 n "ø\87¿¿¿" - 5 f8:87:bf:bf:bf - 5 bytes, need 4
904.2.5 n "ü\83¿¿¿¿" - 6 fc:83:bf:bf:bf:bf - 6 bytes, need 5
914.3 Overlong representation of the NUL character
924.3.1 n "À\80" - 2 c0:80 - 2 bytes, need 1
934.3.2 n "à\80\80" - 3 e0:80:80 - 3 bytes, need 1
944.3.3 n "ð\80\80\80" - 4 f0:80:80:80 - 4 bytes, need 1
954.3.4 n "ø\80\80\80\80" - 5 f8:80:80:80:80 - 5 bytes, need 1
964.3.5 n "ü\80\80\80\80\80" - 6 fc:80:80:80:80:80 - 6 bytes, need 1
975 Illegal code positions
985.1 Single UTF-16 surrogates
995.1.1 n "" - 3 ed:a0:80 - UTF-16 surrogate 0xd800
1005.1.2 n "" - 3 ed:ad:bf - UTF-16 surrogate 0xdb7f
1015.1.3 n "" - 3 ed:ae:80 - UTF-16 surrogate 0xdb80
1025.1.4 n "" - 3 ed:af:bf - UTF-16 surrogate 0xdbff
1035.1.5 n "" - 3 ed:b0:80 - UTF-16 surrogate 0xdc00
1045.1.6 n "" - 3 ed:be:80 - UTF-16 surrogate 0xdf80
1055.1.7 n "" - 3 ed:bf:bf - UTF-16 surrogate 0xdfff
1065.2 Paired UTF-16 surrogates
1075.2.1 n "" - 6 ed:a0:80:ed:b0:80 - UTF-16 surrogate 0xd800
1085.2.2 n "" - 6 ed:a0:80:ed:bf:bf - UTF-16 surrogate 0xd800
1095.2.3 n "" - 6 ed:ad:bf:ed:b0:80 - UTF-16 surrogate 0xdb7f
1105.2.4 n "" - 6 ed:ad:bf:ed:bf:bf - UTF-16 surrogate 0xdb7f
1115.2.5 n "" - 6 ed:ae:80:ed:b0:80 - UTF-16 surrogate 0xdb80
1125.2.6 n "" - 6 ed:ae:80:ed:bf:bf - UTF-16 surrogate 0xdb80
1135.2.7 n "" - 6 ed:af:bf:ed:b0:80 - UTF-16 surrogate 0xdbff
1145.2.8 n "" - 6 ed:af:bf:ed:bf:bf - UTF-16 surrogate 0xdbff
1155.3 Other illegal code positions
1165.3.1 n "￾" - 3 ef:bf:be - byte order mark 0xfffe
117# The ffff is illegal unless UTF8_ALLOW_FFFF
1185.3.2 n "￿" - 3 ef:bf:bf - character 0xffff
119__EOMK__
120
121# 104..181
122{
123 my $WARNCNT;
124 my $id;
125
126 local $SIG{__WARN__} =
127 sub {
128 # print "# $id: @_";
129 $WARNCNT++;
130 $WARNMSG = "@_";
131 };
132
133 sub moan {
134 print "$id: @_";
135 }
136
137 sub test_unpack_U {
138 $WARNCNT = 0;
139 $WARNMSG = "";
140 unpack('U*', $_[0]);
141 }
142
143 for (@MK) {
144 if (/^(?:\d+(?:\.\d+)?)\s/ || /^#/) {
145 # print "# $_\n";
146 } elsif (/^(\d+\.\d+\.\d+[bu]?)\s+([yn])\s+"(.+)"\s+([0-9a-f]{1,8}|-)\s+(\d+)\s+([0-9a-f]{2}(?::[0-9a-f]{2})*)(?:\s+((?:\d+|-)(?:\s+(.+))?))?$/) {
147 $id = $1;
148 my ($okay, $bytes, $Unicode, $byteslen, $hex, $charslen, $error) =
149 ($2, $3, $4, $5, $6, $7, $8);
150 my @hex = split(/:/, $hex);
151 unless (@hex == $byteslen) {
152 my $nhex = @hex;
153 moan "amount of hex ($nhex) not equal to byteslen ($byteslen)\n";
154 }
155 {
156 use bytes;
157 my $bytesbyteslen = length($bytes);
158 unless ($bytesbyteslen == $byteslen) {
159 moan "bytes length() ($bytesbyteslen) not equal to $byteslen\n";
160 }
161 }
162 if ($okay eq 'y') {
163 test_unpack_U($bytes);
164 if ($WARNCNT) {
165 moan "unpack('U*') false negative\n";
166 print "not ";
167 }
168 } elsif ($okay eq 'n') {
169 test_unpack_U($bytes);
170 if ($WARNCNT == 0 || ($error ne '' && $WARNMSG !~ /$error/)) {
171 moan "unpack('U*') false positive\n";
172 print "not ";
173 }
174 }
175 print "ok $test\n";
176 $test++;
177 } else {
178 moan "unknown format\n";
179 }
180 }
181}