common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[xfstests-dev.git] / tests / generic / 580
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2019 Google LLC
4 #
5 # FS QA Test generic/580
6 #
7 # Basic test of the fscrypt filesystem-level encryption keyring
8 # and v2 encryption policies.
9 #
10
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14 echo
15
16 here=`pwd`
17 tmp=/tmp/$$
18 status=1        # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
20
21 _cleanup()
22 {
23         cd /
24         rm -f $tmp.*
25 }
26
27 # get standard environment, filters and checks
28 . ./common/rc
29 . ./common/filter
30 . ./common/encrypt
31
32 # remove previous $seqres.full before test
33 rm -f $seqres.full
34
35 # real QA test starts here
36 _supported_fs generic
37 _supported_os Linux
38 _require_scratch_encryption -v 2
39
40 _scratch_mkfs_encrypted &>> $seqres.full
41 _scratch_mount
42
43 test_with_policy_version()
44 {
45         local vers=$1
46
47         if (( vers == 1 )); then
48                 local keyspec=$TEST_KEY_DESCRIPTOR
49                 local add_enckey_args="-d $keyspec"
50         else
51                 local keyspec=$TEST_KEY_IDENTIFIER
52                 local add_enckey_args=""
53         fi
54
55         mkdir $dir
56         echo "# Setting v$vers encryption policy"
57         _set_encpolicy $dir $keyspec
58         echo "# Getting v$vers encryption policy"
59         _get_encpolicy $dir | _filter_scratch
60         if (( vers == 1 )); then
61                 echo "# Getting v1 encryption policy using old ioctl"
62                 _get_encpolicy $dir -1 | _filter_scratch
63         fi
64         echo "# Trying to create file without key added yet"
65         $XFS_IO_PROG -f $dir/file |& _filter_scratch
66         echo "# Getting encryption key status"
67         _enckey_status $SCRATCH_MNT $keyspec
68         echo "# Adding encryption key"
69         _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" $add_enckey_args
70         echo "# Creating encrypted file"
71         echo contents > $dir/file
72         echo "# Getting encryption key status"
73         _enckey_status $SCRATCH_MNT $keyspec
74         echo "# Removing encryption key"
75         _rm_enckey $SCRATCH_MNT $keyspec
76         echo "# Getting encryption key status"
77         _enckey_status $SCRATCH_MNT $keyspec
78         echo "# Verifying that the encrypted directory was \"locked\""
79         cat $dir/file |& _filter_scratch
80         cat "$(find $dir -type f)" |& _filter_scratch | cut -d ' ' -f3-
81
82         # Test removing key with a file open.
83         echo "# Re-adding encryption key"
84         _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" $add_enckey_args
85         echo "# Creating another encrypted file"
86         echo foo > $dir/file2
87         echo "# Removing key while an encrypted file is open"
88         exec 3< $dir/file
89         _rm_enckey $SCRATCH_MNT $keyspec
90         echo "# Non-open file should have been evicted"
91         cat $dir/file2 |& _filter_scratch
92         echo "# Open file shouldn't have been evicted"
93         cat $dir/file
94         echo "# Key should be in \"incompletely removed\" state"
95         _enckey_status $SCRATCH_MNT $keyspec
96         echo "# Closing file and removing key for real now"
97         exec 3<&-
98         _rm_enckey $SCRATCH_MNT $keyspec
99         cat $dir/file |& _filter_scratch
100
101         echo "# Cleaning up"
102         rm -rf $dir
103         _scratch_cycle_mount    # Clear all keys
104         echo
105 }
106
107 dir=$SCRATCH_MNT/dir
108
109 test_with_policy_version 1
110
111 test_with_policy_version 2
112
113 echo "# Trying to remove absent key"
114 _rm_enckey $SCRATCH_MNT abcdabcdabcdabcd
115
116 # success, all done
117 status=0
118 exit