22dd1f728346c483bd9c4174cc31911e26014277
[platform/upstream/coreutils.git] / tests / rm / fail-eperm
1 #!/bin/sh
2 # -*- perl -*-
3 # Ensure that rm gives the expected diagnostic when failing to remove a file
4 # owned by some other user in a directory with the sticky bit set.
5
6 if test "$VERBOSE" = yes; then
7   set -x
8   rm --version
9 fi
10
11 # FIXME-someday: when run as root we don't need all of the
12 # searching below.  root can simply create the required
13 # dir/files and run the test as someone else.
14
15 PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check
16
17 : ${PERL=perl}
18 : ${srcdir=.}
19
20 $PERL -e 1 > /dev/null 2>&1 || {
21   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
22     "so can't run this test"
23   exit 77
24 }
25
26 ARGV_0=$0
27 export ARGV_0
28
29 exec $PERL -w -- - << \EOP
30 require 5.003;
31 use strict;
32
33 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
34
35 my $verbose = $ENV{VERBOSE} && $ENV{VERBOSE} eq 'yes';
36
37 # Ensure that the diagnostics are in English.
38 $ENV{LC_ALL} = 'C';
39
40 my @dir_list = qw(/tmp /var/tmp /usr/tmp);
41
42 # Find a directory with the sticky bit set.
43 my $found_dir;
44 my $found_file;
45 foreach my $dir (@dir_list)
46   {
47     if (-d $dir && -k _ && -r _ && -w _ && -x _)
48       {
49         $found_dir = 1;
50
51         # Find a non-directory there that is owned by some other user.
52         opendir DIR_HANDLE, $dir
53           or die "$ME: couldn't open $dir: $!\n";
54
55         foreach my $f (readdir DIR_HANDLE)
56           {
57             my $target_file = "$dir/$f";
58             $verbose
59               and warn "$ME: considering $target_file\n";
60
61             # Skip files owned by self, symlinks, and directories.
62             # It's not technically necessary to skip symlinks, but it's simpler.
63             # SVR4-like systems (e.g., Solaris 9) let you unlink files that
64             # you can write, so skip writable files too.
65             -l $target_file || -o _ || -d _ || -w _
66               and next;
67
68             $found_file = 1;
69
70             # Invoke rm on this file and ensure that we get the
71             # expected exit code and diagnostic.
72             my $cmd = "rm -f $target_file";
73             open RM, "$cmd 2>&1 |"
74               or die "$ME: cannot execute `$cmd'\n";
75
76             my $line = <RM>;
77
78             close RM;
79             my $status = $? >> 8;
80             $status == 1
81               or die "$ME: unexpected exit status from `$cmd';\n"
82                 . "  got $status, expected 1\n";
83
84             my $exp = "rm: cannot remove `$target_file':";
85             $line
86               or die "$ME: no output from `$cmd';\n"
87                 . "expected something like `$exp ...'\n";
88
89             my $regex = quotemeta $exp;
90             $line =~ /^$regex/
91               or die "$ME: unexpected dignostic from `$cmd';\n"
92                 . "  got      $line"
93                 . "  expected $exp ...\n";
94
95             last;
96           }
97
98         closedir DIR_HANDLE;
99         $found_file
100           and last;
101       }
102   }
103
104 if ( ! $found_dir)
105   {
106     warn "$ME: couldn't find a directory with the sticky bit set;"
107       . " skipping this test\n";
108     exit 77;
109   }
110
111 if ( ! $found_file)
112   {
113     warn "$ME: couldn't find a file not owned by you\n"
114       . " in any of the following directories:\n  @dir_list\n"
115       . "...so, skipping this test\n";
116     exit 77;
117   }
118 EOP