common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[xfstests-dev.git] / tests / generic / 395
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Google, Inc.  All Rights Reserved.
4 #
5 # FS QA Test generic/395
6 #
7 # Test setting and getting encryption policies.
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 _cleanup()
19 {
20         cd /
21         rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27 . ./common/encrypt
28
29 # remove previous $seqres.full before test
30 rm -f $seqres.full
31
32 # real QA test starts here
33 _supported_fs generic
34 _supported_os Linux
35 _require_scratch_encryption
36 _require_xfs_io_command "get_encpolicy"
37 _require_user
38
39 _scratch_mkfs_encrypted &>> $seqres.full
40 _scratch_mount
41
42 check_no_policy()
43 {
44         # When a file is unencrypted, FS_IOC_GET_ENCRYPTION_POLICY currently
45         # fails with ENOENT on ext4 but with ENODATA on f2fs.  TODO: it's
46         # planned to consistently use ENODATA.  For now this test accepts both.
47         _get_encpolicy $1 |&
48                 sed -e 's/No such file or directory/No data available/'
49 }
50
51 # Should be able to set an encryption policy on an empty directory
52 empty_dir=$SCRATCH_MNT/empty_dir
53 echo -e "\n*** Setting encryption policy on empty directory ***"
54 mkdir $empty_dir
55 check_no_policy $empty_dir |& _filter_scratch
56 _set_encpolicy $empty_dir 0000111122223333
57 _get_encpolicy $empty_dir | _filter_scratch
58
59 # Should be able to set the same policy again, but not a different one.
60 # TODO: the error code for "already has a different policy" is planned to switch
61 # from EINVAL to EEXIST.  For now this test accepts both.
62 echo -e "\n*** Setting encryption policy again ***"
63 _set_encpolicy $empty_dir 0000111122223333
64 _get_encpolicy $empty_dir | _filter_scratch
65 _set_encpolicy $empty_dir 4444555566667777 |& \
66         _filter_scratch | sed -e 's/Invalid argument/File exists/'
67 _get_encpolicy $empty_dir | _filter_scratch
68
69 # Should *not* be able to set an encryption policy on a nonempty directory
70 nonempty_dir=$SCRATCH_MNT/nonempty_dir
71 echo -e "\n*** Setting encryption policy on nonempty directory ***"
72 mkdir $nonempty_dir
73 touch $nonempty_dir/file
74 _set_encpolicy $nonempty_dir |& _filter_scratch
75 check_no_policy $nonempty_dir |& _filter_scratch
76
77 # Should *not* be able to set an encryption policy on a nondirectory file, even
78 # an empty one.  Regression test for 002ced4be642: "fscrypto: only allow setting
79 # encryption policy on directories".
80 # TODO: the error code for "not a directory" is planned to switch from EINVAL to
81 # ENOTDIR.  For now this test accepts both.
82 nondirectory=$SCRATCH_MNT/nondirectory
83 echo -e "\n*** Setting encryption policy on nondirectory ***"
84 touch $nondirectory
85 _set_encpolicy $nondirectory |& \
86         _filter_scratch | sed -e 's/Invalid argument/Not a directory/'
87 check_no_policy $nondirectory |& _filter_scratch
88
89 # Should *not* be able to set an encryption policy on another user's directory.
90 # Regression test for 163ae1c6ad62: "fscrypto: add authorization check for
91 # setting encryption policy".
92 unauthorized_dir=$SCRATCH_MNT/unauthorized_dir
93 echo -e "\n*** Setting encryption policy on another user's directory ***"
94 mkdir $unauthorized_dir
95 _user_do_set_encpolicy $unauthorized_dir |& _filter_scratch
96 check_no_policy $unauthorized_dir |& _filter_scratch
97
98 # Should *not* be able to set an encryption policy on a directory on a
99 # filesystem mounted readonly.  Regression test for ba63f23d69a3: "fscrypto:
100 # require write access to mount to set encryption policy".  Test both a regular
101 # readonly filesystem and a readonly bind mount of a read-write filesystem.
102 echo -e "\n*** Setting encryption policy on readonly filesystem ***"
103 mkdir $SCRATCH_MNT/ro_dir $SCRATCH_MNT/ro_bind_mnt
104 _scratch_remount ro
105 _set_encpolicy $SCRATCH_MNT/ro_dir |& _filter_scratch
106 check_no_policy $SCRATCH_MNT/ro_dir |& _filter_scratch
107 _scratch_remount rw
108 mount --bind $SCRATCH_MNT $SCRATCH_MNT/ro_bind_mnt
109 mount -o remount,ro,bind $SCRATCH_MNT/ro_bind_mnt
110 _set_encpolicy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
111 check_no_policy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
112 umount $SCRATCH_MNT/ro_bind_mnt
113
114 # success, all done
115 status=0
116 exit