Commit | Line | Data |
1c19c868 |
1 | #!/usr/local/bin/perl -w |
262eb13a |
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 | |
8 | use strict; |
9 | use Test; |
10 | BEGIN { plan tests => 13} |
11 | |
12 | use File::Spec; |
1c19c868 |
13 | |
14 | # Set up END block - this needs to happen before we load |
15 | # File::Temp since this END block must be evaluated after the |
16 | # END block configured by File::Temp |
17 | my @files; # list of files to remove |
18 | END { foreach (@files) { ok( !(-e $_) )} } |
19 | |
262eb13a |
20 | use File::Temp qw/ tempfile unlink0 /; |
21 | ok(1); |
22 | |
23 | # The high security tests must currently be skipped on Windows |
24 | my $skipplat = ( $^O eq 'MSWin32' ? 1 : 0 ); |
25 | |
26 | # Can not run high security tests in perls before 5.6.0 |
27 | my $skipperl = ($] < 5.006 ? 1 : 0 ); |
28 | |
29 | # Determine whether we need to skip things and why |
30 | my $skip = 0; |
31 | if ($skipplat) { |
32 | $skip = "Skip Not supported on this platform"; |
33 | } elsif ($skipperl) { |
34 | $skip = "Skip Perl version must be v5.6.0 for these tests"; |
35 | |
36 | } |
37 | |
38 | print "# We will be skipping some tests : $skip\n" if $skip; |
39 | |
40 | # start off with basic checking |
41 | |
42 | File::Temp->safe_level( File::Temp::STANDARD ); |
43 | |
44 | print "# Testing with STANDARD security...\n"; |
45 | |
46 | &test_security(0); |
47 | |
48 | # Try medium |
49 | |
50 | File::Temp->safe_level( File::Temp::MEDIUM ) |
51 | unless $skip; |
52 | |
53 | print "# Testing with MEDIUM security...\n"; |
54 | |
55 | # Now we need to start skipping tests |
56 | &test_security($skip); |
57 | |
58 | # Try HIGH |
59 | |
60 | File::Temp->safe_level( File::Temp::HIGH ) |
61 | unless $skip; |
62 | |
63 | print "# Testing with HIGH security...\n"; |
64 | |
65 | &test_security($skip); |
66 | |
67 | exit; |
68 | |
69 | # Subroutine to open two temporary files. |
70 | # one is opened in the current dir and the other in the temp dir |
71 | |
72 | sub test_security { |
73 | |
74 | # Read in the skip flag |
75 | my $skip = shift; |
76 | |
77 | # If we are skipping we need to simply fake the correct number |
78 | # of tests -- we dont use skip since the tempfile() commands will |
79 | # fail with MEDIUM/HIGH security before the skip() command would be run |
80 | if ($skip) { |
81 | |
82 | skip($skip,1); |
83 | skip($skip,1); |
84 | |
85 | # plus we need an end block so the tests come out in the right order |
86 | eval q{ END { skip($skip,1); skip($skip,1) } 1; } || die; |
87 | |
88 | return; |
89 | } |
90 | |
1c19c868 |
91 | # Create the tempfile |
262eb13a |
92 | my $template = "temptestXXXXXXXX"; |
93 | my ($fh1, $fname1) = tempfile ( $template, |
94 | DIR => File::Spec->curdir, |
95 | UNLINK => 1, |
96 | ); |
97 | print "# Fname1 = $fname1\n"; |
98 | ok( ( -e $fname1) ); |
99 | |
100 | # Explicitly |
101 | my ($fh2, $fname2) = tempfile ($template, UNLINK => 1 ); |
102 | ok( (-e $fname2) ); |
103 | close($fh2); |
104 | |
105 | # Store filenames for the end block |
106 | push(@files, $fname1, $fname2); |
107 | |
108 | |
109 | |
110 | } |