Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / chgrp / basic
1 #!/bin/sh
2 # make sure chgrp is reasonable
3
4 # Copyright (C) 2000-2007 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 2 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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   chgrp --version
24 fi
25
26 . $srcdir/../envvar-check
27 . $srcdir/../lang-default
28 . $srcdir/../group-names
29
30 pwd=`pwd`
31 tmp=basic.$$
32 trap 'status=$?; cd "$pwd" && rm -rf $tmp && exit $status' 0
33 trap '(exit $?); exit' 1 2 13 15
34
35 framework_failure=0
36 mkdir $tmp || framework_failure=1
37 cd $tmp || framework_failure=1
38
39 if test $framework_failure = 1; then
40   echo 'failure in testing framework' 1>&2
41   (exit 1); exit 1
42 fi
43
44 fail=0
45
46 set _ $groups; shift
47 g1=$1
48 g2=$2
49 mkdir d
50 touch f f2 d/f3
51 chgrp $g1 f || fail=1
52 chgrp $g2 f || fail=1
53 chgrp $g2 f2 || fail=1
54 chgrp -R $g1 d || fail=1
55
56 d_files='d d/f3'
57
58 chgrp $g1 f || fail=1  ; test `stat --p=%g f` = $g1 || fail=1
59 chgrp $g2 f || fail=1  ; test `stat --p=%g f` = $g2 || fail=1
60 chgrp $g2 f || fail=1  ; test `stat --p=%g f` = $g2 || fail=1
61 chgrp '' f  || fail=1  ; test `stat --p=%g f` = $g2 || fail=1
62 chgrp $g1 f || fail=1  ; test `stat --p=%g f` = $g1 || fail=1
63 chgrp $g1 f || fail=1  ; test `stat --p=%g f` = $g1 || fail=1
64 chgrp --reference=f2 f ; test `stat --p=%g f` = $g2 || fail=1
65
66 chgrp -R $g2 d ||fail=1; test `stat --p=%g: $d_files` = "$g2:$g2:" || fail=1
67 chgrp -R $g1 d ||fail=1; test `stat --p=%g: $d_files` = "$g1:$g1:" || fail=1
68 chgrp -R $g2 d ||fail=1; test `stat --p=%g: $d_files` = "$g2:$g2:" || fail=1
69 chgrp -R $g1 d ||fail=1; test `stat --p=%g: $d_files` = "$g1:$g1:" || fail=1
70 chgrp $g2 d    ||fail=1; test `stat --p=%g: $d_files` = "$g2:$g1:" || fail=1
71
72 rm -f f
73 touch f
74 ln -s f symlink
75 chgrp $g1 f
76 test `stat --printf=%g f` = $g1 || fail=1
77
78 # This should not change the group of f.
79 chgrp -h $g2 symlink
80 test `stat --printf=%g f` = $g1 || fail=1
81
82 # Don't fail if chgrp failed to set the group of a symlink.
83 # Some systems don't support that.
84 test `stat --printf=%g symlink` = $g2 ||
85   echo 'info: failed to set group of symlink' 1>&2
86
87 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
88
89 # This *should* change the group of f.
90 # Though note that the diagnostic is misleading in that
91 # it says the `group of `symlink'' has been changed.
92 chgrp $g1 symlink; test `stat --printf=%g f` = $g1 || fail=1
93 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
94
95 # If -R is specified without -H or L, -h is assumed.
96 chgrp -h $g1 f symlink; test `stat --printf=%g symlink` = $g1 || fail=1
97 chgrp -R $g2 symlink
98 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
99
100 # Make sure we can change the group of inaccessible files.
101 chmod a-r f
102 chown --from=:$g2 :$g1 f; test `stat --printf=%g f` = $g1 || fail=1
103 chmod 0 f
104 chown --from=:$g1 :$g2 f; test `stat --printf=%g f` = $g2 || fail=1
105
106 # chown() must not be optimized away even when
107 # the file's owner and group already have the desired value.
108 rm -f f g
109 touch f g
110 chgrp $g1 f g
111 chgrp $g2 g
112 sleep 1
113 chgrp $g1 f
114
115 # The following no-change chgrp command is supposed to update f's ctime,
116 # but on OpenBSD and Darwin 7.9.0 and 8.8.0 (aka MacOS X 10.3.9 and 10.4),
117 # it appears to be a no-op for some file system types (at least NFS) so g's
118 # ctime is more recent.  This is not a big deal;
119 # this test works fine when the files are on a local file system (/tmp).
120 chgrp '' f
121 test "`ls -C -c -t f g`" = 'f  g' || \
122   {
123     case $host_triplet in
124       *openbsd*) echo ignoring known OpenBSD-specific chgrp failure 1>&2 ;;
125       *darwin7.9.*|*darwin8.8.*)
126         echo ignoring known MacOS X-specific chgrp failure 1>&2 ;;
127       *) echo $host_triplet: no-change chgrp failed to update ctime 1>&2;
128             fail=1 ;;
129     esac
130   }
131
132 (exit $fail); exit $fail