From fa80b08fef0eaa600339caa678fdf80a8aec3ce3 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Mon, 18 Nov 2024 14:22:22 +0100 Subject: [PATCH 1/2] test: Revert to random path element --- src/test/util/setup_common.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 46a6daa88cf..ee2ef51f96c 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -75,6 +75,8 @@ using node::VerifyLoadedChainstate; const std::function G_TRANSLATION_FUN = nullptr; constexpr inline auto TEST_DIR_PATH_ELEMENT{"test_common bitcoin"}; // Includes a space to catch possible path escape issues. +/** Random context to get unique temp data dirs. Separate from m_rng, which can be seeded from a const env var */ +static FastRandomContext g_rng_temp_path; struct NetworkSetup { @@ -138,8 +140,12 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts) const std::string test_name{G_TEST_GET_FULL_NAME ? G_TEST_GET_FULL_NAME() : ""}; if (!m_node.args->IsArgSet("-testdatadir")) { - const auto now{TicksSinceEpoch(SystemClock::now())}; - m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / util::ToString(now); + // To avoid colliding with a leftover prior datadir, and to allow + // tests, such as the fuzz tests to run in several processes at the + // same time, add a random element to the path. Keep it small enough to + // avoid a MAX_PATH violation on Windows. + const auto rand{HexStr(g_rng_temp_path.randbytes(10))}; + m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / rand; TryCreateDirectories(m_path_root); } else { // Custom data directory From faaaf59f71ede057b2c1d369ef8db973c2f2dbc2 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 19 Nov 2024 17:30:40 +0100 Subject: [PATCH 2/2] test: Make g_rng_temp_path rand, not dependent on SeedRandomForTest --- src/test/util/setup_common.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index ee2ef51f96c..2cd739d7e39 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -77,6 +77,11 @@ const std::function G_TRANSLATION_FUN = nullptr; constexpr inline auto TEST_DIR_PATH_ELEMENT{"test_common bitcoin"}; // Includes a space to catch possible path escape issues. /** Random context to get unique temp data dirs. Separate from m_rng, which can be seeded from a const env var */ static FastRandomContext g_rng_temp_path; +static const bool g_rng_temp_path_init{[] { + // Must be initialized before any SeedRandomForTest + (void)g_rng_temp_path.rand64(); + return true; +}()}; struct NetworkSetup { @@ -134,8 +139,6 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts) } } - // Use randomly chosen seed for deterministic PRNG, so that (by default) test - // data directories use a random name that doesn't overlap with other tests. SeedRandomForTest(SeedRand::FIXED_SEED); const std::string test_name{G_TEST_GET_FULL_NAME ? G_TEST_GET_FULL_NAME() : ""};