Skip to content

Commit 246daf1

Browse files
author
MarcoFalke
committed
Merge bitcoin/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
2 parents b2f5c38 + fa485d0 commit 246daf1

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
@@ -32,6 +32,13 @@ void initialize_banman()
3232
static const auto testing_setup = MakeNoLogFileContext<>();
3333
}
3434

35+
static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
36+
{
37+
return lhs.nVersion == rhs.nVersion &&
38+
lhs.nCreateTime == rhs.nCreateTime &&
39+
lhs.nBanUntil == rhs.nBanUntil;
40+
}
41+
3542
FUZZ_TARGET_INIT(banman, initialize_banman)
3643
{
3744
// The complexity is O(N^2), where N is the input size, because each call
@@ -43,19 +50,20 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
4350
fs::path banlist_file = gArgs.GetDataDirNet() / "fuzzed_banlist";
4451

4552
const bool start_with_corrupted_banlist{fuzzed_data_provider.ConsumeBool()};
53+
bool force_read_and_write_to_err{false};
4654
if (start_with_corrupted_banlist) {
4755
const std::string sfx{fuzzed_data_provider.ConsumeBool() ? ".dat" : ".json"};
4856
assert(WriteBinaryFile(banlist_file.string() + sfx,
4957
fuzzed_data_provider.ConsumeRandomLengthString()));
5058
} else {
51-
const bool force_read_and_write_to_err{fuzzed_data_provider.ConsumeBool()};
59+
force_read_and_write_to_err = fuzzed_data_provider.ConsumeBool();
5260
if (force_read_and_write_to_err) {
5361
banlist_file = fs::path{"path"} / "to" / "inaccessible" / "fuzzed_banlist";
5462
}
5563
}
5664

5765
{
58-
BanMan ban_man{banlist_file, nullptr, ConsumeBanTimeOffset(fuzzed_data_provider)};
66+
BanMan ban_man{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ ConsumeBanTimeOffset(fuzzed_data_provider)};
5967
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
6068
CallOneOf(
6169
fuzzed_data_provider,
@@ -93,6 +101,16 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
93101
ban_man.Discourage(ConsumeNetAddr(fuzzed_data_provider));
94102
});
95103
}
104+
if (!force_read_and_write_to_err) {
105+
ban_man.DumpBanlist();
106+
SetMockTime(ConsumeTime(fuzzed_data_provider));
107+
banmap_t banmap;
108+
ban_man.GetBanned(banmap);
109+
BanMan ban_man_read{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ 0};
110+
banmap_t banmap_read;
111+
ban_man_read.GetBanned(banmap_read);
112+
assert(banmap == banmap_read);
113+
}
96114
}
97115
fs::remove(banlist_file.string() + ".dat");
98116
fs::remove(banlist_file.string() + ".json");

0 commit comments

Comments
 (0)