Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that the -o digest=XXX mount option work correctly #194

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion tests/test-lib.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#!/usr/bin/bash

fatal() {
echo $@ 1>&2; exit 1
}

# Dump ls -al + file contents to stderr, then fatal()
_fatal_print_file() {
file="$1"
shift
ls -al "$file" >&2
sed -e 's/^/# /' < "$file" >&2
fatal "$@"
}

assert_file_has_content () {
fpath=$1
shift
for re in "$@"; do
if ! grep -q -e "$re" "$fpath"; then
_fatal_print_file "$fpath" "File '$fpath' doesn't match regexp '$re'"
fi
done
}

check_whiteout () {
tmpfile=$(mktemp /tmp/lcfs-whiteout.XXXXXX)
rm -f $tmpfile
Expand Down Expand Up @@ -31,8 +54,17 @@ check_erofs_fsck () {
fi
}

check_fsverity () {
fsverity --version >/dev/null 2>&1 || return 1
tmpfile=$(mktemp /var/tmp/lcfs-fsverity.XXXXXX)
echo foo > $tmpfile
fsverity enable $tmpfile >/dev/null 2>&1 || return 1
return 0
}

[[ -v can_whiteout ]] || can_whiteout=$(check_whiteout)
[[ -v has_fuse ]] || has_fuse=$(if check_fuse; then echo y; else echo n; fi)
[[ -v has_fsck ]] || has_fsck=$(check_erofs_fsck)
[[ -v has_fsverity ]] || has_fsverity=$(if check_fsverity; then echo y; else echo n; fi)

echo Test options: can_whiteout=$can_whiteout has_fuse=$has_fuse has_fsck=$has_fsck
echo Test options: can_whiteout=$can_whiteout has_fuse=$has_fuse has_fsck=$has_fsck has_fsverity=$has_fsverity
29 changes: 26 additions & 3 deletions tests/test-units.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ set -e
workdir=$(mktemp -d /var/tmp/lcfs-test.XXXXXX)
trap 'rm -rf -- "$workdir"' EXIT

. test-lib.sh

function makeimage () {
local dir=$1
${VALGRIND_PREFIX} $BINDIR/mkcomposefs --digest-store=$dir/objects $dir/root $dir/test.cfs
Expand Down Expand Up @@ -44,12 +46,33 @@ function test_objects () {
fi
}

TESTS="test_inline test_objects"
function test_mount_digest () {
local dir=$1

if [ $has_fsverity = y ]; then
echo foo > $dir/root/a-file
makeimage $dir

$BINDIR/mount.composefs -o basedir=$dir/objects,digest=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa $dir/test.cfs $dir/mnt 2> $dir/stderr && fatal "non-fsverity mount should not succeed"
assert_file_has_content $dir/stderr "Image has no fs-verity"

fsverity enable $dir/test.cfs

$BINDIR/mount.composefs -o basedir=$dir/objects,digest=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa $dir/test.cfs $dir/mnt 2> $dir/stderr && fatal "wrong fsverity mount should not succeed"
assert_file_has_content $dir/stderr "Image has wrong fs-verity"

local DIGEST=$(fsverity measure $dir/test.cfs | awk "{ print \$1 }" | sed s/sha256://)

$BINDIR/mount.composefs -o basedir=$dir/objects,digest=$DIGEST $dir/test.cfs $dir/mnt 2> $dir/stderr || assert_file_has_content $dir/stderr "Permission denied"
umount $dir/mnt 2> $dir/stderr || true
fi
}

TESTS="test_inline test_objects test_mount_digest"
res=0
for i in $TESTS; do
testdir=$(mktemp -d $workdir/$i.XXXXXX)
mkdir $testdir/root
mkdir $testdir/objects
mkdir $testdir/root $testdir/objects $testdir/mnt
if $i $testdir ; then
echo "Test $i: OK"
else
Expand Down
Loading