tests: remove skip_test_ function; use new skip_ instead
[coreutils/ericb.git] / tests / cp / preserve-gid
blob6893ba2b962b9cb1a200eff1ac4d6c7db00ce3d6
1 #!/bin/sh
2 # Verify that cp -p preserves GID when it is possible.
4 # Copyright (C) 2007-2011 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=.}/init.sh"; path_prepend_ ../src
20 print_ver_ cp
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 nameless_uid=`$PERL -le '
56 foreach my $i (1000..16*1024-1)
58 getpwuid $i or (print $i), exit
61 nameless_gid1=`$PERL -le '
62 foreach my $i (1000..16*1024)
64 getgrgid $i or (print $i), exit
67 nameless_gid2=`$PERL -le '
68 foreach my $i ('"$nameless_gid1"'+1..16*1024)
70 getgrgid $i or (print $i), exit
74 if test -z "$nameless_uid" \
75 || test -z "$nameless_gid1" \
76 || test -z "$nameless_gid2"; then
77 skip_ "couldn't find a nameless UID or GID"
80 chown "+$nameless_uid:+0" .
82 create a0 0 0
83 create b0 "$nameless_uid" "$nameless_gid1"
84 create b1 "$nameless_uid" "$nameless_gid2"
85 create c0 0 "$nameless_gid1"
86 create c1 0 "$nameless_gid2"
88 t0 a0 0 0 cp
89 t0 b0 0 0 cp
90 t0 b1 0 0 cp
91 t0 c0 0 0 cp
92 t0 c1 0 0 cp
94 t0 a0 0 0 cp -p
95 t0 b0 "$nameless_uid" "$nameless_gid1" cp -p
96 t0 b1 "$nameless_uid" "$nameless_gid2" cp -p
97 t0 c0 0 "$nameless_gid1" cp -p
98 t0 c1 0 "$nameless_gid2" cp -p
100 # For the remaining tests, we need a cp binary that is accessible to a user
101 # with UID of $nameless_uid. The build directory may not be accessible,
102 # so create a temporary directory and copy cp into it, ensure that
103 # $nameless_uid can access it and then make that directory the search path.
104 tmp_path=
105 cleanup_() { rm -rf "$tmp_path"; }
107 # Cause mktemp to create a directory directly under /tmp.
108 # Setting TMPDIR explicitly is required here, in case $TMPDIR
109 # is not readable by our nameless IDs.
110 test -d /tmp && TMPDIR=/tmp
111 tmp_path=$(mktemp -d) || fail_ "failed to create temporary directory"
112 cp "$abs_path_dir_/cp" "$tmp_path"
113 chmod -R a+rx "$tmp_path"
115 t1() {
116 f=$1; shift
117 u=$1; shift
118 g=$1; shift
119 t0 "$f" "$u" "$g" \
120 setuidgid -g "$nameless_gid1,$nameless_gid2" \
121 "$nameless_uid" env PATH="$tmp_path" "$@"
124 t1 a0 "$nameless_uid" "$nameless_gid1" cp
125 t1 b0 "$nameless_uid" "$nameless_gid1" cp
126 t1 b1 "$nameless_uid" "$nameless_gid1" cp
127 t1 c0 "$nameless_uid" "$nameless_gid1" cp
128 t1 c1 "$nameless_uid" "$nameless_gid1" cp
130 t1 a0 "$nameless_uid" "$nameless_gid1" cp -p
131 t1 b0 "$nameless_uid" "$nameless_gid1" cp -p
132 t1 b1 "$nameless_uid" "$nameless_gid2" cp -p
133 t1 c0 "$nameless_uid" "$nameless_gid1" cp -p
134 t1 c1 "$nameless_uid" "$nameless_gid2" cp -p
136 Exit $fail