bookmarkfs/tests/testsuite.at
2025-06-02 09:20:19 +08:00

222 lines
5.4 KiB
Text

dnl
dnl Copyright (C) 2025 CismonX <admin@cismon.net>
dnl
dnl Copying and distribution of this file, with or without modification,
dnl are permitted in any medium without royalty,
dnl provided the copyright notice and this notice are preserved.
dnl This file is offered as-is, without any warranty.
dnl
AT_INIT
dnl
dnl ATX_REPEAT(n, string)
dnl
dnl Repeat string n times.
dnl
m4_define([ATX_REPEAT], [m4_for([unused_], [1], [$1], , [$2])])
dnl
dnl ATX_OPT_MODULE(name)
dnl
dnl Expands to a string which can be used for `-o backend=NAME`
dnl or `-o handler=NAME` options of a BookmarkFS program.
dnl
m4_define([ATX_OPT_MODULE], [$buildsrcdir/.libs/$1.so:bookmarkfs_$1])
dnl
dnl ATX_FEAT_VAR(feature)
dnl
dnl Expands to the shell variable indicating whether the given feature
dnl is defined by EX_FEAT() and enabled during `configure`.
dnl
dnl A non-empty value indicates that the feature is enabled.
dnl
m4_define([ATX_FEAT_VAR], [[${feat_]m4_translit($1, [-], [_])[}]])
dnl
dnl ATX_FEAT_IF(feature, [action-if-enabled], [action-if-disabled])
dnl
dnl Conditionally run script depending on whether a feature is enabled.
dnl
m4_define([ATX_FEAT_IF], [
if test -n "ATX_FEAT_VAR($1)"; then :
$2
else :
$3
fi
])
dnl
dnl ATX_FEAT_PREREQ(features...)
dnl
dnl If any of the given features is not enabled, skip the current test.
dnl
m4_define([ATX_FEAT_PREREQ], [
if test "m4_map([ATX_FEAT_VAR], [$@])" != ATX_REPEAT([$#], [y]); then
exit 77
fi
])
dnl
dnl ATX_RUN_REPEAT(n, script)
dnl
dnl Run script n times. Cannot be nested.
dnl
m4_define([ATX_RUN_REPEAT], [
atx_counter_=$1
while test $atx_counter_ -gt 0; do
$2
atx_counter_=$(($atx_counter_ - 1))
done
])
dnl
dnl ATX_RUN_IN_DIR(pathname, script)
dnl
m4_define([ATX_RUN_IN_DIR], [
atx_pwd=$(pwd)
cd "$1" || exit 1
$2
cd "$atx_pwd"
])
dnl
dnl ATX_RUN_ONE(cmd)
dnl
dnl Run a pipeline of shell command(s).
dnl If it exits with a non-zero status, fail the current test.
dnl
m4_define([ATX_RUN_ONE], [
m4_ifnblank([$1], [$1 || exit $?])
])
dnl
dnl ATX_RUN(cmd_list)
dnl
dnl Like ATX_RUN_ONE, but takes multiple pipelines, separated by newline.
dnl If any exits with a non-zero status, fail the current test.
dnl
m4_define([ATX_RUN], [
m4_map([ATX_RUN_ONE], m4_split([$1], m4_newline([+])))
])
dnl
dnl ATX_CHECK_SIMPLE(script, [cleanup])
dnl
m4_define([ATX_CHECK_SIMPLE], [
AT_CHECK([$1], , [ignore], [ignore], [$2], [$2])
])
dnl
dnl ATX_CHECK_LIB(script)
dnl
m4_define([ATX_CHECK_LIB], [
ATX_CHECK_SIMPLE([
ATX_FEAT_PREREQ([bookmarkfs-util])
$1
])
])
dnl
dnl ATX_CHECK_FS(backend, [options], src, target, [prepare], [check])
dnl
dnl Check for a BookmarkFS filesystem.
dnl
m4_define([ATX_CHECK_FS], [
ATX_CHECK_SIMPLE([
ATX_FEAT_PREREQ([bookmarkfs-mount], [backend-$1])
$5
"$buildsrcdir/mount.bookmarkfs" -F \
-o "backend=ATX_OPT_MODULE([backend_$1])" \
-o "fsname=check-$1,no_sandbox,$2" \
"$3" "$4" 2>mount.bookmarkfs.stderr &
while ! check-fs ismount "$4"; do
check-fs sleep 100
if ! jobs %%; then
echo 'fs daemon terminated unexpectedly'
exit 1
fi
done
$6
ATX_RUN_ONE([check-fs ismount "$4"])
], [
umount "$4"
])
])
dnl
dnl ATX_CHECK_FS_NEW(backend, [options], target, [prepare], [check])
dnl
dnl Perform filesystem checks using a new bookmark storage
dnl created with mkfs.bookmarkfs.
dnl
m4_define([ATX_CHECK_FS_NEW], [
ATX_CHECK_FS([$1], [rw,$2], [bookmarks-$1], [$3], [
ATX_FEAT_PREREQ([bookmarkfs-mkfs], [backend-$1-write])
$4
"$buildsrcdir/mkfs.bookmarkfs" \
-o "backend=ATX_OPT_MODULE([backend_$1]),force" \
"bookmarks-$1" || exit 1
mkdir -p "$3"
], [$5])
])
dnl
dnl ATX_CHECK_FS_NEW_ANY([options], [prepare], [check])
dnl
m4_define([ATX_CHECK_FS_NEW_ANY], [
ATX_CHECK_FS_NEW([firefox], [$1], [mnt.tmp], [$2], [
ATX_RUN_IN_DIR([mnt.tmp/bookmarks/unfiled], [$3])
])
ATX_CHECK_FS_NEW([chromium], [$1], [mnt.tmp], [$2], [
ATX_RUN_IN_DIR([mnt.tmp/bookmarks/Other bookmarks], [$3])
])
])
dnl
dnl ATX_CHECK_FS_NEW_ASSOC([options], [prepare], [check])
dnl
m4_define([ATX_CHECK_FS_NEW_ASSOC], [
ATX_CHECK_FS_NEW([firefox], [$1], [mnt.tmp], [$2], [
atx_tags=../../tags
atx_keywords=../../keywords
ATX_RUN_IN_DIR([mnt.tmp/bookmarks/unfiled], [$3])
])
])
dnl -- Helper functions --
AT_TEST_HELPER_FN([rand_u64_hex], , , [
echo $(od -vAn -N8 -tx8 /dev/urandom | tr -d ' ')
])
AT_TEST_HELPER_FN([rand_base64], , , [
# The `base64` utility is not specified by POSIX,
# but present in GNU Coreutils, BusyBox and the FreeBSD base system.
echo $(head -c$1 /dev/urandom | base64 -w0)
])
AT_TEST_HELPER_FN([prng_seed], , , [
seed_1=$(ath_fn_rand_u64_hex)
seed_2=$(ath_fn_rand_u64_hex)
seed_3=$(ath_fn_rand_u64_hex)
seed_4=$(ath_fn_rand_u64_hex)
echo $seed_1$seed_2$seed_3$seed_4
])
dnl -- Test groups --
AT_BANNER([The Utility Library])
m4_include([lib_hash.at])
m4_include([lib_prng.at])
m4_include([lib_watcher.at])
m4_include([lib_sandbox.at])
m4_include([lib_hashmap.at])
AT_BANNER([The Filesystem])
m4_include([fs_basic.at])
m4_include([fs_regrw.at])
m4_include([fs_dents.at])
m4_include([fs_assoc.at])