Skip to content

Commit dfa03f3

Browse files
committed
fuzz: cleanup statically-named datadir
1 parent c426b80 commit dfa03f3

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/test/fuzz/cmpctblock.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <net_processing.h>
1010
#include <pow.h>
1111
#include <protocol.h>
12+
#include <random.h>
1213
#include <script/script.h>
1314
#include <streams.h>
1415
#include <sync.h>
@@ -23,6 +24,7 @@
2324
#include <test/util/txmempool.h>
2425
#include <test/util/validation.h>
2526
#include <util/fs_helpers.h>
27+
#include <util/strencodings.h>
2628
#include <util/time.h>
2729
#include <validationinterface.h>
2830

@@ -32,6 +34,8 @@
3234
#include <utility>
3335
#include <vector>
3436

37+
using namespace util::hex_literals;
38+
3539
namespace {
3640

3741
//! Fee each created tx will pay.
@@ -64,12 +68,29 @@ class FuzzedCBlockHeaderAndShortTxIDs : public CBlockHeaderAndShortTxIDs
6468
shorttxids.erase(shorttxids.begin() + index);
6569
}
6670
};
71+
//! Class to delete the statically-named datadir at the end of a fuzzing run.
72+
class FuzzedDirectoryWrapper
73+
{
74+
private:
75+
fs::path staticdir;
76+
77+
public:
78+
FuzzedDirectoryWrapper(fs::path name) : staticdir(name) {}
79+
80+
~FuzzedDirectoryWrapper()
81+
{
82+
fs::remove_all(staticdir);
83+
}
84+
};
6785

6886
} // namespace
6987

7088
void initialize_cmpctblock() {
89+
FastRandomContext ctx;
90+
7191
std::string testdatadir = "-testdatadir=";
72-
g_cached_path = fs::temp_directory_path() / "cmpctblock_cached";
92+
std::string staticdir = "cmpctblock_cached" + HexStr(ctx.randbytes(10));
93+
g_cached_path = fs::temp_directory_path() / staticdir;
7394
auto cached_datadir_arg = testdatadir + g_cached_path.string();
7495

7596
const auto initial_setup = MakeNoLogFileContext<const TestingSetup>(
@@ -78,6 +99,8 @@ void initialize_cmpctblock() {
7899
.coins_db_in_memory = false,
79100
.block_tree_db_in_memory = false});
80101

102+
static const FuzzedDirectoryWrapper wrapper(g_cached_path);
103+
81104
SetMockTime(Params().GenesisBlock().Time());
82105

83106
node::BlockAssembler::Options options;

0 commit comments

Comments
 (0)