summaryrefslogtreecommitdiff
path: root/tests/rm/fail-eperm
blob: 2e4d64cb3e5c195fb412a8114884593a618b27c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/sh
# -*- perl -*-
# Ensure that rm gives the expected diagnostic when failing to remove a file
# owned by some other user in a directory with the sticky bit set.

: ${PERL=perl}
: ${srcdir=.}

$PERL -e 1 > /dev/null 2>&1 || {
  echo 1>&2 "$0: configure didn't find a usable version of Perl," \
    "so can't run this test"
  exit 77
}

ARGV_0=$0
export ARGV_0

exec $PERL -w -- - << \EOP
require 5.003;
use strict;

(my $ME = $ENV{ARGV_0}) =~ s|.*/||;

my $verbose = $ENV{VERBOSE} && $ENV{VERBOSE} eq 'yes';
$verbose
  and system qw (rm --version);

# Ensure that the diagnostics are in English.
$ENV{LC_ALL} = 'C';

# Ensure that the effective UID is not 0.
if ($> == 0)
  {
    warn "$ME: you may not run this test as root;  skipping it.\n";
    exit 77;
  }

my @dir_list = qw(/tmp /var/tmp /usr/tmp);

# Find a directory with the sticky bit set.
my $found_dir;
my $found_file;
foreach my $dir (@dir_list)
  {
    if (-d $dir && -k _ && -r _ && -w _ && -x _)
      {
	$found_dir = 1;

	# Find a non-directory there that is owned by some other user.
	opendir DIR_HANDLE, $dir
	  or die "$ME: couldn't open $dir: $!\n";

	foreach my $f (readdir DIR_HANDLE)
	  {
	    my $target_file = "$dir/$f";
	    $verbose
	      and warn "$ME: considering $target_file\n";

	    # Skip files owned by self, symlinks, and directories.
	    # It's not technically necessary to skip symlinks, but it's simpler.
	    -l $target_file || -o _ || -d _
	      and next;

	    $found_file = 1;

	    # Invoke rm on this file and ensure that we get the
	    # expected exit code and diagnostic.
	    my $cmd = "rm -f $target_file";
	    open RM, "$cmd 2>&1 |"
	      or die "$ME: cannot execute `$cmd'\n";

	    my $line = <RM>;

	    close RM;
	    my $status = $? >> 8;
	    $status == 1
	      or die "$ME: unexpected exit status from `$cmd';\n"
		. "  got $status, expected 1\n";

	    my $exp = "rm: cannot remove `$target_file':";
	    $line
	      or die "$ME: no output from `$cmd';\n"
		. "expected something like `$exp ...'\n";

	    my $regex = quotemeta $exp;
	    $line =~ /^$regex/
	      or die "$ME: unexpected dignostic from `$cmd';\n"
		. "  got      $line"
		. "  expected $exp ...\n";

	    last;
	  }

	closedir DIR_HANDLE;
	$found_file
	  and last;
      }
  }

if ( ! $found_dir)
  {
    warn "$ME: couldn't find a directory with the sticky bit set;"
      . " skipping this test\n";
    exit 77;
  }

if ( ! $found_file)
  {
    warn "$ME: couldn't find a file not owned by you\n"
      . " in any of the following directories:\n  @dir_list\n"
      . "...so, skipping this test\n";
    exit 77;
  }
EOP