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.
This commit is contained in:
CismonX 2025-06-19 16:51:35 +08:00
parent 6c38779e78
commit ffa28a4091
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
13 changed files with 36 additions and 76 deletions

View file

@ -239,18 +239,13 @@ check_fs_dents (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
char const *seed = NULL;
int n = -1; int n = -1;
OPT_START(argc, argv, "n:s:") OPT_START(argc, argv, "n:")
OPT_OPT('n') { OPT_OPT('n') {
n = atoi(optarg); n = atoi(optarg);
break; break;
} }
OPT_OPT('s') {
seed = optarg;
break;
}
OPT_END OPT_END
if (n <= 0) { if (n <= 0) {
@ -263,7 +258,7 @@ check_fs_dents (
} }
char const *path = argv[0]; char const *path = argv[0];
if (0 != prng_seed_from_hex(seed)) { if (0 != prng_seed_from_env()) {
return -1; return -1;
} }
int dirfd = open(path, O_RDONLY | O_DIRECTORY); int dirfd = open(path, O_RDONLY | O_DIRECTORY);

View file

@ -167,18 +167,13 @@ check_fs_regrw (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
char const *seed = NULL;
int file_max = -1; int file_max = -1;
OPT_START(argc, argv, "n:s:") OPT_START(argc, argv, "n:")
OPT_OPT('n') { OPT_OPT('n') {
file_max = atoi(optarg); file_max = atoi(optarg);
break; break;
} }
OPT_OPT('s') {
seed = optarg;
break;
}
OPT_END OPT_END
if (file_max <= 0 || file_max % sizeof(uint64_t) != 0) { if (file_max <= 0 || file_max % sizeof(uint64_t) != 0) {
@ -191,7 +186,7 @@ check_fs_regrw (
} }
char const *path = argv[0]; char const *path = argv[0];
if (0 != prng_seed_from_hex(seed)) { if (0 != prng_seed_from_env()) {
return -1; return -1;
} }
return do_check_fs_regrw(path, file_max); return do_check_fs_regrw(path, file_max);

View file

@ -160,18 +160,13 @@ check_fs_times (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
char const *seed = NULL;
int rounds = -1; int rounds = -1;
OPT_START(argc, argv, "r:s:") OPT_START(argc, argv, "r:")
OPT_OPT('r') { OPT_OPT('r') {
rounds = atoi(optarg); rounds = atoi(optarg);
break; break;
} }
OPT_OPT('s') {
seed = optarg;
break;
}
OPT_END OPT_END
if (rounds < 0) { if (rounds < 0) {
@ -184,7 +179,7 @@ check_fs_times (
} }
char const *path = argv[0]; char const *path = argv[0];
if (0 != prng_seed_from_hex(seed)) { if (0 != prng_seed_from_env()) {
return -1; return -1;
} }
int dirfd = open(path, O_RDONLY | O_DIRECTORY); int dirfd = open(path, O_RDONLY | O_DIRECTORY);

View file

@ -168,15 +168,10 @@ check_hashmap (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
char const *seed = NULL;
int size_exp = -1; int size_exp = -1;
int rounds = -1; int rounds = -1;
OPT_START(argc, argv, "s:n:r:") OPT_START(argc, argv, "n:r:")
OPT_OPT('s') {
seed = optarg;
break;
}
OPT_OPT('n') { OPT_OPT('n') {
size_exp = atoi(optarg); size_exp = atoi(optarg);
break; break;
@ -196,7 +191,7 @@ check_hashmap (
return -1; return -1;
} }
if (0 != prng_seed_from_hex(seed)) { if (0 != prng_seed_from_env()) {
return -1; return -1;
} }
return do_check_hashmap(1u << size_exp, rounds); return do_check_hashmap(1u << size_exp, rounds);

View file

@ -104,21 +104,16 @@ subcmd_prng (
int argc, int argc,
char *argv[] char *argv[]
) { ) {
char const *seed = NULL;
int n = 0; int n = 0;
OPT_START(argc, argv, "s:n:") OPT_START(argc, argv, "n:")
OPT_OPT('s') {
seed = optarg;
break;
}
OPT_OPT('n') { OPT_OPT('n') {
n = atoi(optarg); n = atoi(optarg);
break; break;
} }
OPT_END OPT_END
if (0 != prng_seed_from_hex(seed)) { if (0 != prng_seed_from_env()) {
return -1; return -1;
} }
for (; n > 0; --n) { for (; n > 0; --n) {

View file

@ -26,25 +26,25 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "prng.h" #include "prng.h"
int int
prng_seed_from_hex ( prng_seed_from_env (void)
char const *str {
) { char const *seed_str = getenv("BOOKMARKFS_TEST_PRNG_SEED");
uint64_t buf[4], *seed = NULL; if (seed_str == NULL) {
return prng_seed(NULL);
}
if (str != NULL) { uint64_t seed[4];
int cnt = sscanf(str, int cnt = sscanf(seed_str,
"%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64, "%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64,
&buf[0], &buf[1], &buf[2], &buf[3]); &seed[0], &seed[1], &seed[2], &seed[3]);
if (cnt != 4) { if (cnt != 4) {
log_puts("bad prng seed"); log_puts("bad prng seed");
return -1; return -1;
}
seed = buf;
log_printf("prng seed: '%s'", str);
} }
return prng_seed(seed); return prng_seed(seed);
} }

View file

@ -72,8 +72,6 @@ check_watcher (
); );
int int
prng_seed_from_hex ( prng_seed_from_env (void);
char const *str
);
#endif /* !defined(BOOKMARKFS_CHECK_UTIL_H_) */ #endif /* !defined(BOOKMARKFS_CHECK_UTIL_H_) */

View file

@ -13,11 +13,10 @@ AT_KEYWORDS([fs dents])
ATX_CHECK_FS_NEW_ANY(, , [ ATX_CHECK_FS_NEW_ANY(, , [
ATX_RUN_REPEAT([8], [ ATX_RUN_REPEAT([8], [
name=$(ath_fn_rand_u64_hex) name=$(ath_fn_rand_u64_hex)
seed=$(ath_fn_prng_seed)
mkdir $name mkdir $name
ATX_RUN([ ATX_RUN([
check-fs dents -n 1024 -s "$seed" $name check-fs dents -n 1024 $name
]) ])
]) ])
]) ])

View file

@ -12,10 +12,9 @@ AT_KEYWORDS([fs regrw])
ATX_CHECK_FS_NEW_ANY([file_max=524288], , [ ATX_CHECK_FS_NEW_ANY([file_max=524288], , [
name=$(ath_fn_rand_u64_hex) name=$(ath_fn_rand_u64_hex)
seed=$(ath_fn_prng_seed)
ATX_RUN([ ATX_RUN([
check-fs regrw -n 524288 -s "$seed" $name check-fs regrw -n 524288 $name
]) ])
]) ])

View file

@ -12,11 +12,10 @@ AT_KEYWORDS([fs times])
ATX_CHECK_FS_NEW_ANY(, , [ ATX_CHECK_FS_NEW_ANY(, , [
name=$(ath_fn_rand_u64_hex) name=$(ath_fn_rand_u64_hex)
seed=$(ath_fn_prng_seed)
mkdir $name mkdir $name
ATX_RUN([ ATX_RUN([
check-fs times -s "$seed" -r 512 $name check-fs times -r 512 $name
]) ])
]) ])

View file

@ -11,11 +11,6 @@ AT_SETUP([util lib: hashmap])
AT_KEYWORDS([lib hashmap]) AT_KEYWORDS([lib hashmap])
ATX_CHECK_LIB([ ATX_CHECK_LIB([
seed="${CHECK_HASHMAP_PRNG_SEED}"
if test -z "$seed"; then
seed=$(ath_fn_prng_seed)
fi
size="${CHECK_HASHMAP_DATA_SIZE}" size="${CHECK_HASHMAP_DATA_SIZE}"
if test -z "$size"; then if test -z "$size"; then
# Requires at least 128 MiB of memory on a 64-bit platform. # Requires at least 128 MiB of memory on a 64-bit platform.
@ -28,7 +23,7 @@ ATX_CHECK_LIB([
fi fi
ATX_RUN([ ATX_RUN([
check-util-lib hashmap -s "$seed" -n "$size" -r "$rounds" check-util-lib hashmap -n "$size" -r "$rounds"
]) ])
]) ])

View file

@ -15,11 +15,15 @@ AT_KEYWORDS([lib prng])
# For reliable testing of PRNGs, see <https://prng.di.unimi.it/#quality>. # For reliable testing of PRNGs, see <https://prng.di.unimi.it/#quality>.
ATX_CHECK_LIB([ ATX_CHECK_LIB([
gen_num() { 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], [ repeat=1
seed=$(ath_fn_prng_seed) 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 count=32
num_1=$(gen_num $seed $count) num_1=$(gen_num $seed $count)

View file

@ -199,15 +199,6 @@ AT_TEST_HELPER_FN([rand_base64], , , [
echo $(head -c$1 /dev/urandom | base64 -w0) 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 -- dnl -- Test groups --
AT_BANNER([The Utility Library]) AT_BANNER([The Utility Library])