aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorstickies-v <stickies-v@protonmail.com>2024-07-30 17:30:59 +0100
committerstickies-v <stickies-v@protonmail.com>2024-08-23 13:53:40 +0100
commit18d65d27726bf9fc7629b8e794047a10c9cf6156 (patch)
tree60809c56f64d264f55b740d217cbb7854f905ce1 /src
parent6819e5a329c3bf38e47a07434e2a3c0031f808d0 (diff)
downloadbitcoin-18d65d27726bf9fc7629b8e794047a10c9cf6156.tar.xz
test: use uint256::FromUserHex for RANDOM_CTX_SEED
Removes dependency on unsafe and deprecated uint256S. This makes parsing more strict, by requiring RANDOM_CTX_SEED to be a string of up to 64 hex digits (optionally prefixed with "0x"), whereas previously any string would be accepted, with non-hex characters silently ignored and input longer than 64 characters (ignoring "0x" prefix) silently trimmed. Can be tested with: $ RANDOM_CTX_SEED=z ./src/test/test_bitcoin --log_level=all --run_test=timeoffsets_tests/timeoffsets_warning -- -printtoconsole=1 | grep RANDOM_CTX_SEED RANDOM_CTX_SEED must consist of up to 64 hex digits ("0x" prefix allowed), it was set to: 'z'. Co-Authored-By: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
Diffstat (limited to 'src')
-rw-r--r--src/test/util/random.cpp17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/test/util/random.cpp b/src/test/util/random.cpp
index 47d03055e2..8e0623ea06 100644
--- a/src/test/util/random.cpp
+++ b/src/test/util/random.cpp
@@ -7,9 +7,10 @@
#include <logging.h>
#include <random.h>
#include <uint256.h>
+#include <util/check.h>
#include <cstdlib>
-#include <string>
+#include <iostream>
FastRandomContext g_insecure_rand_ctx;
@@ -17,7 +18,7 @@ extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
void SeedRandomForTest(SeedRand seedtype)
{
- static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
+ constexpr auto RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
// Do this once, on the first call, regardless of seedtype, because once
// MakeRandDeterministicDANGEROUS is called, the output of GetRandHash is
@@ -25,14 +26,20 @@ void SeedRandomForTest(SeedRand seedtype)
// process.
static const uint256 ctx_seed = []() {
// If RANDOM_CTX_SEED is set, use that as seed.
- const char* num = std::getenv(RANDOM_CTX_SEED.c_str());
- if (num) return uint256S(num);
+ if (const char* num{std::getenv(RANDOM_CTX_SEED)}) {
+ if (auto num_parsed{uint256::FromUserHex(num)}) {
+ return *num_parsed;
+ } else {
+ std::cerr << RANDOM_CTX_SEED << " must consist of up to " << uint256::size() * 2 << " hex digits (\"0x\" prefix allowed), it was set to: '" << num << "'.\n";
+ std::abort();
+ }
+ }
// Otherwise use a (truly) random value.
return GetRandHash();
}();
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
- LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
+ LogInfo("Setting random seed for current tests to %s=%s\n", RANDOM_CTX_SEED, seed.GetHex());
MakeRandDeterministicDANGEROUS(seed);
g_insecure_rand_ctx.Reseed(GetRandHash());
}