aboutsummaryrefslogtreecommitdiff
path: root/src/test/rng_test_helpers.c
diff options
context:
space:
mode:
authorDavid Goulet <dgoulet@torproject.org>2019-05-23 09:50:28 -0400
committerDavid Goulet <dgoulet@torproject.org>2019-05-23 09:50:28 -0400
commit29955f13e5bc8e61724759ec7245aae602672111 (patch)
tree5e432fe63b8b94593ac0114b0b389cc439809e27 /src/test/rng_test_helpers.c
parente13e2012b9d1bbde73bea22d9fd13fb0b88c04de (diff)
parent9e5c27bd2c278238d003b2fc1891d5de48b766d7 (diff)
downloadtor-29955f13e5bc8e61724759ec7245aae602672111.tar.gz
tor-29955f13e5bc8e61724759ec7245aae602672111.zip
Merge branch 'tor-github/pr/1022'
Diffstat (limited to 'src/test/rng_test_helpers.c')
-rw-r--r--src/test/rng_test_helpers.c36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/test/rng_test_helpers.c b/src/test/rng_test_helpers.c
index d268cb64b7..f4e8df7e7b 100644
--- a/src/test/rng_test_helpers.c
+++ b/src/test/rng_test_helpers.c
@@ -17,6 +17,7 @@
#include "core/or/or.h"
#include "lib/crypt_ops/crypto_rand.h"
+#include "ext/tinytest.h"
#include "test/rng_test_helpers.h"
@@ -54,7 +55,8 @@ static uint8_t rng_seed[16];
static crypto_xof_t *rng_xof = NULL;
/**
- * Print the seed for our PRNG to stdout. We use this when we're
+ * Print the seed for our PRNG to stdout. We use this when we're failed
+ * test that had a reproducible RNG set.
**/
void
testing_dump_reproducible_rng_seed(void)
@@ -122,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));
+ }
}
/**
@@ -228,3 +243,16 @@ testing_disable_rng_override(void)
rng_is_replaced = false;
}
+
+/**
+ * As testing_disable_rng_override(), but dump the seed if the current
+ * test has failed.
+ */
+void
+testing_disable_reproducible_rng(void)
+{
+ if (tinytest_cur_test_has_failed()) {
+ testing_dump_reproducible_rng_seed();
+ }
+ testing_disable_rng_override();
+}