common: kill _supported_os
[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 _require_scratch_encryption
35 _require_xfs_io_command "get_encpolicy"
36 _require_user
37
38 _scratch_mkfs_encrypted &>> $seqres.full
39 _scratch_mount
40
41 check_no_policy()
42 {
43         # When a file is unencrypted, FS_IOC_GET_ENCRYPTION_POLICY currently
44         # fails with ENOENT on ext4 but with ENODATA on f2fs.  TODO: it's
45         # planned to consistently use ENODATA.  For now this test accepts both.
46         _get_encpolicy $1 |&
47                 sed -e 's/No such file or directory/No data available/'
48 }
49
50 # Should be able to set an encryption policy on an empty directory
51 empty_dir=$SCRATCH_MNT/empty_dir
52 echo -e "\n*** Setting encryption policy on empty directory ***"
53 mkdir $empty_dir
54 check_no_policy $empty_dir |& _filter_scratch
55 _set_encpolicy $empty_dir 0000111122223333
56 _get_encpolicy $empty_dir | _filter_scratch
57
58 # Should be able to set the same policy again, but not a different one.
59 # TODO: the error code for "already has a different policy" is planned to switch
60 # from EINVAL to EEXIST.  For now this test accepts both.
61 echo -e "\n*** Setting encryption policy again ***"
62 _set_encpolicy $empty_dir 0000111122223333
63 _get_encpolicy $empty_dir | _filter_scratch
64 _set_encpolicy $empty_dir 4444555566667777 |& \
65         _filter_scratch | sed -e 's/Invalid argument/File exists/'
66 _get_encpolicy $empty_dir | _filter_scratch
67
68 # Should *not* be able to set an encryption policy on a nonempty directory
69 nonempty_dir=$SCRATCH_MNT/nonempty_dir
70 echo -e "\n*** Setting encryption policy on nonempty directory ***"
71 mkdir $nonempty_dir
72 touch $nonempty_dir/file
73 _set_encpolicy $nonempty_dir |& _filter_scratch
74 check_no_policy $nonempty_dir |& _filter_scratch
75
76 # Should *not* be able to set an encryption policy on a nondirectory file, even
77 # an empty one.  Regression test for 002ced4be642: "fscrypto: only allow setting
78 # encryption policy on directories".
79 # TODO: the error code for "not a directory" is planned to switch from EINVAL to
80 # ENOTDIR.  For now this test accepts both.
81 nondirectory=$SCRATCH_MNT/nondirectory
82 echo -e "\n*** Setting encryption policy on nondirectory ***"
83 touch $nondirectory
84 _set_encpolicy $nondirectory |& \
85         _filter_scratch | sed -e 's/Invalid argument/Not a directory/'
86 check_no_policy $nondirectory |& _filter_scratch
87
88 # Should *not* be able to set an encryption policy on another user's directory.
89 # Regression test for 163ae1c6ad62: "fscrypto: add authorization check for
90 # setting encryption policy".
91 unauthorized_dir=$SCRATCH_MNT/unauthorized_dir
92 echo -e "\n*** Setting encryption policy on another user's directory ***"
93 mkdir $unauthorized_dir
94 _user_do_set_encpolicy $unauthorized_dir |& _filter_scratch
95 check_no_policy $unauthorized_dir |& _filter_scratch
96
97 # Should *not* be able to set an encryption policy on a directory on a
98 # filesystem mounted readonly.  Regression test for ba63f23d69a3: "fscrypto:
99 # require write access to mount to set encryption policy".  Test both a regular
100 # readonly filesystem and a readonly bind mount of a read-write filesystem.
101 echo -e "\n*** Setting encryption policy on readonly filesystem ***"
102 mkdir $SCRATCH_MNT/ro_dir $SCRATCH_MNT/ro_bind_mnt
103 _scratch_remount ro
104 _set_encpolicy $SCRATCH_MNT/ro_dir |& _filter_scratch
105 check_no_policy $SCRATCH_MNT/ro_dir |& _filter_scratch
106 _scratch_remount rw
107 mount --bind $SCRATCH_MNT $SCRATCH_MNT/ro_bind_mnt
108 mount -o remount,ro,bind $SCRATCH_MNT/ro_bind_mnt
109 _set_encpolicy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
110 check_no_policy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
111 umount $SCRATCH_MNT/ro_bind_mnt
112
113 # success, all done
114 status=0
115 exit