Skip to content

Commit fa19af5

Browse files
author
MarcoFalke
committed
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.
1 parent 3dc527f commit fa19af5

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

src/test/fuzz/fuzz.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ void initialize()
106106
// randomness during the fuzz test, except:
107107
// - GetStrongRandBytes(), which is used for the creation of private key material.
108108
// - Creating a BasicTestingSetup or derived class will switch to a random seed.
109-
SeedRandomForTest(SeedRand::ZEROS);
109+
SeedRandomStateForTest(SeedRand::ZEROS);
110+
g_insecure_rand_ctx.Reseed(GetRandHash());
110111

111112
// Terminate immediately if a fuzzing harness ever tries to create a socket.
112113
// Individual tests can override this by pointing CreateSock to a mocked alternative.

src/test/util/random.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ FastRandomContext g_insecure_rand_ctx;
1515

1616
extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
1717

18-
void SeedRandomForTest(SeedRand seedtype)
18+
void SeedRandomStateForTest(SeedRand seedtype)
1919
{
2020
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
2121

@@ -34,5 +34,4 @@ void SeedRandomForTest(SeedRand seedtype)
3434
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
3535
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
3636
MakeRandDeterministicDANGEROUS(seed);
37-
g_insecure_rand_ctx.Reseed(GetRandHash());
3837
}

src/test/util/random.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ enum class SeedRand {
2424
SEED, //!< Use (and report) random seed from environment, or a (truly) random one.
2525
};
2626

27-
/** Seed the RNG for testing. This affects all randomness, except GetStrongRandBytes(). */
28-
void SeedRandomForTest(SeedRand seed = SeedRand::SEED);
27+
/** Seed the global RNG state for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */
28+
void SeedRandomStateForTest(SeedRand seed);
2929

3030
static inline uint32_t InsecureRand32()
3131
{

src/test/util/setup_common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ struct TestOpts {
6565
struct BasicTestingSetup {
6666
util::SignalInterrupt m_interrupt;
6767
node::NodeContext m_node; // keep as first member to be destructed last
68+
6869
FastRandomContext& m_rng{g_insecure_rand_ctx}; // Alias (reference) for the global, to allow easy removal of the global in the future.
70+
/** Seed the global RNG state and m_rng for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */
71+
void SeedRandomForTest(SeedRand seed = SeedRand::SEED)
72+
{
73+
SeedRandomStateForTest(seed);
74+
m_rng.Reseed(GetRandHash());
75+
}
6976

7077
explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, TestOpts = {});
7178
~BasicTestingSetup();

0 commit comments

Comments
 (0)