Skip to content

Commit 5a9ee08

Browse files
tests: Add fuzzing harness for node eviction logic
1 parent b440c33 commit 5a9ee08

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ test_fuzz_fuzz_SOURCES = \
233233
test/fuzz/net.cpp \
234234
test/fuzz/net_permissions.cpp \
235235
test/fuzz/netaddress.cpp \
236+
test/fuzz/node_eviction.cpp \
236237
test/fuzz/p2p_transport_deserializer.cpp \
237238
test/fuzz/parse_hd_keypath.cpp \
238239
test/fuzz/parse_iso8601.cpp \

src/test/fuzz/node_eviction.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 <net.h>
6+
#include <optional.h>
7+
#include <protocol.h>
8+
#include <test/fuzz/FuzzedDataProvider.h>
9+
#include <test/fuzz/fuzz.h>
10+
#include <test/fuzz/util.h>
11+
12+
#include <algorithm>
13+
#include <cassert>
14+
#include <cstdint>
15+
#include <optional>
16+
#include <vector>
17+
18+
FUZZ_TARGET(node_eviction)
19+
{
20+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
21+
std::vector<NodeEvictionCandidate> eviction_candidates;
22+
while (fuzzed_data_provider.ConsumeBool()) {
23+
eviction_candidates.push_back({
24+
fuzzed_data_provider.ConsumeIntegral<NodeId>(),
25+
fuzzed_data_provider.ConsumeIntegral<int64_t>(),
26+
fuzzed_data_provider.ConsumeIntegral<int64_t>(),
27+
fuzzed_data_provider.ConsumeIntegral<int64_t>(),
28+
fuzzed_data_provider.ConsumeIntegral<int64_t>(),
29+
fuzzed_data_provider.ConsumeBool(),
30+
fuzzed_data_provider.ConsumeBool(),
31+
fuzzed_data_provider.ConsumeBool(),
32+
fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
33+
fuzzed_data_provider.ConsumeBool(),
34+
fuzzed_data_provider.ConsumeBool(),
35+
});
36+
}
37+
// Make a copy since eviction_candidates may be in some valid but otherwise
38+
// indeterminate state after the SelectNodeToEvict(&&) call.
39+
const std::vector<NodeEvictionCandidate> eviction_candidates_copy = eviction_candidates;
40+
const Optional<NodeId> node_to_evict = SelectNodeToEvict(std::move(eviction_candidates));
41+
if (node_to_evict) {
42+
assert(std::any_of(eviction_candidates_copy.begin(), eviction_candidates_copy.end(), [&node_to_evict](const NodeEvictionCandidate& eviction_candidate) { return *node_to_evict == eviction_candidate.id; }));
43+
}
44+
}

0 commit comments

Comments
 (0)