1#!/usr/bin/perl -w
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
8use Test::More tests => 12;
9
10use strict;
11use File::Spec;
12
13# Set up END block - this needs to happen before we load
14# File::Temp since this END block must be evaluated after the
15# END block configured by File::Temp
16my @files; # list of files to remove
17END { foreach (@files) { ok( !(-e $_) )} }
18
19use File::Temp qw/ tempfile unlink0 /;
20
21# The high security tests must currently be skipped on some platforms
22my $skipplat = ( (
23		  # No sticky bits.
24		  $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'os2' || $^O eq 'dos' || $^O eq 'mpeix' || $^O eq 'MacOS'
25		  ) ? 1 : 0 );
26
27# Can not run high security tests in perls before 5.6.0
28my $skipperl  = ($] < 5.006 ? 1 : 0 );
29
30# Determine whether we need to skip things and why
31my $skip = 0;
32if ($skipplat) {
33  $skip = "Not supported on this platform";
34} elsif ($skipperl) {
35  $skip = "Perl version must be v5.6.0 for these tests";
36
37}
38
39print "# We will be skipping some tests : $skip\n" if $skip;
40
41# start off with basic checking
42
43File::Temp->safe_level( File::Temp::STANDARD );
44
45print "# Testing with STANDARD security...\n";
46
47test_security();
48
49SKIP: {
50  skip $skip, 8 if $skip;
51
52  # Try medium
53
54  File::Temp->safe_level( File::Temp::MEDIUM );
55
56  print "# Testing with MEDIUM security...\n";
57
58  # Now we need to start skipping tests
59  test_security();
60
61  # Try HIGH
62
63  File::Temp->safe_level( File::Temp::HIGH );
64
65  print "# Testing with HIGH security...\n";
66
67  test_security();
68}
69
70exit;
71
72# Subroutine to open two temporary files.
73# one is opened in the current dir and the other in the temp dir
74
75sub test_security {
76
77  # Create the tempfile
78  my $template = "tmpXXXXX";
79  my ($fh1, $fname1) = eval { tempfile ( $template, 
80				  DIR => File::Temp::_wrap_file_spec_tmpdir(),
81				  UNLINK => 1,
82				);
83			    };
84
85  SKIP: {
86    if (defined $fname1) {
87        print "# fname1 = $fname1\n";
88        ok( (-e $fname1) );
89        push(@files, $fname1); # store for end block
90    } elsif (File::Temp->safe_level() != File::Temp::STANDARD) {
91        chomp($@);
92        my $msg = File::Temp::_wrap_file_spec_tmpdir() . " possibly insecure: $@";
93        skip $msg, 2; # one here and one in END
94    } else {
95        ok(0);
96    }
97  }
98
99  SKIP: {
100    # Explicitly 
101    if ( $< < File::Temp->top_system_uid() ){
102        skip("Skip Test inappropriate for root", 2);
103        return;
104    }
105    my ($fh2, $fname2) = eval { tempfile ($template,  UNLINK => 1 ); };
106    if (defined $fname2) {
107        print "# fname2 = $fname2\n";
108        ok( (-e $fname2) );
109        push(@files, $fname2); # store for end block
110        close($fh2);
111    } elsif (File::Temp->safe_level() != File::Temp::STANDARD) {
112        chomp($@);
113        my $msg = "current directory possibly insecure: $@";
114        skip $msg, 2; # one here and one in END
115    } else {
116        ok(0);
117    }
118  }
119}
120
121# vim: ts=2 sts=2 sw=2 et:
122