Skip to content

Commit 97e16f5

Browse files
committed
tests: make fuzz tests (mostly) deterministic with fixed seed
1 parent 2c91330 commit 97e16f5

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/test/fuzz/fuzz.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ void ResetCoverageCounters() {}
102102

103103
void initialize()
104104
{
105+
// By default, make the RNG deterministic with a fixed seed. This will affect all
106+
// randomness during the fuzz test, except:
107+
// - GetStrongRandBytes(), which is used for the creation of private key material.
108+
// - Creating a BasicTestingSetup or derived class will switch to a random seed.
109+
SeedRandomForTest(SeedRand::ZEROS);
110+
105111
// Terminate immediately if a fuzzing harness ever tries to create a socket.
106112
// Individual tests can override this by pointing CreateSock to a mocked alternative.
107113
CreateSock = [](int, int, int) -> std::unique_ptr<Sock> { std::terminate(); };

src/test/util/setup_common.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, const std::vecto
145145
}
146146
}
147147

148+
// Use randomly chosen seed for deterministic PRNG, so that (by default) test
149+
// data directories use a random name that doesn't overlap with other tests.
150+
SeedRandomForTest(SeedRand::SEED);
151+
148152
if (!m_node.args->IsArgSet("-testdatadir")) {
149153
// By default, the data directory has a random name
150154
const auto rand_str{g_insecure_rand_ctx_temp_path.rand256().ToString()};
@@ -178,7 +182,6 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, const std::vecto
178182
gArgs.ForceSetArg("-datadir", fs::PathToString(m_path_root));
179183

180184
SelectParams(chainType);
181-
SeedRandomForTest();
182185
if (G_TEST_LOG_FUN) LogInstance().PushBackCallback(G_TEST_LOG_FUN);
183186
InitLogging(*m_node.args);
184187
AppInitParameterInteraction(*m_node.args);

0 commit comments

Comments
 (0)