3 # Tests for Perl run-time environment variable settings
5 # $PERL5OPT, $PERL5LIB, etc.
10 require Config; import Config;
12 skip_all_without_config('d_fork');
17 my $STDOUT = tempfile();
18 my $STDERR = tempfile();
20 my $FAILURE_CODE = 119;
23 delete $ENV{PERL5LIB};
24 delete $ENV{PERL5OPT};
25 delete $ENV{PERL_USE_UNSAFE_INC};
28 # Run perl with specified environment and arguments, return (STDOUT, STDERR)
29 sub runperl_and_capture {
31 my ($env, $args) = @_;
35 delete $ENV{PERL5LIB};
36 delete $ENV{PERL5OPT};
37 delete $ENV{PERL_USE_UNSAFE_INC};
39 return (0, "Couldn't fork: $!") unless defined $pid; # failure
42 return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE;
44 open my $stdout, '<', $STDOUT
45 or return (0, "Couldn't read $STDOUT file: $!");
46 open my $stderr, '<', $STDERR
47 or return (0, "Couldn't read $STDERR file: $!");
49 # Empty file with <$stderr> returns nothing in list context
50 # (because there are no lines) Use scalar to force it to ''
51 return (scalar <$stdout>, scalar <$stderr>);
53 for my $k (keys %$env) {
54 $ENV{$k} = $env->{$k};
56 open STDOUT, '>', $STDOUT or exit $FAILURE_CODE;
57 open STDERR, '>', $STDERR and do { exec $PERL, @$args };
59 print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n";
65 my ($env, $args, $stdout, $stderr) = @_;
66 my ($actual_stdout, $actual_stderr) = runperl_and_capture($env, $args);
67 local $::Level = $::Level + 1;
69 for my $k (sort keys %$env) {
70 push @envpairs, "$k => $env->{$k}";
72 my $label = join(',' => (@envpairs, @$args));
74 ok ( $actual_stdout =~/$stdout/, $label . ' stdout' );
76 is ( $actual_stdout, $stdout, $label . ' stdout' );
79 ok ( $actual_stderr =~/$stderr/, $label . ' stderr' );
81 is ( $actual_stderr, $stderr, $label . ' stderr' );
85 # PERL5OPT Command-line options (switches). Switches in
86 # this variable are taken as if they were on
87 # every Perl command line. Only the -[DIMUdmtw]
88 # switches are allowed. When running taint
89 # checks (because the program was running setuid
90 # or setgid, or the -T switch was used), this
91 # variable is ignored. If PERL5OPT begins with
92 # -T, tainting will be enabled, and any
93 # subsequent options ignored.
95 try({PERL5OPT => '-w'}, ['-e', 'print $::x'],
97 qq{Name "main::x" used only once: possible typo at -e line 1.\nUse of uninitialized value \$x in print at -e line 1.\n});
99 try({PERL5OPT => '-Mstrict'}, ['-I../lib', '-e', 'print $::x'],
102 try({PERL5OPT => '-Mstrict'}, ['-I../lib', '-e', 'print $x'],
104 qq{Global symbol "\$x" requires explicit package name (did you forget to declare "my \$x"?) at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
107 try({PERL5OPT => '-Mstrict -w'}, ['-I../lib', '-e', 'print $x'],
109 qq{Global symbol "\$x" requires explicit package name (did you forget to declare "my \$x"?) at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
112 try({PERL5OPT => '-w -Mstrict'}, ['-I../lib', '-e', 'print $::x'],
115 Name "main::x" used only once: possible typo at -e line 1.
116 Use of uninitialized value \$x in print at -e line 1.
121 try({PERL5OPT => '-w -Mstrict'}, ['-I../lib', '-e', 'print $::x'],
124 Name "main::x" used only once: possible typo at -e line 1.
125 Use of uninitialized value \$x in print at -e line 1.
129 try({PERL5OPT => '-MExporter'}, ['-I../lib', '-e0'],
134 try({PERL5OPT => '-MExporter -MExporter'}, ['-I../lib', '-e0'],
138 try({PERL5OPT => '-Mstrict -Mwarnings'},
139 ['-I../lib', '-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'],
143 open my $fh, ">", "tmpOooof.pm" or die "Can't write tmpOooof.pm: $!";
144 print $fh "package tmpOooof; 1;\n";
146 END { 1 while unlink "tmpOooof.pm" }
148 try({PERL5OPT => '-I. -MtmpOooof'},
149 ['-e', 'print "ok" if $INC{"tmpOooof.pm"} eq "tmpOooof.pm"'],
153 try({PERL5OPT => '-I./ -MtmpOooof'},
154 ['-e', 'print "ok" if $INC{"tmpOooof.pm"} eq "tmpOooof.pm"'],
158 try({PERL5OPT => '-w -w'},
159 ['-e', 'print $ENV{PERL5OPT}'],
163 try({PERL5OPT => '-t'},
164 ['-e', 'print ${^TAINT}'],
168 try({PERL5OPT => '-W'},
169 ['-I../lib','-e', 'local $^W = 0; no warnings; print $x'],
172 Name "main::x" used only once: possible typo at -e line 1.
173 Use of uninitialized value \$x in print at -e line 1.
177 try({PERLLIB => "foobar$Config{path_sep}42"},
178 ['-e', 'print grep { $_ eq "foobar" } @INC'],
182 try({PERLLIB => "foobar$Config{path_sep}42"},
183 ['-e', 'print grep { $_ eq "42" } @INC'],
187 try({PERL5LIB => "foobar$Config{path_sep}42"},
188 ['-e', 'print grep { $_ eq "foobar" } @INC'],
192 try({PERL5LIB => "foobar$Config{path_sep}42"},
193 ['-e', 'print grep { $_ eq "42" } @INC'],
197 try({PERL5LIB => "foo",
199 ['-e', 'print grep { $_ eq "foo" } @INC'],
203 try({PERL5LIB => "foo",
205 ['-e', 'print grep { $_ eq "bar" } @INC'],
211 skip "NO_PERL_HASH_SEED_DEBUG set", 4
212 if $Config{ccflags} =~ /-DNO_PERL_HASH_SEED_DEBUG\b/;
214 try({PERL_HASH_SEED_DEBUG => 1},
217 qr/HASH_FUNCTION =/);
219 try({PERL_HASH_SEED_DEBUG => 1},
227 skip "NO_PERL_HASH_ENV or NO_PERL_HASH_SEED_DEBUG set", 16
228 if $Config{ccflags} =~ /-DNO_PERL_HASH_ENV\b/ ||
229 $Config{ccflags} =~ /-DNO_PERL_HASH_SEED_DEBUG\b/;
231 # special case, seed "0" implies disabled hash key traversal randomization
232 try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "0"},
235 qr/PERTURB_KEYS = 0/);
237 # check that setting it to a different value with the same logical value
238 # triggers the normal "deterministic mode".
239 try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "0x0"},
242 qr/PERTURB_KEYS = 2/);
244 try({PERL_HASH_SEED_DEBUG => 1, PERL_PERTURB_KEYS => "0"},
247 qr/PERTURB_KEYS = 0/);
249 try({PERL_HASH_SEED_DEBUG => 1, PERL_PERTURB_KEYS => "1"},
252 qr/PERTURB_KEYS = 1/);
254 try({PERL_HASH_SEED_DEBUG => 1, PERL_PERTURB_KEYS => "2"},
257 qr/PERTURB_KEYS = 2/);
259 try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "12345678"},
262 qr/HASH_SEED = 0x12345678/);
264 try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "12"},
267 qr/HASH_SEED = 0x12000000/);
269 try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "123456789"},
272 qr/HASH_SEED = 0x12345678/);
274 # Test that PERL_PERTURB_KEYS works as expected. We check that we get the same
275 # results if we use PERL_PERTURB_KEYS = 0 or 2 and we reuse the seed from previous run.
276 my @print_keys = ( '-e', '@_{"A".."Z"}=(); print keys %_');
277 for my $mode ( 0,1, 2 ) { # disabled and deterministic respectively
278 my %base_opts = ( PERL_PERTURB_KEYS => $mode, PERL_HASH_SEED_DEBUG => 1 ),
279 my ($out, $err) = runperl_and_capture( { %base_opts }, [ @print_keys ]);
280 if ($err=~/HASH_SEED = (0x[a-f0-9]+)/) {
282 my($out2, $err2) = runperl_and_capture( { %base_opts, PERL_HASH_SEED => $seed }, [ @print_keys ]);
284 isnt ($out,$out2,"PERL_PERTURB_KEYS = $mode results in different key order with the same key");
286 is ($out,$out2,"PERL_PERTURB_KEYS = $mode allows one to recreate a random hash");
288 is ($err,$err2,"Got the same debug output when we set PERL_HASH_SEED and PERL_PERTURB_KEYS");
293 # Tests for S_incpush_use_sep():
295 my @dump_inc = ('-e', 'print "$_\n" foreach @INC');
297 my ($out, $err) = runperl_and_capture({}, [@dump_inc]);
299 is ($err, '', 'No errors when determining @INC');
301 my @default_inc = split /\n/, $out;
304 skip_if_miniperl("under miniperl", 3);
305 if ($Config{default_inc_excludes_dot}) {
306 ok !(grep { $_ eq '.' } @default_inc), '. is not in @INC';
307 ($out, $err) = runperl_and_capture({ PERL_USE_UNSAFE_INC => 1 }, [@dump_inc]);
309 is ($err, '', 'No errors when determining unsafe @INC');
311 my @unsafe_inc = split /\n/, $out;
313 ok (eq_array([@unsafe_inc], [@default_inc, '.']), '. last in unsafe @INC')
314 or diag 'Unsafe @INC is: ', @unsafe_inc;
317 is ($default_inc[-1], '.', '. is last in @INC');
318 skip('Not testing unsafe @INC when it includes . by default', 2);
322 my $sep = $Config{path_sep};
323 foreach (['nothing', ''],
324 ['something', 'zwapp', 'zwapp'],
325 ['two things', "zwapp${sep}bam", 'zwapp', 'bam'],
326 ['two things, ::', "zwapp${sep}${sep}bam", 'zwapp', 'bam'],
327 [': at start', "${sep}zwapp", 'zwapp'],
328 [': at end', "zwapp${sep}", 'zwapp'],
329 [':: sandwich ::', "${sep}${sep}zwapp${sep}${sep}", 'zwapp'],
331 ['::', "${sep}${sep}"],
332 [':::', "${sep}${sep}${sep}"],
333 ['two things and :', "zwapp${sep}bam${sep}", 'zwapp', 'bam'],
334 [': and two things', "${sep}zwapp${sep}bam", 'zwapp', 'bam'],
335 [': two things :', "${sep}zwapp${sep}bam${sep}", 'zwapp', 'bam'],
336 ['three things', "zwapp${sep}bam${sep}${sep}owww",
337 'zwapp', 'bam', 'owww'],
339 my ($name, $lib, @expect) = @$_;
340 push @expect, @default_inc;
342 ($out, $err) = runperl_and_capture({PERL5LIB => $lib}, [@dump_inc]);
344 is ($err, '', "No errors when determining \@INC for $name");
346 my @inc = split /\n/, $out;
348 is (scalar @inc, scalar @expect,
349 "expected number of elements in \@INC for $name");
351 is ("@inc", "@expect", "expected elements in \@INC for $name");
354 # PERL5LIB tests with included arch directories still missing