fstests: add a filter for the new getcap output
[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 _require_scratch
27 _require_command "$SETCAP_PROG" setcap
28 _require_command "$GETCAP_PROG" getcap
29
30 FS1="$SCRATCH_MNT/fs1"
31 FS2="$SCRATCH_MNT/fs2"
32
33 cleanup()
34 {
35         cd /
36         rm -f $tmp.*
37 }
38
39 check_capabilities()
40 {
41         local file
42         local cap
43         local ret
44         file="$1"
45         cap="$2"
46         ret=$(_getcap "$file")
47         if [ -z "$ret" ]; then
48                 echo "$ret"
49                 echo "missing capability in file $file"
50         fi
51         if [[ "$ret" != *$cap* ]]; then
52                 echo "$cap"
53                 echo "Capabilities do not match. Output: $ret"
54         fi
55 }
56
57 setup()
58 {
59         _scratch_mkfs >/dev/null
60         _scratch_mount
61
62         $BTRFS_UTIL_PROG subvolume create "$FS1" > /dev/null
63         $BTRFS_UTIL_PROG subvolume create "$FS2" > /dev/null
64 }
65
66 full_nocap_inc_withcap_send()
67 {
68         local ret
69
70         setup
71
72         # Test full send containing a file without capabilities
73         touch "$FS1/foo.bar"
74         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_init" >/dev/null
75         $BTRFS_UTIL_PROG send "$FS1/snap_init" -q | $BTRFS_UTIL_PROG receive "$FS2" -q
76         # ensure that we don't have capabilities set
77         ret=$(_getcap "$FS2/snap_init/foo.bar")
78         if [ -n "$ret" ]; then
79                 echo "File contains capabilities when it shouldn't"
80         fi
81
82         # Test if incremental send brings the newly added capability
83         $SETCAP_PROG "cap_sys_ptrace+ep cap_sys_nice+ep" "$FS1/foo.bar"
84         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_inc" >/dev/null
85         $BTRFS_UTIL_PROG send -p "$FS1/snap_init" "$FS1/snap_inc" -q | \
86                                         $BTRFS_UTIL_PROG receive "$FS2" -q
87         check_capabilities "$FS2/snap_inc/foo.bar" "cap_sys_ptrace,cap_sys_nice=ep"
88
89         _scratch_unmount
90 }
91
92 roundtrip_send()
93 {
94         local files
95
96         # files should include foo.bar
97         files="$1"
98
99         setup
100
101         # create files on fs1, must contain foo.bar
102         for f in $files; do
103                 touch "$FS1/$f"
104         done
105
106         # Test full send, checking if the receiving side keeps the capabilities
107         $SETCAP_PROG "cap_sys_ptrace+ep cap_sys_nice+ep" "$FS1/foo.bar"
108         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_init" >/dev/null
109         $BTRFS_UTIL_PROG send "$FS1/snap_init" -q | $BTRFS_UTIL_PROG receive "$FS2" -q
110         check_capabilities "$FS2/snap_init/foo.bar" "cap_sys_ptrace,cap_sys_nice=ep"
111
112         # Test incremental send with different owner/group but same capabilities
113         chgrp 100 "$FS1/foo.bar"
114         $SETCAP_PROG "cap_sys_ptrace+ep cap_sys_nice+ep" "$FS1/foo.bar"
115         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_inc" >/dev/null
116         check_capabilities "$FS1/snap_inc/foo.bar" "cap_sys_ptrace,cap_sys_nice=ep"
117         $BTRFS_UTIL_PROG send -p "$FS1/snap_init" "$FS1/snap_inc" -q | \
118                                 $BTRFS_UTIL_PROG receive "$FS2" -q
119         check_capabilities "$FS2/snap_inc/foo.bar" "cap_sys_ptrace,cap_sys_nice=ep"
120
121         # Test capabilities after incremental send with different group and capabilities
122         chgrp 0 "$FS1/foo.bar"
123         $SETCAP_PROG "cap_sys_time+ep cap_syslog+ep" "$FS1/foo.bar"
124         $BTRFS_UTIL_PROG subvolume snapshot -r "$FS1" "$FS1/snap_inc2" >/dev/null
125         check_capabilities "$FS1/snap_inc2/foo.bar" "cap_sys_time,cap_syslog=ep"
126         $BTRFS_UTIL_PROG send -p "$FS1/snap_inc" "$FS1/snap_inc2" -q | \
127                                 $BTRFS_UTIL_PROG receive "$FS2"  -q
128         check_capabilities "$FS2/snap_inc2/foo.bar" "cap_sys_time,cap_syslog=ep"
129
130         _scratch_unmount
131 }
132
133 # real QA test starts here
134
135 echo "Test full send + file without capabilities, then incremental send bringing a new capability"
136 full_nocap_inc_withcap_send
137
138 echo "Testing if foo.bar alone can keep its capabilities"
139 roundtrip_send "foo.bar"
140
141 echo "Test foo.bar being the first item among other files"
142 roundtrip_send "foo.bar foo.bax foo.baz"
143
144 echo "Test foo.bar with objectid between two other files"
145 roundtrip_send "foo1 foo.bar foo3"
146
147 echo "Test foo.bar being the last item among other files"
148 roundtrip_send "foo1 foo2 foo.bar"
149
150 status=0
151 exit