This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix PathTools taint handling for Perl 5.6
[perl5.git] / dist / PathTools / t / taint.t
CommitLineData
ea715489
JH
1#!./perl -Tw
2# Testing Cwd under taint mode.
3
99f36a73
RGS
4use strict;
5
39f7733f 6use Cwd;
2adbc9b6 7chdir 't' unless $ENV{PERL_CORE};
ea715489 8
99f36a73
RGS
9use File::Spec;
10use lib File::Spec->catdir('t', 'lib');
284167a5
SM
11use Test::More;
12BEGIN {
13 plan(
2ba334c7 14 !eval { eval("1".substr($^X,0,0)) }
0b6f9303 15 ? (tests => 21)
284167a5
SM
16 : (skip_all => "A perl without taint support")
17 );
18}
99f36a73 19
248785eb 20use Scalar::Util qw/tainted/;
ea715489 21
3ee63918
MS
22my @Functions = qw(getcwd cwd fastcwd fastgetcwd
23 abs_path fast_abs_path
24 realpath fast_realpath
25 );
248785eb 26
3ee63918
MS
27foreach my $func (@Functions) {
28 no strict 'refs';
29 my $cwd;
30 eval { $cwd = &{'Cwd::'.$func} };
a9939470
NC
31 is( $@, '', "$func() should not explode under taint mode" );
32 ok( tainted($cwd), "its return value should be tainted" );
3ee63918 33}
60598624
RGS
34
35# Previous versions of Cwd tainted $^O
36is !tainted($^O), 1, "\$^O should not be tainted";
0b6f9303
TC
37
38{
39 # [perl #126862] canonpath() loses taint
40 my $tainted = substr($ENV{PATH}, 0, 0);
41 # yes, getcwd()'s result should be tainted, and is tested above
42 # but be sure
43 ok tainted(File::Spec->canonpath($tainted . Cwd::getcwd)),
44 "canonpath() keeps taint on non-empty string";
45 ok tainted(File::Spec->canonpath($tainted)),
46 "canonpath() keeps taint on empty string";
47
48 (Cwd::getcwd() =~ /^(.*)/);
49 my $untainted = $1;
50 ok !tainted($untainted), "make sure our untainted value is untainted";
51 ok !tainted(File::Spec->canonpath($untainted)),
52 "canonpath() doesn't add taint to untainted string";
53}