[PATCH 2/3] common/encrypt: add helper function for filtering no-key names

Eric Biggers
2021-07-18T19:08:33Z

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>
---
common/encrypt | 17 +++++++++++++++++
tests/generic/419 | 2 +-
tests/generic/419.out | 2 +-
tests/generic/429 | 10 +++-------
4 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/common/encrypt b/common/encrypt
index c4cc2d83..766a6d81 100644
--- a/common/encrypt
+++ b/common/encrypt
@@ -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"
+}
diff --git a/tests/generic/419 b/tests/generic/419
index 61f9d605..6be7865c 100755
--- a/tests/generic/419
+++ b/tests/generic/419
@@ -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
diff --git a/tests/generic/419.out b/tests/generic/419.out
index 1bba10fd..67bb7fb1 100644
--- a/tests/generic/419.out
+++ b/tests/generic/419.out
@@ -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
diff --git a/tests/generic/429 b/tests/generic/429
index ba2281c9..558e93ca 100755
--- a/tests/generic/429
+++ b/tests/generic/429
@@ -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
--
2.32.0