1 // Copyright (c) 2020 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_UTIL_NET_H
6 #define BITCOIN_TEST_UTIL_NET_H
7 
8 #include <compat.h>
9 #include <netaddress.h>
10 #include <net.h>
11 #include <util/sock.h>
12 
13 #include <array>
14 #include <cassert>
15 #include <cstring>
16 #include <string>
17 
18 struct ConnmanTestMsg : public CConnman {
19     using CConnman::CConnman;
AddTestNodeConnmanTestMsg20     void AddTestNode(CNode& node)
21     {
22         LOCK(cs_vNodes);
23         vNodes.push_back(&node);
24     }
ClearTestNodesConnmanTestMsg25     void ClearTestNodes()
26     {
27         LOCK(cs_vNodes);
28         for (CNode* node : vNodes) {
29             delete node;
30         }
31         vNodes.clear();
32     }
33 
ProcessMessagesOnceConnmanTestMsg34     void ProcessMessagesOnce(CNode& node) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }
35 
36     void NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const;
37 
38     bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
39 };
40 
41 constexpr ServiceFlags ALL_SERVICE_FLAGS[]{
42     NODE_NONE,
43     NODE_NETWORK,
44     NODE_BLOOM,
45     NODE_WITNESS,
46     NODE_COMPACT_FILTERS,
47     NODE_NETWORK_LIMITED,
48 };
49 
50 constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]{
51     NetPermissionFlags::None,
52     NetPermissionFlags::BloomFilter,
53     NetPermissionFlags::Relay,
54     NetPermissionFlags::ForceRelay,
55     NetPermissionFlags::NoBan,
56     NetPermissionFlags::Mempool,
57     NetPermissionFlags::Addr,
58     NetPermissionFlags::Download,
59     NetPermissionFlags::Implicit,
60     NetPermissionFlags::All,
61 };
62 
63 constexpr ConnectionType ALL_CONNECTION_TYPES[]{
64     ConnectionType::INBOUND,
65     ConnectionType::OUTBOUND_FULL_RELAY,
66     ConnectionType::MANUAL,
67     ConnectionType::FEELER,
68     ConnectionType::BLOCK_RELAY,
69     ConnectionType::ADDR_FETCH,
70 };
71 
72 constexpr auto ALL_NETWORKS = std::array{
73     Network::NET_UNROUTABLE,
74     Network::NET_IPV4,
75     Network::NET_IPV6,
76     Network::NET_ONION,
77     Network::NET_I2P,
78     Network::NET_CJDNS,
79     Network::NET_INTERNAL,
80 };
81 
82 /**
83  * A mocked Sock alternative that returns a statically contained data upon read and succeeds
84  * and ignores all writes. The data to be returned is given to the constructor and when it is
85  * exhausted an EOF is returned by further reads.
86  */
87 class StaticContentsSock : public Sock
88 {
89 public:
StaticContentsSock(const std::string & contents)90     explicit StaticContentsSock(const std::string& contents) : m_contents{contents}, m_consumed{0}
91     {
92         // Just a dummy number that is not INVALID_SOCKET.
93         m_socket = INVALID_SOCKET - 1;
94     }
95 
~StaticContentsSock()96     ~StaticContentsSock() override { Reset(); }
97 
98     StaticContentsSock& operator=(Sock&& other) override
99     {
100         assert(false && "Move of Sock into MockSock not allowed.");
101         return *this;
102     }
103 
Reset()104     void Reset() override
105     {
106         m_socket = INVALID_SOCKET;
107     }
108 
Send(const void *,size_t len,int)109     ssize_t Send(const void*, size_t len, int) const override { return len; }
110 
Recv(void * buf,size_t len,int flags)111     ssize_t Recv(void* buf, size_t len, int flags) const override
112     {
113         const size_t consume_bytes{std::min(len, m_contents.size() - m_consumed)};
114         std::memcpy(buf, m_contents.data() + m_consumed, consume_bytes);
115         if ((flags & MSG_PEEK) == 0) {
116             m_consumed += consume_bytes;
117         }
118         return consume_bytes;
119     }
120 
Connect(const sockaddr *,socklen_t)121     int Connect(const sockaddr*, socklen_t) const override { return 0; }
122 
GetSockOpt(int level,int opt_name,void * opt_val,socklen_t * opt_len)123     int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
124     {
125         std::memset(opt_val, 0x0, *opt_len);
126         return 0;
127     }
128 
129     bool Wait(std::chrono::milliseconds timeout,
130               Event requested,
131               Event* occurred = nullptr) const override
132     {
133         if (occurred != nullptr) {
134             *occurred = requested;
135         }
136         return true;
137     }
138 
139 private:
140     const std::string m_contents;
141     mutable size_t m_consumed;
142 };
143 
144 std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
145 
146 #endif // BITCOIN_TEST_UTIL_NET_H
147