xref: /openbsd/gnu/usr.bin/perl/t/run/switcht.t (revision eac174f2)
155745691Smillert#!./perl -t
255745691Smillert
355745691SmillertBEGIN {
4b8851fccSafresh1    chdir 't' if -d 't';
555745691Smillert    @INC = '../lib';
655745691Smillert    require './test.pl';
755745691Smillert}
855745691Smillert
9*eac174f2Safresh1use Config;
10*eac174f2Safresh1
11*eac174f2Safresh1if (exists($Config{taint_support}) && !$Config{taint_support}) {
12*eac174f2Safresh1    skip_all("perl built without taint support");
13*eac174f2Safresh1}
14*eac174f2Safresh1
1543003dfeSmillertplan tests => 13;
1655745691Smillert
1755745691Smillertmy $Perl = which_perl();
1855745691Smillert
1955745691Smillertmy $warning;
2055745691Smillertlocal $SIG{__WARN__} = sub { $warning = join "\n", @_; };
2155745691Smillertmy $Tmsg = 'while running with -t switch';
2255745691Smillert
2385009909Smillertis( ${^TAINT}, -1, '${^TAINT} == -1' );
2455745691Smillert
2555745691Smillertmy $out = `$Perl -le "print q(Hello)"`;
2655745691Smillertis( $out, "Hello\n",                      '`` worked' );
2755745691Smillertlike( $warning, qr/^Insecure .* $Tmsg/, '    taint warn' );
2855745691Smillert
2955745691Smillert{
3055745691Smillert    no warnings 'taint';
3155745691Smillert    $warning = '';
3255745691Smillert    my $out = `$Perl -le "print q(Hello)"`;
3355745691Smillert    is( $out, "Hello\n",                      '`` worked' );
3455745691Smillert    is( $warning, '',                       '   no warnings "taint"' );
3555745691Smillert}
3655745691Smillert
3755745691Smillert# Get ourselves a tainted variable.
3843003dfeSmillertmy $filename = tempfile();
3955745691Smillert$file = $0;
4043003dfeSmillert$file =~ s/.*/$filename/;
4155745691Smillertok( open(FILE, ">$file"),   'open >' ) or DIE $!;
4255745691Smillertprint FILE "Stuff\n";
4355745691Smillertclose FILE;
4455745691Smillertlike( $warning, qr/^Insecure dependency in open $Tmsg/, 'open > taint warn' );
4555745691Smillertok( -e $file,   '   file written' );
4655745691Smillert
4755745691Smillertunlink($file);
4855745691Smillertlike( $warning, qr/^Insecure dependency in unlink $Tmsg/,
4955745691Smillert                                                  'unlink() taint warn' );
5055745691Smillertok( !-e $file,  'unlink worked' );
5155745691Smillert
5255745691Smillertok( !$^W,   "-t doesn't enable regular warnings" );
5343003dfeSmillert
5443003dfeSmillert
5543003dfeSmillertmkdir('ttdir');
5643003dfeSmillertopen(FH,'>','ttdir/ttest.pl')or DIE $!;
5743003dfeSmillertprint FH 'return 42';
5843003dfeSmillertclose FH or DIE $!;
5943003dfeSmillert
6043003dfeSmillertSKIP: {
6143003dfeSmillert    ($^O eq 'MSWin32') || skip('skip tainted do test with \ separator');
6243003dfeSmillert    my $test = 0;
6343003dfeSmillert    $test =  do '.\ttdir/ttest.pl';
6443003dfeSmillert    is($test, 42, 'Could "do" .\ttdir/ttest.pl');
6543003dfeSmillert}
6643003dfeSmillert{
6743003dfeSmillert    my $test = 0;
6843003dfeSmillert    $test =  do './ttdir/ttest.pl';
6943003dfeSmillert    is($test, 42, 'Could "do" ./ttdir/ttest.pl');
7043003dfeSmillert}
7143003dfeSmillertunlink ('./ttdir/ttest.pl');
7243003dfeSmillertrmdir ('ttdir');
73