Skip to content

Commit fd3e797

Browse files
committed
Make unit tests use the insecure_rand_ctx exclusively
1 parent 8d98d42 commit fd3e797

File tree

3 files changed

+15
-32
lines changed

3 files changed

+15
-32
lines changed

src/test/cuckoocache_tests.cpp

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,23 @@
2121
* using BOOST_CHECK_CLOSE to fail.
2222
*
2323
*/
24-
FastRandomContext local_rand_ctx(true);
25-
2624
BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
2725

28-
29-
/** insecure_GetRandHash fills in a uint256 from local_rand_ctx
30-
*/
31-
static void insecure_GetRandHash(uint256& t)
32-
{
33-
uint32_t* ptr = (uint32_t*)t.begin();
34-
for (uint8_t j = 0; j < 8; ++j)
35-
*(ptr++) = local_rand_ctx.rand32();
36-
}
37-
38-
39-
4026
/* Test that no values not inserted into the cache are read out of it.
4127
*
4228
* There are no repeats in the first 200000 insecure_GetRandHash calls
4329
*/
4430
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
4531
{
46-
local_rand_ctx = FastRandomContext(true);
32+
SeedInsecureRand(true);
4733
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
4834
size_t megabytes = 4;
4935
cc.setup_bytes(megabytes << 20);
50-
uint256 v;
5136
for (int x = 0; x < 100000; ++x) {
52-
insecure_GetRandHash(v);
53-
cc.insert(v);
37+
cc.insert(InsecureRand256());
5438
}
5539
for (int x = 0; x < 100000; ++x) {
56-
insecure_GetRandHash(v);
57-
BOOST_CHECK(!cc.contains(v, false));
40+
BOOST_CHECK(!cc.contains(InsecureRand256(), false));
5841
}
5942
};
6043

@@ -64,7 +47,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
6447
template <typename Cache>
6548
static double test_cache(size_t megabytes, double load)
6649
{
67-
local_rand_ctx = FastRandomContext(true);
50+
SeedInsecureRand(true);
6851
std::vector<uint256> hashes;
6952
Cache set{};
7053
size_t bytes = megabytes * (1 << 20);
@@ -74,7 +57,7 @@ static double test_cache(size_t megabytes, double load)
7457
for (uint32_t i = 0; i < n_insert; ++i) {
7558
uint32_t* ptr = (uint32_t*)hashes[i].begin();
7659
for (uint8_t j = 0; j < 8; ++j)
77-
*(ptr++) = local_rand_ctx.rand32();
60+
*(ptr++) = InsecureRand32();
7861
}
7962
/** We make a copy of the hashes because future optimizations of the
8063
* cuckoocache may overwrite the inserted element, so the test is
@@ -135,7 +118,7 @@ template <typename Cache>
135118
static void test_cache_erase(size_t megabytes)
136119
{
137120
double load = 1;
138-
local_rand_ctx = FastRandomContext(true);
121+
SeedInsecureRand(true);
139122
std::vector<uint256> hashes;
140123
Cache set{};
141124
size_t bytes = megabytes * (1 << 20);
@@ -145,7 +128,7 @@ static void test_cache_erase(size_t megabytes)
145128
for (uint32_t i = 0; i < n_insert; ++i) {
146129
uint32_t* ptr = (uint32_t*)hashes[i].begin();
147130
for (uint8_t j = 0; j < 8; ++j)
148-
*(ptr++) = local_rand_ctx.rand32();
131+
*(ptr++) = InsecureRand32();
149132
}
150133
/** We make a copy of the hashes because future optimizations of the
151134
* cuckoocache may overwrite the inserted element, so the test is
@@ -198,7 +181,7 @@ template <typename Cache>
198181
static void test_cache_erase_parallel(size_t megabytes)
199182
{
200183
double load = 1;
201-
local_rand_ctx = FastRandomContext(true);
184+
SeedInsecureRand(true);
202185
std::vector<uint256> hashes;
203186
Cache set{};
204187
size_t bytes = megabytes * (1 << 20);
@@ -208,7 +191,7 @@ static void test_cache_erase_parallel(size_t megabytes)
208191
for (uint32_t i = 0; i < n_insert; ++i) {
209192
uint32_t* ptr = (uint32_t*)hashes[i].begin();
210193
for (uint8_t j = 0; j < 8; ++j)
211-
*(ptr++) = local_rand_ctx.rand32();
194+
*(ptr++) = InsecureRand32();
212195
}
213196
/** We make a copy of the hashes because future optimizations of the
214197
* cuckoocache may overwrite the inserted element, so the test is
@@ -300,7 +283,7 @@ static void test_cache_generations()
300283
// iterations with non-deterministic values, so it isn't "overfit" to the
301284
// specific entropy in FastRandomContext(true) and implementation of the
302285
// cache.
303-
local_rand_ctx = FastRandomContext(true);
286+
SeedInsecureRand(true);
304287

305288
// block_activity models a chunk of network activity. n_insert elements are
306289
// added to the cache. The first and last n/4 are stored for removal later
@@ -317,7 +300,7 @@ static void test_cache_generations()
317300
for (uint32_t i = 0; i < n_insert; ++i) {
318301
uint32_t* ptr = (uint32_t*)inserts[i].begin();
319302
for (uint8_t j = 0; j < 8; ++j)
320-
*(ptr++) = local_rand_ctx.rand32();
303+
*(ptr++) = InsecureRand32();
321304
}
322305
for (uint32_t i = 0; i < n_insert / 4; ++i)
323306
reads.push_back(inserts[i]);

src/test/denialofservice_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
111111

112112
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
113113
{
114-
CAddress addr(ip(GetRandInt(0xffffffff)), NODE_NONE);
114+
CAddress addr(ip(insecure_rand_ctx.randbits(32)), NODE_NONE);
115115
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
116116
CNode &node = *vNodes.back();
117117
node.SetSendVersion(PROTOCOL_VERSION);

src/test/validation_block_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ void BuildChain(const uint256& root, int height, const unsigned int invalid_rate
104104
{
105105
if (height <= 0 || blocks.size() >= max_size) return;
106106

107-
bool gen_invalid = GetRand(100) < invalid_rate;
108-
bool gen_fork = GetRand(100) < branch_rate;
107+
bool gen_invalid = InsecureRandRange(100) < invalid_rate;
108+
bool gen_fork = InsecureRandRange(100) < branch_rate;
109109

110110
const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
111111
blocks.push_back(pblock);
@@ -157,7 +157,7 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
157157
threads.create_thread([&blocks]() {
158158
bool ignored;
159159
for (int i = 0; i < 1000; i++) {
160-
auto block = blocks[GetRand(blocks.size() - 1)];
160+
auto block = blocks[InsecureRandRange(blocks.size() - 1)];
161161
ProcessNewBlock(Params(), block, true, &ignored);
162162
}
163163

0 commit comments

Comments
 (0)