9691045256f1486e8c32d0b69e991229cff48033
[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 . $srcdir/../very-expensive
5
6 if test "$VERBOSE" = yes; then
7   set -x
8   cp --version
9   mv --version
10 fi
11
12 . $srcdir/../envvar-check
13
14 pwd=`pwd`
15 t0=`echo "$0"|sed 's,.*/,,'`.tmp;tmp=$t0/$$
16 trap 'status=$?; cd $pwd; rm -rf $t0 && exit $status' 0
17 trap '(exit $?); exit' 1 2 13 15
18
19 framework_failure=0
20 mkdir -p $tmp || framework_failure=1
21 cd $tmp || framework_failure=1
22
23 umask 037
24
25 if test $framework_failure = 1; then
26   echo 'failure in testing framework'
27   exit 1
28 fi
29
30 fail=0
31
32 # Now, try it with `mv', with combinations of --force, no-f and
33 # existing-destination and not.
34 for u in 31 37 2; do
35   echo umask: $u
36   umask $u
37   for cmd in mv 'cp -p' cp; do
38     for force in '' -f; do
39       for existing_dest in yes no; do
40         for g_perm in r w x rw wx xr rwx; do
41           for o_perm in r w x rw wx xr rwx; do
42             touch src || exit 1
43             chmod u=r,g=rx,o= src || exit 1
44             set _ `ls -l src`
45             shift
46             expected_perms=$1
47             rm -f dest
48             test $existing_dest = yes && {
49               touch dest || exit 1
50               chmod u=rw,g=$g_perm,o=$o_perm dest || exit 1
51               }
52             $cmd $force src dest || exit 1
53             test "$cmd" = mv && test -f src && exit 1
54             test "$cmd" = cp && { test -f src || exit 1; }
55             set _ `ls -l dest`
56             shift
57
58             case "$cmd:$force:$existing_dest" in
59               cp:*:yes)
60                 _g_perm=`echo rwx|sed 's/[^'$g_perm']/-/g'`
61                 _o_perm=`echo rwx|sed 's/[^'$o_perm']/-/g'`
62                 expected_perms=-rw-$_g_perm$_o_perm
63                 ;;
64               cp:*:no)
65                 test $u = 37 &&
66                   expected_perms=`echo $expected_perms|sed 's/.....$/-----/'`
67                 test $u = 31 &&
68                   expected_perms=`echo $expected_perms|sed 's/..\(..\).$/--\1-/'`
69                 ;;
70             esac
71             test _$1 = _$expected_perms || exit 1
72             # Perform only one iteration when there's no existing destination.
73             test $existing_dest = no && break 3
74           done
75         done
76       done
77     done
78   done
79 done
80
81 (exit $fail); exit $fail