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