Skip to content

Commit fae43a9

Browse files
author
MarcoFalke
committed
test: Seed test RNG context for each test case, print seed
1 parent ab765c2 commit fae43a9

File tree

9 files changed

+46
-15
lines changed

9 files changed

+46
-15
lines changed

src/test/bloom_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static std::vector<unsigned char> RandomData()
461461

462462
BOOST_AUTO_TEST_CASE(rolling_bloom)
463463
{
464-
SeedInsecureRand(/* deterministic */ true);
464+
SeedInsecureRand(SeedRand::ZEROS);
465465
g_mock_deterministic_tests = true;
466466

467467
// last-100-entry, 1% false positive:

src/test/coins_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
279279
// has the expected effect (the other duplicate is overwritten at all cache levels)
280280
BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
281281
{
282-
SeedInsecureRand(/* deterministic */ true);
282+
SeedInsecureRand(SeedRand::ZEROS);
283283
g_mock_deterministic_tests = true;
284284

285285
bool spent_a_duplicate_coinbase = false;

src/test/cuckoocache_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
2929
*/
3030
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
3131
{
32-
SeedInsecureRand(true);
32+
SeedInsecureRand(SeedRand::ZEROS);
3333
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
3434
size_t megabytes = 4;
3535
cc.setup_bytes(megabytes << 20);
@@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
4747
template <typename Cache>
4848
static double test_cache(size_t megabytes, double load)
4949
{
50-
SeedInsecureRand(true);
50+
SeedInsecureRand(SeedRand::ZEROS);
5151
std::vector<uint256> hashes;
5252
Cache set{};
5353
size_t bytes = megabytes * (1 << 20);
@@ -118,7 +118,7 @@ template <typename Cache>
118118
static void test_cache_erase(size_t megabytes)
119119
{
120120
double load = 1;
121-
SeedInsecureRand(true);
121+
SeedInsecureRand(SeedRand::ZEROS);
122122
std::vector<uint256> hashes;
123123
Cache set{};
124124
size_t bytes = megabytes * (1 << 20);
@@ -181,7 +181,7 @@ template <typename Cache>
181181
static void test_cache_erase_parallel(size_t megabytes)
182182
{
183183
double load = 1;
184-
SeedInsecureRand(true);
184+
SeedInsecureRand(SeedRand::ZEROS);
185185
std::vector<uint256> hashes;
186186
Cache set{};
187187
size_t bytes = megabytes * (1 << 20);
@@ -285,7 +285,7 @@ static void test_cache_generations()
285285
// iterations with non-deterministic values, so it isn't "overfit" to the
286286
// specific entropy in FastRandomContext(true) and implementation of the
287287
// cache.
288-
SeedInsecureRand(true);
288+
SeedInsecureRand(SeedRand::ZEROS);
289289

290290
// block_activity models a chunk of network activity. n_insert elements are
291291
// added to the cache. The first and last n/4 are stored for removal later

src/test/pmt_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
3030

3131
BOOST_AUTO_TEST_CASE(pmt_test1)
3232
{
33-
SeedInsecureRand(false);
3433
static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
3534

3635
for (int i = 0; i < 12; i++) {

src/test/setup_common.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@
3333
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
3434

3535
FastRandomContext g_insecure_rand_ctx;
36+
/** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
37+
static FastRandomContext g_insecure_rand_ctx_temp_path;
38+
39+
/** Return the unsigned from the environment var if available, otherwise 0 */
40+
static uint256 GetUintFromEnv(const std::string& env_name)
41+
{
42+
const char* num = std::getenv(env_name.c_str());
43+
if (!num) return {};
44+
return uint256S(num);
45+
}
46+
47+
void Seed(FastRandomContext& ctx)
48+
{
49+
// Should be enough to get the seed once for the process
50+
static uint256 seed{};
51+
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
52+
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
53+
if (seed.IsNull()) seed = GetRandHash();
54+
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
55+
ctx = FastRandomContext(seed);
56+
}
3657

3758
std::ostream& operator<<(std::ostream& os, const uint256& num)
3859
{
@@ -41,12 +62,13 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
4162
}
4263

4364
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
44-
: m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
65+
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / std::to_string(g_insecure_rand_ctx_temp_path.rand32())}
4566
{
4667
fs::create_directories(m_path_root);
4768
gArgs.ForceSetArg("-datadir", m_path_root.string());
4869
ClearDatadirCache();
4970
SelectParams(chainName);
71+
SeedInsecureRand();
5072
gArgs.ForceSetArg("-printtoconsole", "0");
5173
InitLogging();
5274
LogInstance().StartLogging();

src/test/setup_common.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,21 @@ extern FastRandomContext g_insecure_rand_ctx;
3838
*/
3939
extern bool g_mock_deterministic_tests;
4040

41-
static inline void SeedInsecureRand(bool deterministic = false)
41+
enum class SeedRand {
42+
ZEROS, //!< Seed with a compile time constant of zeros
43+
SEED, //!< Call the Seed() helper
44+
};
45+
46+
/** Seed the given random ctx or use the seed passed in via an environment var */
47+
void Seed(FastRandomContext& ctx);
48+
49+
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
4250
{
43-
g_insecure_rand_ctx = FastRandomContext(deterministic);
51+
if (seed == SeedRand::ZEROS) {
52+
g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
53+
} else {
54+
Seed(g_insecure_rand_ctx);
55+
}
4456
}
4557

4658
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }

src/test/sighash_tests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
119119

120120
BOOST_AUTO_TEST_CASE(sighash_test)
121121
{
122-
SeedInsecureRand(false);
123-
124122
#if defined(PRINT_SIGHASH_JSON)
125123
std::cout << "[\n";
126124
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";

src/test/streams_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
339339
BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
340340
{
341341
// Make this test deterministic.
342-
SeedInsecureRand(true);
342+
SeedInsecureRand(SeedRand::ZEROS);
343343

344344
for (int rep = 0; rep < 50; ++rep) {
345345
FILE* file = fsbridge::fopen("streams_test_tmp", "w+b");

src/test/util_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ BOOST_AUTO_TEST_CASE(util_IsHexNumber)
10361036

10371037
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
10381038
{
1039-
SeedInsecureRand(true);
1039+
SeedInsecureRand(SeedRand::ZEROS);
10401040
for (int mod=2;mod<11;mod++)
10411041
{
10421042
int mask = 1;

0 commit comments

Comments
 (0)