Testing: allow the user to pass a seed in for reproducible-RNG tests

The environment variable TOR_TEST_RNG_SEED, if provided, is a hex
value for the RNG seed.
This commit is contained in:
Nick Mathewson 2019-05-14 20:05:53 -04:00
parent 2d467544fe
commit 72e9c427b8

View File

@ -124,9 +124,22 @@ enable_deterministic_rng_impl(const uint8_t *seed, size_t seed_len)
void
testing_enable_reproducible_rng(void)
{
uint8_t seed[16];
crypto_rand((char*)seed, sizeof(seed));
enable_deterministic_rng_impl(seed, sizeof(seed));
const char *provided_seed = getenv("TOR_TEST_RNG_SEED");
if (provided_seed) {
size_t hexlen = strlen(provided_seed);
size_t seedlen = hexlen / 2;
uint8_t *seed = tor_malloc(hexlen / 2);
if (base16_decode((char*)seed, seedlen, provided_seed, hexlen) < 0) {
puts("Cannot decode value in TOR_TEST_RNG_SEED");
exit(1);
}
enable_deterministic_rng_impl(seed, seedlen);
tor_free(seed);
} else {
uint8_t seed[16];
crypto_rand((char*)seed, sizeof(seed));
enable_deterministic_rng_impl(seed, sizeof(seed));
}
}
/**