6 require Config; import Config;
7 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
15 my $have_setlocale = 0;
18 import POSIX ':locale_h';
22 print "1..", ($have_setlocale ? 102 : 98), "\n";
25 $English $German $French $Spanish
26 @C @English @German @French @Spanish
27 $Locale @Locale %iLocale %UPPER %lower @Neoalpha);
32 my ($n, $result) = @_;
34 print 'not ' unless ($result);
38 # First we'll do a lot of taint checking for locales.
39 # This is the easiest to test, actually, as any locale,
40 # even the default locale will taint under 'use locale'.
42 sub is_tainted { # hello, camel two.
43 local $^W; # no warnings 'undef'
45 not eval { $dummy = join("", @_), kill 0; 1 }
48 sub check_taint ($$) {
49 ok $_[0], is_tainted($_[1]);
52 sub check_taint_not ($$) {
53 ok $_[0], not is_tainted($_[1]);
56 use locale; # engage locale and therefore locale taint.
58 check_taint_not 1, $a;
60 check_taint 2, uc($a);
61 check_taint 3, "\U$a";
62 check_taint 4, ucfirst($a);
63 check_taint 5, "\u$a";
64 check_taint 6, lc($a);
65 check_taint 7, "\L$a";
66 check_taint 8, lcfirst($a);
67 check_taint 9, "\l$a";
69 check_taint 10, sprintf('%e', 123.456);
70 check_taint 11, sprintf('%f', 123.456);
71 check_taint 12, sprintf('%g', 123.456);
72 check_taint_not 13, sprintf('%d', 123.456);
73 check_taint_not 14, sprintf('%x', 123.456);
77 $_ = uc($a); # taint $_
81 /(\w)/; # taint $&, $`, $', $+, $1.
87 check_taint_not 21, $2;
89 /(.)/; # untaint $&, $`, $', $+, $1.
90 check_taint_not 22, $&;
91 check_taint_not 23, $`;
92 check_taint_not 24, $';
93 check_taint_not 25, $+;
94 check_taint_not 26, $1;
95 check_taint_not 27, $2;
97 /(\W)/; # taint $&, $`, $', $+, $1.
103 check_taint_not 33, $2;
105 /(\s)/; # taint $&, $`, $', $+, $1.
111 check_taint_not 39, $2;
113 /(\S)/; # taint $&, $`, $', $+, $1.
119 check_taint_not 45, $2;
121 $_ = $a; # untaint $_
123 check_taint_not 46, $_;
125 /(b)/; # this must not taint
126 check_taint_not 47, $&;
127 check_taint_not 48, $`;
128 check_taint_not 49, $';
129 check_taint_not 50, $+;
130 check_taint_not 51, $1;
131 check_taint_not 52, $2;
133 $_ = $a; # untaint $_
135 check_taint_not 53, $_;
137 $b = uc($a); # taint $b
138 s/(.+)/$b/; # this must taint only the $_
141 check_taint_not 55, $&;
142 check_taint_not 56, $`;
143 check_taint_not 57, $';
144 check_taint_not 58, $+;
145 check_taint_not 59, $1;
146 check_taint_not 60, $2;
148 $_ = $a; # untaint $_
150 s/(.+)/b/; # this must not taint
151 check_taint_not 61, $_;
152 check_taint_not 62, $&;
153 check_taint_not 63, $`;
154 check_taint_not 64, $';
155 check_taint_not 65, $+;
156 check_taint_not 66, $1;
157 check_taint_not 67, $2;
159 $b = $a; # untaint $b
161 ($b = $a) =~ s/\w/$&/;
162 check_taint 68, $b; # $b should be tainted.
163 check_taint_not 69, $a; # $a should be not.
165 $_ = $a; # untaint $_
167 s/(\w)/\l$1/; # this must taint
174 check_taint_not 76, $2;
176 $_ = $a; # untaint $_
178 s/(\w)/\L$1/; # this must taint
185 check_taint_not 83, $2;
187 $_ = $a; # untaint $_
189 s/(\w)/\u$1/; # this must taint
196 check_taint_not 90, $2;
198 $_ = $a; # untaint $_
200 s/(\w)/\U$1/; # this must taint
207 check_taint_not 97, $2;
209 # After all this tainting $a should be cool.
211 check_taint_not 98, $a;
213 # I think we've seen quite enough of taint.
214 # Let us do some *real* locale work now,
215 # unless setlocale() is missing (i.e. minitest).
217 exit unless $have_setlocale;
220 sort grep /\w/, map { chr } 0..255
223 sub locatelocale ($$@) {
224 my ($lcall, $alnum, @try) = @_;
229 local $^W = 0; # suppress "Subroutine LC_ALL redefined"
230 if (setlocale(&LC_ALL, $_)) {
237 @$alnum = () unless (defined $$lcall);
240 # Find some default locale
242 locatelocale(\$Locale, \@Locale, qw(C POSIX));
244 # Find some English locale
246 locatelocale(\$English, \@English,
247 qw(en_US.ISO8859-1 en_GB.ISO8859-1
248 en en_US en_UK en_IE en_CA en_AU en_NZ
249 english english.iso88591
250 american american.iso88591
251 british british.iso88591
254 # Find some German locale
256 locatelocale(\$German, \@German,
257 qw(de_DE.ISO8859-1 de_AT.ISO8859-1 de_CH.ISO8859-1
259 german german.iso88591));
261 # Find some French locale
263 locatelocale(\$French, \@French,
264 qw(fr_FR.ISO8859-1 fr_BE.ISO8859-1 fr_CA.ISO8859-1 fr_CH.ISO8859-1
265 fr fr_FR fr_BE fr_CA fr_CH
266 french french.iso88591));
268 # Find some Spanish locale
270 locatelocale(\$Spanish, \@Spanish,
271 qw(es_AR.ISO8859-1 es_BO.ISO8859-1 es_CL.ISO8859-1
272 es_CO.ISO8859-1 es_CR.ISO8859-1 es_EC.ISO8859-1
273 es_ES.ISO8859-1 es_GT.ISO8859-1 es_MX.ISO8859-1
274 es_NI.ISO8859-1 es_PA.ISO8859-1 es_PE.ISO8859-1
275 es_PY.ISO8859-1 es_SV.ISO8859-1 es_UY.ISO8859-1 es_VE.ISO8859-1
280 es_PY es_SV es_UY es_VE
281 spanish spanish.iso88591));
283 # Select the largest of the alpha(num)bets.
285 ($Locale, @Locale) = ($English, @English)
286 if (length(@English) > length(@Locale));
287 ($Locale, @Locale) = ($German, @German)
288 if (length(@German) > length(@Locale));
289 ($Locale, @Locale) = ($French, @French)
290 if (length(@French) > length(@Locale));
291 ($Locale, @Locale) = ($Spanish, @Spanish)
292 if (length(@Spanish) > length(@Locale));
294 print "# Locale = $Locale\n";
295 print "# Alnum_ = @Locale\n";
299 setlocale(&LC_ALL, $Locale);
310 # Sieve the uppercase and the lowercase.
313 if (/[^\d_]/) { # skip digits and the _
322 # Find the alphabets that are not alphabets in the default locale.
327 for (keys %UPPER, keys %lower) {
328 push(@Neoalpha, $_) if (/\W/);
332 @Neoalpha = sort @Neoalpha;
337 my $word = join('', @Neoalpha);
341 print 'not ' if ($1 ne $word);
345 # Find places where the collation order differs from the default locale.
347 print "# testing 100\n";
354 @k = sort (keys %UPPER, keys %lower);
357 for ($i = 0; $i < @k; $i++) {
358 for ($j = $i + 1; $j < @k; $j++) {
359 if ($iLocale{$k[$j]} < $iLocale{$k[$i]}) {
360 push(@d, [$k[$j], $k[$i]]);
365 # Cross-check those places.
370 print "# failed 100 at:\n";
371 print "# i = $i, j = $j, i ",
372 $i le $j ? 'le' : 'gt', " j\n";
380 # Cross-check whole character set.
382 print "# testing 101\n";
383 for (map { chr } 0..255) {
384 if (/\w/ and /\W/) { print 'not '; last }
385 if (/\d/ and /\D/) { print 'not '; last }
386 if (/\s/ and /\S/) { print 'not '; last }
387 if (/\w/ and /\D/ and not /_/ and
388 not (exists $UPPER{$_} or exists $lower{$_})) {
389 print "# failed 101 at:\n";
390 print "# ", ord($_), " '$_'\n";
397 # The @Locale should be internally consistent.
399 print "# testing 102\n";
401 my ($from, $to, $lesser, $greater, @test, %test, $test);
405 $from = int(($_*@Locale)/10);
406 $to = $from + int(@Locale/10);
407 $to = $#Locale if ($to > $#Locale);
408 $lesser = join('', @Locale[$from..$to]);
409 # Select a slice one character on.
411 $to = $#Locale if ($to > $#Locale);
412 $greater = join('', @Locale[$from..$to]);
415 'not ($lesser lt $greater)', # 0
416 'not ($lesser le $greater)', # 1
417 'not ($lesser ne $greater)', # 2
418 ' ($lesser eq $greater)', # 3
419 ' ($lesser ge $greater)', # 4
420 ' ($lesser gt $greater)', # 5
421 ' ($greater lt $lesser )', # 6
422 ' ($greater le $lesser )', # 7
423 'not ($greater ne $lesser )', # 8
424 ' ($greater eq $lesser )', # 9
425 'not ($greater ge $lesser )', # 10
426 'not ($greater gt $lesser )', # 11
427 # Well, these two are sort of redundant
428 # because @Locale was derived using cmp.
429 'not (($lesser cmp $greater) == -1)', # 12
430 'not (($greater cmp $lesser ) == 1)' # 13
432 @test{@test} = 0 x @test;
434 for my $ti (@test) { $test{$ti} = eval $ti ; $test ||= $test{$ti} }
436 print "# failed 102 at:\n";
437 print "# lesser = '$lesser'\n";
438 print "# greater = '$greater'\n";
439 print "# (greater) from = $from, to = $to\n";
441 printf("# %-40s %-4s", $ti,
442 $test{$ti} ? 'FAIL' : 'ok');
443 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
444 printf("(%s == %4d)", $1, eval $1);