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