mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-08 03:33:32 +01:00
Merge bitcoin/bitcoin#23604: Use Sock in CNode
ef5014d256style: wrap long lines in CNode creation and add some comments (Vasil Dimov)b683491648scripted-diff: rename CNode::cs_hSocket to CNode::m_sock_mutex (Vasil Dimov)c41a1162acnet: use Sock in CNode (Vasil Dimov)c5dd72e146fuzz: move FuzzedSock earlier in src/test/fuzz/util.h (Vasil Dimov) Pull request description: _This is a piece of #21878, chopped off to ease review._ Change `CNode` to use a pointer to `Sock` instead of a bare `SOCKET`. This will help mocking / testing / fuzzing more code. ACKs for top commit: jonatack: re-ACKef5014d256changes since last review are the removal of an unneeded dtor and the addition of a style commit w0xlt: reACKef5014dPastaPastaPasta: utACKef5014d256, I have reviewed the code, and believe it makes sense to merge theStack: Cod-review ACKef5014d256Tree-SHA512: 7f5414dd339cd2f16f7cbdc5fcec238d68b6d50072934aea10b901f409da28ff1ece6db6e899196616aa8127b8b25ab5b86d000bdcee58b4cadd7a3c1cf560c5
This commit is contained in:
@@ -38,6 +38,46 @@
|
||||
|
||||
class PeerManager;
|
||||
|
||||
class FuzzedSock : public Sock
|
||||
{
|
||||
FuzzedDataProvider& m_fuzzed_data_provider;
|
||||
|
||||
/**
|
||||
* Data to return when `MSG_PEEK` is used as a `Recv()` flag.
|
||||
* If `MSG_PEEK` is used, then our `Recv()` returns some random data as usual, but on the next
|
||||
* `Recv()` call we must return the same data, thus we remember it here.
|
||||
*/
|
||||
mutable std::optional<uint8_t> m_peek_data;
|
||||
|
||||
public:
|
||||
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
|
||||
|
||||
~FuzzedSock() override;
|
||||
|
||||
FuzzedSock& operator=(Sock&& other) override;
|
||||
|
||||
void Reset() override;
|
||||
|
||||
ssize_t Send(const void* data, size_t len, int flags) const override;
|
||||
|
||||
ssize_t Recv(void* buf, size_t len, int flags) const override;
|
||||
|
||||
int Connect(const sockaddr*, socklen_t) const override;
|
||||
|
||||
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
|
||||
|
||||
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
|
||||
|
||||
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
|
||||
|
||||
bool IsConnected(std::string& errmsg) const override;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)
|
||||
{
|
||||
return FuzzedSock{fuzzed_data_provider};
|
||||
}
|
||||
|
||||
template <typename... Callables>
|
||||
size_t CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
|
||||
{
|
||||
@@ -250,7 +290,7 @@ auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<N
|
||||
{
|
||||
const NodeId node_id = node_id_in.value_or(fuzzed_data_provider.ConsumeIntegralInRange<NodeId>(0, std::numeric_limits<NodeId>::max()));
|
||||
const ServiceFlags local_services = ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS);
|
||||
const SOCKET socket = INVALID_SOCKET;
|
||||
const auto sock = std::make_shared<FuzzedSock>(fuzzed_data_provider);
|
||||
const CAddress address = ConsumeAddress(fuzzed_data_provider);
|
||||
const uint64_t keyed_net_group = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
|
||||
const uint64_t local_host_nonce = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
|
||||
@@ -259,9 +299,27 @@ auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<N
|
||||
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES);
|
||||
const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false};
|
||||
if constexpr (ReturnUniquePtr) {
|
||||
return std::make_unique<CNode>(node_id, local_services, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion);
|
||||
return std::make_unique<CNode>(node_id,
|
||||
local_services,
|
||||
sock,
|
||||
address,
|
||||
keyed_net_group,
|
||||
local_host_nonce,
|
||||
addr_bind,
|
||||
addr_name,
|
||||
conn_type,
|
||||
inbound_onion);
|
||||
} else {
|
||||
return CNode{node_id, local_services, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion};
|
||||
return CNode{node_id,
|
||||
local_services,
|
||||
sock,
|
||||
address,
|
||||
keyed_net_group,
|
||||
local_host_nonce,
|
||||
addr_bind,
|
||||
addr_name,
|
||||
conn_type,
|
||||
inbound_onion};
|
||||
}
|
||||
}
|
||||
inline std::unique_ptr<CNode> ConsumeNodeAsUniquePtr(FuzzedDataProvider& fdp, const std::optional<NodeId>& node_id_in = std::nullopt) { return ConsumeNode<true>(fdp, node_id_in); }
|
||||
@@ -373,44 +431,4 @@ void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) no
|
||||
}
|
||||
}
|
||||
|
||||
class FuzzedSock : public Sock
|
||||
{
|
||||
FuzzedDataProvider& m_fuzzed_data_provider;
|
||||
|
||||
/**
|
||||
* Data to return when `MSG_PEEK` is used as a `Recv()` flag.
|
||||
* If `MSG_PEEK` is used, then our `Recv()` returns some random data as usual, but on the next
|
||||
* `Recv()` call we must return the same data, thus we remember it here.
|
||||
*/
|
||||
mutable std::optional<uint8_t> m_peek_data;
|
||||
|
||||
public:
|
||||
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
|
||||
|
||||
~FuzzedSock() override;
|
||||
|
||||
FuzzedSock& operator=(Sock&& other) override;
|
||||
|
||||
void Reset() override;
|
||||
|
||||
ssize_t Send(const void* data, size_t len, int flags) const override;
|
||||
|
||||
ssize_t Recv(void* buf, size_t len, int flags) const override;
|
||||
|
||||
int Connect(const sockaddr*, socklen_t) const override;
|
||||
|
||||
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
|
||||
|
||||
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
|
||||
|
||||
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
|
||||
|
||||
bool IsConnected(std::string& errmsg) const override;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)
|
||||
{
|
||||
return FuzzedSock{fuzzed_data_provider};
|
||||
}
|
||||
|
||||
#endif // BITCOIN_TEST_FUZZ_UTIL_H
|
||||
|
||||
Reference in New Issue
Block a user