From ffa28a40911323b65d123bcee34f4e9cbc4e0ba8 Mon Sep 17 00:00:00 2001 From: CismonX Date: Thu, 19 Jun 2025 16:51:35 +0800 Subject: [PATCH] test: fetch prng seed from environment variable Tests that require PRNG are by default seeded from `/dev/urandom`. However, the user should be able to override with a given seed to reproduce a failing test. Instead of messing with command-line arguments, a cleaner approach is to fetch the seed from an environment variable. --- tests/check_fs_dents.c | 9 ++------- tests/check_fs_regrw.c | 9 ++------- tests/check_fs_times.c | 9 ++------- tests/check_hashmap.c | 9 ++------- tests/check_lib.c | 9 ++------- tests/check_util.c | 28 ++++++++++++++-------------- tests/check_util.h | 4 +--- tests/fs_dents.at | 3 +-- tests/fs_regrw.at | 3 +-- tests/fs_times.at | 3 +-- tests/lib_hashmap.at | 7 +------ tests/lib_prng.at | 10 +++++++--- tests/testsuite.at | 9 --------- 13 files changed, 36 insertions(+), 76 deletions(-) diff --git a/tests/check_fs_dents.c b/tests/check_fs_dents.c index cc1a963..ab80ca4 100644 --- a/tests/check_fs_dents.c +++ b/tests/check_fs_dents.c @@ -239,18 +239,13 @@ check_fs_dents ( int argc, char *argv[] ) { - char const *seed = NULL; int n = -1; - OPT_START(argc, argv, "n:s:") + OPT_START(argc, argv, "n:") OPT_OPT('n') { n = atoi(optarg); break; } - OPT_OPT('s') { - seed = optarg; - break; - } OPT_END if (n <= 0) { @@ -263,7 +258,7 @@ check_fs_dents ( } char const *path = argv[0]; - if (0 != prng_seed_from_hex(seed)) { + if (0 != prng_seed_from_env()) { return -1; } int dirfd = open(path, O_RDONLY | O_DIRECTORY); diff --git a/tests/check_fs_regrw.c b/tests/check_fs_regrw.c index 48e247f..8dbf51f 100644 --- a/tests/check_fs_regrw.c +++ b/tests/check_fs_regrw.c @@ -167,18 +167,13 @@ check_fs_regrw ( int argc, char *argv[] ) { - char const *seed = NULL; int file_max = -1; - OPT_START(argc, argv, "n:s:") + OPT_START(argc, argv, "n:") OPT_OPT('n') { file_max = atoi(optarg); break; } - OPT_OPT('s') { - seed = optarg; - break; - } OPT_END if (file_max <= 0 || file_max % sizeof(uint64_t) != 0) { @@ -191,7 +186,7 @@ check_fs_regrw ( } char const *path = argv[0]; - if (0 != prng_seed_from_hex(seed)) { + if (0 != prng_seed_from_env()) { return -1; } return do_check_fs_regrw(path, file_max); diff --git a/tests/check_fs_times.c b/tests/check_fs_times.c index b466089..86b62ba 100644 --- a/tests/check_fs_times.c +++ b/tests/check_fs_times.c @@ -160,18 +160,13 @@ check_fs_times ( int argc, char *argv[] ) { - char const *seed = NULL; int rounds = -1; - OPT_START(argc, argv, "r:s:") + OPT_START(argc, argv, "r:") OPT_OPT('r') { rounds = atoi(optarg); break; } - OPT_OPT('s') { - seed = optarg; - break; - } OPT_END if (rounds < 0) { @@ -184,7 +179,7 @@ check_fs_times ( } char const *path = argv[0]; - if (0 != prng_seed_from_hex(seed)) { + if (0 != prng_seed_from_env()) { return -1; } int dirfd = open(path, O_RDONLY | O_DIRECTORY); diff --git a/tests/check_hashmap.c b/tests/check_hashmap.c index 7adf90d..2ffec57 100644 --- a/tests/check_hashmap.c +++ b/tests/check_hashmap.c @@ -168,15 +168,10 @@ check_hashmap ( int argc, char *argv[] ) { - char const *seed = NULL; int size_exp = -1; int rounds = -1; - OPT_START(argc, argv, "s:n:r:") - OPT_OPT('s') { - seed = optarg; - break; - } + OPT_START(argc, argv, "n:r:") OPT_OPT('n') { size_exp = atoi(optarg); break; @@ -196,7 +191,7 @@ check_hashmap ( return -1; } - if (0 != prng_seed_from_hex(seed)) { + if (0 != prng_seed_from_env()) { return -1; } return do_check_hashmap(1u << size_exp, rounds); diff --git a/tests/check_lib.c b/tests/check_lib.c index 0eded62..2aa4ec0 100644 --- a/tests/check_lib.c +++ b/tests/check_lib.c @@ -104,21 +104,16 @@ subcmd_prng ( int argc, char *argv[] ) { - char const *seed = NULL; int n = 0; - OPT_START(argc, argv, "s:n:") - OPT_OPT('s') { - seed = optarg; - break; - } + OPT_START(argc, argv, "n:") OPT_OPT('n') { n = atoi(optarg); break; } OPT_END - if (0 != prng_seed_from_hex(seed)) { + if (0 != prng_seed_from_env()) { return -1; } for (; n > 0; --n) { diff --git a/tests/check_util.c b/tests/check_util.c index 3636464..ae715c2 100644 --- a/tests/check_util.c +++ b/tests/check_util.c @@ -26,25 +26,25 @@ #include #include +#include #include "prng.h" int -prng_seed_from_hex ( - char const *str -) { - uint64_t buf[4], *seed = NULL; +prng_seed_from_env (void) +{ + char const *seed_str = getenv("BOOKMARKFS_TEST_PRNG_SEED"); + if (seed_str == NULL) { + return prng_seed(NULL); + } - if (str != NULL) { - int cnt = sscanf(str, - "%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64, - &buf[0], &buf[1], &buf[2], &buf[3]); - if (cnt != 4) { - log_puts("bad prng seed"); - return -1; - } - seed = buf; - log_printf("prng seed: '%s'", str); + uint64_t seed[4]; + int cnt = sscanf(seed_str, + "%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64, + &seed[0], &seed[1], &seed[2], &seed[3]); + if (cnt != 4) { + log_puts("bad prng seed"); + return -1; } return prng_seed(seed); } diff --git a/tests/check_util.h b/tests/check_util.h index e4c3374..c3e31f8 100644 --- a/tests/check_util.h +++ b/tests/check_util.h @@ -72,8 +72,6 @@ check_watcher ( ); int -prng_seed_from_hex ( - char const *str -); +prng_seed_from_env (void); #endif /* !defined(BOOKMARKFS_CHECK_UTIL_H_) */ diff --git a/tests/fs_dents.at b/tests/fs_dents.at index abd0441..ed8b61e 100644 --- a/tests/fs_dents.at +++ b/tests/fs_dents.at @@ -13,11 +13,10 @@ AT_KEYWORDS([fs dents]) ATX_CHECK_FS_NEW_ANY(, , [ ATX_RUN_REPEAT([8], [ name=$(ath_fn_rand_u64_hex) - seed=$(ath_fn_prng_seed) mkdir $name ATX_RUN([ - check-fs dents -n 1024 -s "$seed" $name + check-fs dents -n 1024 $name ]) ]) ]) diff --git a/tests/fs_regrw.at b/tests/fs_regrw.at index f4de87e..b91155a 100644 --- a/tests/fs_regrw.at +++ b/tests/fs_regrw.at @@ -12,10 +12,9 @@ AT_KEYWORDS([fs regrw]) ATX_CHECK_FS_NEW_ANY([file_max=524288], , [ name=$(ath_fn_rand_u64_hex) - seed=$(ath_fn_prng_seed) ATX_RUN([ - check-fs regrw -n 524288 -s "$seed" $name + check-fs regrw -n 524288 $name ]) ]) diff --git a/tests/fs_times.at b/tests/fs_times.at index 0ceff43..6b67afa 100644 --- a/tests/fs_times.at +++ b/tests/fs_times.at @@ -12,11 +12,10 @@ AT_KEYWORDS([fs times]) ATX_CHECK_FS_NEW_ANY(, , [ name=$(ath_fn_rand_u64_hex) - seed=$(ath_fn_prng_seed) mkdir $name ATX_RUN([ - check-fs times -s "$seed" -r 512 $name + check-fs times -r 512 $name ]) ]) diff --git a/tests/lib_hashmap.at b/tests/lib_hashmap.at index 2518d7d..329bbc7 100644 --- a/tests/lib_hashmap.at +++ b/tests/lib_hashmap.at @@ -11,11 +11,6 @@ AT_SETUP([util lib: hashmap]) AT_KEYWORDS([lib hashmap]) ATX_CHECK_LIB([ - seed="${CHECK_HASHMAP_PRNG_SEED}" - if test -z "$seed"; then - seed=$(ath_fn_prng_seed) - fi - size="${CHECK_HASHMAP_DATA_SIZE}" if test -z "$size"; then # Requires at least 128 MiB of memory on a 64-bit platform. @@ -28,7 +23,7 @@ ATX_CHECK_LIB([ fi ATX_RUN([ - check-util-lib hashmap -s "$seed" -n "$size" -r "$rounds" + check-util-lib hashmap -n "$size" -r "$rounds" ]) ]) diff --git a/tests/lib_prng.at b/tests/lib_prng.at index f82c03b..e8c82ce 100644 --- a/tests/lib_prng.at +++ b/tests/lib_prng.at @@ -15,11 +15,15 @@ AT_KEYWORDS([lib prng]) # For reliable testing of PRNGs, see . ATX_CHECK_LIB([ gen_num() { - check-util-lib prng -s$1 -n$2 + env BOOKMARKFS_TEST_PRNG_SEED=$1 check-util-lib prng -n$2 } - ATX_RUN_REPEAT([16], [ - seed=$(ath_fn_prng_seed) + repeat=1 + if test -z "$BOOKMARKFS_TEST_PRNG_SEED"; then + repeat=16 + fi + ATX_RUN_REPEAT([$repeat], [ + seed=$(check-util-lib prng -n4 | tr -d '\n') count=32 num_1=$(gen_num $seed $count) diff --git a/tests/testsuite.at b/tests/testsuite.at index f63060a..c4c61d5 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -199,15 +199,6 @@ AT_TEST_HELPER_FN([rand_base64], , , [ 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])