[perl #32039] Chained goto &sub drops data too early.
[p5sagit/p5-mst-13.2.git] / t / op / taint.t
index d010afe..2204632 100755 (executable)
@@ -14,9 +14,11 @@ BEGIN {
 
 use strict;
 use Config;
+use File::Spec::Functions;
 
+my $total_tests = 236;
 my $test = 177;
-sub ok {
+sub ok ($;$) {
     my($ok, $name) = @_;
 
     # You have to do it this way or VMS will get confused.
@@ -31,10 +33,6 @@ sub ok {
 
 $| = 1;
 
-# We do not want the whole taint.t to fail
-# just because Errno possibly failing.
-eval { require Errno; import Errno };
-
 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
 
 BEGIN {
@@ -47,11 +45,12 @@ BEGIN {
       eval { require IPC::SysV };
       unless ($@) {
          $ipcsysv++;
-         IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU));
+         IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
       }
   }
 }
 
+my $Is_MacOS = $^O eq 'MacOS';
 my $Is_VMS = $^O eq 'VMS';
 my $Is_MSWin32 = $^O eq 'MSWin32';
 my $Is_NetWare = $^O eq 'NetWare';
@@ -59,6 +58,7 @@ my $Is_Dos = $^O eq 'dos';
 my $Is_Cygwin = $^O eq 'cygwin';
 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
                   ($Is_MSWin32 ? '.\perl' :
+                  $Is_MacOS ? ':perl' :
                   ($Is_NetWare ? 'perl' : './perl'));
 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
 
@@ -116,14 +116,16 @@ sub test ($$;$) {
 }
 
 # We need an external program to call.
-my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
+my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
 END { unlink $ECHO }
 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
 print PROG 'print "@ARGV\n"', "\n";
 close PROG;
 my $echo = "$Invoke_Perl $ECHO";
 
-print "1..179\n";
+my $TEST = catfile(curdir(), 'TEST');
+
+print "1..$total_tests\n";
 
 # First, let's make sure that Perl is checking the dangerous
 # environment variables. Maybe they aren't set yet, so we'll
@@ -138,12 +140,22 @@ print "1..179\n";
     if ($Is_Cygwin && ! -f 'cygwin1.dll') {
        system("/usr/bin/cp /usr/bin/cygwin1.dll .") &&
            die "$0: failed to cp cygwin1.dll: $!\n";
-       END { unlink "cygwin1.dll" } # yes, done for all platforms...
+       eval q{
+           END { unlink "cygwin1.dll" }
+       };
+    }
+
+    if ($Is_Cygwin && ! -f 'cygcrypt-0.dll' && -f '/usr/bin/cygcrypt-0.dll') {
+       system("/usr/bin/cp /usr/bin/cygcrypt-0.dll .") &&
+           die "$0: failed to cp cygcrypt-0.dll: $!\n";
+       eval q{
+           END { unlink "cygcrypt-0.dll" }
+       };
     }
 
     test 1, eval { `$echo 1` } eq "1\n";
 
-    if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos) {
+    if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS) {
        print "# Environment tainting tests skipped\n";
        for (2..5) { print "ok $_\n" }
     }
@@ -259,8 +271,8 @@ print "1..179\n";
 
 # How about command-line arguments? The problem is that we don't
 # always get some, so we'll run another process with some.
-{
-    my $arg = "./arg$$";
+SKIP: {
+    my $arg = catfile(curdir(), "arg$$");
     open PROG, "> $arg" or die "Can't create $arg: $!";
     print PROG q{
        eval { join('', @ARGV), kill 0 };
@@ -276,8 +288,7 @@ print "1..179\n";
 
 # Reading from a file should be tainted
 {
-    my $file = './TEST';
-    test 32, open(FILE, $file), "Couldn't open '$file': $!";
+    test 32, open(FILE, $TEST), "Couldn't open '$TEST': $!";
 
     my $block;
     sysread(FILE, $block, 100);
@@ -409,7 +420,9 @@ else {
     test 72, $@ eq '', $@;             # NB: This should be allowed
 
     # Try first new style but allow also old style.
-    test 73, $!{ENOENT} ||
+    # We do not want the whole taint.t to fail
+    # just because Errno possibly failing.
+    test 73, eval('$!{ENOENT}') ||
        $! == 2 || # File not found
        ($Is_Dos && $! == 22) ||
        ($^O eq 'mint' && $! == 33);
@@ -450,7 +463,7 @@ else {
        test 87, $@ eq '', $@;
     }
     else {
-       for (86..87) { print "ok $_ # Skipped: this is not VMS\n"; }
+       for (86..87) { print "ok $_ # Skipped: This is not VMS\n"; }
     }
 }
 
@@ -577,7 +590,7 @@ else {
 # Test for system/library calls returning string data of dubious origin.
 {
     # No reliable %Config check for getpw*
-    if (eval { setpwent(); getpwent(); 1 }) {
+    if (eval { setpwent(); getpwent() }) {
        setpwent();
        my @getpwent = getpwent();
        die "getpwent: $!\n" unless (@getpwent);
@@ -608,7 +621,10 @@ else {
     if ($Config{d_readlink} && $Config{d_symlink}) {
        my $symlink = "sl$$";
        unlink($symlink);
-       symlink("/something/naughty", $symlink) or die "symlink: $!\n";
+       my $sl = "/something/naughty";
+       # it has to be a real path on Mac OS
+       $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
+       symlink($sl, $symlink) or die "symlink: $!\n";
        my $readlink = readlink($symlink);
        test 144, tainted $readlink;
        unlink($symlink);
@@ -695,14 +711,14 @@ else {
        my $type_rcvd;
 
        if (defined $id) {
-           if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) {
-               if (msgrcv($id, $rcvd, 60, 0, 0)) {
+           if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
+               if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
                    ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
                } else {
-                   warn "# msgrcv failed\n";
+                   warn "# msgrcv failed: $!\n";
                }
            } else {
-               warn "# msgsnd failed\n";
+               warn "# msgsnd failed: $!\n";
            }
            msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
        } else {
@@ -722,7 +738,7 @@ else {
 {
     # bug id 20001004.006
 
-    open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
+    open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
     local $/;
     my $a = <IN>;
     my $b = <IN>;
@@ -734,7 +750,7 @@ else {
 {
     # bug id 20001004.007
 
-    open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
+    open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
     my $a = <IN>;
 
     my $c = { a => 42,
@@ -839,7 +855,7 @@ else {
 
     use warnings;
 
-    $SIG{__WARN__} = sub { print "not " };
+    local $SIG{__WARN__} = sub { print "not " };
 
     sub fmi {
        my $divnum = shift()/1;
@@ -889,8 +905,8 @@ else {
     my @untainted;
     while (my ($k, $v) = each %ENV) {
        if (!tainted($v) &&
-           # These we have untainted explicitly earlier.
-           $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|TEMP|TERM|TMP)$/) {
+           # These we have explicitly untainted or set earlier.
+           $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
            push @untainted, "# '$k' = '$v'\n";
        }
     }
@@ -899,10 +915,170 @@ else {
 }
 
 
-ok( ${^TAINT},  '$^TAINT is on' );
+ok( ${^TAINT} == 1, '$^TAINT is on' );
 
 eval { ${^TAINT} = 0 };
 ok( ${^TAINT},  '$^TAINT is not assignable' );
 ok( $@ =~ /^Modification of a read-only value attempted/,
-                                'Assigning to taint pukes properly' );
+                                'Assigning to ${^TAINT} fails' );
+
+{
+    # bug 20011111.105
+    
+    my $re1 = qr/x$TAINT/;
+    test 180, tainted $re1;
+    
+    my $re2 = qr/^$re1\z/;
+    test 181, tainted $re2;
+    
+    my $re3 = "$re2";
+    test 182, tainted $re3;
+}
+
+if ($Is_MSWin32) {
+    print "ok 183 # Skipped: system {} has different semantics\n"; 
+}
+else
+{
+    # bug 20010221.005
+    local $ENV{PATH} .= $TAINT;
+    eval { system { "echo" } "/arg0", "arg1" };
+    test 183, $@ =~ /^Insecure \$ENV/;
+}
+if ($Is_VMS) {
+    for (184..205) {print "not ok $_ # TODO tainted %ENV warning occludes tainted arguments warning\n";}
+}
+else 
+{
+    # bug 20020208.005 plus some extras
+    # single arg exec/system are tests 80-83
+    my $err = qr/^Insecure dependency/ ;
+    test 184, eval { exec $TAINT, $TAINT } eq '', 'exec';
+    test 185, $@ =~ $err, $@;
+    test 186, eval { exec $TAINT $TAINT } eq '', 'exec';
+    test 187, $@ =~ $err, $@;
+    test 188, eval { exec $TAINT $TAINT, $TAINT } eq '', 'exec';
+    test 189, $@ =~ $err, $@;
+    test 190, eval { exec $TAINT 'notaint' } eq '', 'exec';
+    test 191, $@ =~ $err, $@;
+    test 192, eval { exec {'notaint'} $TAINT } eq '', 'exec';
+    test 193, $@ =~ $err, $@;
+
+    test 194, eval { system $TAINT, $TAINT } eq '', 'system';
+    test 195, $@ =~ $err, $@;
+    test 196, eval { system $TAINT $TAINT } eq '', 'system';
+    test 197, $@ =~ $err, $@;
+    test 198, eval { system $TAINT $TAINT, $TAINT } eq '', 'system';
+    test 199, $@ =~ $err, $@;
+    test 200, eval { system $TAINT 'notaint' } eq '', 'system';
+    test 201, $@ =~ $err, $@;
+    test 202, eval { system {'notaint'} $TAINT } eq '', 'system';
+    test 203, $@ =~ $err, $@;
+
+    eval { system("lskdfj does not exist","with","args"); };
+    test 204, $@ eq '';
+    if ($Is_MacOS) {
+       print "ok 205 # no exec()\n";
+    } else {
+       eval { exec("lskdfj does not exist","with","args"); };
+       test 205, $@ eq '';
+    }
+
+    # If you add tests here update also the above skip block for VMS.
+}
+
+{
+    # [ID 20020704.001] taint propagation failure
+    use re 'taint';
+    $TAINT =~ /(.*)/;
+    test 206, tainted(my $foo = $1);
+}
+
+{
+    # [perl #24291] this used to dump core
+    our %nonmagicalenv = ( PATH => "util" );
+    local *ENV = \%nonmagicalenv;
+    eval { system("lskdfj"); };
+    test 207, $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
+    local *ENV = *nonmagicalenv;
+    eval { system("lskdfj"); };
+    test 208, $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
+}
+{
+    # [perl #24248]
+    $TAINT =~ /(.*)/;
+    test 209, !tainted($1);
+    my $notaint = $1;
+    test 210, !tainted($notaint);
+
+    my $l;
+    $notaint =~ /($notaint)/;
+    $l = $1;
+    test 211, !tainted($1);
+    test 212, !tainted($l);
+    $notaint =~ /($TAINT)/;
+    $l = $1;
+    test 213, tainted($1);
+    test 214, tainted($l);
+
+    $TAINT =~ /($notaint)/;
+    $l = $1;
+    test 215, !tainted($1);
+    test 216, !tainted($l);
+    $TAINT =~ /($TAINT)/;
+    $l = $1;
+    test 217, tainted($1);
+    test 218, tainted($l);
+
+    my $r;
+    ($r = $TAINT) =~ /($notaint)/;
+    test 219, !tainted($1);
+    ($r = $TAINT) =~ /($TAINT)/;
+    test 220, tainted($1);
+
+    #  [perl #24674]
+    # accessing $^O  shoudn't taint it as a side-effect;
+    # assigning tainted data to it is now an error
+
+    test 221, !tainted($^O);
+    if (!$^X) { } elsif ($^O eq 'bar') { }
+    test 222, !tainted($^O);
+    eval '$^O = $^X';
+    test 223, $@ =~ /Insecure dependency in/;
+}
 
+EFFECTIVELY_CONSTANTS: {
+    my $tainted_number = 12 + $TAINT0;
+    test 224, tainted( $tainted_number );
+
+    # Even though it's always 0, it's still tainted
+    my $tainted_product = $tainted_number * 0;
+    test 225, tainted( $tainted_product );
+    test 226, $tainted_product == 0;
+}
+
+TERNARY_CONDITIONALS: {
+    my $tainted_true  = $TAINT . "blah blah blah";
+    my $tainted_false = $TAINT0;
+    test 227, tainted( $tainted_true );
+    test 228, tainted( $tainted_false );
+
+    my $result = $tainted_true ? "True" : "False";
+    test 229, $result eq "True";
+    test 230, !tainted( $result );
+
+    $result = $tainted_false ? "True" : "False";
+    test 231, $result eq "False";
+    test 232, !tainted( $result );
+
+    my $untainted_whatever = "The Fabulous Johnny Cash";
+    my $tainted_whatever = "Soft Cell" . $TAINT;
+
+    $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
+    test 233, $result eq "Soft Cell";
+    test 234, tainted( $result );
+
+    $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
+    test 235, $result eq "The Fabulous Johnny Cash";
+    test 236, !tainted( $result );
+}