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