Commit | Line | Data |
---|---|---|
e2cf2bdb | 1 | #!/usr/bin/perl -w |
262eb13a GS |
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 | ||
e2cf2bdb JH |
8 | BEGIN { |
9 | chdir 't' if -d 't'; | |
20822f61 | 10 | @INC = '../lib'; |
e2cf2bdb | 11 | require Test; import Test; |
2898e2cc | 12 | plan(tests => 13); |
e2cf2bdb | 13 | } |
262eb13a | 14 | |
e2cf2bdb | 15 | use strict; |
262eb13a | 16 | use File::Spec; |
1c19c868 JH |
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 | |
21 | my @files; # list of files to remove | |
22 | END { foreach (@files) { ok( !(-e $_) )} } | |
23 | ||
262eb13a GS |
24 | use File::Temp qw/ tempfile unlink0 /; |
25 | ok(1); | |
26 | ||
781948c1 JH |
27 | # The high security tests must currently be skipped on some platforms |
28 | my $skipplat = ( ( | |
29 | # No sticky bits. | |
30 | $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'dos' | |
31 | ) ? 1 : 0 ); | |
262eb13a GS |
32 | |
33 | # Can not run high security tests in perls before 5.6.0 | |
34 | my $skipperl = ($] < 5.006 ? 1 : 0 ); | |
35 | ||
36 | # Determine whether we need to skip things and why | |
37 | my $skip = 0; | |
38 | if ($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 | ||
45 | print "# We will be skipping some tests : $skip\n" if $skip; | |
46 | ||
47 | # start off with basic checking | |
48 | ||
49 | File::Temp->safe_level( File::Temp::STANDARD ); | |
50 | ||
51 | print "# Testing with STANDARD security...\n"; | |
52 | ||
53 | &test_security(0); | |
54 | ||
55 | # Try medium | |
56 | ||
57 | File::Temp->safe_level( File::Temp::MEDIUM ) | |
58 | unless $skip; | |
59 | ||
60 | print "# Testing with MEDIUM security...\n"; | |
61 | ||
62 | # Now we need to start skipping tests | |
63 | &test_security($skip); | |
64 | ||
65 | # Try HIGH | |
66 | ||
67 | File::Temp->safe_level( File::Temp::HIGH ) | |
68 | unless $skip; | |
69 | ||
70 | print "# Testing with HIGH security...\n"; | |
71 | ||
72 | &test_security($skip); | |
73 | ||
74 | exit; | |
75 | ||
76 | # Subroutine to open two temporary files. | |
77 | # one is opened in the current dir and the other in the temp dir | |
78 | ||
79 | sub 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) { | |
669b450a | 88 | |
262eb13a GS |
89 | skip($skip,1); |
90 | skip($skip,1); | |
669b450a | 91 | |
262eb13a GS |
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; | |
669b450a | 94 | |
262eb13a GS |
95 | return; |
96 | } | |
97 | ||
1c19c868 | 98 | # Create the tempfile |
781948c1 | 99 | my $template = "tmpXXXXX"; |
262eb13a | 100 | my ($fh1, $fname1) = tempfile ( $template, |
da7cc32f | 101 | DIR => File::Spec->tmpdir, |
262eb13a GS |
102 | UNLINK => 1, |
103 | ); | |
781948c1 JH |
104 | if (defined $fname1) { |
105 | print "# fname1 = $fname1\n"; | |
106 | ok( (-e $fname1) ); | |
107 | } elsif (File::Temp->safe_level() != File::Temp::STANDARD) { | |
108 | skip("system possibly insecure, see INSTALL, section 'make test'", 1); | |
109 | } else { | |
110 | ok(0); | |
111 | } | |
262eb13a GS |
112 | |
113 | # Explicitly | |
781948c1 JH |
114 | my ($fh2, $fname2) = tempfile ($template, UNLINK => 1 ); |
115 | if (defined $fname2) { | |
116 | print "# fname2 = $fname2\n"; | |
117 | ok( (-e $fname2) ); | |
118 | close($fh2); | |
119 | } elsif (File::Temp->safe_level() != File::Temp::STANDARD) { | |
120 | skip("system possibly insecure, see INSTALL, section 'make test'", 1); | |
121 | } else { | |
122 | ok(0); | |
123 | } | |
262eb13a GS |
124 | |
125 | # Store filenames for the end block | |
126 | push(@files, $fname1, $fname2); | |
262eb13a | 127 | } |