cksum: ensure appropriate "binary" mode with --untagged
[coreutils.git] / tests / rm / fail-eperm.xpl
blob97a103645e547e9ee84f6925486589f978d8cefd
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-2024 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 <https://www.gnu.org/licenses/>.
20 use strict;
22 (my $ME = $0) =~ s|.*/||;
24 my $uid = $<;
25 # skip if root
26 $uid == 0
27 and CuSkip::skip "$ME: can't run this test as root: skipping this test";
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 $ENV{IFS} = '';
37 # Taint checking requires a sanitized $PATH. This script performs no $PATH
38 # search, so on most Unix-based systems, it is fine simply to clear $ENV{PATH}.
39 # However, on Cygwin, it's used to find cygwin1.dll, so set it.
40 $ENV{PATH} = '/bin:/usr/bin';
42 my @dir_list = qw(/tmp /var/tmp /usr/tmp);
43 my $rm = "$ENV{abs_top_builddir}/src/rm";
45 # Untaint for upcoming popen.
46 $rm =~ m!^([-+\@\w./]+)$!
47 or CuSkip::skip "$ME: unusual absolute builddir name; skipping this test\n";
48 $rm = $1;
50 # Find a directory with the sticky bit set.
51 my $found_dir;
52 my $found_file;
53 foreach my $dir (@dir_list)
55 if (-d $dir && -k _ && -r _ && -w _ && -x _)
57 $found_dir = 1;
59 # Find a non-directory there that is owned by some other user.
60 opendir DIR_HANDLE, $dir
61 or die "$ME: couldn't open $dir: $!\n";
63 foreach my $f (readdir DIR_HANDLE)
65 # Consider only names containing "safe" characters.
66 $f =~ /^([-\@\w.]+)$/
67 or next;
68 $f = $1; # untaint $f
70 my $target_file = "$dir/$f";
71 $verbose
72 and warn "$ME: considering $target_file\n";
74 # Skip files owned by self, symlinks, and directories.
75 # It's not technically necessary to skip symlinks, but it's simpler.
76 # SVR4-like systems (e.g., Solaris 9) let you unlink files that
77 # you can write, so skip writable files too.
78 -l $target_file || -o _ || -d _ || -w _
79 and next;
81 $found_file = 1;
83 # Invoke rm on this file and ensure that we get the
84 # expected exit code and diagnostic.
85 my $cmd = "$rm -f -- $target_file";
86 open RM, "$cmd 2>&1 |"
87 or die "$ME: cannot execute '$cmd'\n";
89 my $line = <RM>;
91 close RM;
92 my $rc = $?;
93 # This test opportunistically looks for files that can't
94 # be removed but those files may already have been removed
95 # by their owners by the time we get to them. It is a
96 # race condition. If so then the rm is successful and our
97 # test is thwarted. Detect this case and ignore.
98 if ($rc == 0)
100 next if ! -e $target_file;
101 die "$ME: unexpected exit status from '$cmd';\n"
102 . " got 0, expected 1\n";
104 if (0x80 < $rc)
106 my $status = $rc >> 8;
107 $status == 1
108 or die "$ME: unexpected exit status from '$cmd';\n"
109 . " got $status, expected 1\n";
111 else
113 # Terminated by a signal.
114 my $sig_num = $rc & 0x7F;
115 die "$ME: command '$cmd' died with signal $sig_num\n";
118 my $exp = "rm: cannot remove '$target_file':";
119 $line
120 or die "$ME: no output from '$cmd';\n"
121 . "expected something like '$exp ...'\n";
123 # Transform the actual diagnostic so that it starts with "rm:".
124 # Depending on your system, it might be "rm:" already, or
125 # "../../src/rm:".
126 $line =~ s,^\Q$rm\E:,rm:,;
128 my $regex = quotemeta $exp;
129 $line =~ /^$regex/
130 or die "$ME: unexpected diagnostic from '$cmd';\n"
131 . " got $line"
132 . " expected $exp ...\n";
134 last;
137 closedir DIR_HANDLE;
138 $found_file
139 and last;
143 $found_dir
144 or CuSkip::skip "$ME: couldn't find a directory with the sticky bit set;"
145 . " skipping this test\n";
147 $found_file
148 or CuSkip::skip "$ME: couldn't find a file not owned by you\n"
149 . " in any of the following directories:\n @dir_list\n"
150 . "...so, skipping this test\n";