2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2020-2021 The Bitcoin Core developers
|
2020-04-04 07:30:51 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_TEST_UTIL_NET_H
|
|
|
|
#define BITCOIN_TEST_UTIL_NET_H
|
|
|
|
|
2021-03-11 16:27:22 +01:00
|
|
|
#include <compat.h>
|
2022-05-26 16:07:04 +02:00
|
|
|
#include <node/eviction.h>
|
2021-05-17 11:11:02 +02:00
|
|
|
#include <netaddress.h>
|
2020-04-04 07:30:51 +08:00
|
|
|
#include <net.h>
|
2021-03-11 16:27:22 +01:00
|
|
|
#include <util/sock.h>
|
|
|
|
|
2021-05-17 11:11:02 +02:00
|
|
|
#include <array>
|
2021-03-11 16:27:22 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2021-04-23 09:43:43 +02:00
|
|
|
#include <memory>
|
2021-03-11 16:27:22 +01:00
|
|
|
#include <string>
|
2020-04-04 07:30:51 +08:00
|
|
|
|
|
|
|
struct ConnmanTestMsg : public CConnman {
|
|
|
|
using CConnman::CConnman;
|
2021-10-07 13:22:51 +02:00
|
|
|
|
2020-07-10 18:19:11 +02:00
|
|
|
void SetPeerConnectTimeout(std::chrono::seconds timeout)
|
2021-10-07 13:22:51 +02:00
|
|
|
{
|
|
|
|
m_peer_connect_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
2020-04-04 07:30:51 +08:00
|
|
|
void AddTestNode(CNode& node)
|
|
|
|
{
|
2021-08-28 20:57:52 +02:00
|
|
|
LOCK(m_nodes_mutex);
|
|
|
|
m_nodes.push_back(&node);
|
2020-04-04 07:30:51 +08:00
|
|
|
}
|
|
|
|
void ClearTestNodes()
|
|
|
|
{
|
2021-08-28 20:57:52 +02:00
|
|
|
LOCK(m_nodes_mutex);
|
|
|
|
for (CNode* node : m_nodes) {
|
2020-04-04 07:30:51 +08:00
|
|
|
delete node;
|
|
|
|
}
|
2021-08-28 20:57:52 +02:00
|
|
|
m_nodes.clear();
|
2020-04-04 07:30:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessMessagesOnce(CNode& node) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }
|
|
|
|
|
2020-11-20 10:16:10 +01:00
|
|
|
void NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const;
|
2020-04-04 07:30:51 +08:00
|
|
|
|
|
|
|
bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
|
|
|
|
};
|
|
|
|
|
2021-01-02 10:14:39 +01:00
|
|
|
constexpr ServiceFlags ALL_SERVICE_FLAGS[]{
|
|
|
|
NODE_NONE,
|
|
|
|
NODE_NETWORK,
|
|
|
|
NODE_BLOOM,
|
|
|
|
NODE_WITNESS,
|
|
|
|
NODE_COMPACT_FILTERS,
|
|
|
|
NODE_NETWORK_LIMITED,
|
|
|
|
};
|
|
|
|
|
2020-12-28 21:58:00 +01:00
|
|
|
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]{
|
2021-03-21 22:46:50 +01:00
|
|
|
NetPermissionFlags::None,
|
|
|
|
NetPermissionFlags::BloomFilter,
|
|
|
|
NetPermissionFlags::Relay,
|
|
|
|
NetPermissionFlags::ForceRelay,
|
|
|
|
NetPermissionFlags::NoBan,
|
|
|
|
NetPermissionFlags::Mempool,
|
|
|
|
NetPermissionFlags::Addr,
|
|
|
|
NetPermissionFlags::Download,
|
|
|
|
NetPermissionFlags::Implicit,
|
|
|
|
NetPermissionFlags::All,
|
2020-12-28 21:58:00 +01:00
|
|
|
};
|
|
|
|
|
2020-12-28 21:31:33 +01:00
|
|
|
constexpr ConnectionType ALL_CONNECTION_TYPES[]{
|
|
|
|
ConnectionType::INBOUND,
|
|
|
|
ConnectionType::OUTBOUND_FULL_RELAY,
|
|
|
|
ConnectionType::MANUAL,
|
|
|
|
ConnectionType::FEELER,
|
|
|
|
ConnectionType::BLOCK_RELAY,
|
|
|
|
ConnectionType::ADDR_FETCH,
|
|
|
|
};
|
|
|
|
|
2021-05-17 11:11:02 +02:00
|
|
|
constexpr auto ALL_NETWORKS = std::array{
|
|
|
|
Network::NET_UNROUTABLE,
|
|
|
|
Network::NET_IPV4,
|
|
|
|
Network::NET_IPV6,
|
|
|
|
Network::NET_ONION,
|
|
|
|
Network::NET_I2P,
|
|
|
|
Network::NET_CJDNS,
|
|
|
|
Network::NET_INTERNAL,
|
|
|
|
};
|
|
|
|
|
2021-03-11 16:27:22 +01:00
|
|
|
/**
|
|
|
|
* A mocked Sock alternative that returns a statically contained data upon read and succeeds
|
|
|
|
* and ignores all writes. The data to be returned is given to the constructor and when it is
|
|
|
|
* exhausted an EOF is returned by further reads.
|
|
|
|
*/
|
|
|
|
class StaticContentsSock : public Sock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit StaticContentsSock(const std::string& contents) : m_contents{contents}, m_consumed{0}
|
|
|
|
{
|
|
|
|
// Just a dummy number that is not INVALID_SOCKET.
|
2021-04-14 22:18:43 +00:00
|
|
|
m_socket = INVALID_SOCKET - 1;
|
2021-03-11 16:27:22 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 15:23:37 +02:00
|
|
|
~StaticContentsSock() override { m_socket = INVALID_SOCKET; }
|
2021-03-11 16:27:22 +01:00
|
|
|
|
|
|
|
StaticContentsSock& operator=(Sock&& other) override
|
|
|
|
{
|
|
|
|
assert(false && "Move of Sock into MockSock not allowed.");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Send(const void*, size_t len, int) const override { return len; }
|
|
|
|
|
|
|
|
ssize_t Recv(void* buf, size_t len, int flags) const override
|
|
|
|
{
|
|
|
|
const size_t consume_bytes{std::min(len, m_contents.size() - m_consumed)};
|
|
|
|
std::memcpy(buf, m_contents.data() + m_consumed, consume_bytes);
|
|
|
|
if ((flags & MSG_PEEK) == 0) {
|
|
|
|
m_consumed += consume_bytes;
|
|
|
|
}
|
|
|
|
return consume_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Connect(const sockaddr*, socklen_t) const override { return 0; }
|
|
|
|
|
2021-04-13 16:31:04 +02:00
|
|
|
int Bind(const sockaddr*, socklen_t) const override { return 0; }
|
|
|
|
|
2021-04-13 16:43:04 +02:00
|
|
|
int Listen(int) const override { return 0; }
|
|
|
|
|
2021-04-23 09:43:43 +02:00
|
|
|
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
|
|
|
|
{
|
|
|
|
if (addr != nullptr) {
|
|
|
|
// Pretend all connections come from 5.5.5.5:6789
|
|
|
|
memset(addr, 0x00, *addr_len);
|
|
|
|
const socklen_t write_len = static_cast<socklen_t>(sizeof(sockaddr_in));
|
|
|
|
if (*addr_len >= write_len) {
|
|
|
|
*addr_len = write_len;
|
|
|
|
sockaddr_in* addr_in = reinterpret_cast<sockaddr_in*>(addr);
|
|
|
|
addr_in->sin_family = AF_INET;
|
|
|
|
memset(&addr_in->sin_addr, 0x05, sizeof(addr_in->sin_addr));
|
|
|
|
addr_in->sin_port = htons(6789);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_unique<StaticContentsSock>("");
|
|
|
|
};
|
|
|
|
|
2021-03-11 16:27:22 +01:00
|
|
|
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
|
|
|
|
{
|
|
|
|
std::memset(opt_val, 0x0, *opt_len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:31:49 +02:00
|
|
|
int SetSockOpt(int, int, const void*, socklen_t) const override { return 0; }
|
|
|
|
|
2021-04-13 14:37:16 +02:00
|
|
|
int GetSockName(sockaddr* name, socklen_t* name_len) const override
|
|
|
|
{
|
|
|
|
std::memset(name, 0x0, *name_len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-11 16:27:22 +01:00
|
|
|
bool Wait(std::chrono::milliseconds timeout,
|
|
|
|
Event requested,
|
|
|
|
Event* occurred = nullptr) const override
|
|
|
|
{
|
|
|
|
if (occurred != nullptr) {
|
|
|
|
*occurred = requested;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-04 16:24:57 +02:00
|
|
|
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override
|
|
|
|
{
|
|
|
|
for (auto& [sock, events] : events_per_sock) {
|
|
|
|
(void)sock;
|
|
|
|
events.occurred = events.requested;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-11 16:27:22 +01:00
|
|
|
private:
|
|
|
|
const std::string m_contents;
|
|
|
|
mutable size_t m_consumed;
|
|
|
|
};
|
|
|
|
|
2021-07-01 12:48:51 +02:00
|
|
|
std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
|
|
|
|
|
2020-04-04 07:30:51 +08:00
|
|
|
#endif // BITCOIN_TEST_UTIL_NET_H
|