2 # Test for File::Temp - Security levels
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
11 require Test; import Test;
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
21 my @files; # list of files to remove
22 END { foreach (@files) { ok( !(-e $_) )} }
24 use File::Temp qw/ tempfile unlink0 /;
27 # The high security tests must currently be skipped on some platforms
30 $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'os2' || $^O eq 'dos'
33 # Can not run high security tests in perls before 5.6.0
34 my $skipperl = ($] < 5.006 ? 1 : 0 );
36 # Determine whether we need to skip things and why
39 $skip = "Skip Not supported on this platform";
41 $skip = "Skip Perl version must be v5.6.0 for these tests";
45 print "# We will be skipping some tests : $skip\n" if $skip;
47 # start off with basic checking
49 File::Temp->safe_level( File::Temp::STANDARD );
51 print "# Testing with STANDARD security...\n";
57 File::Temp->safe_level( File::Temp::MEDIUM )
60 print "# Testing with MEDIUM security...\n";
62 # Now we need to start skipping tests
63 &test_security($skip);
67 File::Temp->safe_level( File::Temp::HIGH )
70 print "# Testing with HIGH security...\n";
72 &test_security($skip);
76 # Subroutine to open two temporary files.
77 # one is opened in the current dir and the other in the temp dir
81 # Read in the skip flag
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
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;
99 my $template = "tmpXXXXX";
100 my ($fh1, $fname1) = eval { tempfile ( $template,
101 DIR => File::Spec->tmpdir,
106 if (defined $fname1) {
107 print "# fname1 = $fname1\n";
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'";
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;
120 if ( $< < File::Temp->top_system_uid() ){
121 skip("Skip Test inappropriate for root", 1);
122 eval q{ END { skip($skip,1); } 1; } || die;
125 my ($fh2, $fname2) = eval { tempfile ($template, UNLINK => 1 ); };
126 if (defined $fname2) {
127 print "# fname2 = $fname2\n";
129 push(@files, $fname2); # store for end block
131 } elsif (File::Temp->safe_level() != File::Temp::STANDARD) {
132 my $skip2 = "Skip system possibly insecure, see INSTALL, section 'make test'";
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;