Tizen 2.0 Release
[external/tizen-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 # Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # 02110-1301, USA.
22
23 if test "$VERBOSE" = yes; then
24   set -x
25   rm --version
26 fi
27
28 # FIXME-someday: when run as root we don't need all of the
29 # searching below.  root can simply create the required
30 # dir/files and run the test as someone else.
31
32 PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check
33
34 : ${PERL=perl}
35 : ${srcdir=.}
36
37 $PERL -e 1 > /dev/null 2>&1 || {
38   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
39     "so can't run this test"
40   (exit 77); exit 77
41 }
42
43 ARGV_0=$0
44 export ARGV_0
45
46 exec $PERL -Tw -- - << \EOP
47 require 5.003;
48 use strict;
49
50 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
51
52 my $verbose = $ENV{VERBOSE} && $ENV{VERBOSE} eq 'yes';
53
54 # Ensure that the diagnostics are in English.
55 $ENV{LC_ALL} = 'C';
56
57 # Set up a safe, well-known environment
58 delete @ENV{qw(BASH_ENV CDPATH ENV PATH)};
59 $ENV{IFS}  = '';
60
61 my @dir_list = qw(/tmp /var/tmp /usr/tmp);
62 my $rm = '../../src/rm';
63
64 # Find a directory with the sticky bit set.
65 my $found_dir;
66 my $found_file;
67 foreach my $dir (@dir_list)
68   {
69     if (-d $dir && -k _ && -r _ && -w _ && -x _)
70       {
71         $found_dir = 1;
72
73         # Find a non-directory there that is owned by some other user.
74         opendir DIR_HANDLE, $dir
75           or die "$ME: couldn't open $dir: $!\n";
76
77         foreach my $f (readdir DIR_HANDLE)
78           {
79             # Consider only names containing "safe" characters.
80             $f =~ /^([-\@\w.]+)$/
81               or next;
82             $f = $1;    # untaint $f
83
84             my $target_file = "$dir/$f";
85             $verbose
86               and warn "$ME: considering $target_file\n";
87
88             # Skip files owned by self, symlinks, and directories.
89             # It's not technically necessary to skip symlinks, but it's simpler.
90             # SVR4-like systems (e.g., Solaris 9) let you unlink files that
91             # you can write, so skip writable files too.
92             -l $target_file || -o _ || -d _ || -w _
93               and next;
94
95             $found_file = 1;
96
97             # Invoke rm on this file and ensure that we get the
98             # expected exit code and diagnostic.
99             my $cmd = "$rm -f -- $target_file";
100             open RM, "$cmd 2>&1 |"
101               or die "$ME: cannot execute `$cmd'\n";
102
103             my $line = <RM>;
104
105             close RM;
106             my $rc = $?;
107             if (0x80 < $rc)
108               {
109                 my $status = $rc >> 8;
110                 $status == 1
111                   or die "$ME: unexpected exit status from `$cmd';\n"
112                     . "  got $status, expected 1\n";
113               }
114             else
115               {
116                 # Terminated by a signal.
117                 my $sig_num = $rc & 0x7F;
118                 die "$ME: command `$cmd' died with signal $sig_num\n";
119               }
120
121             my $exp = "rm: cannot remove `$target_file':";
122             $line
123               or die "$ME: no output from `$cmd';\n"
124                 . "expected something like `$exp ...'\n";
125
126             # Transform the actual diagnostic so that it starts with "rm:".
127             # Depending on your system, it might be "rm:" already, or
128             # "../../src/rm:".
129             $line =~ s,^\Q$rm\E:,rm:,;
130
131             my $regex = quotemeta $exp;
132             $line =~ /^$regex/
133               or die "$ME: unexpected diagnostic from `$cmd';\n"
134                 . "  got      $line"
135                 . "  expected $exp ...\n";
136
137             last;
138           }
139
140         closedir DIR_HANDLE;
141         $found_file
142           and last;
143       }
144   }
145
146 if ( ! $found_dir)
147   {
148     warn "$ME: couldn't find a directory with the sticky bit set;"
149       . " skipping this test\n";
150     exit 77;
151   }
152
153 if ( ! $found_file)
154   {
155     warn "$ME: couldn't find a file not owned by you\n"
156       . " in any of the following directories:\n  @dir_list\n"
157       . "...so, skipping this test\n";
158     exit 77;
159   }
160 EOP