aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/denialofservice_tests.cpp1
-rw-r--r--src/test/fuzz/addrman.cpp4
-rw-r--r--src/test/fuzz/crypto.cpp2
-rw-r--r--src/test/fuzz/golomb_rice.cpp2
-rw-r--r--src/test/hash_tests.cpp16
-rw-r--r--src/test/miniscript_tests.cpp1
-rw-r--r--src/test/random_tests.cpp1
-rw-r--r--src/test/util/random.cpp33
-rw-r--r--src/test/util/random.h32
-rw-r--r--src/test/util/setup_common.cpp22
-rw-r--r--src/test/util/setup_common.h33
11 files changed, 82 insertions, 65 deletions
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/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp
index f0035ddf21..8ac1330dcb 100644
--- a/src/test/fuzz/addrman.cpp
+++ b/src/test/fuzz/addrman.cpp
@@ -171,8 +171,8 @@ public:
hasher.Write(a.source.GetNetwork());
hasher.Write(addr_key.size());
hasher.Write(source_key.size());
- hasher.Write(addr_key.data(), addr_key.size());
- hasher.Write(source_key.data(), source_key.size());
+ hasher.Write(addr_key);
+ hasher.Write(source_key);
return (size_t)hasher.Finalize();
};
diff --git a/src/test/fuzz/crypto.cpp b/src/test/fuzz/crypto.cpp
index 84b95117e2..ca8c1cd033 100644
--- a/src/test/fuzz/crypto.cpp
+++ b/src/test/fuzz/crypto.cpp
@@ -57,7 +57,7 @@ FUZZ_TARGET(crypto)
(void)sha256.Write(data.data(), data.size());
(void)sha3.Write(data);
(void)sha512.Write(data.data(), data.size());
- (void)sip_hasher.Write(data.data(), data.size());
+ (void)sip_hasher.Write(data);
(void)Hash(data);
(void)Hash160(data);
diff --git a/src/test/fuzz/golomb_rice.cpp b/src/test/fuzz/golomb_rice.cpp
index 1a1225b635..e006653ca9 100644
--- a/src/test/fuzz/golomb_rice.cpp
+++ b/src/test/fuzz/golomb_rice.cpp
@@ -23,7 +23,7 @@ namespace {
uint64_t HashToRange(const std::vector<uint8_t>& element, const uint64_t f)
{
const uint64_t hash = CSipHasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL)
- .Write(element.data(), element.size())
+ .Write(element)
.Finalize();
return FastRange64(hash, f);
}
diff --git a/src/test/hash_tests.cpp b/src/test/hash_tests.cpp
index f1f435591b..a990797ca7 100644
--- a/src/test/hash_tests.cpp
+++ b/src/test/hash_tests.cpp
@@ -83,21 +83,21 @@ BOOST_AUTO_TEST_CASE(siphash)
CSipHasher hasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x726fdb47dd0e0e31ull);
static const unsigned char t0[1] = {0};
- hasher.Write(t0, 1);
+ hasher.Write(t0);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x74f839c593dc67fdull);
static const unsigned char t1[7] = {1,2,3,4,5,6,7};
- hasher.Write(t1, 7);
+ hasher.Write(t1);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x93f5f5799a932462ull);
hasher.Write(0x0F0E0D0C0B0A0908ULL);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x3f2acc7f57c29bdbull);
static const unsigned char t2[2] = {16,17};
- hasher.Write(t2, 2);
+ hasher.Write(t2);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x4bc1b3f0968dd39cull);
static const unsigned char t3[9] = {18,19,20,21,22,23,24,25,26};
- hasher.Write(t3, 9);
+ hasher.Write(t3);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x2f2e6163076bcfadull);
static const unsigned char t4[5] = {27,28,29,30,31};
- hasher.Write(t4, 5);
+ hasher.Write(t4);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x7127512f72f27cceull);
hasher.Write(0x2726252423222120ULL);
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x0e3ea96b5304a7d0ull);
@@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(siphash)
for (uint8_t x=0; x<std::size(siphash_4_2_testvec); ++x)
{
BOOST_CHECK_EQUAL(hasher2.Finalize(), siphash_4_2_testvec[x]);
- hasher2.Write(&x, 1);
+ hasher2.Write(Span{&x, 1});
}
// Check test vectors from spec, eight bytes at a time
CSipHasher hasher3(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
@@ -140,9 +140,9 @@ BOOST_AUTO_TEST_CASE(siphash)
uint8_t nb[4];
WriteLE32(nb, n);
CSipHasher sip256(k1, k2);
- sip256.Write(x.begin(), 32);
+ sip256.Write(x);
CSipHasher sip288 = sip256;
- sip288.Write(nb, 4);
+ sip288.Write(nb);
BOOST_CHECK_EQUAL(SipHashUint256(k1, k2, x), sip256.Finalize());
BOOST_CHECK_EQUAL(SipHashUint256Extra(k1, k2, x, n), sip288.Finalize());
}
diff --git a/src/test/miniscript_tests.cpp b/src/test/miniscript_tests.cpp
index 2d183c8844..9c811db3e9 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 d8f30bdc6e..6ae2187974 100644
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -32,6 +32,7 @@
#include <policy/fees.h>
#include <policy/fees_args.h>
#include <pow.h>
+#include <random.h>
#include <rpc/blockchain.h>
#include <rpc/register.h>
#include <rpc/server.h>
@@ -40,6 +41,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 +75,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..7cd4fdb417 100644
--- a/src/test/util/setup_common.h
+++ b/src/test/util/setup_common.h
@@ -11,7 +11,6 @@
#include <node/context.h> // IWYU pragma: export
#include <primitives/transaction.h>
#include <pubkey.h>
-#include <random.h>
#include <stdexcept>
#include <util/chaintype.h>
#include <util/check.h>
@@ -25,6 +24,7 @@
class CFeeRate;
class Chainstate;
+class FastRandomContext;
/** This is connected to the logger. Can be used to redirect logs to any other log */
extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
@@ -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.