Skip to content

Commit a8695db

Browse files
tests: Add fuzzing harness for functions in addrdb.h
1 parent 97b0687 commit a8695db

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
FUZZ_TARGETS = \
66
test/fuzz/addr_info_deserialize \
7+
test/fuzz/addrdb \
78
test/fuzz/address_deserialize \
89
test/fuzz/addrman_deserialize \
910
test/fuzz/asmap \
@@ -288,6 +289,12 @@ test_fuzz_addr_info_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
288289
test_fuzz_addr_info_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
289290
test_fuzz_addr_info_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
290291

292+
test_fuzz_addrdb_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
293+
test_fuzz_addrdb_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
294+
test_fuzz_addrdb_LDADD = $(FUZZ_SUITE_LD_COMMON)
295+
test_fuzz_addrdb_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
296+
test_fuzz_addrdb_SOURCES = $(FUZZ_SUITE) test/fuzz/addrdb.cpp
297+
291298
test_fuzz_address_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DADDRESS_DESERIALIZE=1
292299
test_fuzz_address_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
293300
test_fuzz_address_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/addrdb.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <addrdb.h>
6+
#include <optional.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/fuzz.h>
9+
#include <test/fuzz/util.h>
10+
11+
#include <cassert>
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19+
20+
const CBanEntry ban_entry = [&] {
21+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 3)) {
22+
case 0:
23+
return CBanEntry{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
24+
break;
25+
case 1:
26+
return CBanEntry{fuzzed_data_provider.ConsumeIntegral<int64_t>(), fuzzed_data_provider.PickValueInArray<BanReason>({
27+
BanReason::BanReasonUnknown,
28+
BanReason::BanReasonNodeMisbehaving,
29+
BanReason::BanReasonManuallyAdded,
30+
})};
31+
break;
32+
case 2: {
33+
const Optional<CBanEntry> ban_entry = ConsumeDeserializable<CBanEntry>(fuzzed_data_provider);
34+
if (ban_entry) {
35+
return *ban_entry;
36+
}
37+
break;
38+
}
39+
}
40+
return CBanEntry{};
41+
}();
42+
assert(!ban_entry.banReasonToString().empty());
43+
}

0 commit comments

Comments
 (0)