Commit | Line | Data |
---|---|---|
262eb13a GS |
1 | #!./perl |
2 | ||
3 | BEGIN { | |
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 | ||
14 | use strict; | |
15 | use Test; | |
16 | BEGIN { plan tests => 13} | |
17 | ||
18 | use File::Spec; | |
19 | use File::Temp qw/ tempfile unlink0 /; | |
20 | ok(1); | |
21 | ||
22 | # The high security tests must currently be skipped on Windows | |
23 | my $skipplat = ( $^O eq 'MSWin32' ? 1 : 0 ); | |
24 | ||
25 | # Can not run high security tests in perls before 5.6.0 | |
26 | my $skipperl = ($] < 5.006 ? 1 : 0 ); | |
27 | ||
28 | # Determine whether we need to skip things and why | |
29 | my $skip = 0; | |
30 | if ($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 | ||
37 | print "# We will be skipping some tests : $skip\n" if $skip; | |
38 | ||
39 | # start off with basic checking | |
40 | ||
41 | File::Temp->safe_level( File::Temp::STANDARD ); | |
42 | ||
43 | print "# Testing with STANDARD security...\n"; | |
44 | ||
45 | &test_security(0); | |
46 | ||
47 | # Try medium | |
48 | ||
49 | File::Temp->safe_level( File::Temp::MEDIUM ) | |
50 | unless $skip; | |
51 | ||
52 | print "# Testing with MEDIUM security...\n"; | |
53 | ||
54 | # Now we need to start skipping tests | |
55 | &test_security($skip); | |
56 | ||
57 | # Try HIGH | |
58 | ||
59 | File::Temp->safe_level( File::Temp::HIGH ) | |
60 | unless $skip; | |
61 | ||
62 | print "# Testing with HIGH security...\n"; | |
63 | ||
64 | &test_security($skip); | |
65 | ||
66 | exit; | |
67 | ||
68 | # Subroutine to open two temporary files. | |
69 | # one is opened in the current dir and the other in the temp dir | |
70 | ||
71 | sub 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 | } |