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