bookmarkfs/tests/check_util.c
CismonX ffa28a4091
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.
2025-06-19 16:51:35 +08:00

50 lines
1.3 KiB
C

/**
* bookmarkfs/tests/check_util.c
* ----
*
* Copyright (C) 2025 CismonX <admin@cismon.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "check_util.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "prng.h"
int
prng_seed_from_env (void)
{
char const *seed_str = getenv("BOOKMARKFS_TEST_PRNG_SEED");
if (seed_str == NULL) {
return prng_seed(NULL);
}
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);
}