tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / chgrp / basic
1 #!/bin/sh
2 # make sure chgrp is reasonable
3
4 # Copyright (C) 2000-2009 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 <http://www.gnu.org/licenses/>.
18
19 if test "$VERBOSE" = yes; then
20   set -x
21   chgrp --version
22 fi
23
24 . $srcdir/test-lib.sh
25 require_membership_in_two_groups_
26
27
28 set _ $groups; shift
29 g1=$1
30 g2=$2
31 mkdir d
32 touch f f2 d/f3
33 chgrp $g1 f || fail=1
34 chgrp $g2 f || fail=1
35 chgrp $g2 f2 || fail=1
36 chgrp -R $g1 d || fail=1
37
38 d_files='d d/f3'
39
40 chgrp $g1 f || fail=1  ; test `stat --p=%g f` = $g1 || fail=1
41 chgrp $g2 f || fail=1  ; test `stat --p=%g f` = $g2 || fail=1
42 chgrp $g2 f || fail=1  ; test `stat --p=%g f` = $g2 || fail=1
43 chgrp '' f  || fail=1  ; test `stat --p=%g f` = $g2 || fail=1
44 chgrp $g1 f || fail=1  ; test `stat --p=%g f` = $g1 || fail=1
45 chgrp $g1 f || fail=1  ; test `stat --p=%g f` = $g1 || fail=1
46 chgrp --reference=f2 f ; test `stat --p=%g f` = $g2 || fail=1
47
48 chgrp -R $g2 d ||fail=1; test `stat --p=%g: $d_files` = "$g2:$g2:" || fail=1
49 chgrp -R $g1 d ||fail=1; test `stat --p=%g: $d_files` = "$g1:$g1:" || fail=1
50 chgrp -R $g2 d ||fail=1; test `stat --p=%g: $d_files` = "$g2:$g2:" || fail=1
51 chgrp -R $g1 d ||fail=1; test `stat --p=%g: $d_files` = "$g1:$g1:" || fail=1
52 chgrp $g2 d    ||fail=1; test `stat --p=%g: $d_files` = "$g2:$g1:" || fail=1
53
54 rm -f f
55 touch f
56 ln -s f symlink
57 chgrp $g1 f
58 test `stat --printf=%g f` = $g1 || fail=1
59
60 # This should not change the group of f.
61 chgrp -h $g2 symlink
62 test `stat --printf=%g f` = $g1 || fail=1
63
64 # Don't fail if chgrp failed to set the group of a symlink.
65 # Some systems don't support that.
66 test `stat --printf=%g symlink` = $g2 ||
67   echo 'info: failed to set group of symlink' 1>&2
68
69 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
70
71 # This *should* change the group of f.
72 # Though note that the diagnostic is misleading in that
73 # it says the `group of `symlink'' has been changed.
74 chgrp $g1 symlink; test `stat --printf=%g f` = $g1 || fail=1
75 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
76
77 # If -R is specified without -H or L, -h is assumed.
78 chgrp -h $g1 f symlink; test `stat --printf=%g symlink` = $g1 || fail=1
79 chgrp -R $g2 symlink
80 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
81
82 # Make sure we can change the group of inaccessible files.
83 chmod a-r f
84 chown --from=:$g2 :$g1 f; test `stat --printf=%g f` = $g1 || fail=1
85 chmod 0 f
86 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
87
88 # chown() must not be optimized away even when
89 # the file's owner and group already have the desired value.
90 rm -f f g
91 touch f g
92 chgrp $g1 f g
93 chgrp $g2 g
94 sleep 1
95 chgrp $g1 f
96
97 # The following no-change chgrp command is supposed to update f's ctime,
98 # but on OpenBSD and Darwin 7.9.0-8.11.1 (aka MacOS X 10.3.9 - 10.4.11)
99 # it appears to be a no-op for some file system types (at least NFS) so g's
100 # ctime is more recent.  This is not a big deal;
101 # this test works fine when the files are on a local file system (/tmp).
102 chgrp '' f
103 test "`ls -C -c -t f g`" = 'f  g' || \
104   {
105     case $host_triplet in
106       *openbsd*) echo ignoring known OpenBSD-specific chgrp failure 1>&2 ;;
107       *darwin7.9.*|*darwin8.*)
108         echo ignoring known MacOS X-specific chgrp failure 1>&2 ;;
109       *) echo $host_triplet: no-change chgrp failed to update ctime 1>&2;
110             fail=1 ;;
111     esac
112   }
113
114 Exit $fail