scripted-diff: Use randbits/bool instead of randrange where possible

-BEGIN VERIFY SCRIPT-
sed -i 's/insecure_randbits(1)/insecure_randbool()/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(2)/insecure_randbool()/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(4)/insecure_randbits(2)/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(32)/insecure_randbits(5)/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(256)/insecure_randbits(8)/g' src/test/*_tests.cpp
-END VERIFY SCRIPT-
pull/365/head
Pieter Wuille 8 years ago
parent 2ada678521
commit 2fcd9cc86b

@ -43,7 +43,7 @@ public:
return false; return false;
} }
coin = it->second; coin = it->second;
if (coin.IsSpent() && insecure_randrange(2) == 0) { if (coin.IsSpent() && insecure_randbool() == 0) {
// Randomly return false in case of an empty entry. // Randomly return false in case of an empty entry.
return false; return false;
} }
@ -200,20 +200,20 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
if (insecure_randrange(100) == 0) { if (insecure_randrange(100) == 0) {
// Every 100 iterations, flush an intermediate cache // Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && insecure_randrange(2) == 0) { if (stack.size() > 1 && insecure_randbool() == 0) {
unsigned int flushIndex = insecure_randrange(stack.size() - 1); unsigned int flushIndex = insecure_randrange(stack.size() - 1);
stack[flushIndex]->Flush(); stack[flushIndex]->Flush();
} }
} }
if (insecure_randrange(100) == 0) { if (insecure_randrange(100) == 0) {
// Every 100 iterations, change the cache stack. // Every 100 iterations, change the cache stack.
if (stack.size() > 0 && insecure_randrange(2) == 0) { if (stack.size() > 0 && insecure_randbool() == 0) {
//Remove the top cache //Remove the top cache
stack.back()->Flush(); stack.back()->Flush();
delete stack.back(); delete stack.back();
stack.pop_back(); stack.pop_back();
} }
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) { if (stack.size() == 0 || (stack.size() < 4 && insecure_randbool())) {
//Add a new cache //Add a new cache
CCoinsView* tip = &base; CCoinsView* tip = &base;
if (stack.size() > 0) { if (stack.size() > 0) {
@ -433,19 +433,19 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
if (insecure_randrange(100) == 0) { if (insecure_randrange(100) == 0) {
// Every 100 iterations, flush an intermediate cache // Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && insecure_randrange(2) == 0) { if (stack.size() > 1 && insecure_randbool() == 0) {
unsigned int flushIndex = insecure_randrange(stack.size() - 1); unsigned int flushIndex = insecure_randrange(stack.size() - 1);
stack[flushIndex]->Flush(); stack[flushIndex]->Flush();
} }
} }
if (insecure_randrange(100) == 0) { if (insecure_randrange(100) == 0) {
// Every 100 iterations, change the cache stack. // Every 100 iterations, change the cache stack.
if (stack.size() > 0 && insecure_randrange(2) == 0) { if (stack.size() > 0 && insecure_randbool() == 0) {
stack.back()->Flush(); stack.back()->Flush();
delete stack.back(); delete stack.back();
stack.pop_back(); stack.pop_back();
} }
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) { if (stack.size() == 0 || (stack.size() < 4 && insecure_randbool())) {
CCoinsView* tip = &base; CCoinsView* tip = &base;
if (stack.size() > 0) { if (stack.size() > 0) {
tip = stack.back(); tip = stack.back();

@ -22,7 +22,7 @@ public:
// flip one bit in one of the hashes - this should break the authentication // flip one bit in one of the hashes - this should break the authentication
void Damage() { void Damage() {
unsigned int n = insecure_randrange(vHash.size()); unsigned int n = insecure_randrange(vHash.size());
int bit = insecure_randrange(256); int bit = insecure_randbits(8);
*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7); *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
} }
}; };

@ -209,10 +209,10 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
test.resize(new_size); test.resize(new_size);
} }
if (insecure_randbits(3) == 3) { if (insecure_randbits(3) == 3) {
test.insert(insecure_randrange(test.size() + 1), 1 + insecure_randrange(2), insecure_rand()); test.insert(insecure_randrange(test.size() + 1), 1 + insecure_randbool(), insecure_rand());
} }
if (insecure_randbits(3) == 4) { if (insecure_randbits(3) == 4) {
int del = std::min<int>(test.size(), 1 + (insecure_randrange(2))); int del = std::min<int>(test.size(), 1 + (insecure_randbool()));
int beg = insecure_randrange(test.size() + 1 - del); int beg = insecure_randrange(test.size() + 1 - del);
test.erase(beg, beg + del); test.erase(beg, beg + del);
} }
@ -224,19 +224,19 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
} }
if (insecure_randbits(5) == 7) { if (insecure_randbits(5) == 7) {
int values[4]; int values[4];
int num = 1 + (insecure_randrange(4)); int num = 1 + (insecure_randbits(2));
for (int k = 0; k < num; k++) { for (int k = 0; k < num; k++) {
values[k] = insecure_rand(); values[k] = insecure_rand();
} }
test.insert_range(insecure_randrange(test.size() + 1), values, values + num); test.insert_range(insecure_randrange(test.size() + 1), values, values + num);
} }
if (insecure_randbits(5) == 8) { if (insecure_randbits(5) == 8) {
int del = std::min<int>(test.size(), 1 + (insecure_randrange(4))); int del = std::min<int>(test.size(), 1 + (insecure_randbits(2)));
int beg = insecure_randrange(test.size() + 1 - del); int beg = insecure_randrange(test.size() + 1 - del);
test.erase(beg, beg + del); test.erase(beg, beg + del);
} }
if (insecure_randbits(5) == 9) { if (insecure_randbits(5) == 9) {
test.reserve(insecure_randrange(32)); test.reserve(insecure_randbits(5));
} }
if (insecure_randbits(6) == 10) { if (insecure_randbits(6) == 10) {
test.shrink_to_fit(); test.shrink_to_fit();
@ -248,7 +248,7 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
test.clear(); test.clear();
} }
if (insecure_randbits(9) == 12) { if (insecure_randbits(9) == 12) {
test.assign(insecure_randrange(32), insecure_rand()); test.assign(insecure_randbits(5), insecure_rand());
} }
if (insecure_randbits(3) == 3) { if (insecure_randbits(3) == 3) {
test.swap(); test.swap();

@ -98,16 +98,16 @@ void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
tx.nVersion = insecure_rand(); tx.nVersion = insecure_rand();
tx.vin.clear(); tx.vin.clear();
tx.vout.clear(); tx.vout.clear();
tx.nLockTime = (insecure_randrange(2)) ? insecure_rand() : 0; tx.nLockTime = (insecure_randbool()) ? insecure_rand() : 0;
int ins = (insecure_randrange(4)) + 1; int ins = (insecure_randbits(2)) + 1;
int outs = fSingle ? ins : (insecure_randrange(4)) + 1; int outs = fSingle ? ins : (insecure_randbits(2)) + 1;
for (int in = 0; in < ins; in++) { for (int in = 0; in < ins; in++) {
tx.vin.push_back(CTxIn()); tx.vin.push_back(CTxIn());
CTxIn &txin = tx.vin.back(); CTxIn &txin = tx.vin.back();
txin.prevout.hash = insecure_rand256(); txin.prevout.hash = insecure_rand256();
txin.prevout.n = insecure_randrange(4); txin.prevout.n = insecure_randbits(2);
RandomScript(txin.scriptSig); RandomScript(txin.scriptSig);
txin.nSequence = (insecure_randrange(2)) ? insecure_rand() : (unsigned int)-1; txin.nSequence = (insecure_randbool()) ? insecure_rand() : (unsigned int)-1;
} }
for (int out = 0; out < outs; out++) { for (int out = 0; out < outs; out++) {
tx.vout.push_back(CTxOut()); tx.vout.push_back(CTxOut());

Loading…
Cancel
Save