SYN SYN
[p5sagit/p5-mst-13.2.git] / t / lib / ftmp-security.t
CommitLineData
4b19af01 1#!/usr/bin/perl -w
ee8c7f54 2# Test for File::Temp - Security levels
3
4# Some of the security checking will not work on all platforms
5# Test a simple open in the cwd and tmpdir foreach of the
6# security levels
7
4b19af01 8BEGIN {
9 chdir 't' if -d 't';
22d4bb9c 10 @INC = '../lib';
4b19af01 11 require Test; import Test;
12 plan(tests => 13);
13}
ee8c7f54 14
4b19af01 15use strict;
ee8c7f54 16use File::Spec;
4b19af01 17
18# Set up END block - this needs to happen before we load
19# File::Temp since this END block must be evaluated after the
20# END block configured by File::Temp
21my @files; # list of files to remove
22END { foreach (@files) { ok( !(-e $_) )} }
23
ee8c7f54 24use File::Temp qw/ tempfile unlink0 /;
25ok(1);
26
22d4bb9c 27# The high security tests must currently be skipped on some platforms
28my $skipplat = ( (
29 # No sticky bits.
30 $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'dos'
31 ) ? 1 : 0 );
ee8c7f54 32
33# Can not run high security tests in perls before 5.6.0
34my $skipperl = ($] < 5.006 ? 1 : 0 );
35
36# Determine whether we need to skip things and why
37my $skip = 0;
38if ($skipplat) {
39 $skip = "Skip Not supported on this platform";
40} elsif ($skipperl) {
41 $skip = "Skip Perl version must be v5.6.0 for these tests";
42
43}
44
45print "# We will be skipping some tests : $skip\n" if $skip;
46
47# start off with basic checking
48
49File::Temp->safe_level( File::Temp::STANDARD );
50
51print "# Testing with STANDARD security...\n";
52
53&test_security(0);
54
55# Try medium
56
57File::Temp->safe_level( File::Temp::MEDIUM )
58 unless $skip;
59
60print "# Testing with MEDIUM security...\n";
61
62# Now we need to start skipping tests
63&test_security($skip);
64
65# Try HIGH
66
67File::Temp->safe_level( File::Temp::HIGH )
68 unless $skip;
69
70print "# Testing with HIGH security...\n";
71
72&test_security($skip);
73
74exit;
75
76# Subroutine to open two temporary files.
77# one is opened in the current dir and the other in the temp dir
78
79sub test_security {
80
81 # Read in the skip flag
82 my $skip = shift;
83
84 # If we are skipping we need to simply fake the correct number
85 # of tests -- we dont use skip since the tempfile() commands will
86 # fail with MEDIUM/HIGH security before the skip() command would be run
87 if ($skip) {
4b19af01 88
ee8c7f54 89 skip($skip,1);
90 skip($skip,1);
4b19af01 91
ee8c7f54 92 # plus we need an end block so the tests come out in the right order
93 eval q{ END { skip($skip,1); skip($skip,1) } 1; } || die;
4b19af01 94
ee8c7f54 95 return;
96 }
97
4b19af01 98 # Create the tempfile
22d4bb9c 99 my $template = "tmpXXXXX";
100 my ($fh1, $fname1) = eval { tempfile ( $template,
101 DIR => File::Spec->tmpdir,
ee8c7f54 102 UNLINK => 1,
103 );
22d4bb9c 104 };
105
106 if (defined $fname1) {
107 print "# fname1 = $fname1\n";
108 ok( (-e $fname1) );
109 push(@files, $fname1); # store for end block
110 } elsif (File::Temp->safe_level() != File::Temp::STANDARD) {
111 my $skip2 = "Skip system possibly insecure, see INSTALL, section 'make test'";
112 skip($skip2, 1);
113 # plus we need an end block so the tests come out in the right order
114 eval q{ END { skip($skip2,1); } 1; } || die;
115 } else {
116 ok(0);
117 }
ee8c7f54 118
119 # Explicitly
22d4bb9c 120 if ( $< < File::Temp->top_system_uid() ){
121 skip("Skip Test inappropriate for root", 1);
122 eval q{ END { skip($skip,1); } 1; } || die;
123 return;
124 }
125 my ($fh2, $fname2) = eval { tempfile ($template, UNLINK => 1 ); };
126 if (defined $fname2) {
127 print "# fname2 = $fname2\n";
128 ok( (-e $fname2) );
129 push(@files, $fname2); # store for end block
130 close($fh2);
131 } elsif (File::Temp->safe_level() != File::Temp::STANDARD) {
132 my $skip2 = "Skip system possibly insecure, see INSTALL, section 'make test'";
133 skip($skip2, 1);
134 # plus we need an end block so the tests come out in the right order
135 eval q{ END { skip($skip2,1); } 1; } || die;
136 } else {
137 ok(0);
138 }
ee8c7f54 139
140}