Skip to content

Commit 0eeb9b0

Browse files
committed
[fuzz] Move ConsumeNetAddr to fuzz/util/net.h
1 parent 291c869 commit 0eeb9b0

File tree

10 files changed

+59
-30
lines changed

10 files changed

+59
-30
lines changed

src/Makefile.test_fuzz.include

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ TEST_FUZZ_H = \
1111
test/fuzz/fuzz.h \
1212
test/fuzz/FuzzedDataProvider.h \
1313
test/fuzz/util.h \
14-
test/fuzz/util/mempool.h
14+
test/fuzz/util/mempool.h \
15+
test/fuzz/util/net.h
1516

1617
libtest_fuzz_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
1718
libtest_fuzz_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1819
libtest_fuzz_a_SOURCES = \
1920
test/fuzz/fuzz.cpp \
2021
test/fuzz/util.cpp \
2122
test/fuzz/util/mempool.cpp \
23+
test/fuzz/util/net.cpp \
2224
$(TEST_FUZZ_H)

src/test/fuzz/addrman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <test/fuzz/FuzzedDataProvider.h>
1212
#include <test/fuzz/fuzz.h>
1313
#include <test/fuzz/util.h>
14+
#include <test/fuzz/util/net.h>
1415
#include <test/util/setup_common.h>
1516
#include <time.h>
1617
#include <util/asmap.h>

src/test/fuzz/banman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <test/fuzz/FuzzedDataProvider.h>
99
#include <test/fuzz/fuzz.h>
1010
#include <test/fuzz/util.h>
11+
#include <test/fuzz/util/net.h>
1112
#include <test/util/setup_common.h>
1213
#include <util/readwritefile.h>
1314
#include <util/system.h>

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <test/fuzz/FuzzedDataProvider.h>
1212
#include <test/fuzz/fuzz.h>
1313
#include <test/fuzz/util.h>
14+
#include <test/fuzz/util/net.h>
1415
#include <test/util/setup_common.h>
1516
#include <util/system.h>
1617
#include <util/translation.h>

src/test/fuzz/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <netaddress.h>
66
#include <test/fuzz/FuzzedDataProvider.h>
77
#include <test/fuzz/fuzz.h>
8-
#include <test/fuzz/util.h>
8+
#include <test/fuzz/util/net.h>
99

1010
#include <cassert>
1111
#include <cstdint>

src/test/fuzz/netbase_dns_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <netbase.h>
77
#include <test/fuzz/FuzzedDataProvider.h>
88
#include <test/fuzz/fuzz.h>
9-
#include <test/fuzz/util.h>
9+
#include <test/fuzz/util/net.h>
1010

1111
#include <cstdint>
1212
#include <string>

src/test/fuzz/util.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -508,31 +508,6 @@ bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) n
508508
return false;
509509
}
510510

511-
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept
512-
{
513-
const Network network = fuzzed_data_provider.PickValueInArray({Network::NET_IPV4, Network::NET_IPV6, Network::NET_INTERNAL, Network::NET_ONION});
514-
CNetAddr net_addr;
515-
if (network == Network::NET_IPV4) {
516-
in_addr v4_addr = {};
517-
v4_addr.s_addr = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
518-
net_addr = CNetAddr{v4_addr};
519-
} else if (network == Network::NET_IPV6) {
520-
if (fuzzed_data_provider.remaining_bytes() >= 16) {
521-
in6_addr v6_addr = {};
522-
memcpy(v6_addr.s6_addr, fuzzed_data_provider.ConsumeBytes<uint8_t>(16).data(), 16);
523-
net_addr = CNetAddr{v6_addr, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
524-
}
525-
} else if (network == Network::NET_INTERNAL) {
526-
net_addr.SetInternal(fuzzed_data_provider.ConsumeBytesAsString(32));
527-
} else if (network == Network::NET_ONION) {
528-
auto pub_key{fuzzed_data_provider.ConsumeBytes<uint8_t>(ADDR_TORV3_SIZE)};
529-
pub_key.resize(ADDR_TORV3_SIZE);
530-
const bool ok{net_addr.SetSpecial(OnionToString(pub_key))};
531-
assert(ok);
532-
}
533-
return net_addr;
534-
}
535-
536511
CAddress ConsumeAddress(FuzzedDataProvider& fuzzed_data_provider) noexcept
537512
{
538513
return {ConsumeService(fuzzed_data_provider), ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS), NodeSeconds{std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<uint32_t>()}}};

src/test/fuzz/util.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <streams.h>
2323
#include <test/fuzz/FuzzedDataProvider.h>
2424
#include <test/fuzz/fuzz.h>
25+
#include <test/fuzz/util/net.h>
2526
#include <test/util/net.h>
2627
#include <uint256.h>
2728
#include <version.h>
@@ -283,8 +284,6 @@ inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
283284
return result;
284285
}
285286

286-
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept;
287-
288287
inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept
289288
{
290289
return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()};

src/test/fuzz/util/net.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2009-2021 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 <compat/compat.h>
6+
#include <netaddress.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <util/strencodings.h>
9+
10+
#include <cstdint>
11+
#include <vector>
12+
13+
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept
14+
{
15+
const Network network = fuzzed_data_provider.PickValueInArray({Network::NET_IPV4, Network::NET_IPV6, Network::NET_INTERNAL, Network::NET_ONION});
16+
CNetAddr net_addr;
17+
if (network == Network::NET_IPV4) {
18+
in_addr v4_addr = {};
19+
v4_addr.s_addr = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
20+
net_addr = CNetAddr{v4_addr};
21+
} else if (network == Network::NET_IPV6) {
22+
if (fuzzed_data_provider.remaining_bytes() >= 16) {
23+
in6_addr v6_addr = {};
24+
memcpy(v6_addr.s6_addr, fuzzed_data_provider.ConsumeBytes<uint8_t>(16).data(), 16);
25+
net_addr = CNetAddr{v6_addr, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
26+
}
27+
} else if (network == Network::NET_INTERNAL) {
28+
net_addr.SetInternal(fuzzed_data_provider.ConsumeBytesAsString(32));
29+
} else if (network == Network::NET_ONION) {
30+
auto pub_key{fuzzed_data_provider.ConsumeBytes<uint8_t>(ADDR_TORV3_SIZE)};
31+
pub_key.resize(ADDR_TORV3_SIZE);
32+
const bool ok{net_addr.SetSpecial(OnionToString(pub_key))};
33+
assert(ok);
34+
}
35+
return net_addr;
36+
}

src/test/fuzz/util/net.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2009-2021 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+
#ifndef BITCOIN_TEST_FUZZ_UTIL_NET_H
6+
#define BITCOIN_TEST_FUZZ_UTIL_NET_H
7+
8+
#include <netaddress.h>
9+
10+
class FuzzedDataProvider;
11+
12+
CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept;
13+
14+
#endif // BITCOIN_TEST_FUZZ_UTIL_NET_H

0 commit comments

Comments
 (0)