diff options
-rw-r--r-- | src/Makefile.test_util.include | 1 | ||||
-rw-r--r-- | src/test/denialofservice_tests.cpp | 1 | ||||
-rw-r--r-- | src/test/miniscript_tests.cpp | 1 | ||||
-rw-r--r-- | src/test/random_tests.cpp | 1 | ||||
-rw-r--r-- | src/test/util/random.cpp | 33 | ||||
-rw-r--r-- | src/test/util/random.h | 32 | ||||
-rw-r--r-- | src/test/util/setup_common.cpp | 21 | ||||
-rw-r--r-- | src/test/util/setup_common.h | 31 | ||||
-rw-r--r-- | src/wallet/test/wallet_tests.cpp | 1 |
9 files changed, 70 insertions, 52 deletions
diff --git a/src/Makefile.test_util.include b/src/Makefile.test_util.include index 11b93ad13e..5a43e20764 100644 --- a/src/Makefile.test_util.include +++ b/src/Makefile.test_util.include @@ -38,6 +38,7 @@ libtest_util_a_SOURCES = \ test/util/logging.cpp \ test/util/mining.cpp \ test/util/net.cpp \ + test/util/random.cpp \ test/util/script.cpp \ test/util/setup_common.cpp \ test/util/str.cpp \ diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp index 1a06f16155..13349329ff 100644 --- a/src/test/denialofservice_tests.cpp +++ b/src/test/denialofservice_tests.cpp @@ -15,6 +15,7 @@ #include <script/standard.h> #include <serialize.h> #include <test/util/net.h> +#include <test/util/random.h> #include <test/util/setup_common.h> #include <timedata.h> #include <util/string.h> diff --git a/src/test/miniscript_tests.cpp b/src/test/miniscript_tests.cpp index 42e441c41a..7dbf3dcd80 100644 --- a/src/test/miniscript_tests.cpp +++ b/src/test/miniscript_tests.cpp @@ -6,6 +6,7 @@ #include <string> #include <vector> +#include <test/util/random.h> #include <test/util/setup_common.h> #include <boost/test/unit_test.hpp> diff --git a/src/test/random_tests.cpp b/src/test/random_tests.cpp index 414e4509f5..43d887b5c9 100644 --- a/src/test/random_tests.cpp +++ b/src/test/random_tests.cpp @@ -4,6 +4,7 @@ #include <random.h> +#include <test/util/random.h> #include <test/util/setup_common.h> #include <util/time.h> diff --git a/src/test/util/random.cpp b/src/test/util/random.cpp new file mode 100644 index 0000000000..4c87ab8df8 --- /dev/null +++ b/src/test/util/random.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2023 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include <test/util/random.h> + +#include <logging.h> +#include <random.h> +#include <uint256.h> + +#include <cstdlib> +#include <string> + +FastRandomContext g_insecure_rand_ctx; + +/** 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); +} diff --git a/src/test/util/random.h b/src/test/util/random.h index 7997e8a346..c910bd6a3a 100644 --- a/src/test/util/random.h +++ b/src/test/util/random.h @@ -7,11 +7,41 @@ #include <consensus/amount.h> #include <random.h> -#include <test/util/setup_common.h> #include <uint256.h> #include <cstdint> +/** + * This global and the helpers that use it are not thread-safe. + * + * If thread-safety is needed, the global could be made thread_local (given + * that thread_local is supported on all architectures we support) or a + * per-thread instance could be used in the multi-threaded test. + */ +extern FastRandomContext g_insecure_rand_ctx; + +/** + * Flag to make GetRand in random.h return the same number + */ +extern bool g_mock_deterministic_tests; + +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) +{ + if (seed == SeedRand::ZEROS) { + g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true); + } else { + Seed(g_insecure_rand_ctx); + } +} + static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 483404779e..6f91761d15 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -40,6 +40,7 @@ #include <shutdown.h> #include <streams.h> #include <test/util/net.h> +#include <test/util/random.h> #include <test/util/txmempool.h> #include <timedata.h> #include <txdb.h> @@ -73,29 +74,9 @@ using node::VerifyLoadedChainstate; const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr; UrlDecodeFn* const URL_DECODE = nullptr; -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) { os << num.ToString(); diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index b7429df02c..88697300f5 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -41,37 +41,6 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os } } // namespace std -/** - * This global and the helpers that use it are not thread-safe. - * - * If thread-safety is needed, the global could be made thread_local (given - * that thread_local is supported on all architectures we support) or a - * per-thread instance could be used in the multi-threaded test. - */ -extern FastRandomContext g_insecure_rand_ctx; - -/** - * Flag to make GetRand in random.h return the same number - */ -extern bool g_mock_deterministic_tests; - -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) -{ - if (seed == SeedRand::ZEROS) { - g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true); - } else { - Seed(g_insecure_rand_ctx); - } -} - static constexpr CAmount CENT{1000000}; /** Basic testing setup. diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 65b02c267d..dd914f159c 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -15,6 +15,7 @@ #include <policy/policy.h> #include <rpc/server.h> #include <test/util/logging.h> +#include <test/util/random.h> #include <test/util/setup_common.h> #include <util/translation.h> #include <validation.h> |