Skip to content

Commit e945848

Browse files
committed
scripted-diff: Use new naming style for insecure_rand* functions
-BEGIN VERIFY SCRIPT- sed -i 's/\<insecure_randbits(/InsecureRandBits(/g' src/test/*.cpp src/test/*.h src/wallet/test/*.cpp sed -i 's/\<insecure_randbool(/InsecureRandBool(/g' src/test/*.cpp src/test/*.h src/wallet/test/*.cpp sed -i 's/\<insecure_randrange(/InsecureRandRange(/g' src/test/*.cpp src/test/*.h src/wallet/test/*.cpp sed -i 's/\<insecure_randbytes(/InsecureRandBytes(/g' src/test/*.cpp src/test/*.h src/wallet/test/*.cpp sed -i 's/\<insecure_rand256(/InsecureRand256(/g' src/test/*.cpp src/test/*.h src/wallet/test/*.cpp sed -i 's/\<insecure_rand(/InsecureRand32(/g' src/test/*.cpp src/test/*.h src/wallet/test/*.cpp sed -i 's/\<seed_insecure_rand(/SeedInsecureRand(/g' src/test/*.cpp src/test/*.h src/wallet/test/*.cpp -END VERIFY SCRIPT-
1 parent 2fcd9cc commit e945848

20 files changed

+142
-142
lines changed

src/test/DoS_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
129129
CTransactionRef RandomOrphan()
130130
{
131131
std::map<uint256, COrphanTx>::iterator it;
132-
it = mapOrphanTransactions.lower_bound(insecure_rand256());
132+
it = mapOrphanTransactions.lower_bound(InsecureRand256());
133133
if (it == mapOrphanTransactions.end())
134134
it = mapOrphanTransactions.begin();
135135
return it->second.tx;
@@ -148,7 +148,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
148148
CMutableTransaction tx;
149149
tx.vin.resize(1);
150150
tx.vin[0].prevout.n = 0;
151-
tx.vin[0].prevout.hash = insecure_rand256();
151+
tx.vin[0].prevout.hash = InsecureRand256();
152152
tx.vin[0].scriptSig << OP_1;
153153
tx.vout.resize(1);
154154
tx.vout[0].nValue = 1*CENT;

src/test/blockencodings_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ static CBlock BuildBlockTestCase() {
3030
block.vtx.resize(3);
3131
block.vtx[0] = MakeTransactionRef(tx);
3232
block.nVersion = 42;
33-
block.hashPrevBlock = insecure_rand256();
33+
block.hashPrevBlock = InsecureRand256();
3434
block.nBits = 0x207fffff;
3535

36-
tx.vin[0].prevout.hash = insecure_rand256();
36+
tx.vin[0].prevout.hash = InsecureRand256();
3737
tx.vin[0].prevout.n = 0;
3838
block.vtx[1] = MakeTransactionRef(tx);
3939

4040
tx.vin.resize(10);
4141
for (size_t i = 0; i < tx.vin.size(); i++) {
42-
tx.vin[i].prevout.hash = insecure_rand256();
42+
tx.vin[i].prevout.hash = InsecureRand256();
4343
tx.vin[i].prevout.n = 0;
4444
}
4545
block.vtx[2] = MakeTransactionRef(tx);
@@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
283283
block.vtx.resize(1);
284284
block.vtx[0] = MakeTransactionRef(std::move(coinbase));
285285
block.nVersion = 42;
286-
block.hashPrevBlock = insecure_rand256();
286+
block.hashPrevBlock = InsecureRand256();
287287
block.nBits = 0x207fffff;
288288

289289
bool mutated;
@@ -316,7 +316,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
316316

317317
BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
318318
BlockTransactionsRequest req1;
319-
req1.blockhash = insecure_rand256();
319+
req1.blockhash = InsecureRand256();
320320
req1.indexes.resize(4);
321321
req1.indexes[0] = 0;
322322
req1.indexes[1] = 1;

src/test/bloom_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
463463

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

src/test/checkqueue_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void Correct_Queue_range(std::vector<size_t> range)
160160
FakeCheckCheckCompletion::n_calls = 0;
161161
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
162162
while (total) {
163-
vChecks.resize(std::min(total, (size_t) insecure_randrange(10)));
163+
vChecks.resize(std::min(total, (size_t) InsecureRandRange(10)));
164164
total -= vChecks.size();
165165
control.Add(vChecks);
166166
}
@@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
204204
{
205205
std::vector<size_t> range;
206206
range.reserve(100000/1000);
207-
for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)insecure_randrange(std::min((size_t)1000, ((size_t)100000) - i))))
207+
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))))
208208
range.push_back(i);
209209
Correct_Queue_range(range);
210210
}
@@ -224,7 +224,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
224224
CCheckQueueControl<FailingCheck> control(fail_queue.get());
225225
size_t remaining = i;
226226
while (remaining) {
227-
size_t r = insecure_randrange(10);
227+
size_t r = InsecureRandRange(10);
228228

229229
std::vector<FailingCheck> vChecks;
230230
vChecks.reserve(r);
@@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
286286
{
287287
CCheckQueueControl<UniqueCheck> control(queue.get());
288288
while (total) {
289-
size_t r = insecure_randrange(10);
289+
size_t r = InsecureRandRange(10);
290290
std::vector<UniqueCheck> vChecks;
291291
for (size_t k = 0; k < r && total; k++)
292292
vChecks.emplace_back(--total);
@@ -320,7 +320,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
320320
{
321321
CCheckQueueControl<MemoryCheck> control(queue.get());
322322
while (total) {
323-
size_t r = insecure_randrange(10);
323+
size_t r = InsecureRandRange(10);
324324
std::vector<MemoryCheck> vChecks;
325325
for (size_t k = 0; k < r && total; k++) {
326326
total--;

src/test/coins_tests.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CCoinsViewTest : public CCoinsView
4343
return false;
4444
}
4545
coin = it->second;
46-
if (coin.IsSpent() && insecure_randbool() == 0) {
46+
if (coin.IsSpent() && InsecureRandBool() == 0) {
4747
// Randomly return false in case of an empty entry.
4848
return false;
4949
}
@@ -64,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
6464
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
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() && insecure_randrange(3) == 0) {
67+
if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
6868
// Randomly delete empty entries on write.
6969
map_.erase(it->first);
7070
}
@@ -139,31 +139,31 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
139139
std::vector<uint256> txids;
140140
txids.resize(NUM_SIMULATION_ITERATIONS / 8);
141141
for (unsigned int i = 0; i < txids.size(); i++) {
142-
txids[i] = insecure_rand256();
142+
txids[i] = InsecureRand256();
143143
}
144144

145145
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
146146
// Do a random modification.
147147
{
148-
uint256 txid = txids[insecure_randrange(txids.size())]; // txid we're going to modify in this iteration.
148+
uint256 txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
149149
Coin& coin = result[COutPoint(txid, 0)];
150-
const Coin& entry = (insecure_randrange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
150+
const Coin& entry = (InsecureRandRange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
151151
BOOST_CHECK(coin == entry);
152152

153-
if (insecure_randrange(5) == 0 || coin.IsSpent()) {
153+
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
154154
Coin newcoin;
155-
newcoin.out.nValue = insecure_rand();
155+
newcoin.out.nValue = InsecureRand32();
156156
newcoin.nHeight = 1;
157-
if (insecure_randrange(16) == 0 && coin.IsSpent()) {
158-
newcoin.out.scriptPubKey.assign(1 + insecure_randbits(6), OP_RETURN);
157+
if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
158+
newcoin.out.scriptPubKey.assign(1 + InsecureRandBits(6), OP_RETURN);
159159
BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
160160
added_an_unspendable_entry = true;
161161
} else {
162-
newcoin.out.scriptPubKey.assign(insecure_randbits(6), 0); // Random sizes so we can test memory usage accounting
162+
newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0); // Random sizes so we can test memory usage accounting
163163
(coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
164164
coin = newcoin;
165165
}
166-
stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), !coin.IsSpent() || insecure_rand() & 1);
166+
stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), !coin.IsSpent() || InsecureRand32() & 1);
167167
} else {
168168
removed_an_entry = true;
169169
coin.Clear();
@@ -172,15 +172,15 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
172172
}
173173

174174
// One every 10 iterations, remove a random entry from the cache
175-
if (insecure_randrange(10) == 0) {
176-
COutPoint out(txids[insecure_rand() % txids.size()], 0);
177-
int cacheid = insecure_rand() % stack.size();
175+
if (InsecureRandRange(10) == 0) {
176+
COutPoint out(txids[InsecureRand32() % txids.size()], 0);
177+
int cacheid = InsecureRand32() % stack.size();
178178
stack[cacheid]->Uncache(out);
179179
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
180180
}
181181

182182
// Once every 1000 iterations and at the end, verify the full cache.
183-
if (insecure_randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
183+
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
184184
for (auto it = result.begin(); it != result.end(); it++) {
185185
bool have = stack.back()->HaveCoin(it->first);
186186
const Coin& coin = stack.back()->AccessCoin(it->first);
@@ -198,22 +198,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
198198
}
199199
}
200200

201-
if (insecure_randrange(100) == 0) {
201+
if (InsecureRandRange(100) == 0) {
202202
// Every 100 iterations, flush an intermediate cache
203-
if (stack.size() > 1 && insecure_randbool() == 0) {
204-
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
203+
if (stack.size() > 1 && InsecureRandBool() == 0) {
204+
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
205205
stack[flushIndex]->Flush();
206206
}
207207
}
208-
if (insecure_randrange(100) == 0) {
208+
if (InsecureRandRange(100) == 0) {
209209
// Every 100 iterations, change the cache stack.
210-
if (stack.size() > 0 && insecure_randbool() == 0) {
210+
if (stack.size() > 0 && InsecureRandBool() == 0) {
211211
//Remove the top cache
212212
stack.back()->Flush();
213213
delete stack.back();
214214
stack.pop_back();
215215
}
216-
if (stack.size() == 0 || (stack.size() < 4 && insecure_randbool())) {
216+
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
217217
//Add a new cache
218218
CCoinsView* tip = &base;
219219
if (stack.size() > 0) {
@@ -253,7 +253,7 @@ UtxoData utxoData;
253253

254254
UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
255255
assert(utxoSet.size());
256-
auto utxoSetIt = utxoSet.lower_bound(COutPoint(insecure_rand256(), 0));
256+
auto utxoSetIt = utxoSet.lower_bound(COutPoint(InsecureRand256(), 0));
257257
if (utxoSetIt == utxoSet.end()) {
258258
utxoSetIt = utxoSet.begin();
259259
}
@@ -286,22 +286,22 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
286286
std::set<COutPoint> utxoset;
287287

288288
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
289-
uint32_t randiter = insecure_rand();
289+
uint32_t randiter = InsecureRand32();
290290

291291
// 19/20 txs add a new transaction
292292
if (randiter % 20 < 19) {
293293
CMutableTransaction tx;
294294
tx.vin.resize(1);
295295
tx.vout.resize(1);
296296
tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
297-
tx.vout[0].scriptPubKey.assign(insecure_rand() & 0x3F, 0); // Random sizes so we can test memory usage accounting
298-
unsigned int height = insecure_rand();
297+
tx.vout[0].scriptPubKey.assign(InsecureRand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
298+
unsigned int height = InsecureRand32();
299299
Coin old_coin;
300300

301301
// 2/20 times create a new coinbase
302302
if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
303303
// 1/10 of those times create a duplicate coinbase
304-
if (insecure_randrange(10) == 0 && coinbase_coins.size()) {
304+
if (InsecureRandRange(10) == 0 && coinbase_coins.size()) {
305305
auto utxod = FindRandomFrom(coinbase_coins);
306306
// Reuse the exact same coinbase
307307
tx = std::get<0>(utxod->second);
@@ -411,7 +411,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
411411
}
412412

413413
// Once every 1000 iterations and at the end, verify the full cache.
414-
if (insecure_randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
414+
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
415415
for (auto it = result.begin(); it != result.end(); it++) {
416416
bool have = stack.back()->HaveCoin(it->first);
417417
const Coin& coin = stack.back()->AccessCoin(it->first);
@@ -421,31 +421,31 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
421421
}
422422

423423
// One every 10 iterations, remove a random entry from the cache
424-
if (utxoset.size() > 1 && insecure_randrange(30) == 0) {
425-
stack[insecure_rand() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
424+
if (utxoset.size() > 1 && InsecureRandRange(30) == 0) {
425+
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
426426
}
427-
if (disconnected_coins.size() > 1 && insecure_randrange(30) == 0) {
428-
stack[insecure_rand() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
427+
if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) {
428+
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
429429
}
430-
if (duplicate_coins.size() > 1 && insecure_randrange(30) == 0) {
431-
stack[insecure_rand() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
430+
if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) {
431+
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
432432
}
433433

434-
if (insecure_randrange(100) == 0) {
434+
if (InsecureRandRange(100) == 0) {
435435
// Every 100 iterations, flush an intermediate cache
436-
if (stack.size() > 1 && insecure_randbool() == 0) {
437-
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
436+
if (stack.size() > 1 && InsecureRandBool() == 0) {
437+
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
438438
stack[flushIndex]->Flush();
439439
}
440440
}
441-
if (insecure_randrange(100) == 0) {
441+
if (InsecureRandRange(100) == 0) {
442442
// Every 100 iterations, change the cache stack.
443-
if (stack.size() > 0 && insecure_randbool() == 0) {
443+
if (stack.size() > 0 && InsecureRandBool() == 0) {
444444
stack.back()->Flush();
445445
delete stack.back();
446446
stack.pop_back();
447447
}
448-
if (stack.size() == 0 || (stack.size() < 4 && insecure_randbool())) {
448+
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
449449
CCoinsView* tip = &base;
450450
if (stack.size() > 0) {
451451
tip = stack.back();

src/test/crypto_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void TestVector(const Hasher &h, const In &in, const Out &out) {
3838
Hasher hasher(h);
3939
size_t pos = 0;
4040
while (pos < in.size()) {
41-
size_t len = insecure_randrange((in.size() - pos + 1) / 2 + 1);
41+
size_t len = InsecureRandRange((in.size() - pos + 1) / 2 + 1);
4242
hasher.Write((unsigned char*)&in[pos], len);
4343
pos += len;
4444
if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {

src/test/dbwrapper_tests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
3131
fs::path ph = fs::temp_directory_path() / fs::unique_path();
3232
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
3333
char key = 'k';
34-
uint256 in = insecure_rand256();
34+
uint256 in = InsecureRand256();
3535
uint256 res;
3636

3737
// Ensure that we're doing real obfuscation when obfuscate=true
@@ -53,11 +53,11 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
5353
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
5454

5555
char key = 'i';
56-
uint256 in = insecure_rand256();
56+
uint256 in = InsecureRand256();
5757
char key2 = 'j';
58-
uint256 in2 = insecure_rand256();
58+
uint256 in2 = InsecureRand256();
5959
char key3 = 'k';
60-
uint256 in3 = insecure_rand256();
60+
uint256 in3 = InsecureRand256();
6161

6262
uint256 res;
6363
CDBBatch batch(dbw);
@@ -91,10 +91,10 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
9191

9292
// The two keys are intentionally chosen for ordering
9393
char key = 'j';
94-
uint256 in = insecure_rand256();
94+
uint256 in = InsecureRand256();
9595
BOOST_CHECK(dbw.Write(key, in));
9696
char key2 = 'k';
97-
uint256 in2 = insecure_rand256();
97+
uint256 in2 = InsecureRand256();
9898
BOOST_CHECK(dbw.Write(key2, in2));
9999

100100
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
@@ -132,7 +132,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
132132
// Set up a non-obfuscated wrapper to write some initial data.
133133
CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false);
134134
char key = 'k';
135-
uint256 in = insecure_rand256();
135+
uint256 in = InsecureRand256();
136136
uint256 res;
137137

138138
BOOST_CHECK(dbw->Write(key, in));
@@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
155155
BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
156156
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
157157

158-
uint256 in2 = insecure_rand256();
158+
uint256 in2 = InsecureRand256();
159159
uint256 res3;
160160

161161
// Check that we can write successfully
@@ -174,7 +174,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
174174
// Set up a non-obfuscated wrapper to write some initial data.
175175
CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false);
176176
char key = 'k';
177-
uint256 in = insecure_rand256();
177+
uint256 in = InsecureRand256();
178178
uint256 res;
179179

180180
BOOST_CHECK(dbw->Write(key, in));
@@ -193,7 +193,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
193193
BOOST_CHECK(!odbw.Read(key, res2));
194194
BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw)));
195195

196-
uint256 in2 = insecure_rand256();
196+
uint256 in2 = InsecureRand256();
197197
uint256 res3;
198198

199199
// Check that we can write successfully

src/test/hash_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(siphash)
134134
for (int i = 0; i < 16; ++i) {
135135
uint64_t k1 = ctx.rand64();
136136
uint64_t k2 = ctx.rand64();
137-
uint256 x = insecure_rand256();
137+
uint256 x = InsecureRand256();
138138
uint32_t n = ctx.rand32();
139139
uint8_t nb[4];
140140
WriteLE32(nb, n);

0 commit comments

Comments
 (0)