Skip to content

Commit f8c8a0a

Browse files
committed
test: Seed test RNG context for each test case, print seed
also rename the global `insecure_rand_ctx` to `g_insecure_rand_ctx`
1 parent 83743a0 commit f8c8a0a

File tree

10 files changed

+58
-26
lines changed

10 files changed

+58
-26
lines changed

src/test/DoS_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ CTransactionRef RandomOrphan()
167167
static void MakeNewKeyWithFastRandomContext(CKey& key)
168168
{
169169
std::vector<unsigned char> keydata;
170-
keydata = insecure_rand_ctx.randbytes(32);
170+
keydata = g_insecure_rand_ctx.randbytes(32);
171171
key.Set(keydata.data(), keydata.data() + keydata.size(), /*fCompressedIn*/ true);
172172
assert(key.IsValid());
173173
}
@@ -180,7 +180,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
180180
// ecdsa_signature_parse_der_lax are executed during this test.
181181
// Specifically branches that run only when an ECDSA
182182
// signature's R and S values have leading zeros.
183-
insecure_rand_ctx = FastRandomContext(ArithToUint256(arith_uint256(33)));
183+
g_insecure_rand_ctx = FastRandomContext(ArithToUint256(arith_uint256(33)));
184184

185185
CKey key;
186186
MakeNewKeyWithFastRandomContext(key);

src/test/base58_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
272272
for (int n = 0; n < 1000; ++n) {
273273
unsigned int len = 1 + InsecureRandBits(8);
274274
unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
275-
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), insecure_rand_ctx.randbytes(len - zeroes));
275+
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
276276
auto encoded = EncodeBase58Check(data);
277277
std::vector<unsigned char> decoded;
278278
auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));

src/test/bloom_tests.cpp

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

461461
BOOST_AUTO_TEST_CASE(rolling_bloom)
462462
{
463-
SeedInsecureRand(true);
463+
SeedInsecureRand(SeedRand::ZEROS);
464464
g_mock_deterministic_tests = true;
465465

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

src/test/cuckoocache_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
3333
*/
3434
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
3535
{
36-
SeedInsecureRand(true);
36+
SeedInsecureRand(SeedRand::ZEROS);
3737
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
3838
size_t megabytes = 4;
3939
cc.setup_bytes(megabytes << 20);
@@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
5252
template <typename Cache>
5353
double test_cache(size_t megabytes, double load)
5454
{
55-
SeedInsecureRand(true);
55+
SeedInsecureRand(SeedRand::ZEROS);
5656
std::vector<uint256> hashes;
5757
Cache set{};
5858
size_t bytes = megabytes * (1 << 20);
@@ -123,7 +123,7 @@ template <typename Cache>
123123
void test_cache_erase(size_t megabytes)
124124
{
125125
double load = 1;
126-
SeedInsecureRand(true);
126+
SeedInsecureRand(SeedRand::ZEROS);
127127
std::vector<uint256> hashes;
128128
Cache set{};
129129
size_t bytes = megabytes * (1 << 20);
@@ -186,7 +186,7 @@ template <typename Cache>
186186
void test_cache_erase_parallel(size_t megabytes)
187187
{
188188
double load = 1;
189-
SeedInsecureRand(true);
189+
SeedInsecureRand(SeedRand::ZEROS);
190190
std::vector<uint256> hashes;
191191
Cache set{};
192192
size_t bytes = megabytes * (1 << 20);
@@ -288,7 +288,7 @@ void test_cache_generations()
288288
// iterations with non-deterministic values, so it isn't "overfit" to the
289289
// specific entropy in FastRandomContext(true) and implementation of the
290290
// cache.
291-
SeedInsecureRand(true);
291+
SeedInsecureRand(SeedRand::ZEROS);
292292

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

src/test/net_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CAddrManSerializationMock : public CAddrMan
3030
void MakeDeterministic()
3131
{
3232
nKey.SetNull();
33-
SeedInsecureRand(true);
33+
SeedInsecureRand(SeedRand::ZEROS);
3434
}
3535
};
3636

src/test/sighash_tests.cpp

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

149149
BOOST_AUTO_TEST_CASE(sighash_test)
150150
{
151-
SeedInsecureRand(false);
152-
153151
#if defined(PRINT_SIGHASH_JSON)
154152
std::cout << "[\n";
155153
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
@@ -200,7 +200,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
200200
BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
201201
{
202202
// Make this test deterministic.
203-
SeedInsecureRand(true);
203+
SeedInsecureRand(SeedRand::ZEROS);
204204

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

src/test/test_pivx.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,28 @@ std::unique_ptr<CConnman> g_connman;
3232

3333
CClientUIInterface uiInterface; // Declared but not defined in guiinterface.h
3434

35-
FastRandomContext insecure_rand_ctx;
35+
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
extern bool fPrintToConsole;
3859
extern void noui_connect();
@@ -44,14 +65,15 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
4465
}
4566

4667
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
47-
: m_path_root(fs::temp_directory_path() / "test_pivx" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
68+
: m_path_root{fs::temp_directory_path() / "test_pivx" / std::to_string(g_insecure_rand_ctx_temp_path.rand32())}
4869
{
4970
ECC_Start();
5071
BLSInit();
5172
SetupEnvironment();
5273
InitSignatureCache();
5374
fCheckBlockIndex = true;
5475
SelectParams(chainName);
76+
SeedInsecureRand();
5577
evoDb.reset(new CEvoDB(1 << 20, true, true));
5678
deterministicMNManager.reset(new CDeterministicMNManager(*evoDb));
5779
}

src/test/test_pivx.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,37 @@
1313

1414
#include <boost/thread.hpp>
1515

16-
extern FastRandomContext insecure_rand_ctx;
16+
extern FastRandomContext g_insecure_rand_ctx;
1717

1818
/**
1919
* Flag to make GetRand in random.h return the same number
2020
*/
2121
extern bool g_mock_deterministic_tests;
2222

23-
static inline void SeedInsecureRand(bool deterministic = false)
23+
enum class SeedRand {
24+
ZEROS, //!< Seed with a compile time constant of zeros
25+
SEED, //!< Call the Seed() helper
26+
};
27+
28+
/** Seed the given random ctx or use the seed passed in via an environment var */
29+
void Seed(FastRandomContext& ctx);
30+
31+
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
2432
{
25-
insecure_rand_ctx = FastRandomContext(deterministic);
33+
if (seed == SeedRand::ZEROS) {
34+
g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
35+
} else {
36+
Seed(g_insecure_rand_ctx);
37+
}
2638
}
2739

28-
static inline uint16_t InsecureRand16() { return insecure_rand_ctx.rand16(); }
29-
static inline uint32_t InsecureRand32() { return insecure_rand_ctx.rand32(); }
30-
static inline uint256 InsecureRand256() { return insecure_rand_ctx.rand256(); }
31-
static inline uint64_t InsecureRandBits(int bits) { return insecure_rand_ctx.randbits(bits); }
32-
static inline uint64_t InsecureRandRange(uint64_t range) { return insecure_rand_ctx.randrange(range); }
33-
static inline bool InsecureRandBool() { return insecure_rand_ctx.randbool(); }
34-
static inline std::vector<unsigned char> InsecureRandBytes(size_t len) { return insecure_rand_ctx.randbytes(len); }
40+
static inline uint16_t InsecureRand16() { return g_insecure_rand_ctx.rand16(); }
41+
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
42+
static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
43+
static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
44+
static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
45+
static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }
46+
static inline std::vector<unsigned char> InsecureRandBytes(size_t len) { return g_insecure_rand_ctx.randbytes(len); }
3547

3648
/** Basic testing setup.
3749
* This just configures logging and chain parameters.

src/test/util_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
647647
int i;
648648
int count=0;
649649

650-
SeedInsecureRand(true);
650+
SeedInsecureRand(SeedRand::ZEROS);
651651

652652
for (int mod=2;mod<11;mod++)
653653
{

0 commit comments

Comments
 (0)