Skip to content

Commit

Permalink
Merge 246daf1 into merged_master (Bitcoin PR bitcoin/bitcoin#22322)
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Jul 29, 2021
2 parents 505e45c + 246daf1 commit 9a6ea2c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/test/fuzz/banman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ void initialize_banman()
static const auto testing_setup = MakeNoLogFileContext<>();
}

static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
{
return lhs.nVersion == rhs.nVersion &&
lhs.nCreateTime == rhs.nCreateTime &&
lhs.nBanUntil == rhs.nBanUntil;
}

FUZZ_TARGET_INIT(banman, initialize_banman)
{
// The complexity is O(N^2), where N is the input size, because each call
Expand All @@ -43,19 +50,20 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
fs::path banlist_file = gArgs.GetDataDirNet() / "fuzzed_banlist";

const bool start_with_corrupted_banlist{fuzzed_data_provider.ConsumeBool()};
bool force_read_and_write_to_err{false};
if (start_with_corrupted_banlist) {
const std::string sfx{fuzzed_data_provider.ConsumeBool() ? ".dat" : ".json"};
assert(WriteBinaryFile(banlist_file.string() + sfx,
fuzzed_data_provider.ConsumeRandomLengthString()));
} else {
const bool force_read_and_write_to_err{fuzzed_data_provider.ConsumeBool()};
force_read_and_write_to_err = fuzzed_data_provider.ConsumeBool();
if (force_read_and_write_to_err) {
banlist_file = fs::path{"path"} / "to" / "inaccessible" / "fuzzed_banlist";
}
}

{
BanMan ban_man{banlist_file, nullptr, ConsumeBanTimeOffset(fuzzed_data_provider)};
BanMan ban_man{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ ConsumeBanTimeOffset(fuzzed_data_provider)};
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
CallOneOf(
fuzzed_data_provider,
Expand Down Expand Up @@ -93,6 +101,16 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
ban_man.Discourage(ConsumeNetAddr(fuzzed_data_provider));
});
}
if (!force_read_and_write_to_err) {
ban_man.DumpBanlist();
SetMockTime(ConsumeTime(fuzzed_data_provider));
banmap_t banmap;
ban_man.GetBanned(banmap);
BanMan ban_man_read{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ 0};
banmap_t banmap_read;
ban_man_read.GetBanned(banmap_read);
assert(banmap == banmap_read);
}
}
fs::remove(banlist_file.string() + ".dat");
fs::remove(banlist_file.string() + ".json");
Expand Down

0 comments on commit 9a6ea2c

Please sign in to comment.