|
| 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