tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / misc / xattr
1 #!/bin/sh
2 # Ensure that cp --preserve=xattr, cp --preserve=all and mv preserve extended
3 # attributes and install does not preserve extended attributes.
4 # cp -a should preserve xattr, error diagnostics should not be displayed
5
6 # Copyright (C) 2009 Free Software Foundation, Inc.
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   cp --version
24   mv --version
25   ginstall --version
26 fi
27
28 . $srcdir/test-lib.sh
29
30 # Skip this test if cp was built without xattr support:
31 touch src dest || framework_failure
32 cp --preserve=xattr -n src dest \
33   || skip_test_ "coreutils built without xattr support"
34
35 # this code was taken from test mv/backup-is-src
36 cleanup_() { rm -rf "$other_partition_tmpdir"; }
37 . "$abs_srcdir/other-fs-tmpdir"
38 b_other="$other_partition_tmpdir/b"
39 rm -f "$b_other" || framework_failure
40
41 # testing xattr name-value pair
42 xattr_name="user.foo"
43 xattr_value="bar"
44 xattr_pair="$xattr_name=\"$xattr_value\""
45
46 # create new file and check its xattrs
47 touch a || framework_failure
48 getfattr -d a >out_a || skip_test_ "failed to get xattr of file"
49 grep -F "$xattr_pair" out_a && framework_failure
50
51 # try to set user xattr on file
52 setfattr -n "$xattr_name" -v "$xattr_value" a >out_a \
53   || skip_test_ "failed to set xattr of file"
54 getfattr -d a >out_a || skip_test_ "failed to get xattr of file"
55 grep -F "$xattr_pair" out_a \
56   || skip_test_ "failed to set xattr of file"
57
58
59 # cp should not preserve xattr by default
60 cp a b || fail=1
61 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
62 grep -F "$xattr_pair" out_b && fail=1
63
64 # test if --preserve=xattr option works
65 cp --preserve=xattr a b || fail=1
66 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
67 grep -F "$xattr_pair" out_b || fail=1
68
69 #test if --preserve=all option works
70 cp --preserve=all a c || fail=1
71 getfattr -d c >out_c || skip_test_ "failed to get xattr of file"
72 grep -F "$xattr_pair" out_c || fail=1
73
74 #test if -a option works without any diagnostics
75 cp -a a d 2>err && test -s err && fail=1
76 getfattr -d d >out_d || skip_test_ "failed to get xattr of file"
77 grep -F "$xattr_pair" out_d || fail=1
78
79 #test if --preserve=xattr works even for files without write access
80 chmod a-w a || framework_failure
81 rm -f e
82 cp --preserve=xattr a e || fail=1
83 getfattr -d e >out_e || skip_test_ "failed to get xattr of file"
84 grep -F "$xattr_pair" out_e || fail=1
85
86 #Ensure that permission bits are preserved, too.
87 src_perm=$(stat --format=%a a)
88 dst_perm=$(stat --format=%a e)
89 test "$dst_perm" = "$src_perm" || fail=1
90
91 chmod u+w a || framework_failure
92
93 rm b || framework_failure
94
95 # install should never preserve xattr
96 ginstall a b || fail=1
97 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
98 grep -F "$xattr_pair" out_b && fail=1
99
100 # mv should preserve xattr when renaming within a file system.
101 # This is implicitly done by rename () and doesn't need explicit
102 # xattr support in mv.
103 mv a b || fail=1
104 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
105 grep -F "$xattr_pair" out_b || cat >&2 <<EOF
106 =================================================================
107 $0: WARNING!!!
108 rename () does not preserve extended attributes
109 =================================================================
110 EOF
111
112 # try to set user xattr on file on other partition
113 test_mv=1
114 touch "$b_other" || framework_failure
115 setfattr -n "$xattr_name" -v "$xattr_value" "$b_other" >out_a \
116   || test_mv=0
117 getfattr -d "$b_other" >out_b || test_mv=0
118 grep -F "$xattr_pair" out_b || test_mv=0
119 rm -f "$b_other" || framework_failure
120
121 if test $test_mv -eq 1; then
122   # mv should preserve xattr when copying content from one partition to another
123   mv b "$b_other" || fail=1
124   getfattr -d "$b_other" >out_b ||
125     skip_test_ "failed to get xattr of file"
126   grep -F "$xattr_pair" out_b || fail=1
127 else
128   cat >&2 <<EOF
129 =================================================================
130 $0: WARNING!!!
131 failed to set xattr of file $b_other
132 =================================================================
133 EOF
134 fi
135
136 Exit $fail