This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regen Configure to jive with #6149.
[perl5.git] / t / lib / ftmp-security.t
CommitLineData
262eb13a
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 unshift @INC, '../lib';
6}
7
8# Test for File::Temp - Security levels
9
10# Some of the security checking will not work on all platforms
11# Test a simple open in the cwd and tmpdir foreach of the
12# security levels
13
14use strict;
15use Test;
16BEGIN { plan tests => 13}
17
18use File::Spec;
19use File::Temp qw/ tempfile unlink0 /;
20ok(1);
21
22# The high security tests must currently be skipped on Windows
23my $skipplat = ( $^O eq 'MSWin32' ? 1 : 0 );
24
25# Can not run high security tests in perls before 5.6.0
26my $skipperl = ($] < 5.006 ? 1 : 0 );
27
28# Determine whether we need to skip things and why
29my $skip = 0;
30if ($skipplat) {
31 $skip = "Skip Not supported on this platform";
32} elsif ($skipperl) {
33 $skip = "Skip Perl version must be v5.6.0 for these tests";
34
35}
36
37print "# We will be skipping some tests : $skip\n" if $skip;
38
39# start off with basic checking
40
41File::Temp->safe_level( File::Temp::STANDARD );
42
43print "# Testing with STANDARD security...\n";
44
45&test_security(0);
46
47# Try medium
48
49File::Temp->safe_level( File::Temp::MEDIUM )
50 unless $skip;
51
52print "# Testing with MEDIUM security...\n";
53
54# Now we need to start skipping tests
55&test_security($skip);
56
57# Try HIGH
58
59File::Temp->safe_level( File::Temp::HIGH )
60 unless $skip;
61
62print "# Testing with HIGH security...\n";
63
64&test_security($skip);
65
66exit;
67
68# Subroutine to open two temporary files.
69# one is opened in the current dir and the other in the temp dir
70
71sub test_security {
72
73 # Read in the skip flag
74 my $skip = shift;
75
76 # If we are skipping we need to simply fake the correct number
77 # of tests -- we dont use skip since the tempfile() commands will
78 # fail with MEDIUM/HIGH security before the skip() command would be run
79 if ($skip) {
80
81 skip($skip,1);
82 skip($skip,1);
83
84 # plus we need an end block so the tests come out in the right order
85 eval q{ END { skip($skip,1); skip($skip,1) } 1; } || die;
86
87 return;
88 }
89
90
91 # End blocks are evaluated in reverse order
92 # If I want to check that the file was unlinked by the autmoatic
93 # feature of the module I have to set up the end block before
94 # creating the file.
95 # Use quoted end block to retain access to lexicals
96 my @files;
97
98 eval q{ END { foreach (@files) { ok( !(-e $_) )} } 1; } || die;
99
100
101 my $template = "temptestXXXXXXXX";
102 my ($fh1, $fname1) = tempfile ( $template,
103 DIR => File::Spec->curdir,
104 UNLINK => 1,
105 );
106 print "# Fname1 = $fname1\n";
107 ok( ( -e $fname1) );
108
109 # Explicitly
110 my ($fh2, $fname2) = tempfile ($template, UNLINK => 1 );
111 ok( (-e $fname2) );
112 close($fh2);
113
114 # Store filenames for the end block
115 push(@files, $fname1, $fname2);
116
117
118
119}