diff options
author | John Newbery <john@johnnewbery.com> | 2021-09-21 10:48:32 +0100 |
---|---|---|
committer | John Newbery <john@johnnewbery.com> | 2021-10-05 16:59:07 +0100 |
commit | 90ad8ad61a38dbb1f247a5f3d5f649a856d9938a (patch) | |
tree | bccec98a77f7adb2cc56e5e842790c62c76f53d1 /src/test/fuzz/addrman.cpp | |
parent | 491975c596ebce93ae8de192c9ef171f002fac7c (diff) | |
download | bitcoin-90ad8ad61a38dbb1f247a5f3d5f649a856d9938a.tar.xz |
[fuzz] Make RandAddr() a free function in fuzz/addrman.cpp
It doesn't require access to CAddrManDeterministic
Diffstat (limited to 'src/test/fuzz/addrman.cpp')
-rw-r--r-- | src/test/fuzz/addrman.cpp | 77 |
1 files changed, 38 insertions, 39 deletions
diff --git a/src/test/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp index d797b3dd7a..7b88e9b6c2 100644 --- a/src/test/fuzz/addrman.cpp +++ b/src/test/fuzz/addrman.cpp @@ -36,52 +36,51 @@ FUZZ_TARGET_INIT(data_stream_addr_man, initialize_addrman) } } -class AddrManDeterministic : public AddrMan +/** + * Generate a random address. Always returns a valid address. + */ +CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& fast_random_context) { -public: - explicit AddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider) - : AddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0) - { - WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)}); - } + CNetAddr addr; + if (fuzzed_data_provider.remaining_bytes() > 1 && fuzzed_data_provider.ConsumeBool()) { + addr = ConsumeNetAddr(fuzzed_data_provider); + } else { + // The networks [1..6] correspond to CNetAddr::BIP155Network (private). + static const std::map<uint8_t, uint8_t> net_len_map = {{1, ADDR_IPV4_SIZE}, + {2, ADDR_IPV6_SIZE}, + {4, ADDR_TORV3_SIZE}, + {5, ADDR_I2P_SIZE}, + {6, ADDR_CJDNS_SIZE}}; + uint8_t net = fast_random_context.randrange(5) + 1; // [1..5] + if (net == 3) { + net = 6; + } - /** - * Generate a random address. Always returns a valid address. - */ - CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& fast_random_context) - EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs) - { - CNetAddr addr; - if (fuzzed_data_provider.remaining_bytes() > 1 && fuzzed_data_provider.ConsumeBool()) { - addr = ConsumeNetAddr(fuzzed_data_provider); - } else { - // The networks [1..6] correspond to CNetAddr::BIP155Network (private). - static const std::map<uint8_t, uint8_t> net_len_map = {{1, ADDR_IPV4_SIZE}, - {2, ADDR_IPV6_SIZE}, - {4, ADDR_TORV3_SIZE}, - {5, ADDR_I2P_SIZE}, - {6, ADDR_CJDNS_SIZE}}; - uint8_t net = fast_random_context.randrange(5) + 1; // [1..5] - if (net == 3) { - net = 6; - } + CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT); - CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT); + s << net; + s << fast_random_context.randbytes(net_len_map.at(net)); - s << net; - s << fast_random_context.randbytes(net_len_map.at(net)); + s >> addr; + } - s >> addr; - } + // Return a dummy IPv4 5.5.5.5 if we generated an invalid address. + if (!addr.IsValid()) { + in_addr v4_addr = {}; + v4_addr.s_addr = 0x05050505; + addr = CNetAddr{v4_addr}; + } - // Return a dummy IPv4 5.5.5.5 if we generated an invalid address. - if (!addr.IsValid()) { - in_addr v4_addr = {}; - v4_addr.s_addr = 0x05050505; - addr = CNetAddr{v4_addr}; - } + return addr; +} - return addr; +class AddrManDeterministic : public AddrMan +{ +public: + explicit AddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider) + : AddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0) + { + WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)}); } /** |