Skip to content

Commit 6ba92a6

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#22322: fuzz: Check banman roundtrip
fa485d0 fuzz: Check banman roundtrip (MarcoFalke) Pull request description: ACKs for top commit: practicalswift: cr ACK fa485d0 vasild: ACK fa485d0 Tree-SHA512: 84e297c0b90ef68d72afd2053bfda2888496c1b180233516a8caaf76d6c03403f1e4ed59f1eb32d799873fc34009634b4ce372244b9d546d04626af41ac4d1d7
1 parent f4ea109 commit 6ba92a6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/test/fuzz/banman.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ void initialize_banman()
3131
static const auto testing_setup = MakeNoLogFileContext<>();
3232
}
3333

34+
static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
35+
{
36+
return lhs.nVersion == rhs.nVersion &&
37+
lhs.nCreateTime == rhs.nCreateTime &&
38+
lhs.nBanUntil == rhs.nBanUntil;
39+
}
40+
3441
FUZZ_TARGET_INIT(banman, initialize_banman)
3542
{
3643
// The complexity is O(N^2), where N is the input size, because each call
@@ -42,18 +49,19 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
4249
fs::path banlist_file = GetDataDir() / "fuzzed_banlist";
4350

4451
const bool start_with_corrupted_banlist{fuzzed_data_provider.ConsumeBool()};
52+
bool force_read_and_write_to_err{false};
4553
if (start_with_corrupted_banlist) {
4654
assert(WriteBinaryFile(banlist_file.string() + ".json",
4755
fuzzed_data_provider.ConsumeRandomLengthString()));
4856
} else {
49-
const bool force_read_and_write_to_err{fuzzed_data_provider.ConsumeBool()};
57+
force_read_and_write_to_err = fuzzed_data_provider.ConsumeBool();
5058
if (force_read_and_write_to_err) {
5159
banlist_file = fs::path{"path"} / "to" / "inaccessible" / "fuzzed_banlist";
5260
}
5361
}
5462

5563
{
56-
BanMan ban_man{banlist_file, nullptr, ConsumeBanTimeOffset(fuzzed_data_provider)};
64+
BanMan ban_man{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ ConsumeBanTimeOffset(fuzzed_data_provider)};
5765
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
5866
CallOneOf(
5967
fuzzed_data_provider,
@@ -91,6 +99,16 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
9199
ban_man.Discourage(ConsumeNetAddr(fuzzed_data_provider));
92100
});
93101
}
102+
if (!force_read_and_write_to_err) {
103+
ban_man.DumpBanlist();
104+
SetMockTime(ConsumeTime(fuzzed_data_provider));
105+
banmap_t banmap;
106+
ban_man.GetBanned(banmap);
107+
BanMan ban_man_read{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ 0};
108+
banmap_t banmap_read;
109+
ban_man_read.GetBanned(banmap_read);
110+
assert(banmap == banmap_read);
111+
}
94112
}
95113
fs::remove(banlist_file.string() + ".json");
96114
}

0 commit comments

Comments
 (0)