3 binmode STDOUT, ':utf8';
4 binmode STDERR, ':utf8';
10 require Config; import Config;
11 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
24 my $dumper = Dumpvalue->new(
31 my($mess) = join "", @_;
33 print $dumper->stringify($mess,1), "\n";
40 my $have_setlocale = 0;
43 import POSIX ':locale_h';
47 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
48 # and mingw32 uses said silly CRT
49 # This doesn't seem to be an issue any more, at least on Windows XP,
50 # so re-enable the tests for Windows XP onwards.
51 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
52 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
53 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
54 $Config{cc} =~ /^(cl|gcc)/i);
56 # UWIN seems to loop after taint tests, just skip for now
57 $have_setlocale = 0 if ($^O =~ /^uwin/);
66 my ($result, $message) = @_;
67 $message = "" unless defined $message;
69 print 'not ' unless ($result);
70 print "ok " . ++$test_num;
75 # First we'll do a lot of taint checking for locales.
76 # This is the easiest to test, actually, as any locale,
77 # even the default locale will taint under 'use locale'.
79 sub is_tainted { # hello, camel two.
80 no warnings 'uninitialized' ;
83 not eval { $dummy = join("", @_), kill 0; 1 }
87 ok is_tainted($_[0]), "verify that is tainted";
90 sub check_taint_not ($) {
91 ok((not is_tainted($_[0])), "verify that isn't tainted");
94 use locale; # engage locale and therefore locale taint.
100 check_taint ucfirst($a);
104 check_taint lcfirst($a);
107 check_taint_not sprintf('%e', 123.456);
108 check_taint_not sprintf('%f', 123.456);
109 check_taint_not sprintf('%g', 123.456);
110 check_taint_not sprintf('%d', 123.456);
111 check_taint_not sprintf('%x', 123.456);
113 $_ = $a; # untaint $_
115 $_ = uc($a); # taint $_
119 /(\w)/; # taint $&, $`, $', $+, $1.
127 /(.)/; # untaint $&, $`, $', $+, $1.
135 /(\W)/; # taint $&, $`, $', $+, $1.
143 /(\s)/; # taint $&, $`, $', $+, $1.
151 /(\S)/; # taint $&, $`, $', $+, $1.
159 $_ = $a; # untaint $_
163 /(b)/; # this must not taint
171 $_ = $a; # untaint $_
175 $b = uc($a); # taint $b
176 s/(.+)/$b/; # this must taint only the $_
186 $_ = $a; # untaint $_
188 s/(.+)/b/; # this must not taint
197 $b = $a; # untaint $b
199 ($b = $a) =~ s/\w/$&/;
200 check_taint $b; # $b should be tainted.
201 check_taint_not $a; # $a should be not.
203 $_ = $a; # untaint $_
205 s/(\w)/\l$1/; # this must taint
214 $_ = $a; # untaint $_
216 s/(\w)/\L$1/; # this must taint
225 $_ = $a; # untaint $_
227 s/(\w)/\u$1/; # this must taint
236 $_ = $a; # untaint $_
238 s/(\w)/\U$1/; # this must taint
247 # After all this tainting $a should be cool.
251 # I think we've seen quite enough of taint.
252 # Let us do some *real* locale work now,
253 # unless setlocale() is missing (i.e. minitest).
255 unless ($have_setlocale) {
256 print "1..$test_num\n";
260 # The test number before our first setlocale()
261 my $last_without_setlocale = $test_num;
265 debug "# Scanning for locales...\n";
267 # Note that it's okay that some languages have their native names
268 # capitalized here even though that's not "right". They are lowercased
269 # anyway later during the scanning process (and besides, some clueless
270 # vendor might have them capitalized erroneously anyway).
274 Arabic:ar:dz eg sa:6 arabic8
275 Brezhoneg Breton:br:fr:1 15
276 Bulgarski Bulgarian:bg:bg:5
277 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
278 Hrvatski Croatian:hr:hr:2
279 Cymraeg Welsh:cy:cy:1 14 15
281 Dansk Danish:dk:da:1 15
282 Nederlands Dutch:nl:be nl:1 15
283 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
285 Eesti Estonian:et:ee:4 6 13
286 Suomi Finnish:fi:fi:1 15
288 Deutsch German:de:at be ch de lu:1 15
289 Euskaraz Basque:eu:es fr:1 15
290 Galego Galician:gl:es:1 15
291 Ellada Greek:el:gr:7 g8
293 Greenlandic:kl:gl:4 6
294 Hebrew:iw:il:8 hebrew8
296 Indonesian:in:id:1 15
297 Gaeilge Irish:ga:IE:1 14 15
298 Italiano Italian:it:ch it:1 15
299 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
301 Latine Latin:la:va:1 15
303 Lithuanian:lt:lt:4 6 13
304 Macedonian:mk:mk:1 15
307 Norsk Norwegian:no no\@nynorsk:no:1 15
309 Polski Polish:pl:pl:2
311 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
312 Serbski Serbian:sr:yu:5
314 Slovene Slovenian:sl:si:2
315 Sqhip Albanian:sq:sq:1 15
316 Svenska Swedish:sv:fi se:1 15
318 Turkish:tr:tr:9 turkish8
322 if ($^O eq 'os390') {
323 # These cause heartburn. Broken locales?
324 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
325 $locales =~ s/Thai:th:th:11 tis620\n//;
328 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
331 require "lib/locale/utf8";
333 require "lib/locale/latin1";
344 sort grep /\w/, map { chr } 0..255
349 return if grep { $locale eq $_ } @Locale;
350 if (setlocale(LC_ALL, $locale)) {
351 push @Locale, $locale;
355 sub decode_encodings {
358 foreach (split(/ /, shift)) {
360 push @enc, "ISO8859-$1";
361 push @enc, "iso8859$1"; # HP
363 push @enc, "roman8"; # HP
367 push @enc, "$_.UTF-8";
370 if ($^O eq 'os390') {
371 push @enc, qw(IBM-037 IBM-819 IBM-1047);
380 trylocale("ISO8859-$_");
381 trylocale("iso8859$_");
382 trylocale("iso8859-$_");
383 trylocale("iso_8859_$_");
384 trylocale("isolatin$_");
385 trylocale("isolatin-$_");
386 trylocale("iso_latin_$_");
389 # Sanitize the environment so that we can run the external 'locale'
390 # program without the taint mode getting grumpy.
392 # $ENV{PATH} is special in VMS.
393 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
395 # Other subversive stuff.
396 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
398 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
400 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
401 # ain't great when we're running this testPERL_UNICODE= so that utf8
402 # locales will cause all IO hadles to default to (assume) utf8
403 next unless utf8::valid($_);
408 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
409 # The SYS$I18N_LOCALE logical name search list was not present on
410 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
411 opendir(LOCALES, "SYS\$I18N_LOCALE:");
412 while ($_ = readdir(LOCALES)) {
417 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
419 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
420 # is much easier and faster than the last resort method.
422 opendir(LOCALES, '/usr/share/locale');
423 while ($_ = readdir(LOCALES)) {
430 # This is going to be slow.
432 foreach my $locale (split(/\n/, $locales)) {
433 my ($locale_name, $language_codes, $country_codes, $encodings) =
435 my @enc = decode_encodings($encodings);
436 foreach my $loc (split(/ /, $locale_name)) {
438 foreach my $enc (@enc) {
439 trylocale("$loc.$enc");
442 foreach my $enc (@enc) {
443 trylocale("$loc.$enc");
446 foreach my $lang (split(/ /, $language_codes)) {
448 foreach my $country (split(/ /, $country_codes)) {
449 my $lc = "${lang}_${country}";
451 foreach my $enc (@enc) {
452 trylocale("$lc.$enc");
454 my $lC = "${lang}_\U${country}";
456 foreach my $enc (@enc) {
457 trylocale("$lC.$enc");
464 setlocale(LC_ALL, "C");
466 if ($^O eq 'darwin') {
467 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
468 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
469 (my $v) = $Config{osvers} =~ /^(\d+)/;
470 if ($v >= 8 and $v < 10) {
471 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
472 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
474 debug "# Skipping be_BY locales -- buggy in Darwin\n";
475 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
479 @Locale = sort @Locale;
481 debug "# Locales =\n";
493 my ($Locale, $i, $test) = @_;
495 $Problem{$i}{$Locale} = 1;
496 debug "# failed $i with locale '$Locale'\n";
498 push @{$Okay{$i}}, $Locale;
502 foreach $Locale (@Locale) {
503 debug "# Locale = $Locale\n";
505 unless (setlocale(LC_ALL, $Locale)) {
507 $Problem{$_}{$Locale} = -1;
512 @Alnum_ = getalnum_();
513 debug "# w = ", join("",@Alnum_), "\n";
515 # Sieve the uppercase and the lowercase.
521 if (/[^\d_]/) { # skip digits and the _
530 foreach (keys %UPPER) {
531 $BoThCaSe{$_}++ if exists $lower{$_};
533 foreach (keys %lower) {
534 $BoThCaSe{$_}++ if exists $UPPER{$_};
536 foreach (keys %BoThCaSe) {
541 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
542 debug "# lower = ", join("", sort keys %lower ), "\n";
543 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
545 # Find the alphabets that are not alphabets in the default locale.
551 for (keys %UPPER, keys %lower) {
552 push(@Neoalpha, $_) if (/\W/);
557 @Neoalpha = sort @Neoalpha;
559 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
561 if (@Neoalpha == 0) {
562 # If we have no Neoalphas the remaining tests are no-ops.
563 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
565 push @{$Okay{$_}}, $Locale;
571 my $word = join('', @Neoalpha);
575 local $SIG{__WARN__} = sub {
576 $badutf8 = $_[0] =~ /Malformed UTF-8/;
578 $Locale =~ /utf-?8/i;
582 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
583 } elsif ($Locale =~ /utf-?8/i) {
584 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
585 push @{$Okay{99}}, $Locale;
587 if ($word =~ /^(\w+)$/) {
588 tryneoalpha($Locale, 99, 1);
590 tryneoalpha($Locale, 99, 0);
594 # Cross-check the whole 8-bit character set.
596 for (map { chr } 0..255) {
597 tryneoalpha($Locale, 100,
603 # Test for read-only scalars' locale vs non-locale comparisons.
610 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
615 my ($from, $to, $lesser, $greater,
616 @test, %test, $test, $yes, $no, $sign);
620 $from = int(($_*@Alnum_)/10);
621 $to = $from + int(@Alnum_/10);
622 $to = $#Alnum_ if ($to > $#Alnum_);
623 $lesser = join('', @Alnum_[$from..$to]);
624 # Select a slice one character on.
626 $to = $#Alnum_ if ($to > $#Alnum_);
627 $greater = join('', @Alnum_[$from..$to]);
628 ($yes, $no, $sign) = ($lesser lt $greater
630 : ("not ", " ", -1));
631 # all these tests should FAIL (return 0).
632 # Exact lt or gt cannot be tested because
633 # in some locales, say, eacute and E may test equal.
636 $no.' ($lesser le $greater)', # 1
637 'not ($lesser ne $greater)', # 2
638 ' ($lesser eq $greater)', # 3
639 $yes.' ($lesser ge $greater)', # 4
640 $yes.' ($lesser ge $greater)', # 5
641 $yes.' ($greater le $lesser )', # 7
642 'not ($greater ne $lesser )', # 8
643 ' ($greater eq $lesser )', # 9
644 $no.' ($greater ge $lesser )', # 10
645 'not (($lesser cmp $greater) == -($sign))' # 11
647 @test{@test} = 0 x @test;
650 $test{$ti} = eval $ti;
653 tryneoalpha($Locale, 102, $test == 0);
655 debug "# lesser = '$lesser'\n";
656 debug "# greater = '$greater'\n";
657 debug "# lesser cmp greater = ",
658 $lesser cmp $greater, "\n";
659 debug "# greater cmp lesser = ",
660 $greater cmp $lesser, "\n";
661 debug "# (greater) from = $from, to = $to\n";
663 debugf("# %-40s %-4s", $ti,
664 $test{$ti} ? 'FAIL' : 'ok');
665 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
666 debugf("(%s == %4d)", $1, eval $1);
679 my ($x, $y) = (1.23, 1.23);
682 printf ''; # printf used to reset locale to "C"
685 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
687 tryneoalpha($Locale, 103, $a eq $b);
690 my $z = sprintf ''; # sprintf used to reset locale to "C"
693 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
695 tryneoalpha($Locale, 104, $c eq $d);
700 local $SIG{__WARN__} =
706 # The == (among other ops) used to warn for locales
707 # that had something else than "." as the radix character.
709 tryneoalpha($Locale, 105, $c == 1.23);
711 tryneoalpha($Locale, 106, $c == $x);
713 tryneoalpha($Locale, 107, $c == $d);
716 # no locale; # XXX did this ever work correctly?
720 debug "# 108..110: e = $e, Locale = $Locale\n";
722 tryneoalpha($Locale, 108, $e == 1.23);
724 tryneoalpha($Locale, 109, $e == $x);
726 tryneoalpha($Locale, 110, $e == $c);
732 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
734 tryneoalpha($Locale, 111, $f == 1.23);
736 tryneoalpha($Locale, 112, $f == $x);
738 tryneoalpha($Locale, 113, $f == $c);
740 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
742 tryneoalpha($Locale, 115, $w == 0);
745 # Does taking lc separately differ from taking
746 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
747 # The bug was in the caching of the 'o'-magic.
754 return $lc0 cmp $lc1;
758 return lc($_[0]) cmp lc($_[1]);
765 tryneoalpha($Locale, 116,
766 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
767 lcA($x, $z) == 0 && lcB($x, $z) == 0);
770 # Does lc of an UPPER (if different from the UPPER) match
771 # case-insensitively the UPPER, and does the UPPER match
772 # case-insensitively the lc of the UPPER. And vice versa.
776 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
779 foreach my $x (keys %UPPER) {
781 next unless uc $y eq $x;
782 print "# UPPER $x lc $y ",
783 $x =~ /$y/i ? 1 : 0, " ",
784 $y =~ /$x/i ? 1 : 0, "\n" if 0;
786 # If $x and $y contain regular expression characters
787 # AND THEY lowercase (/i) to regular expression characters,
788 # regcomp() will be mightily confused. No, the \Q doesn't
789 # help here (maybe regex engine internal lowercasing
790 # is done after the \Q?) An example of this happening is
791 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
792 # the chr(173) (the "[") is the lowercase of the chr(235).
794 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
795 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
796 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
797 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
798 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
799 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
801 # Similar things can happen even under (bastardised)
802 # non-EBCDIC locales: in many European countries before the
803 # advent of ISO 8859-x nationally customised versions of
804 # ISO 646 were devised, reusing certain punctuation
805 # characters for modified characters needed by the
806 # country/language. For example, the "|" might have
807 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
809 if ($x =~ $re || $y =~ $re) {
810 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
813 # With utf8 both will fail since the locale concept
814 # of upper/lower does not work well in Unicode.
815 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
818 foreach my $x (keys %lower) {
820 next unless lc $y eq $x;
821 print "# lower $x uc $y ",
822 $x =~ /$y/i ? 1 : 0, " ",
823 $y =~ /$x/i ? 1 : 0, "\n" if 0;
824 if ($x =~ $re || $y =~ $re) { # See above.
825 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
828 # With utf8 both will fail since the locale concept
829 # of upper/lower does not work well in Unicode.
830 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
832 tryneoalpha($Locale, 117, @f == 0);
834 print "# failed 117 locale '$Locale' characters @f\n"
839 my $last_locales = $have_setlocale ? &last_locales : $last_without_setlocale;
841 # Recount the errors.
843 foreach ($last_without_setlocale+1..$last_locales) {
844 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
846 print "# The failure of test 102 is not necessarily fatal.\n";
847 print "# It usually indicates a problem in the environment,\n";
848 print "# not in Perl itself.\n";
859 foreach (99..$last_locales) {
861 my @f = sort keys %{ $Problem{$_} };
862 my $f = join(" ", @f);
863 $f =~ s/(.{50,60}) /$1\n#\t/g;
866 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
868 "# on your system may have errors because the locale test $_\n",
869 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
873 # If your users are not using these locales you are safe for the moment,
874 # but please report this failure first to perlbug\@perl.com using the
875 # perlbug script (as described in the INSTALL file) so that the exact
876 # details of the failures can be sorted out first and then your operating
877 # system supplier can be alerted about these anomalies.
884 # Tell which locales were okay and which were not.
889 foreach my $l (@Locale) {
891 foreach my $t (102..$last_locales) {
892 $p++ if $Problem{$t}{$l};
894 push @s, $l if $p == 0;
895 push @F, $l unless $p == 0;
899 my $s = join(" ", @s);
900 $s =~ s/(.{50,60}) /$1\n#\t/g;
903 "# The following locales\n#\n",
905 "# tested okay.\n#\n",
907 warn "# None of your locales were fully okay.\n";
911 my $F = join(" ", @F);
912 $F =~ s/(.{50,60}) /$1\n#\t/g;
915 "# The following locales\n#\n",
917 "# had problems.\n#\n",
919 warn "# None of your locales were broken.\n";
923 my $S = join(" ", @utf8locale);
924 $S =~ s/(.{50,60}) /$1\n#\t/g;
926 warn "#\n# The following locales\n#\n",
928 "# were skipped for the tests ",
929 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
930 "# because UTF-8 and locales do not work together in Perl.\n#\n";
934 sub last_locales { 117 }
936 $test_num = $last_locales;
938 # Test that tainting and case changing works on utf8 strings. These tests are
939 # placed last to avoid disturbing the hard-coded test numbers above this in
941 setlocale(LC_ALL, "C");
945 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
946 my @list; # List of code points to test for $function
948 # Used to calculate the changed case for ASCII characters by using the
949 # ord, instead of using one of the functions under test.
950 my $ascii_case_change_delta;
951 my $above_latin1_case_change_delta; # Same for the specific ords > 255
954 # We test an ASCII character, which should change case and be tainted;
955 # a Latin1 character, which shouldn't change case under this C locale,
957 # an above-Latin1 character that when the case is changed would cross
958 # the 255/256 boundary, so doesn't change case and isn't tainted
959 # (the \x{149} is one of these, but changes into 2 characters, the
960 # first one of which doesn't cross the boundary.
961 # the final one in each list is an above-Latin1 character whose case
962 # does change, and shouldn't be tainted. The code below uses its
963 # position in its list as a marker to indicate that it, unlike the
964 # other code points above ASCII, has a successful case change
965 if ($function =~ /^u/) {
966 #@list = ("\xff", "\x{fb00}", "\x{149}", "\x{101}");
967 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
968 $ascii_case_change_delta = -32;
969 $above_latin1_case_change_delta = -1;
972 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
973 $ascii_case_change_delta = +32;
974 $above_latin1_case_change_delta = +1;
977 foreach my $j (0 .. $#list) {
978 my $char = $list[$j];
979 #print STDERR __LINE__, ": $char\n";
980 #check_taint_not($i++, $char);
981 utf8::upgrade($char);
982 #check_taint_not($i++, $char);
983 my $should_be = ($j == $#list)
984 ? chr(ord($char) + $above_latin1_case_change_delta)
985 : (length $char == 0 || ord($char) > 127)
987 : chr(ord($char) + $ascii_case_change_delta);
989 # This monstrosity is in order to avoid using an eval, which might
990 # perturb the results
991 my $changed = ($function eq "uc")
993 : ($function eq "ucfirst")
995 : ($function eq "lc")
997 : ($function eq "lcfirst")
999 : croak("Unexpected function \"$function\"");
1000 ok($changed eq $should_be, "$function(\"$char\") in C locale should be \"$should_be\", got \"$changed\"");
1002 # Tainting shouldn't happen for empty strings, or those characters
1004 #print STDERR __LINE__, ": $char\n";
1005 (length($char) > 0 && ord($char) < 256)
1006 ? check_taint($changed)
1007 : check_taint_not($changed);
1013 print "1..$test_num\n";