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";
341 sort grep /\w/, map { chr } 0..255
346 return if grep { $locale eq $_ } @Locale;
347 if (setlocale(LC_ALL, $locale)) {
348 push @Locale, $locale;
352 sub decode_encodings {
355 foreach (split(/ /, shift)) {
357 push @enc, "ISO8859-$1";
358 push @enc, "iso8859$1"; # HP
360 push @enc, "roman8"; # HP
364 push @enc, "$_.UTF-8";
367 if ($^O eq 'os390') {
368 push @enc, qw(IBM-037 IBM-819 IBM-1047);
377 trylocale("ISO8859-$_");
378 trylocale("iso8859$_");
379 trylocale("iso8859-$_");
380 trylocale("iso_8859_$_");
381 trylocale("isolatin$_");
382 trylocale("isolatin-$_");
383 trylocale("iso_latin_$_");
386 # Sanitize the environment so that we can run the external 'locale'
387 # program without the taint mode getting grumpy.
389 # $ENV{PATH} is special in VMS.
390 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
392 # Other subversive stuff.
393 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
395 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
397 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
398 # ain't great when we're running this testPERL_UNICODE= so that utf8
399 # locales will cause all IO hadles to default to (assume) utf8
400 next unless utf8::valid($_);
405 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
406 # The SYS$I18N_LOCALE logical name search list was not present on
407 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
408 opendir(LOCALES, "SYS\$I18N_LOCALE:");
409 while ($_ = readdir(LOCALES)) {
414 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
416 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
417 # is much easier and faster than the last resort method.
419 opendir(LOCALES, '/usr/share/locale');
420 while ($_ = readdir(LOCALES)) {
427 # This is going to be slow.
429 foreach my $locale (split(/\n/, $locales)) {
430 my ($locale_name, $language_codes, $country_codes, $encodings) =
432 my @enc = decode_encodings($encodings);
433 foreach my $loc (split(/ /, $locale_name)) {
435 foreach my $enc (@enc) {
436 trylocale("$loc.$enc");
439 foreach my $enc (@enc) {
440 trylocale("$loc.$enc");
443 foreach my $lang (split(/ /, $language_codes)) {
445 foreach my $country (split(/ /, $country_codes)) {
446 my $lc = "${lang}_${country}";
448 foreach my $enc (@enc) {
449 trylocale("$lc.$enc");
451 my $lC = "${lang}_\U${country}";
453 foreach my $enc (@enc) {
454 trylocale("$lC.$enc");
461 setlocale(LC_ALL, "C");
463 if ($^O eq 'darwin') {
464 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
465 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
466 (my $v) = $Config{osvers} =~ /^(\d+)/;
467 if ($v >= 8 and $v < 10) {
468 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
469 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
471 debug "# Skipping be_BY locales -- buggy in Darwin\n";
472 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
476 @Locale = sort @Locale;
478 debug "# Locales =\n";
489 my ($Locale, $i, $test) = @_;
491 $Problem{$i}{$Locale} = 1;
492 debug "# failed $i with locale '$Locale'\n";
494 push @{$Okay{$i}}, $Locale;
498 foreach $Locale (@Locale) {
499 debug "# Locale = $Locale\n";
501 unless (setlocale(LC_ALL, $Locale)) {
503 $Problem{$_}{$Locale} = -1;
508 @Alnum_ = getalnum_();
509 debug "# w = ", join("",@Alnum_), "\n";
511 # Sieve the uppercase and the lowercase.
517 if (/[^\d_]/) { # skip digits and the _
526 foreach (keys %UPPER) {
527 $BoThCaSe{$_}++ if exists $lower{$_};
529 foreach (keys %lower) {
530 $BoThCaSe{$_}++ if exists $UPPER{$_};
532 foreach (keys %BoThCaSe) {
537 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
538 debug "# lower = ", join("", sort keys %lower ), "\n";
539 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
541 # Find the alphabets that are not alphabets in the default locale.
547 for (keys %UPPER, keys %lower) {
548 push(@Neoalpha, $_) if (/\W/);
552 @Neoalpha = sort @Neoalpha;
554 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
556 if (@Neoalpha == 0) {
557 # If we have no Neoalphas the remaining tests are no-ops.
558 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
560 push @{$Okay{$_}}, $Locale;
566 my $word = join('', @Neoalpha);
570 local $SIG{__WARN__} = sub {
571 $badutf8 = $_[0] =~ /Malformed UTF-8/;
573 $Locale =~ /utf-?8/i;
577 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
578 } elsif ($Locale =~ /utf-?8/i) {
579 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
580 push @{$Okay{99}}, $Locale;
582 if ($word =~ /^(\w+)$/) {
583 tryneoalpha($Locale, 99, 1);
585 tryneoalpha($Locale, 99, 0);
589 # Cross-check the whole 8-bit character set.
591 for (map { chr } 0..255) {
592 tryneoalpha($Locale, 100,
598 # Test for read-only scalars' locale vs non-locale comparisons.
605 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
610 my ($from, $to, $lesser, $greater,
611 @test, %test, $test, $yes, $no, $sign);
615 $from = int(($_*@Alnum_)/10);
616 $to = $from + int(@Alnum_/10);
617 $to = $#Alnum_ if ($to > $#Alnum_);
618 $lesser = join('', @Alnum_[$from..$to]);
619 # Select a slice one character on.
621 $to = $#Alnum_ if ($to > $#Alnum_);
622 $greater = join('', @Alnum_[$from..$to]);
623 ($yes, $no, $sign) = ($lesser lt $greater
625 : ("not ", " ", -1));
626 # all these tests should FAIL (return 0).
627 # Exact lt or gt cannot be tested because
628 # in some locales, say, eacute and E may test equal.
631 $no.' ($lesser le $greater)', # 1
632 'not ($lesser ne $greater)', # 2
633 ' ($lesser eq $greater)', # 3
634 $yes.' ($lesser ge $greater)', # 4
635 $yes.' ($lesser ge $greater)', # 5
636 $yes.' ($greater le $lesser )', # 7
637 'not ($greater ne $lesser )', # 8
638 ' ($greater eq $lesser )', # 9
639 $no.' ($greater ge $lesser )', # 10
640 'not (($lesser cmp $greater) == -($sign))' # 11
642 @test{@test} = 0 x @test;
645 $test{$ti} = eval $ti;
648 tryneoalpha($Locale, 102, $test == 0);
650 debug "# lesser = '$lesser'\n";
651 debug "# greater = '$greater'\n";
652 debug "# lesser cmp greater = ",
653 $lesser cmp $greater, "\n";
654 debug "# greater cmp lesser = ",
655 $greater cmp $lesser, "\n";
656 debug "# (greater) from = $from, to = $to\n";
658 debugf("# %-40s %-4s", $ti,
659 $test{$ti} ? 'FAIL' : 'ok');
660 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
661 debugf("(%s == %4d)", $1, eval $1);
674 my ($x, $y) = (1.23, 1.23);
677 printf ''; # printf used to reset locale to "C"
680 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
682 tryneoalpha($Locale, 103, $a eq $b);
685 my $z = sprintf ''; # sprintf used to reset locale to "C"
688 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
690 tryneoalpha($Locale, 104, $c eq $d);
695 local $SIG{__WARN__} =
701 # The == (among other ops) used to warn for locales
702 # that had something else than "." as the radix character.
704 tryneoalpha($Locale, 105, $c == 1.23);
706 tryneoalpha($Locale, 106, $c == $x);
708 tryneoalpha($Locale, 107, $c == $d);
711 # no locale; # XXX did this ever work correctly?
715 debug "# 108..110: e = $e, Locale = $Locale\n";
717 tryneoalpha($Locale, 108, $e == 1.23);
719 tryneoalpha($Locale, 109, $e == $x);
721 tryneoalpha($Locale, 110, $e == $c);
727 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
729 tryneoalpha($Locale, 111, $f == 1.23);
731 tryneoalpha($Locale, 112, $f == $x);
733 tryneoalpha($Locale, 113, $f == $c);
735 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
737 tryneoalpha($Locale, 115, $w == 0);
740 # Does taking lc separately differ from taking
741 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
742 # The bug was in the caching of the 'o'-magic.
749 return $lc0 cmp $lc1;
753 return lc($_[0]) cmp lc($_[1]);
760 tryneoalpha($Locale, 116,
761 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
762 lcA($x, $z) == 0 && lcB($x, $z) == 0);
765 # Does lc of an UPPER (if different from the UPPER) match
766 # case-insensitively the UPPER, and does the UPPER match
767 # case-insensitively the lc of the UPPER. And vice versa.
771 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
774 foreach my $x (keys %UPPER) {
776 next unless uc $y eq $x;
777 print "# UPPER $x lc $y ",
778 $x =~ /$y/i ? 1 : 0, " ",
779 $y =~ /$x/i ? 1 : 0, "\n" if 0;
781 # If $x and $y contain regular expression characters
782 # AND THEY lowercase (/i) to regular expression characters,
783 # regcomp() will be mightily confused. No, the \Q doesn't
784 # help here (maybe regex engine internal lowercasing
785 # is done after the \Q?) An example of this happening is
786 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
787 # the chr(173) (the "[") is the lowercase of the chr(235).
789 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
790 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
791 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
792 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
793 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
794 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
796 # Similar things can happen even under (bastardised)
797 # non-EBCDIC locales: in many European countries before the
798 # advent of ISO 8859-x nationally customised versions of
799 # ISO 646 were devised, reusing certain punctuation
800 # characters for modified characters needed by the
801 # country/language. For example, the "|" might have
802 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
804 if ($x =~ $re || $y =~ $re) {
805 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
808 # With utf8 both will fail since the locale concept
809 # of upper/lower does not work well in Unicode.
810 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
813 foreach my $x (keys %lower) {
815 next unless lc $y eq $x;
816 print "# lower $x uc $y ",
817 $x =~ /$y/i ? 1 : 0, " ",
818 $y =~ /$x/i ? 1 : 0, "\n" if 0;
819 if ($x =~ $re || $y =~ $re) { # See above.
820 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
823 # With utf8 both will fail since the locale concept
824 # of upper/lower does not work well in Unicode.
825 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
827 tryneoalpha($Locale, 117, @f == 0);
829 print "# failed 117 locale '$Locale' characters @f\n"
834 my $last_locales = $have_setlocale ? &last_locales : $last_without_setlocale;
836 # Recount the errors.
838 foreach ($last_without_setlocale+1..$last_locales) {
839 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
841 print "# The failure of test 102 is not necessarily fatal.\n";
842 print "# It usually indicates a problem in the environment,\n";
843 print "# not in Perl itself.\n";
854 foreach (99..$last_locales) {
856 my @f = sort keys %{ $Problem{$_} };
857 my $f = join(" ", @f);
858 $f =~ s/(.{50,60}) /$1\n#\t/g;
861 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
863 "# on your system may have errors because the locale test $_\n",
864 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
868 # If your users are not using these locales you are safe for the moment,
869 # but please report this failure first to perlbug\@perl.com using the
870 # perlbug script (as described in the INSTALL file) so that the exact
871 # details of the failures can be sorted out first and then your operating
872 # system supplier can be alerted about these anomalies.
879 # Tell which locales were okay and which were not.
884 foreach my $l (@Locale) {
886 foreach my $t (102..$last_locales) {
887 $p++ if $Problem{$t}{$l};
889 push @s, $l if $p == 0;
890 push @F, $l unless $p == 0;
894 my $s = join(" ", @s);
895 $s =~ s/(.{50,60}) /$1\n#\t/g;
898 "# The following locales\n#\n",
900 "# tested okay.\n#\n",
902 warn "# None of your locales were fully okay.\n";
906 my $F = join(" ", @F);
907 $F =~ s/(.{50,60}) /$1\n#\t/g;
910 "# The following locales\n#\n",
912 "# had problems.\n#\n",
914 warn "# None of your locales were broken.\n";
918 sub last_locales { 117 }
920 $test_num = $last_locales;
922 # Test that tainting and case changing works on utf8 strings. These tests are
923 # placed last to avoid disturbing the hard-coded test numbers above this in
925 setlocale(LC_ALL, "C");
929 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
930 my @list; # List of code points to test for $function
932 # Used to calculate the changed case for ASCII characters by using the
933 # ord, instead of using one of the functions under test.
934 my $ascii_case_change_delta;
935 my $above_latin1_case_change_delta; # Same for the specific ords > 255
938 # We test an ASCII character, which should change case and be tainted;
939 # a Latin1 character, which shouldn't change case under this C locale,
941 # an above-Latin1 character that when the case is changed would cross
942 # the 255/256 boundary, so doesn't change case and isn't tainted
943 # (the \x{149} is one of these, but changes into 2 characters, the
944 # first one of which doesn't cross the boundary.
945 # the final one in each list is an above-Latin1 character whose case
946 # does change, and shouldn't be tainted. The code below uses its
947 # position in its list as a marker to indicate that it, unlike the
948 # other code points above ASCII, has a successful case change
949 if ($function =~ /^u/) {
950 #@list = ("\xff", "\x{fb00}", "\x{149}", "\x{101}");
951 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
952 $ascii_case_change_delta = -32;
953 $above_latin1_case_change_delta = -1;
956 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
957 $ascii_case_change_delta = +32;
958 $above_latin1_case_change_delta = +1;
961 foreach my $j (0 .. $#list) {
962 my $char = $list[$j];
963 #print STDERR __LINE__, ": $char\n";
964 #check_taint_not($i++, $char);
965 utf8::upgrade($char);
966 #check_taint_not($i++, $char);
967 my $should_be = ($j == $#list)
968 ? chr(ord($char) + $above_latin1_case_change_delta)
969 : (length $char == 0 || ord($char) > 127)
971 : chr(ord($char) + $ascii_case_change_delta);
973 # This monstrosity is in order to avoid using an eval, which might
974 # perturb the results
975 my $changed = ($function eq "uc")
977 : ($function eq "ucfirst")
979 : ($function eq "lc")
981 : ($function eq "lcfirst")
983 : croak("Unexpected function \"$function\"");
984 ok($changed eq $should_be, "$function(\"$char\") in C locale should be \"$should_be\", got \"$changed\"");
986 # Tainting shouldn't happen for empty strings, or those characters
988 #print STDERR __LINE__, ": $char\n";
989 (length($char) > 0 && ord($char) < 256)
990 ? check_taint($changed)
991 : check_taint_not($changed);
997 print "1..$test_num\n";