1#!/bin/sh
2# make sure chgrp is reasonable
3
4# Copyright (C) 2000-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ chgrp
21require_membership_in_two_groups_
22require_local_dir_
23
24set _ $groups; shift
25g1=$1
26g2=$2
27mkdir d
28touch f f2 d/f3
29chgrp $g1 f || fail=1
30chgrp $g2 f || fail=1
31chgrp $g2 f2 || fail=1
32chgrp -R $g1 d || fail=1
33
34d_files='d d/f3'
35
36chgrp $g1 f || fail=1  ; test $(stat --p=%g f) = $g1 || fail=1
37chgrp $g2 f || fail=1  ; test $(stat --p=%g f) = $g2 || fail=1
38chgrp $g2 f || fail=1  ; test $(stat --p=%g f) = $g2 || fail=1
39chgrp '' f  || fail=1  ; test $(stat --p=%g f) = $g2 || fail=1
40chgrp $g1 f || fail=1  ; test $(stat --p=%g f) = $g1 || fail=1
41chgrp $g1 f || fail=1  ; test $(stat --p=%g f) = $g1 || fail=1
42chgrp --reference=f2 f ; test $(stat --p=%g f) = $g2 || fail=1
43
44chgrp -R $g2 d ||fail=1; test $(stat --p=%g: $d_files) = "$g2:$g2:" || fail=1
45chgrp -R $g1 d ||fail=1; test $(stat --p=%g: $d_files) = "$g1:$g1:" || fail=1
46chgrp -R $g2 d ||fail=1; test $(stat --p=%g: $d_files) = "$g2:$g2:" || fail=1
47chgrp -R $g1 d ||fail=1; test $(stat --p=%g: $d_files) = "$g1:$g1:" || fail=1
48chgrp $g2 d    ||fail=1; test $(stat --p=%g: $d_files) = "$g2:$g1:" || fail=1
49
50rm -f f
51touch f
52ln -s f symlink
53chgrp $g1 f
54test $(stat --printf=%g f) = $g1 || fail=1
55
56# This should not change the group of f.
57chgrp -h $g2 symlink
58test $(stat --printf=%g f) = $g1 || fail=1
59
60# Don't fail if chgrp failed to set the group of a symlink.
61# Some systems don't support that.
62test $(stat --printf=%g symlink) = $g2 ||
63  echo 'info: failed to set group of symlink' 1>&2
64
65chown --from=:$g1 :$g2 f; test $(stat --printf=%g f) = $g2 || fail=1
66
67# This *should* change the group of f.
68# Though note that the diagnostic is misleading in that
69# it says the 'group of 'symlink'' has been changed.
70chgrp $g1 symlink; test $(stat --printf=%g f) = $g1 || fail=1
71chown --from=:$g1 :$g2 f; test $(stat --printf=%g f) = $g2 || fail=1
72
73# If -R is specified without -H or L, -h is assumed.
74chgrp -h $g1 f symlink; test $(stat --printf=%g symlink) = $g1 || fail=1
75chgrp -R $g2 symlink
76chown --from=:$g1 :$g2 f; test $(stat --printf=%g f) = $g2 || fail=1
77
78# Make sure we can change the group of inaccessible files.
79chmod a-r f
80chown --from=:$g2 :$g1 f; test $(stat --printf=%g f) = $g1 || fail=1
81chmod 0 f
82chown --from=:$g1 :$g2 f; test $(stat --printf=%g f) = $g2 || fail=1
83
84# chown() must not be optimized away even when
85# the file's owner and group already have the desired value.
86rm -f f g
87touch f g
88chgrp $g1 f g
89chgrp $g2 g
90sleep 1
91chgrp $g1 f
92
93# The following no-change chgrp command is supposed to update f's ctime,
94# but on OpenBSD and Darwin 7.9.0-8.11.1 (aka MacOS X 10.3.9 - 10.4.11)
95# it appears to be a no-op for some file system types (at least NFS) so g's
96# ctime is more recent.  This is not a big deal;
97# this test works fine when the files are on a local file system (/tmp).
98chgrp '' f
99test "$(ls -C -c -t f g)" = 'f  g' || \
100  {
101    case $host_triplet in
102      *openbsd*) echo ignoring known OpenBSD-specific chgrp failure 1>&2 ;;
103      *darwin7.9.*|*darwin8.*)
104        echo ignoring known MacOS X-specific chgrp failure 1>&2 ;;
105      *) echo $host_triplet: no-change chgrp failed to update ctime 1>&2;
106            fail=1 ;;
107    esac
108  }
109
110Exit $fail
111