mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-09 22:28:51 +02:00
Merge bitcoin/bitcoin#28584: Fuzz: extend CConnman tests
0802398e74fuzz: make it possible to mock (fuzz) CThreadInterrupt (Vasil Dimov)6d9e5d130dfuzz: add CConnman::SocketHandler() to the tests (Vasil Dimov)3265df63a4fuzz: add CConnman::InitBinds() to the tests (Vasil Dimov)91cbf4dbd8fuzz: add CConnman::CreateNodeFromAcceptedSocket() to the tests (Vasil Dimov)50da7432ecfuzz: add CConnman::OpenNetworkConnection() to the tests (Vasil Dimov)e6a917c8f8fuzz: add Fuzzed NetEventsInterface and use it in connman tests (Vasil Dimov)e883b37768fuzz: set the output argument of FuzzedSock::Accept() (Vasil Dimov) Pull request description: Extend `CConnman` fuzz tests to also exercise the methods `OpenNetworkConnection()`, `CreateNodeFromAcceptedSocket()`, `InitBinds()` and `SocketHandler()`. Previously fuzzing those methods would have resulted in real socket functions being called in the operating system which is undesirable during fuzzing. Now that https://github.com/bitcoin/bitcoin/pull/21878 is complete all those are mocked to a fuzzed socket and a fuzzed DNS resolver (see how `CreateSock` and `g_dns_lookup` are replaced in the first commit). ACKs for top commit: achow101: ACK0802398e74jonatack: Review re-ACK0802398e74dergoegge: Code review ACK0802398e74Tree-SHA512: a717d4e79f42bacf2b029c821fdc265e10e4e5c41af77cd4cb452cc5720ec83c62789d5b3dfafd39a22cc8c0500b18169aa7864d497dded729a32ab863dd6c4d
This commit is contained in:
@@ -6,12 +6,14 @@
|
||||
#include <chainparams.h>
|
||||
#include <common/args.h>
|
||||
#include <net.h>
|
||||
#include <net_processing.h>
|
||||
#include <netaddress.h>
|
||||
#include <protocol.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
#include <test/fuzz/util/net.h>
|
||||
#include <test/fuzz/util/threadinterrupt.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <util/translation.h>
|
||||
|
||||
@@ -51,19 +53,36 @@ FUZZ_TARGET(connman, .init = initialize_connman)
|
||||
}
|
||||
}
|
||||
AddrManDeterministic& addr_man{*addr_man_ptr};
|
||||
auto net_events{ConsumeNetEvents(fuzzed_data_provider)};
|
||||
|
||||
// Mock CreateSock() to create FuzzedSock.
|
||||
auto CreateSockOrig = CreateSock;
|
||||
CreateSock = [&fuzzed_data_provider](int, int, int) {
|
||||
return std::make_unique<FuzzedSock>(fuzzed_data_provider);
|
||||
};
|
||||
|
||||
// Mock g_dns_lookup() to return a fuzzed address.
|
||||
auto g_dns_lookup_orig = g_dns_lookup;
|
||||
g_dns_lookup = [&fuzzed_data_provider](const std::string&, bool) {
|
||||
return std::vector<CNetAddr>{ConsumeNetAddr(fuzzed_data_provider)};
|
||||
};
|
||||
|
||||
ConnmanTestMsg connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
|
||||
fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
|
||||
addr_man,
|
||||
netgroupman,
|
||||
Params(),
|
||||
fuzzed_data_provider.ConsumeBool()};
|
||||
fuzzed_data_provider.ConsumeBool(),
|
||||
ConsumeThreadInterrupt(fuzzed_data_provider)};
|
||||
|
||||
const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
|
||||
CConnman::Options options;
|
||||
options.m_msgproc = &net_events;
|
||||
options.nMaxOutboundLimit = max_outbound_limit;
|
||||
connman.Init(options);
|
||||
|
||||
CNetAddr random_netaddr;
|
||||
CAddress random_address;
|
||||
CNode random_node = ConsumeNode(fuzzed_data_provider);
|
||||
CSubNet random_subnet;
|
||||
std::string random_string;
|
||||
@@ -79,6 +98,9 @@ FUZZ_TARGET(connman, .init = initialize_connman)
|
||||
[&] {
|
||||
random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
|
||||
},
|
||||
[&] {
|
||||
random_address = ConsumeAddress(fuzzed_data_provider);
|
||||
},
|
||||
[&] {
|
||||
random_subnet = ConsumeSubNet(fuzzed_data_provider);
|
||||
},
|
||||
@@ -143,6 +165,52 @@ FUZZ_TARGET(connman, .init = initialize_connman)
|
||||
},
|
||||
[&] {
|
||||
connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
|
||||
},
|
||||
[&] {
|
||||
ConnectionType conn_type{
|
||||
fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES)};
|
||||
if (conn_type == ConnectionType::INBOUND) { // INBOUND is not allowed
|
||||
conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
|
||||
}
|
||||
|
||||
connman.OpenNetworkConnection(
|
||||
/*addrConnect=*/random_address,
|
||||
/*fCountFailure=*/fuzzed_data_provider.ConsumeBool(),
|
||||
/*grant_outbound=*/{},
|
||||
/*strDest=*/fuzzed_data_provider.ConsumeBool() ? nullptr : random_string.c_str(),
|
||||
/*conn_type=*/conn_type,
|
||||
/*use_v2transport=*/fuzzed_data_provider.ConsumeBool());
|
||||
},
|
||||
[&] {
|
||||
connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
|
||||
const auto peer = ConsumeAddress(fuzzed_data_provider);
|
||||
connman.CreateNodeFromAcceptedSocketPublic(
|
||||
/*sock=*/CreateSock(AF_INET, SOCK_STREAM, IPPROTO_TCP),
|
||||
/*permissions=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS),
|
||||
/*addr_bind=*/ConsumeAddress(fuzzed_data_provider),
|
||||
/*addr_peer=*/peer);
|
||||
},
|
||||
[&] {
|
||||
CConnman::Options options;
|
||||
|
||||
options.vBinds = ConsumeServiceVector(fuzzed_data_provider);
|
||||
|
||||
options.vWhiteBinds = std::vector<NetWhitebindPermissions>{
|
||||
fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 5)};
|
||||
for (auto& wb : options.vWhiteBinds) {
|
||||
wb.m_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
|
||||
wb.m_service = ConsumeService(fuzzed_data_provider);
|
||||
}
|
||||
|
||||
options.onion_binds = ConsumeServiceVector(fuzzed_data_provider);
|
||||
|
||||
options.bind_on_any = options.vBinds.empty() && options.vWhiteBinds.empty() &&
|
||||
options.onion_binds.empty();
|
||||
|
||||
connman.InitBindsPublic(options);
|
||||
},
|
||||
[&] {
|
||||
connman.SocketHandlerPublic();
|
||||
});
|
||||
}
|
||||
(void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
|
||||
@@ -162,4 +230,6 @@ FUZZ_TARGET(connman, .init = initialize_connman)
|
||||
(void)connman.ASMapHealthCheck();
|
||||
|
||||
connman.ClearTestNodes();
|
||||
g_dns_lookup = g_dns_lookup_orig;
|
||||
CreateSock = CreateSockOrig;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user