btrfs: test if the capability is kept on incremental send
[xfstests-dev.git] / tests / btrfs / 214
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2020 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test 214
6 #
7 # Test if the file capabilities aren't lost after full and incremental send
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "cleanup; exit \$status" 0 1 2 3 15
17
18 # get standard environment, filters and checks
19 . ./common/rc
20 . ./common/filter
21
22 # remove previous $seqres.full before test
23 rm -f $seqres.full
24
25 _supported_fs btrfs
26 _supported_os Linux
27 _require_scratch
28 _require_command "$SETCAP_PROG" setcap
29 _require_command "$GETCAP_PROG" getcap
30
31 FS1="$SCRATCH_MNT/fs1"
32 FS2="$SCRATCH_MNT/fs2"
33
34 cleanup()
35 {
36         cd /
37         rm -f $tmp.*
38 }
39
40 check_capabilities()
41 {
42         local file
43         local cap
44         local ret
45         file="$1"
46         cap="$2"
47         ret=$($GETCAP_PROG "$file")
48         if [ -z "$ret" ]; then
49                 echo "$ret"
50                 echo "missing capability in file $file"
51         fi
52         if [[ "$ret" != *$cap* ]]; then
53                 echo "$cap"
54                 echo "Capabilities do not match. Output: $ret"
55         fi
56 }
57
58 setup()
59 {
60         _scratch_mkfs >/dev/null
61         _scratch_mount
62
63         $BTRFS_UTIL_PROG subvolume create "$FS1" > /dev/null
64         $BTRFS_UTIL_PROG subvolume create "$FS2" > /dev/null
65 }
66
67 full_nocap_inc_withcap_send()
68 {
69         local ret
70
71         setup
72
73         # Test full send containing a file without capabilities
74         touch "$FS1/foo.bar"
75         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_init" >/dev/null
76         $BTRFS_UTIL_PROG send "$FS1/snap_init" -q | $BTRFS_UTIL_PROG receive "$FS2" -q
77         # ensure that we don't have capabilities set
78         ret=$($GETCAP_PROG "$FS2/snap_init/foo.bar")
79         if [ -n "$ret" ]; then
80                 echo "File contains capabilities when it shouldn't"
81         fi
82
83         # Test if incremental send brings the newly added capability
84         $SETCAP_PROG "cap_sys_ptrace+ep cap_sys_nice+ep" "$FS1/foo.bar"
85         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_inc" >/dev/null
86         $BTRFS_UTIL_PROG send -p "$FS1/snap_init" "$FS1/snap_inc" -q | \
87                                         $BTRFS_UTIL_PROG receive "$FS2" -q
88         check_capabilities "$FS2/snap_inc/foo.bar" "cap_sys_ptrace,cap_sys_nice+ep"
89
90         _scratch_unmount
91 }
92
93 roundtrip_send()
94 {
95         local files
96
97         # files should include foo.bar
98         files="$1"
99
100         setup
101
102         # create files on fs1, must contain foo.bar
103         for f in $files; do
104                 touch "$FS1/$f"
105         done
106
107         # Test full send, checking if the receiving side keeps the capabilities
108         $SETCAP_PROG "cap_sys_ptrace+ep cap_sys_nice+ep" "$FS1/foo.bar"
109         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_init" >/dev/null
110         $BTRFS_UTIL_PROG send "$FS1/snap_init" -q | $BTRFS_UTIL_PROG receive "$FS2" -q
111         check_capabilities "$FS2/snap_init/foo.bar" "cap_sys_ptrace,cap_sys_nice+ep"
112
113         # Test incremental send with different owner/group but same capabilities
114         chgrp 100 "$FS1/foo.bar"
115         $SETCAP_PROG "cap_sys_ptrace+ep cap_sys_nice+ep" "$FS1/foo.bar"
116         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_inc" >/dev/null
117         check_capabilities "$FS1/snap_inc/foo.bar" "cap_sys_ptrace,cap_sys_nice+ep"
118         $BTRFS_UTIL_PROG send -p "$FS1/snap_init" "$FS1/snap_inc" -q | \
119                                 $BTRFS_UTIL_PROG receive "$FS2" -q
120         check_capabilities "$FS2/snap_inc/foo.bar" "cap_sys_ptrace,cap_sys_nice+ep"
121
122         # Test capabilities after incremental send with different group and capabilities
123         chgrp 0 "$FS1/foo.bar"
124         $SETCAP_PROG "cap_sys_time+ep cap_syslog+ep" "$FS1/foo.bar"
125         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_inc2" >/dev/null
126         check_capabilities "$FS1/snap_inc2/foo.bar" "cap_sys_time,cap_syslog+ep"
127         $BTRFS_UTIL_PROG send -p "$FS1/snap_inc" "$FS1/snap_inc2" -q | \
128                                 $BTRFS_UTIL_PROG receive "$FS2"  -q
129         check_capabilities "$FS2/snap_inc2/foo.bar" "cap_sys_time,cap_syslog+ep"
130
131         _scratch_unmount
132 }
133
134 # real QA test starts here
135
136 echo "Test full send + file without capabilities, then incremental send bringing a new capability"
137 full_nocap_inc_withcap_send
138
139 echo "Testing if foo.bar alone can keep its capabilities"
140 roundtrip_send "foo.bar"
141
142 echo "Test foo.bar being the first item among other files"
143 roundtrip_send "foo.bar foo.bax foo.baz"
144
145 echo "Test foo.bar with objectid between two other files"
146 roundtrip_send "foo1 foo.bar foo3"
147
148 echo "Test foo.bar being the last item among other files"
149 roundtrip_send "foo1 foo2 foo.bar"
150
151 status=0
152 exit