mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
test: Seed test RNG context for each test case, print seed
This commit is contained in:
parent
ab765c2ec7
commit
fae43a97ca
9 changed files with 46 additions and 15 deletions
|
@ -461,7 +461,7 @@ static std::vector<unsigned char> RandomData()
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(rolling_bloom)
|
BOOST_AUTO_TEST_CASE(rolling_bloom)
|
||||||
{
|
{
|
||||||
SeedInsecureRand(/* deterministic */ true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
g_mock_deterministic_tests = true;
|
g_mock_deterministic_tests = true;
|
||||||
|
|
||||||
// last-100-entry, 1% false positive:
|
// last-100-entry, 1% false positive:
|
||||||
|
|
|
@ -279,7 +279,7 @@ UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
|
||||||
// has the expected effect (the other duplicate is overwritten at all cache levels)
|
// has the expected effect (the other duplicate is overwritten at all cache levels)
|
||||||
BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
|
BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
|
||||||
{
|
{
|
||||||
SeedInsecureRand(/* deterministic */ true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
g_mock_deterministic_tests = true;
|
g_mock_deterministic_tests = true;
|
||||||
|
|
||||||
bool spent_a_duplicate_coinbase = false;
|
bool spent_a_duplicate_coinbase = false;
|
||||||
|
|
|
@ -29,7 +29,7 @@ BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
|
||||||
*/
|
*/
|
||||||
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
||||||
{
|
{
|
||||||
SeedInsecureRand(true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
|
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
|
||||||
size_t megabytes = 4;
|
size_t megabytes = 4;
|
||||||
cc.setup_bytes(megabytes << 20);
|
cc.setup_bytes(megabytes << 20);
|
||||||
|
@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
||||||
template <typename Cache>
|
template <typename Cache>
|
||||||
static double test_cache(size_t megabytes, double load)
|
static double test_cache(size_t megabytes, double load)
|
||||||
{
|
{
|
||||||
SeedInsecureRand(true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
std::vector<uint256> hashes;
|
std::vector<uint256> hashes;
|
||||||
Cache set{};
|
Cache set{};
|
||||||
size_t bytes = megabytes * (1 << 20);
|
size_t bytes = megabytes * (1 << 20);
|
||||||
|
@ -118,7 +118,7 @@ template <typename Cache>
|
||||||
static void test_cache_erase(size_t megabytes)
|
static void test_cache_erase(size_t megabytes)
|
||||||
{
|
{
|
||||||
double load = 1;
|
double load = 1;
|
||||||
SeedInsecureRand(true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
std::vector<uint256> hashes;
|
std::vector<uint256> hashes;
|
||||||
Cache set{};
|
Cache set{};
|
||||||
size_t bytes = megabytes * (1 << 20);
|
size_t bytes = megabytes * (1 << 20);
|
||||||
|
@ -181,7 +181,7 @@ template <typename Cache>
|
||||||
static void test_cache_erase_parallel(size_t megabytes)
|
static void test_cache_erase_parallel(size_t megabytes)
|
||||||
{
|
{
|
||||||
double load = 1;
|
double load = 1;
|
||||||
SeedInsecureRand(true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
std::vector<uint256> hashes;
|
std::vector<uint256> hashes;
|
||||||
Cache set{};
|
Cache set{};
|
||||||
size_t bytes = megabytes * (1 << 20);
|
size_t bytes = megabytes * (1 << 20);
|
||||||
|
@ -285,7 +285,7 @@ static void test_cache_generations()
|
||||||
// iterations with non-deterministic values, so it isn't "overfit" to the
|
// iterations with non-deterministic values, so it isn't "overfit" to the
|
||||||
// specific entropy in FastRandomContext(true) and implementation of the
|
// specific entropy in FastRandomContext(true) and implementation of the
|
||||||
// cache.
|
// cache.
|
||||||
SeedInsecureRand(true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
|
|
||||||
// block_activity models a chunk of network activity. n_insert elements are
|
// block_activity models a chunk of network activity. n_insert elements are
|
||||||
// added to the cache. The first and last n/4 are stored for removal later
|
// added to the cache. The first and last n/4 are stored for removal later
|
||||||
|
|
|
@ -30,7 +30,6 @@ BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(pmt_test1)
|
BOOST_AUTO_TEST_CASE(pmt_test1)
|
||||||
{
|
{
|
||||||
SeedInsecureRand(false);
|
|
||||||
static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
|
static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
|
||||||
|
|
||||||
for (int i = 0; i < 12; i++) {
|
for (int i = 0; i < 12; i++) {
|
||||||
|
|
|
@ -33,6 +33,27 @@
|
||||||
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
||||||
|
|
||||||
FastRandomContext g_insecure_rand_ctx;
|
FastRandomContext g_insecure_rand_ctx;
|
||||||
|
/** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
|
||||||
|
static FastRandomContext g_insecure_rand_ctx_temp_path;
|
||||||
|
|
||||||
|
/** Return the unsigned from the environment var if available, otherwise 0 */
|
||||||
|
static uint256 GetUintFromEnv(const std::string& env_name)
|
||||||
|
{
|
||||||
|
const char* num = std::getenv(env_name.c_str());
|
||||||
|
if (!num) return {};
|
||||||
|
return uint256S(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Seed(FastRandomContext& ctx)
|
||||||
|
{
|
||||||
|
// Should be enough to get the seed once for the process
|
||||||
|
static uint256 seed{};
|
||||||
|
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
|
||||||
|
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
|
||||||
|
if (seed.IsNull()) seed = GetRandHash();
|
||||||
|
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
|
||||||
|
ctx = FastRandomContext(seed);
|
||||||
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const uint256& num)
|
std::ostream& operator<<(std::ostream& os, const uint256& num)
|
||||||
{
|
{
|
||||||
|
@ -41,12 +62,13 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
|
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
|
||||||
: m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
|
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / std::to_string(g_insecure_rand_ctx_temp_path.rand32())}
|
||||||
{
|
{
|
||||||
fs::create_directories(m_path_root);
|
fs::create_directories(m_path_root);
|
||||||
gArgs.ForceSetArg("-datadir", m_path_root.string());
|
gArgs.ForceSetArg("-datadir", m_path_root.string());
|
||||||
ClearDatadirCache();
|
ClearDatadirCache();
|
||||||
SelectParams(chainName);
|
SelectParams(chainName);
|
||||||
|
SeedInsecureRand();
|
||||||
gArgs.ForceSetArg("-printtoconsole", "0");
|
gArgs.ForceSetArg("-printtoconsole", "0");
|
||||||
InitLogging();
|
InitLogging();
|
||||||
LogInstance().StartLogging();
|
LogInstance().StartLogging();
|
||||||
|
|
|
@ -38,9 +38,21 @@ extern FastRandomContext g_insecure_rand_ctx;
|
||||||
*/
|
*/
|
||||||
extern bool g_mock_deterministic_tests;
|
extern bool g_mock_deterministic_tests;
|
||||||
|
|
||||||
static inline void SeedInsecureRand(bool deterministic = false)
|
enum class SeedRand {
|
||||||
|
ZEROS, //!< Seed with a compile time constant of zeros
|
||||||
|
SEED, //!< Call the Seed() helper
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Seed the given random ctx or use the seed passed in via an environment var */
|
||||||
|
void Seed(FastRandomContext& ctx);
|
||||||
|
|
||||||
|
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
|
||||||
{
|
{
|
||||||
g_insecure_rand_ctx = FastRandomContext(deterministic);
|
if (seed == SeedRand::ZEROS) {
|
||||||
|
g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
|
||||||
|
} else {
|
||||||
|
Seed(g_insecure_rand_ctx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
|
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
|
||||||
|
|
|
@ -119,8 +119,6 @@ BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(sighash_test)
|
BOOST_AUTO_TEST_CASE(sighash_test)
|
||||||
{
|
{
|
||||||
SeedInsecureRand(false);
|
|
||||||
|
|
||||||
#if defined(PRINT_SIGHASH_JSON)
|
#if defined(PRINT_SIGHASH_JSON)
|
||||||
std::cout << "[\n";
|
std::cout << "[\n";
|
||||||
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
|
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
|
||||||
|
|
|
@ -339,7 +339,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
|
||||||
BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
|
BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
|
||||||
{
|
{
|
||||||
// Make this test deterministic.
|
// Make this test deterministic.
|
||||||
SeedInsecureRand(true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
|
|
||||||
for (int rep = 0; rep < 50; ++rep) {
|
for (int rep = 0; rep < 50; ++rep) {
|
||||||
FILE* file = fsbridge::fopen("streams_test_tmp", "w+b");
|
FILE* file = fsbridge::fopen("streams_test_tmp", "w+b");
|
||||||
|
|
|
@ -1036,7 +1036,7 @@ BOOST_AUTO_TEST_CASE(util_IsHexNumber)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
|
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
|
||||||
{
|
{
|
||||||
SeedInsecureRand(true);
|
SeedInsecureRand(SeedRand::ZEROS);
|
||||||
for (int mod=2;mod<11;mod++)
|
for (int mod=2;mod<11;mod++)
|
||||||
{
|
{
|
||||||
int mask = 1;
|
int mask = 1;
|
||||||
|
|
Loading…
Add table
Reference in a new issue