This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate from macperl:
[perl5.git] / ext / POSIX / t / taint.t
CommitLineData
0e247040
PM
1#!./perl -Tw
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require Config; import Config;
7 if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
8 print "1..0\n";
9 exit 0;
10 }
11}
12
64087619 13use Test::More tests => 7;
0e247040 14use Scalar::Util qw/tainted/;
0e247040
PM
15
16
17use POSIX qw(fcntl_h open read mkfifo);
18use strict ;
19
20$| = 1;
21
22my $buffer;
23my @buffer;
24my $testfd;
25
26# Sources of taint:
27# The empty tainted value, for tainting strings
28
29my $TAINT = substr($^X, 0, 0);
30
943b127a
JH
31# there is a bug in GUSI that causes problems trying to open
32# files and directories ... it is being fixed, this is just
33# a stopgap -- pudge
34my $file = $^O eq 'MacOS' ? 'TEST-OLD' : 'TEST';
35
36eval { mkfifo($TAINT. $file, 0) };
64087619 37like($@, qr/^Insecure dependency/, 'mkfifo with tainted data');
0e247040 38
943b127a 39eval { $testfd = open($TAINT. $file, O_WRONLY, 0) };
64087619 40like($@, qr/^Insecure dependency/, 'open with tainted data');
0e247040 41
943b127a 42eval { $testfd = open($file, O_RDONLY, 0) };
64087619 43is($@, "", 'open with untainted data');
0e247040
PM
44
45read($testfd, $buffer, 2) if $testfd > 2;
46is( $buffer, "#!", ' read' );
47ok(tainted($buffer), ' scalar tainted');
0e247040 48
64087619
MS
49TODO: {
50 local $TODO = "POSIX::read won't taint an array element";
51
52 read($testfd, $buffer[1], 2) if $testfd > 2;
53
54 is( $buffer[1], "./", ' read' );
55 ok(tainted($buffer[1]), ' array element tainted');
56}