tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / cp / perm
1 #!/bin/sh
2 # Make sure the permission-preserving code in copy.c (mv, cp, install) works.
3
4 # Copyright (C) 2000, 2002, 2004-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   cp --version
22   mv --version
23 fi
24
25 . $srcdir/test-lib.sh
26 very_expensive_
27
28 umask 037
29
30
31 # Now, try it with `mv', with combinations of --force, no-f and
32 # existing-destination and not.
33 for u in 31 37 2; do
34   echo umask: $u
35   umask $u
36   for cmd in mv 'cp -p' cp; do
37     for force in '' -f; do
38       for existing_dest in yes no; do
39         for g_perm in r w x rw wx xr rwx; do
40           for o_perm in r w x rw wx xr rwx; do
41             touch src || exit 1
42             chmod u=r,g=rx,o= src || exit 1
43             expected_perms=$(stat --format=%A src)
44             rm -f dest
45             test $existing_dest = yes && {
46               touch dest || exit 1
47               chmod u=rw,g=$g_perm,o=$o_perm dest || exit 1
48               }
49             $cmd $force src dest || exit 1
50             test "$cmd" = mv && test -f src && exit 1
51             test "$cmd" = cp && { test -f src || exit 1; }
52             actual_perms=$(stat --format=%A dest)
53
54             case "$cmd:$force:$existing_dest" in
55               cp:*:yes)
56                 _g_perm=`echo rwx|sed 's/[^'$g_perm']/-/g'`
57                 _o_perm=`echo rwx|sed 's/[^'$o_perm']/-/g'`
58                 expected_perms=-rw-$_g_perm$_o_perm
59                 ;;
60               cp:*:no)
61                 test $u = 37 &&
62                   expected_perms=`echo $expected_perms|sed 's/.....$/-----/'`
63                 test $u = 31 &&
64                   expected_perms=`echo $expected_perms|sed 's/..\(..\).$/--\1-/'`
65                 ;;
66             esac
67             test _$actual_perms = _$expected_perms || exit 1
68             # Perform only one iteration when there's no existing destination.
69             test $existing_dest = no && break 3
70           done
71         done
72       done
73     done
74   done
75 done
76
77 Exit $fail