Skip to content

Commit fa0fe08

Browse files
author
MarcoFalke
committed
scripted-diff: [test] Use g_rng/m_rng directly
-BEGIN VERIFY SCRIPT- # Use m_rng in unit test files ren() { sed -i "s:\<$1\>:$2:g" $( git grep -l "$1" src/test/*.cpp src/wallet/test/*.cpp src/test/util/setup_common.cpp ) ; } ren InsecureRand32 m_rng.rand32 ren InsecureRand256 m_rng.rand256 ren InsecureRandBits m_rng.randbits ren InsecureRandRange m_rng.randrange ren InsecureRandBool m_rng.randbool ren g_insecure_rand_ctx m_rng ren g_insecure_rand_ctx_temp_path g_rng_temp_path -END VERIFY SCRIPT-
1 parent fa54cab commit fa0fe08

40 files changed

+286
-286
lines changed

src/test/base58_tests.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
8484
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
8585
{
8686
for (int n = 0; n < 1000; ++n) {
87-
unsigned int len = 1 + InsecureRandBits(8);
88-
unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
89-
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
87+
unsigned int len = 1 + m_rng.randbits(8);
88+
unsigned int zeroes = m_rng.randbool() ? m_rng.randrange(len + 1) : 0;
89+
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), m_rng.randbytes(len - zeroes));
9090
auto encoded = EncodeBase58Check(data);
9191
std::vector<unsigned char> decoded;
92-
auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
92+
auto ok_too_small = DecodeBase58Check(encoded, decoded, m_rng.randrange(len));
9393
BOOST_CHECK(!ok_too_small);
94-
auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
94+
auto ok = DecodeBase58Check(encoded, decoded, len + m_rng.randrange(257 - len));
9595
BOOST_CHECK(ok);
9696
BOOST_CHECK(data == decoded);
9797
}

src/test/bip324_tests.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void TestBIP324PacketVector(
116116

117117
// Seek to the numbered packet.
118118
if (in_idx == 0 && error == 12) continue;
119-
uint32_t dec_idx = in_idx ^ (error == 12 ? (1U << InsecureRandRange(16)) : 0);
119+
uint32_t dec_idx = in_idx ^ (error == 12 ? (1U << m_rng.randrange(16)) : 0);
120120
for (uint32_t i = 0; i < dec_idx; ++i) {
121121
unsigned use_idx = i < in_idx ? i : 0;
122122
bool dec_ignore{false};
@@ -128,7 +128,7 @@ void TestBIP324PacketVector(
128128
// Decrypt length
129129
auto to_decrypt = ciphertext;
130130
if (error >= 2 && error <= 9) {
131-
to_decrypt[InsecureRandRange(to_decrypt.size())] ^= std::byte(1U << (error - 2));
131+
to_decrypt[m_rng.randrange(to_decrypt.size())] ^= std::byte(1U << (error - 2));
132132
}
133133

134134
// Decrypt length and resize ciphertext to accommodate.
@@ -139,7 +139,7 @@ void TestBIP324PacketVector(
139139
auto dec_aad = in_aad;
140140
if (error == 10) {
141141
if (in_aad.size() == 0) continue;
142-
dec_aad[InsecureRandRange(dec_aad.size())] ^= std::byte(1U << InsecureRandRange(8));
142+
dec_aad[m_rng.randrange(dec_aad.size())] ^= std::byte(1U << m_rng.randrange(8));
143143
}
144144
if (error == 11) dec_aad.push_back({});
145145

src/test/blockencodings_tests.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ BOOST_AUTO_TEST_CASE(ReceiveWithExtraTransactions) {
356356

357357
BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
358358
BlockTransactionsRequest req1;
359-
req1.blockhash = InsecureRand256();
359+
req1.blockhash = m_rng.rand256();
360360
req1.indexes.resize(4);
361361
req1.indexes[0] = 0;
362362
req1.indexes[1] = 1;
@@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
380380
BOOST_AUTO_TEST_CASE(TransactionsRequestDeserializationMaxTest) {
381381
// Check that the highest legal index is decoded correctly
382382
BlockTransactionsRequest req0;
383-
req0.blockhash = InsecureRand256();
383+
req0.blockhash = m_rng.rand256();
384384
req0.indexes.resize(1);
385385
req0.indexes[0] = 0xffff;
386386
DataStream stream{};
@@ -398,7 +398,7 @@ BOOST_AUTO_TEST_CASE(TransactionsRequestDeserializationOverflowTest) {
398398
// a request cannot be created by serializing a real BlockTransactionsRequest
399399
// due to the overflow, so here we'll serialize from raw deltas.
400400
BlockTransactionsRequest req0;
401-
req0.blockhash = InsecureRand256();
401+
req0.blockhash = m_rng.rand256();
402402
req0.indexes.resize(3);
403403
req0.indexes[0] = 0x7000;
404404
req0.indexes[1] = 0x10000 - 0x7000 - 2;

src/test/bloom_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
463463

464464
std::vector<unsigned char> BloomTest::RandomData()
465465
{
466-
uint256 r = InsecureRand256();
466+
uint256 r = m_rng.rand256();
467467
return std::vector<unsigned char>(r.begin(), r.end());
468468
}
469469

src/test/checkqueue_tests.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void CheckQueueTest::Correct_Queue_range(std::vector<size_t> range)
170170
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
171171
while (total) {
172172
vChecks.clear();
173-
vChecks.resize(std::min<size_t>(total, InsecureRandRange(10)));
173+
vChecks.resize(std::min<size_t>(total, m_rng.randrange(10)));
174174
total -= vChecks.size();
175175
control.Add(std::move(vChecks));
176176
}
@@ -211,7 +211,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
211211
{
212212
std::vector<size_t> range;
213213
range.reserve(100000/1000);
214-
for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)InsecureRandRange(std::min((size_t)1000, ((size_t)100000) - i))))
214+
for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)m_rng.randrange(std::min((size_t)1000, ((size_t)100000) - i))))
215215
range.push_back(i);
216216
Correct_Queue_range(range);
217217
}
@@ -225,7 +225,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
225225
CCheckQueueControl<FailingCheck> control(fail_queue.get());
226226
size_t remaining = i;
227227
while (remaining) {
228-
size_t r = InsecureRandRange(10);
228+
size_t r = m_rng.randrange(10);
229229

230230
std::vector<FailingCheck> vChecks;
231231
vChecks.reserve(r);
@@ -272,7 +272,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
272272
{
273273
CCheckQueueControl<UniqueCheck> control(queue.get());
274274
while (total) {
275-
size_t r = InsecureRandRange(10);
275+
size_t r = m_rng.randrange(10);
276276
std::vector<UniqueCheck> vChecks;
277277
for (size_t k = 0; k < r && total; k++)
278278
vChecks.emplace_back(--total);
@@ -304,7 +304,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
304304
{
305305
CCheckQueueControl<MemoryCheck> control(queue.get());
306306
while (total) {
307-
size_t r = InsecureRandRange(10);
307+
size_t r = m_rng.randrange(10);
308308
std::vector<MemoryCheck> vChecks;
309309
for (size_t k = 0; k < r && total; k++) {
310310
total--;

src/test/coins_tests.cpp

+50-50
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CCoinsViewTest : public CCoinsView
4949
return false;
5050
}
5151
coin = it->second;
52-
if (coin.IsSpent() && InsecureRandBool() == 0) {
52+
if (coin.IsSpent() && m_rng.randbool() == 0) {
5353
// Randomly return false in case of an empty entry.
5454
return false;
5555
}
@@ -64,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
6464
if (it->second.IsDirty()) {
6565
// Same optimization used in CCoinsViewDB is to only write dirty entries.
6666
map_[it->first] = it->second.coin;
67-
if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
67+
if (it->second.coin.IsSpent() && m_rng.randrange(3) == 0) {
6868
// Randomly delete empty entries on write.
6969
map_.erase(it->first);
7070
}
@@ -148,26 +148,26 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
148148
std::vector<Txid> txids;
149149
txids.resize(NUM_SIMULATION_ITERATIONS / 8);
150150
for (unsigned int i = 0; i < txids.size(); i++) {
151-
txids[i] = Txid::FromUint256(InsecureRand256());
151+
txids[i] = Txid::FromUint256(m_rng.rand256());
152152
}
153153

154154
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
155155
// Do a random modification.
156156
{
157-
auto txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
157+
auto txid = txids[m_rng.randrange(txids.size())]; // txid we're going to modify in this iteration.
158158
Coin& coin = result[COutPoint(txid, 0)];
159159

160160
// Determine whether to test HaveCoin before or after Access* (or both). As these functions
161161
// can influence each other's behaviour by pulling things into the cache, all combinations
162162
// are tested.
163-
bool test_havecoin_before = InsecureRandBits(2) == 0;
164-
bool test_havecoin_after = InsecureRandBits(2) == 0;
163+
bool test_havecoin_before = m_rng.randbits(2) == 0;
164+
bool test_havecoin_after = m_rng.randbits(2) == 0;
165165

166166
bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
167167

168168
// Infrequently, test usage of AccessByTxid instead of AccessCoin - the
169169
// former just delegates to the latter and returns the first unspent in a txn.
170-
const Coin& entry = (InsecureRandRange(500) == 0) ?
170+
const Coin& entry = (m_rng.randrange(500) == 0) ?
171171
AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
172172
BOOST_CHECK(coin == entry);
173173

@@ -180,23 +180,23 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
180180
BOOST_CHECK(ret == !entry.IsSpent());
181181
}
182182

183-
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
183+
if (m_rng.randrange(5) == 0 || coin.IsSpent()) {
184184
Coin newcoin;
185185
newcoin.out.nValue = RandMoney(m_rng);
186186
newcoin.nHeight = 1;
187187

188188
// Infrequently test adding unspendable coins.
189-
if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
190-
newcoin.out.scriptPubKey.assign(1 + InsecureRandBits(6), OP_RETURN);
189+
if (m_rng.randrange(16) == 0 && coin.IsSpent()) {
190+
newcoin.out.scriptPubKey.assign(1 + m_rng.randbits(6), OP_RETURN);
191191
BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
192192
added_an_unspendable_entry = true;
193193
} else {
194194
// Random sizes so we can test memory usage accounting
195-
newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0);
195+
newcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
196196
(coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
197197
coin = newcoin;
198198
}
199-
bool is_overwrite = !coin.IsSpent() || InsecureRand32() & 1;
199+
bool is_overwrite = !coin.IsSpent() || m_rng.rand32() & 1;
200200
stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), is_overwrite);
201201
} else {
202202
// Spend the coin.
@@ -207,15 +207,15 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
207207
}
208208

209209
// Once every 10 iterations, remove a random entry from the cache
210-
if (InsecureRandRange(10) == 0) {
211-
COutPoint out(txids[InsecureRand32() % txids.size()], 0);
212-
int cacheid = InsecureRand32() % stack.size();
210+
if (m_rng.randrange(10) == 0) {
211+
COutPoint out(txids[m_rng.rand32() % txids.size()], 0);
212+
int cacheid = m_rng.rand32() % stack.size();
213213
stack[cacheid]->Uncache(out);
214214
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
215215
}
216216

217217
// Once every 1000 iterations and at the end, verify the full cache.
218-
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
218+
if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
219219
for (const auto& entry : result) {
220220
bool have = stack.back()->HaveCoin(entry.first);
221221
const Coin& coin = stack.back()->AccessCoin(entry.first);
@@ -233,27 +233,27 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
233233
}
234234
}
235235

236-
if (InsecureRandRange(100) == 0) {
236+
if (m_rng.randrange(100) == 0) {
237237
// Every 100 iterations, flush an intermediate cache
238-
if (stack.size() > 1 && InsecureRandBool() == 0) {
239-
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
240-
if (fake_best_block) stack[flushIndex]->SetBestBlock(InsecureRand256());
241-
bool should_erase = InsecureRandRange(4) < 3;
238+
if (stack.size() > 1 && m_rng.randbool() == 0) {
239+
unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
240+
if (fake_best_block) stack[flushIndex]->SetBestBlock(m_rng.rand256());
241+
bool should_erase = m_rng.randrange(4) < 3;
242242
BOOST_CHECK(should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync());
243243
flushed_without_erase |= !should_erase;
244244
}
245245
}
246-
if (InsecureRandRange(100) == 0) {
246+
if (m_rng.randrange(100) == 0) {
247247
// Every 100 iterations, change the cache stack.
248-
if (stack.size() > 0 && InsecureRandBool() == 0) {
248+
if (stack.size() > 0 && m_rng.randbool() == 0) {
249249
//Remove the top cache
250-
if (fake_best_block) stack.back()->SetBestBlock(InsecureRand256());
251-
bool should_erase = InsecureRandRange(4) < 3;
250+
if (fake_best_block) stack.back()->SetBestBlock(m_rng.rand256());
251+
bool should_erase = m_rng.randrange(4) < 3;
252252
BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
253253
flushed_without_erase |= !should_erase;
254254
stack.pop_back();
255255
}
256-
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
256+
if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
257257
//Add a new cache
258258
CCoinsView* tip = base;
259259
if (stack.size() > 0) {
@@ -300,7 +300,7 @@ UtxoData utxoData;
300300

301301
UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
302302
assert(utxoSet.size());
303-
auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(InsecureRand256()), 0));
303+
auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(m_rng.rand256()), 0));
304304
if (utxoSetIt == utxoSet.end()) {
305305
utxoSetIt = utxoSet.begin();
306306
}
@@ -336,22 +336,22 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
336336
std::set<COutPoint> utxoset;
337337

338338
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
339-
uint32_t randiter = InsecureRand32();
339+
uint32_t randiter = m_rng.rand32();
340340

341341
// 19/20 txs add a new transaction
342342
if (randiter % 20 < 19) {
343343
CMutableTransaction tx;
344344
tx.vin.resize(1);
345345
tx.vout.resize(1);
346346
tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
347-
tx.vout[0].scriptPubKey.assign(InsecureRand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
348-
const int height{int(InsecureRand32() >> 1)};
347+
tx.vout[0].scriptPubKey.assign(m_rng.rand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
348+
const int height{int(m_rng.rand32() >> 1)};
349349
Coin old_coin;
350350

351351
// 2/20 times create a new coinbase
352352
if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
353353
// 1/10 of those times create a duplicate coinbase
354-
if (InsecureRandRange(10) == 0 && coinbase_coins.size()) {
354+
if (m_rng.randrange(10) == 0 && coinbase_coins.size()) {
355355
auto utxod = FindRandomFrom(coinbase_coins);
356356
// Reuse the exact same coinbase
357357
tx = CMutableTransaction{std::get<0>(utxod->second)};
@@ -461,7 +461,7 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
461461
}
462462

463463
// Once every 1000 iterations and at the end, verify the full cache.
464-
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
464+
if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
465465
for (const auto& entry : result) {
466466
bool have = stack.back()->HaveCoin(entry.first);
467467
const Coin& coin = stack.back()->AccessCoin(entry.first);
@@ -471,30 +471,30 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
471471
}
472472

473473
// One every 10 iterations, remove a random entry from the cache
474-
if (utxoset.size() > 1 && InsecureRandRange(30) == 0) {
475-
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
474+
if (utxoset.size() > 1 && m_rng.randrange(30) == 0) {
475+
stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
476476
}
477-
if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) {
478-
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
477+
if (disconnected_coins.size() > 1 && m_rng.randrange(30) == 0) {
478+
stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
479479
}
480-
if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) {
481-
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
480+
if (duplicate_coins.size() > 1 && m_rng.randrange(30) == 0) {
481+
stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
482482
}
483483

484-
if (InsecureRandRange(100) == 0) {
484+
if (m_rng.randrange(100) == 0) {
485485
// Every 100 iterations, flush an intermediate cache
486-
if (stack.size() > 1 && InsecureRandBool() == 0) {
487-
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
486+
if (stack.size() > 1 && m_rng.randbool() == 0) {
487+
unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
488488
BOOST_CHECK(stack[flushIndex]->Flush());
489489
}
490490
}
491-
if (InsecureRandRange(100) == 0) {
491+
if (m_rng.randrange(100) == 0) {
492492
// Every 100 iterations, change the cache stack.
493-
if (stack.size() > 0 && InsecureRandBool() == 0) {
493+
if (stack.size() > 0 && m_rng.randbool() == 0) {
494494
BOOST_CHECK(stack.back()->Flush());
495495
stack.pop_back();
496496
}
497-
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
497+
if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
498498
CCoinsView* tip = &base;
499499
if (stack.size() > 0) {
500500
tip = stack.back().get();
@@ -899,8 +899,8 @@ struct FlushTest : BasicTestingSetup {
899899
Coin MakeCoin()
900900
{
901901
Coin coin;
902-
coin.out.nValue = InsecureRand32();
903-
coin.nHeight = InsecureRandRange(4096);
902+
coin.out.nValue = m_rng.rand32();
903+
coin.nHeight = m_rng.randrange(4096);
904904
coin.fCoinBase = 0;
905905
return coin;
906906
}
@@ -934,12 +934,12 @@ void TestFlushBehavior(
934934
cache->SanityCheck();
935935
// hashBlock must be filled before flushing to disk; value is
936936
// unimportant here. This is normally done during connect/disconnect block.
937-
cache->SetBestBlock(InsecureRand256());
937+
cache->SetBestBlock(m_rng.rand256());
938938
erase ? cache->Flush() : cache->Sync();
939939
}
940940
};
941941

942-
Txid txid = Txid::FromUint256(InsecureRand256());
942+
Txid txid = Txid::FromUint256(m_rng.rand256());
943943
COutPoint outp = COutPoint(txid, 0);
944944
Coin coin = MakeCoin();
945945
// Ensure the coins views haven't seen this coin before.
@@ -1030,7 +1030,7 @@ void TestFlushBehavior(
10301030
// --- Bonus check: ensure that a coin added to the base view via one cache
10311031
// can be spent by another cache which has never seen it.
10321032
//
1033-
txid = Txid::FromUint256(InsecureRand256());
1033+
txid = Txid::FromUint256(m_rng.rand256());
10341034
outp = COutPoint(txid, 0);
10351035
coin = MakeCoin();
10361036
BOOST_CHECK(!base.HaveCoin(outp));
@@ -1053,7 +1053,7 @@ void TestFlushBehavior(
10531053

10541054
// --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
10551055
//
1056-
txid = Txid::FromUint256(InsecureRand256());
1056+
txid = Txid::FromUint256(m_rng.rand256());
10571057
outp = COutPoint(txid, 0);
10581058
coin = MakeCoin();
10591059
CAmount coin_val = coin.out.nValue;

0 commit comments

Comments
 (0)