Skip to content

Commit

Permalink
Merge pull request #2381 from barton2526/FastRandomContext
Browse files Browse the repository at this point in the history
test, refactor: Use FastRandomContext for all tests. Add a header for test_gridcoin
  • Loading branch information
jamescowens authored Nov 14, 2021
2 parents 5d13429 + 8fb0a41 commit d79c2b3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ GRIDCOIN_TESTS =\
test/serialize_tests.cpp \
test/sigopcount_tests.cpp \
test/test_gridcoin.cpp \
test/test_gridcoin.h \
test/transaction_tests.cpp \
test/uint256_tests.cpp \
test/util_tests.cpp \
Expand Down
14 changes: 7 additions & 7 deletions src/test/crypto_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
#include <streams.h>
#include <util/strencodings.h>

#include <test/test_gridcoin.h>

#include <vector>

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(crypto_tests)

FastRandomContext ctx;

template<typename Hasher, typename In, typename Out>
static void TestVector(const Hasher &h, const In &in, const Out &out) {
Out hash;
Expand All @@ -40,7 +40,7 @@ static void TestVector(const Hasher &h, const In &in, const Out &out) {
Hasher hasher(h);
size_t pos = 0;
while (pos < in.size()) {
size_t len = ctx.randrange((in.size() - pos + 1) / 2 + 1);
size_t len = InsecureRandRange((in.size() - pos + 1) / 2 + 1);
hasher.Write((const uint8_t*)in.data() + pos, len);
pos += len;
if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {
Expand Down Expand Up @@ -567,7 +567,7 @@ BOOST_AUTO_TEST_CASE(countbits_tests)
} else {
for (int k = 0; k < 1000; k++) {
// Randomly test 1000 samples of each length above 10 bits.
uint64_t j = ((uint64_t)1) << (i - 1) | ctx.randbits(i - 1);
uint64_t j = ((uint64_t)1) << (i - 1) | InsecureRandBits(i - 1);
BOOST_CHECK_EQUAL(CountBits(j), i);
}
}
Expand All @@ -580,7 +580,7 @@ BOOST_AUTO_TEST_CASE(sha256d64)
unsigned char in[64 * 32];
unsigned char out1[32 * 32], out2[32 * 32];
for (int j = 0; j < 64 * i; ++j) {
in[j] = ctx.randbits(8);
in[j] = InsecureRandBits(8);
}
for (int j = 0; j < i; ++j) {
CHash256().Write(in + 64 * j, 64).Finalize(out1 + 32 * j);
Expand All @@ -604,8 +604,8 @@ static void TestSHA3_256(const std::string& input, const std::string& output)

// Reset and split randomly in 3
sha.Reset();
int s1 = ctx.randrange(in_bytes.size() + 1);
int s2 = ctx.randrange(in_bytes.size() + 1 - s1);
int s1 = InsecureRandRange(in_bytes.size() + 1);
int s2 = InsecureRandRange(in_bytes.size() + 1 - s1);
int s3 = in_bytes.size() - s1 - s2;
sha.Write(MakeSpan(in_bytes).first(s1)).Write(MakeSpan(in_bytes).subspan(s1, s2));
sha.Write(MakeSpan(in_bytes).last(s3)).Finalize(out);
Expand Down
8 changes: 5 additions & 3 deletions src/test/dos_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "random.h"
#include "banman.h"

#include <test/test_gridcoin.h>

#include <stdint.h>

// Tests this internal-to-main.cpp method:
Expand Down Expand Up @@ -124,7 +126,7 @@ BOOST_AUTO_TEST_CASE(DoS_misbehavior_decay)

CTransaction RandomOrphan()
{
auto it = mapOrphanTransactions.lower_bound(GetRandHash());
auto it = mapOrphanTransactions.lower_bound(InsecureRand256());
if (it == mapOrphanTransactions.end())
it = mapOrphanTransactions.begin();
return it->second;
Expand All @@ -143,7 +145,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
CTransaction tx;
tx.vin.resize(1);
tx.vin[0].prevout.n = 0;
tx.vin[0].prevout.hash = GetRandHash();
tx.vin[0].prevout.hash = InsecureRand256();
tx.vin[0].scriptSig << OP_1;
tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT;
Expand Down Expand Up @@ -218,7 +220,7 @@ BOOST_AUTO_TEST_CASE(DoS_checkSig)
CTransaction& tx = orphans[i];
tx.vin.resize(1);
tx.vin[0].prevout.n = 0;
tx.vin[0].prevout.hash = GetRandHash();
tx.vin[0].prevout.hash = InsecureRand256();
tx.vin[0].scriptSig << OP_1;
tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT;
Expand Down
8 changes: 4 additions & 4 deletions src/test/mruset_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ using namespace std;
#include "mruset.h"
#include "random.h"

#include <test/test_gridcoin.h>

#define NUM_TESTS 16
#define MAX_SIZE 100

Expand All @@ -31,26 +33,24 @@ BOOST_AUTO_TEST_SUITE(mruset_tests)
// Test that an mruset behaves like a set, as long as no more than MAX_SIZE elements are in it
BOOST_AUTO_TEST_CASE(mruset_like_set)
{
FastRandomContext ctx;
for (int nTest=0; nTest<NUM_TESTS; nTest++)
{
mrutester tester;
while (tester.size() < MAX_SIZE)
tester.insert(ctx.randrange(2 * MAX_SIZE));
tester.insert(InsecureRandRange(2 * MAX_SIZE));
}

}

// Test that an mruset's size never exceeds its max_size
BOOST_AUTO_TEST_CASE(mruset_limited_size)
{
FastRandomContext ctx;
for (int nTest=0; nTest<NUM_TESTS; nTest++)
{
mruset<int> mru(MAX_SIZE);
for (int nAction=0; nAction<3*MAX_SIZE; nAction++)
{
mru.insert(ctx.randrange(2 * MAX_SIZE));
mru.insert(InsecureRandRange(2 * MAX_SIZE));
BOOST_CHECK(mru.size() <= MAX_SIZE);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/test/test_gridcoin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#define BOOST_TEST_MODULE Gridcoin Test Suite
#include <boost/test/unit_test.hpp>

#include <test/test_gridcoin.h>

#include <leveldb/env.h>
#include <leveldb/helpers/memenv/memenv.h>

Expand All @@ -20,14 +22,15 @@ extern CClientUIInterface uiInterface;
*/
extern bool g_mock_deterministic_tests;

FastRandomContext g_insecure_rand_ctx;

extern void noui_connect();
extern leveldb::Options GetOptions();
extern void InitLogging();


struct TestingSetup {
TestingSetup() {
fs::path m_path_root = fs::temp_directory_path() / "test_common_" PACKAGE_NAME / GetRandHash().ToString();
fs::path m_path_root = fs::temp_directory_path() / "test_common_" PACKAGE_NAME / InsecureRand256().ToString();
fUseFastIndex = true; // Don't verify block hashes when loading
gArgs.ForceSetArg("-datadir", m_path_root.string());
gArgs.ClearPathCache();
Expand Down
21 changes: 21 additions & 0 deletions src/test/test_gridcoin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2015-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef GRIDCOIN_TEST_TEST_GRIDCOIN_H
#define GRIDCOIN_TEST_TEST_GRIDCOIN_H

#include <main.h>
#include <random.h>
#include <wallet/wallet.h>

extern FastRandomContext g_insecure_rand_ctx;

static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }
static inline std::vector<unsigned char> InsecureRandBytes(size_t len) { return g_insecure_rand_ctx.randbytes(len); }

#endif

0 comments on commit d79c2b3

Please sign in to comment.