global: convert indentation-TABs to spaces
[coreutils.git] / tests / rm / fail-eperm
blob9ccb913a6f54ac82408360ac1121c6b332dc70b0
1 #!/usr/bin/perl -Tw
2 # Ensure that rm gives the expected diagnostic when failing to remove a file
3 # owned by some other user in a directory with the sticky bit set.
5 # Copyright (C) 2002-2004, 2006-2009 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 use strict;
22 (my $ME = $0) =~ s|.*/||;
24 my $uid = $<;
25 # skip if root
26 $uid == 0
27 and (warn "$ME: can't run this test as root: skipping this test"), exit 77;
29 my $verbose = $ENV{VERBOSE} && $ENV{VERBOSE} eq 'yes';
31 # Ensure that the diagnostics are in English.
32 $ENV{LC_ALL} = 'C';
34 # Set up a safe, well-known environment
35 delete @ENV{qw(BASH_ENV CDPATH ENV PATH)};
36 $ENV{IFS} = '';
38 my @dir_list = qw(/tmp /var/tmp /usr/tmp);
39 my $rm = "$ENV{abs_top_builddir}/src/rm";
41 # Untaint for upcoming popen.
42 $rm =~ m!^([-+\@\w./]+)$!
43 or (warn "$ME: unusual absolute builddir name; skipping this test\n"),
44 exit 77;
45 $rm = $1;
47 # Find a directory with the sticky bit set.
48 my $found_dir;
49 my $found_file;
50 foreach my $dir (@dir_list)
52 if (-d $dir && -k _ && -r _ && -w _ && -x _)
54 $found_dir = 1;
56 # Find a non-directory there that is owned by some other user.
57 opendir DIR_HANDLE, $dir
58 or die "$ME: couldn't open $dir: $!\n";
60 foreach my $f (readdir DIR_HANDLE)
62 # Consider only names containing "safe" characters.
63 $f =~ /^([-\@\w.]+)$/
64 or next;
65 $f = $1; # untaint $f
67 my $target_file = "$dir/$f";
68 $verbose
69 and warn "$ME: considering $target_file\n";
71 # Skip files owned by self, symlinks, and directories.
72 # It's not technically necessary to skip symlinks, but it's simpler.
73 # SVR4-like systems (e.g., Solaris 9) let you unlink files that
74 # you can write, so skip writable files too.
75 -l $target_file || -o _ || -d _ || -w _
76 and next;
78 $found_file = 1;
80 # Invoke rm on this file and ensure that we get the
81 # expected exit code and diagnostic.
82 my $cmd = "$rm -f -- $target_file";
83 open RM, "$cmd 2>&1 |"
84 or die "$ME: cannot execute `$cmd'\n";
86 my $line = <RM>;
88 close RM;
89 my $rc = $?;
90 # This test opportunistically looks for files that can't
91 # be removed but those files may already have been removed
92 # by their owners by the time we get to them. It is a
93 # race condition. If so then the rm is successful and our
94 # test is thwarted. Detect this case and ignore.
95 if ($rc == 0)
97 next if ! -e $target_file;
98 die "$ME: unexpected exit status from `$cmd';\n"
99 . " got 0, expected 1\n";
101 if (0x80 < $rc)
103 my $status = $rc >> 8;
104 $status == 1
105 or die "$ME: unexpected exit status from `$cmd';\n"
106 . " got $status, expected 1\n";
108 else
110 # Terminated by a signal.
111 my $sig_num = $rc & 0x7F;
112 die "$ME: command `$cmd' died with signal $sig_num\n";
115 my $exp = "rm: cannot remove `$target_file':";
116 $line
117 or die "$ME: no output from `$cmd';\n"
118 . "expected something like `$exp ...'\n";
120 # Transform the actual diagnostic so that it starts with "rm:".
121 # Depending on your system, it might be "rm:" already, or
122 # "../../src/rm:".
123 $line =~ s,^\Q$rm\E:,rm:,;
125 my $regex = quotemeta $exp;
126 $line =~ /^$regex/
127 or die "$ME: unexpected diagnostic from `$cmd';\n"
128 . " got $line"
129 . " expected $exp ...\n";
131 last;
134 closedir DIR_HANDLE;
135 $found_file
136 and last;
140 if ( ! $found_dir)
142 warn "$ME: couldn't find a directory with the sticky bit set;"
143 . " skipping this test\n";
144 exit 77;
147 if ( ! $found_file)
149 warn "$ME: couldn't find a file not owned by you\n"
150 . " in any of the following directories:\n @dir_list\n"
151 . "...so, skipping this test\n";
152 exit 77;