From 5affe9ee02dbc87cad46c7c61597a6db66f7f47f Mon Sep 17 00:00:00 2001 From: CismonX Date: Thu, 20 Feb 2025 11:11:10 +0800 Subject: [PATCH] test: misc updates - Export feature variables for conditional testing. - Add helper macros. - Move helper shell functions from atlocal.in to testsuite.at. The atlocal file should only contain feature variables. - Update existing tests. --- configure.ac | 4 +++- m4/bookmarkfs_ac.m4 | 23 ++++++++++++++++-- tests/Makefile.am | 18 ++++++++------ tests/atlocal.in | 24 ++++++++++++------- tests/lib_hash.at | 20 ++++++++++++---- tests/lib_prng.at | 34 +++++++++++++++++--------- tests/testsuite.at | 58 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 146 insertions(+), 35 deletions(-) diff --git a/configure.ac b/configure.ac index ef29e10..f70579d 100644 --- a/configure.ac +++ b/configure.ac @@ -7,7 +7,7 @@ dnl provided the copyright notice and this notice are preserved. dnl This file is offered as-is, without any warranty. dnl -AC_PREREQ([2.69]) +AC_PREREQ([2.70]) AC_INIT([bookmarkfs], [0.1.0], [bug-bookmarkfs@nongnu.org]) AC_CONFIG_SRCDIR([bookmarkfs.pc.in]) AC_CONFIG_HEADERS([config.h]) @@ -179,6 +179,8 @@ BOOKMARKFS_AMCOND([bookmarkfs-util], [bookmarkctl], [bookmarkfs-fsck], [bookmarkfs-mkfs], [bookmarkfs-mount], [sandbox], [backend-firefox], [backend-firefox-write], [backend-chromium], [backend-chromium-write], [interactive-fsck], [fsck-handler-tcl]) +BOOKMARKFS_FEAT_EXPORT([bookmarkfs-debug], [sandbox-landlock], + [native-watcher]) # -- Checks for compiler builtins and attributes -- diff --git a/m4/bookmarkfs_ac.m4 b/m4/bookmarkfs_ac.m4 index f658d4d..de7d369 100644 --- a/m4/bookmarkfs_ac.m4 +++ b/m4/bookmarkfs_ac.m4 @@ -78,11 +78,30 @@ AC_DEFUN([BOOKMARKFS_DEP], [ dnl dnl BOOKMARKFS_AMCOND([features]...) dnl -dnl Export feature flags to Automake makefiles. +dnl Export feature flags to Makefile templates. dnl AC_DEFUN([BOOKMARKFS_AMCOND], [ m4_foreach([feat_name_], [$@], [ AM_CONDITIONAL(m4_translit(feat_name_, [-a-z], [_A-Z]), - [test x$][{enable_]m4_translit(feat_name_, [-], [_])[} != xno]) + [test x$enable_]m4_translit(feat_name_, [-], [_])[ != xno]) + ]) +]) + +dnl +dnl BOOKMARKFS_FEAT_OUT([features]...) +dnl +dnl Export feature flags to Autoconf output variables, +dnl similar to the ones set by AM_CONTITIONAL() (`xxx_TRUE` only). +dnl +AC_DEFUN([BOOKMARKFS_FEAT_EXPORT], [ + m4_foreach([feat_name_], [$@], [ + m4_pushdef([out_var_], m4_translit(feat_name_, [-a-z], [_A-Z])[_TRUE]) + AS_VAR_IF([enable_]m4_translit(feat_name_, [-], [_]), [yes], [ + AC_SUBST(out_var_, ['']) + ], [ + AC_SUBST(out_var_, ['#']) + ]) + AM_SUBST_NOTMAKE(out_var_) + m4_popdef([out_var_]) ]) ]) diff --git a/tests/Makefile.am b/tests/Makefile.am index 4994068..cb51a91 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,17 +6,21 @@ # this notice are preserved. This file is offered as-is, without any warranty. # -EXTRA_DIST = package.m4 testsuite.at $(TESTSUITE) $(TESTSUITE_AT_) +EXTRA_DIST = package.m4 testsuite.at $(TESTSUITE) $(TESTS_) -TESTSUITE_AT_ = lib_hash.at lib_prng.at +TESTS_ = lib_hash.at lib_prng.at # Helper programs for testing -check_PROGRAMS = check-bookmarkfs-util +check_PROGRAMS = -check_bookmarkfs_util_CPPFLAGS = -I$(top_srcdir)/src -check_bookmarkfs_util_LDADD = $(top_builddir)/src/libbookmarkfs_util.la -check_bookmarkfs_util_SOURCES = check_lib.c +if BOOKMARKFS_UTIL + check_PROGRAMS += check-bookmarkfs-util + + check_bookmarkfs_util_CPPFLAGS = -I$(top_srcdir)/src + check_bookmarkfs_util_LDADD = $(top_builddir)/src/libbookmarkfs_util.la + check_bookmarkfs_util_SOURCES = check_lib.c +endif # BOOKMARKFS_UTIL # Autotest setup @@ -33,7 +37,7 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac echo 'm4_define([AT_PACKAGE_BUGREPORT], [@PACKAGE_BUGREPORT@])'; \ } >'$(srcdir)/package.m4' -$(TESTSUITE): $(srcdir)/testsuite.at $(srcdir)/package.m4 $(TESTSUITE_AT_) +$(TESTSUITE): $(srcdir)/testsuite.at $(srcdir)/package.m4 $(TESTS_) $(AUTOTEST) -I '$(srcdir)' -o $@.tmp $@.at mv $@.tmp $@ diff --git a/tests/atlocal.in b/tests/atlocal.in index c8f2eb3..70c6ba5 100644 --- a/tests/atlocal.in +++ b/tests/atlocal.in @@ -6,12 +6,18 @@ # this notice are preserved. This file is offered as-is, without any warranty. # -rand_u64_hex() { - echo $(od -vAn -N8 -tx8 /dev/urandom | tr -d ' ') -} - -rand_base64() { - # The `base64` utility is not specified by POSIX, - # but present in GNU Coreutils, BusyBox and FreeBSD base system. - echo $(head -c$1 /dev/urandom | base64 -w0) -} +@BOOKMARKFS_UTIL_TRUE@ feat_bookmarkfs_util=y +@BOOKMARKCTL_TRUE@ feat_bookmarkctl=y +@BOOKMARKFS_FSCK_TRUE@ feat_bookmarkfs_fsck=y +@BOOKMARKFS_MKFS_TRUE@ feat_bookmarkfs_mkfs=y +@BOOKMARKFS_MOUNT_TRUE@ feat_bookmarkfs_mount=y +@SANDBOX_TRUE@ feat_sandbox=y +@SANDBOX_LANDLOCK_TRUE@ feat_sandbox_landlock=y +@BOOKMARKFS_DEBUG_TRUE@ feat_bookmarkfs_debug=y +@BACKEND_FIREFOX_TRUE@ feat_backend_firefox=y +@BACKEND_FIREFOX_WRITE_TRUE@ feat_backend_firefox_write=y +@BACKEND_CHROMIUM_TRUE@ feat_backend_chromium=y +@BACKEND_CHROMIUM_WRITE_TRUE@ feat_backend_chromium_write=y +@INTERACTIVE_FSCK_TRUE@ feat_interactive_fsck=y +@NATIVE_WATCHER_TRUE@ feat_native_watcher=y +@FSCK_HANDLER_TCL_TRUE@ feat_fsck_handler_tcl=y diff --git a/tests/lib_hash.at b/tests/lib_hash.at index 1457b68..5fe3bcd 100644 --- a/tests/lib_hash.at +++ b/tests/lib_hash.at @@ -10,22 +10,32 @@ dnl AT_SETUP([util lib: hash function]) AT_KEYWORDS([lib hash]) -AT_CHECK([ +# This test only serves as a basic sanity check. +# +# For reliable testing of hash functions, +# see . +ATX_CHECK_LIB([ calc_digest() { echo "$2" | check-bookmarkfs-util hash -s$1 } - seed=$(rand_u64_hex) - input=$(rand_base64 300) + seed=$(ath_fn_rand_u64_hex) + input=$(ath_fn_rand_base64 300) hash_1=$(calc_digest $seed $input) hash_2=$(calc_digest $seed $input) hash_3=$(calc_digest $seed $input) + hash_x=$(calc_digest $seed $input+) # Given the same seed and input, the digest should be consistent. if test "$hash_1" != "$hash_2" -o "$hash_2" != "$hash_3"; then - exit 99 + ATX_FAIL fi -], , [ignore]) + + # Bad hash function if we're getting collisions. + if test "$hash_1" = "$hash_x"; then + ATX_FAIL + fi +]) AT_CLEANUP diff --git a/tests/lib_prng.at b/tests/lib_prng.at index 9453e49..28dc0cc 100644 --- a/tests/lib_prng.at +++ b/tests/lib_prng.at @@ -10,28 +10,40 @@ dnl AT_SETUP([util lib: prng]) AT_KEYWORDS([lib prng]) -AT_CHECK([ - gen_prng() { +# This test only serves as a basic sanity check. +# +# For reliable testing of PRNGs, see . +ATX_CHECK_LIB([ + gen_num() { check-bookmarkfs-util prng -s$1 -n$2 } - seed=$(rand_u64_hex)$(rand_u64_hex)$(rand_u64_hex)$(rand_u64_hex) + gen_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 + } + + seed=$(gen_seed) count=32 - prng_1=$(gen_prng $seed $count) - prng_2=$(gen_prng $seed $count) - prng_3=$(gen_prng $seed $count) + num_1=$(gen_num $seed $count) + num_2=$(gen_num $seed $count) + num_3=$(gen_num $seed $count) # Given the same seed, the results should be consistent. - if test "$prng_1" != "$prng_2" -o "$prng_2" != "$prng_3"; then - exit 99 + if test "$num_1" != "$num_2" -o "$num_2" != "$num_3"; then + ATX_FAIL fi # Bad PRNG if we're getting duplicates too soon. - uniq_count=$(echo "$prng_1" | sort | uniq | wc -l) + uniq_count=$(echo "$num_1" | sort | uniq | wc -l) if test $uniq_count -ne $count; then - exit 99 + ATX_FAIL fi -], , [ignore]) +]) AT_CLEANUP diff --git a/tests/testsuite.at b/tests/testsuite.at index 1e3d3a8..ebb4209 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -9,6 +9,64 @@ dnl AT_INIT +m4_define([ATX_FAIL], [exit 99]) +m4_define([ATX_SKIP], [exit 77]) + +dnl +dnl ATX_FEAT_NAME(feature) +dnl +dnl Name of the shell variable indicating whether the given feature +dnl is defined by BOOKMARKFS_FEAT() and enabled during `configure`. +dnl +dnl A non-empty value indicates that the feature is enabled. +dnl +m4_define([ATX_FEAT_NAME], [[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_NAME($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 -z "m4_join(, m4_foreach([s_], [$@], [$ATX_FEAT_NAME(s_)]))"; then + ATX_SKIP + fi +]) + +m4_define([ATX_CHECK_SIMPLE], [ + AT_CHECK([$1], , [ignore], [ignore]) +]) + +m4_define([ATX_CHECK_LIB], [ + ATX_CHECK_SIMPLE([ + ATX_FEAT_PREREQ([bookmarkfs-util]) + $1 + ]) +]) + +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_BANNER([The Utility Library]) m4_include([lib_hash.at]) m4_include([lib_prng.at])