d4971194c1dfdce2accf70e02f6616313fd461b4
[platform/upstream/coreutils.git] / tests / chgrp / basic
1 #!/bin/sh
2 # make sure chgrp is reasonable
3
4 if test "$VERBOSE" = yes; then
5   set -x
6   chgrp --version
7 fi
8
9 . $srcdir/../envvar-check
10 . $srcdir/../lang-default
11 . $srcdir/../group-names
12
13 pwd=`pwd`
14 tmp=basic.$$
15 trap 'status=$?; cd $pwd; rm -rf $tmp && exit $status' 0
16 trap '(exit $?); exit' 1 2 13 15
17
18 framework_failure=0
19 mkdir $tmp || framework_failure=1
20 cd $tmp || framework_failure=1
21
22 if test $framework_failure = 1; then
23   echo 'failure in testing framework' 1>&2
24   (exit 1); exit 1
25 fi
26
27 fail=0
28
29 set _ $groups; shift
30 g1=$1
31 g2=$2
32 mkdir d
33 touch f f2 d/f3
34 chgrp $g1 f || fail=1
35 chgrp $g2 f || fail=1
36 chgrp $g2 f2 || fail=1
37 chgrp -R $g1 d || fail=1
38
39 # Don't let verbose output interfere.
40 test "$VERBOSE" = yes && set +x
41
42 (
43   chgrp -c $g1 f
44   chgrp -c $g2 f
45   chgrp -c $g2 f
46   chgrp --verbose '' f
47   chgrp --verbose $g1 f
48   chgrp --verbose $g1 f
49   chgrp --verbose --reference=f2 f
50   chgrp -R --verbose $g2 d
51   chgrp -R --verbose $g1 d
52   chgrp -R -c $g2 d
53   chgrp -R -c $g1 d
54   chgrp -c $g2 d
55
56   rm -f f
57   touch f
58   ln -s f symlink
59   chgrp $g1 f
60   chgrp -h $g2 symlink
61
62   # This should not change the group of f.
63   chgrp -h -c $g2 symlink
64   chown --from=:$g1 -c :$g2 f
65
66   # This *should* change the group of f.
67   # Though note that the diagnostic is misleading in that
68   # it says the `group of `symlink'' has been changed.
69   chgrp -c $g1 symlink
70   chown --from=:$g1 -c :$g2 f
71
72   # If -R is specified without -H or L, -h is assumed.
73   chgrp -h $g1 f symlink
74   chgrp -R $g2 symlink
75   chown --from=:$g1 -c :$g2 f
76
77   # Make sure we can change the group of inaccessible files.
78   chmod a-r f
79   chown --from=:$g2 -c :$g1 f
80   chmod 0 f
81   chown --from=:$g1 -c :$g2 f
82
83   # chown() must not be optimized away even when
84   # the file's owner and group already have the desired value.
85   rm -f f g
86   touch f g
87   chgrp $g1 f g
88   chgrp $g2 g
89   sleep 1
90   chgrp $g1 f
91
92   # The following no-change chgrp command is supposed to update f's ctime,
93   # but on OpenBSD, it appears to be a no-op for some file system types
94   # (at least NFS) so g's ctime is more recent.  This is not a big deal;
95   # this test works fine when the files are on a local file system (/tmp).
96   chgrp '' f
97   ls -c -t f g
98
99 ) 2>&1 | sed "
100   s/' to .*[^0-9:].*/' to SOMENAME/
101   s/\([ :]\)$g1$/\1G1/
102   s/\([ :]\)$g2$/\1G2/
103 " > actual
104
105 cat <<\EOF > expected
106 changed group of `f' to G1
107 changed group of `f' to G2
108 ownership of `f' retained
109 changed group of `f' to G1
110 group of `f' retained as G1
111 changed group of `f' to SOMENAME
112 changed group of `d/f3' to G2
113 changed group of `d' to G2
114 changed group of `d/f3' to G1
115 changed group of `d' to G1
116 changed group of `d/f3' to G2
117 changed group of `d' to G2
118 changed group of `d/f3' to G1
119 changed group of `d' to G1
120 changed group of `d' to G2
121 changed ownership of `f' to :G2
122 changed group of `symlink' to G1
123 changed ownership of `f' to :G2
124 changed ownership of `f' to :G2
125 changed ownership of `f' to :G1
126 changed ownership of `f' to :G2
127 f
128 g
129 EOF
130
131 cmp expected actual \
132   || { diff expected actual 1>&2; fail=1; }
133
134 (exit $fail); exit $fail