common/encrypt: add helper function for filtering no-key names
authorEric Biggers <ebiggers@google.com>
Sun, 18 Jul 2021 19:06:57 +0000 (14:06 -0500)
committerEryu Guan <guaneryu@gmail.com>
Sun, 25 Jul 2021 14:27:26 +0000 (22:27 +0800)
Add a helper function _filter_nokey_filenames() which replaces no-key
filenames with "NOKEY_NAME".  Use it in generic/419 and generic/429.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
common/encrypt
tests/generic/419
tests/generic/419.out
tests/generic/429

index c4cc2d835ffad669cdb6cb8112dd8da1f4f33dff..766a6d817f59753d6e79641375cdf1e655468e84 100644 (file)
@@ -920,3 +920,20 @@ _verify_ciphertext_for_encryption_policy()
                "$crypt_util_contents_args" \
                "$crypt_util_filename_args"
 }
+
+# Replace no-key filenames in the given directory with "NOKEY_NAME".
+#
+# No-key filenames are the filenames that the filesystem shows when userspace
+# lists an encrypted directory without the directory's encryption key being
+# present.  These will differ on every run of the test, even when using the same
+# encryption key, hence the need for this filtering in some cases.
+#
+# Note, this may replace "regular" names too, as technically we can only tell
+# whether a name is definitely a regular name, or either a regular or no-key
+# name.  A directory will only contain one type of name at a time, though.
+_filter_nokey_filenames()
+{
+       local dir=$1
+
+       sed "s|${dir}${dir:+/}[A-Za-z0-9+,_]\+|${dir}${dir:+/}NOKEY_NAME|g"
+}
index 61f9d6052f128eba632f3d0613ac888d6142fcca..6be7865ccce313ad4bf68b746736516514f0868c 100755 (executable)
@@ -41,7 +41,7 @@ _scratch_cycle_mount
 # in a way that does not assume any particular filenames.
 efile1=$(find $SCRATCH_MNT/edir -maxdepth 1 -type f | head -1)
 efile2=$(find $SCRATCH_MNT/edir -maxdepth 1 -type f | tail -1)
-mv $efile1 $efile2 |& _filter_scratch | sed 's|edir/[a-zA-Z0-9+,_]\+|edir/FILENAME|g'
+mv $efile1 $efile2 |& _filter_scratch | _filter_nokey_filenames edir
 $here/src/renameat2 -x $efile1 $efile2
 
 # success, all done
index 1bba10fdaef09f3e57248e8cabc5b8fd50c594a5..67bb7fb1e4b62ac9ac17b3aeb9fe953a76ba440b 100644 (file)
@@ -1,3 +1,3 @@
 QA output created by 419
-mv: cannot move 'SCRATCH_MNT/edir/FILENAME' to 'SCRATCH_MNT/edir/FILENAME': Required key not available
+mv: cannot move 'SCRATCH_MNT/edir/NOKEY_NAME' to 'SCRATCH_MNT/edir/NOKEY_NAME': Required key not available
 Required key not available
index ba2281c9fac546dcf994e575fe0b67a4819bda15..558e93ca9f312fe4a650273f2206e5d908374d76 100755 (executable)
@@ -52,18 +52,13 @@ _set_encpolicy $SCRATCH_MNT/edir $keydesc
 echo contents_@@@ > $SCRATCH_MNT/edir/@@@ # not valid base64
 echo contents_abcd > $SCRATCH_MNT/edir/abcd # valid base64
 
-filter_nokey_filenames()
-{
-       _filter_scratch | sed 's|edir/[a-zA-Z0-9+,_]\+|edir/NOKEY_NAME|g'
-}
-
 show_file_contents()
 {
        echo "--- Contents of files using plaintext names:"
        cat $SCRATCH_MNT/edir/@@@ |& _filter_scratch
        cat $SCRATCH_MNT/edir/abcd |& _filter_scratch
        echo "--- Contents of files using no-key names:"
-       cat ${nokey_names[@]} |& filter_nokey_filenames
+       cat ${nokey_names[@]} |& _filter_scratch | _filter_nokey_filenames edir
 }
 
 show_directory_with_key()
@@ -83,7 +78,8 @@ _unlink_session_encryption_key $keydesc
 _scratch_cycle_mount
 echo "--- Directory listing:"
 nokey_names=( $(find $SCRATCH_MNT/edir -mindepth 1 | sort) )
-printf '%s\n' "${nokey_names[@]}" | filter_nokey_filenames
+printf '%s\n' "${nokey_names[@]}" | \
+       _filter_scratch | _filter_nokey_filenames edir
 show_file_contents
 
 # Without remounting or dropping caches, add the encryption key and view the