tests: convert 'if test "$VERBOSE" = yes; then' to test ... &&
[coreutils/ericb.git] / tests / cp / preserve-gid
bloba5190e4e701cdcba9af021532e17622ff7464762
1 #!/bin/sh
2 # Verify that cp -p preserves GID when it is possible.
4 # Copyright (C) 2007-2010 Free Software Foundation, Inc.
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.
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.
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/>.
19 . $srcdir/test-lib.sh
20 test "$VERBOSE" = yes && cp --version
22 require_root_
24 # Some of the tests expect a umask that grants group and/or world read access.
25 working_umask_or_skip_
27 # Record primary group number, usually 0.
28 # This is the group ID used when cp (without -p) creates a new file.
29 primary_group_num=$(id -g)
31 create() {
32 echo "$1" > "$1" || exit 1
33 chown "+$2:+$3" "$1" || exit 1
36 t0() {
37 f=$1; shift
38 u=$1; shift
39 g=$1; shift
40 rm -f b || exit 1
41 "$@" "$f" b || exit 1
42 s=`stat -c '%u %g' b`
43 if test "x$s" != "x$u $g"; then
44 # Allow the actual group to match that of the parent directory
45 # (it was set to 0 above).
46 if test "x$s" = "x$u $primary_group_num"; then
48 else
49 echo "$0: $* $f b: $u $g != $s" 1>&2
50 Exit 1
55 t1() {
56 f=$1; shift
57 u=$1; shift
58 g=$1; shift
59 t0 "$f" "$u" "$g" setuidgid -g "$nameless_gid1,$nameless_gid2" \
60 "$nameless_uid" "$@"
63 nameless_uid=`$PERL -le 'foreach my $i (1000..16*1024-1) { getpwuid $i or (print $i), exit }'`
64 nameless_gid1=`$PERL -le 'foreach my $i (1000..16*1024) { getgrgid $i or (print $i), exit }'`
65 nameless_gid2=`$PERL -le 'foreach my $i ('"$nameless_gid1"'+1..16*1024) { getgrgid $i or (print $i), exit }'`
67 if test -z "$nameless_uid" \
68 || test -z "$nameless_gid1" \
69 || test -z "$nameless_gid2"; then
70 skip_test_ "couldn't find a nameless UID or GID"
73 chown "+$nameless_uid:+0" .
75 create a0 0 0
76 create b0 "$nameless_uid" "$nameless_gid1"
77 create b1 "$nameless_uid" "$nameless_gid2"
78 create c0 0 "$nameless_gid1"
79 create c1 0 "$nameless_gid2"
81 t0 a0 0 0 cp
82 t0 b0 0 0 cp
83 t0 b1 0 0 cp
84 t0 c0 0 0 cp
85 t0 c1 0 0 cp
87 t0 a0 0 0 cp -p
88 t0 b0 "$nameless_uid" "$nameless_gid1" cp -p
89 t0 b1 "$nameless_uid" "$nameless_gid2" cp -p
90 t0 c0 0 "$nameless_gid1" cp -p
91 t0 c1 0 "$nameless_gid2" cp -p
93 t1 a0 "$nameless_uid" "$nameless_gid1" cp
94 t1 b0 "$nameless_uid" "$nameless_gid1" cp
95 t1 b1 "$nameless_uid" "$nameless_gid1" cp
96 t1 c0 "$nameless_uid" "$nameless_gid1" cp
97 t1 c1 "$nameless_uid" "$nameless_gid1" cp
99 t1 a0 "$nameless_uid" "$nameless_gid1" cp -p
100 t1 b0 "$nameless_uid" "$nameless_gid1" cp -p
101 t1 b1 "$nameless_uid" "$nameless_gid2" cp -p
102 t1 c0 "$nameless_uid" "$nameless_gid1" cp -p
103 t1 c1 "$nameless_uid" "$nameless_gid2" cp -p
105 Exit $fail