diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2024-08-16 11:02:54 +0200 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2024-08-26 11:19:16 +0200 |
commit | fa19af555dff6d6c722caf36319b158699d2aa95 (patch) | |
tree | f5841fbdd2992bf6a9848b301d135dc26cb8e0e8 /src/test | |
parent | 3dc527f4602297ffcec3a578eadc480a620d01ec (diff) |
test: refactor: Move g_insecure_rand_ctx.Reseed out of the helper that calls MakeRandDeterministicDANGEROUS
The global g_insecure_rand_ctx will be removed in the future, so
removing it from this helper is useful.
Also, tying the two concepts of the global internal RNGState and the
global test-only rng context is a bit confusing, because tests can
simply use the m_rng, if it exists. Also, tests may seed more than one
random context, or none at all, or a random context of a different type.
Fix all issues by moving the Reseed call to the two places where it is
used.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/fuzz/fuzz.cpp | 3 | ||||
-rw-r--r-- | src/test/util/random.cpp | 3 | ||||
-rw-r--r-- | src/test/util/random.h | 4 | ||||
-rw-r--r-- | src/test/util/setup_common.h | 7 |
4 files changed, 12 insertions, 5 deletions
diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp index 96283a3e15..61f1d313bf 100644 --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -106,7 +106,8 @@ void initialize() // randomness during the fuzz test, except: // - GetStrongRandBytes(), which is used for the creation of private key material. // - Creating a BasicTestingSetup or derived class will switch to a random seed. - SeedRandomForTest(SeedRand::ZEROS); + SeedRandomStateForTest(SeedRand::ZEROS); + g_insecure_rand_ctx.Reseed(GetRandHash()); // Terminate immediately if a fuzzing harness ever tries to create a socket. // Individual tests can override this by pointing CreateSock to a mocked alternative. diff --git a/src/test/util/random.cpp b/src/test/util/random.cpp index 47d03055e2..da2f51ffbc 100644 --- a/src/test/util/random.cpp +++ b/src/test/util/random.cpp @@ -15,7 +15,7 @@ FastRandomContext g_insecure_rand_ctx; extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept; -void SeedRandomForTest(SeedRand seedtype) +void SeedRandomStateForTest(SeedRand seedtype) { static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"}; @@ -34,5 +34,4 @@ void SeedRandomForTest(SeedRand seedtype) const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO}; LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex()); MakeRandDeterministicDANGEROUS(seed); - g_insecure_rand_ctx.Reseed(GetRandHash()); } diff --git a/src/test/util/random.h b/src/test/util/random.h index 09a475f8b3..5ffb06bde7 100644 --- a/src/test/util/random.h +++ b/src/test/util/random.h @@ -24,8 +24,8 @@ enum class SeedRand { SEED, //!< Use (and report) random seed from environment, or a (truly) random one. }; -/** Seed the RNG for testing. This affects all randomness, except GetStrongRandBytes(). */ -void SeedRandomForTest(SeedRand seed = SeedRand::SEED); +/** Seed the global RNG state for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */ +void SeedRandomStateForTest(SeedRand seed); static inline uint32_t InsecureRand32() { diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 5b8db6ed76..677355c9f5 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -65,7 +65,14 @@ struct TestOpts { struct BasicTestingSetup { util::SignalInterrupt m_interrupt; node::NodeContext m_node; // keep as first member to be destructed last + FastRandomContext& m_rng{g_insecure_rand_ctx}; // Alias (reference) for the global, to allow easy removal of the global in the future. + /** Seed the global RNG state and m_rng for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */ + void SeedRandomForTest(SeedRand seed = SeedRand::SEED) + { + SeedRandomStateForTest(seed); + m_rng.Reseed(GetRandHash()); + } explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, TestOpts = {}); ~BasicTestingSetup(); |