From 5c59b1f51f24a0e80127eb95a1d755f46d6219b1 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Tue, 6 Dec 2022 13:38:21 +0100 Subject: [PATCH] test: put the generic parts from StaticContentsSock into a separate class This allows reusing them in other mocked implementations. Also move the implementation (method definitions) to `test/util/net.cpp` to make the header `test/util/net.h` easier to follow. --- src/test/util/net.cpp | 103 +++++++++++++++++++++++++++++++++++++ src/test/util/net.h | 117 +++++++++++++++--------------------------- 2 files changed, 145 insertions(+), 75 deletions(-) diff --git a/src/test/util/net.cpp b/src/test/util/net.cpp index beefc32bee4b7e..77ce3b7585de4f 100644 --- a/src/test/util/net.cpp +++ b/src/test/util/net.cpp @@ -137,3 +137,106 @@ std::vector GetRandomNodeEvictionCandidates(int n_candida } return candidates; } + +// Have different ZeroSock (or others that inherit from it) objects have different +// m_socket because EqualSharedPtrSock compares m_socket and we want to avoid two +// different objects comparing as equal. +static std::atomic g_mocked_sock_fd{0}; + +ZeroSock::ZeroSock() : Sock{g_mocked_sock_fd++} {} + +// Sock::~Sock() would try to close(2) m_socket if it is not INVALID_SOCKET, avoid that. +ZeroSock::~ZeroSock() { m_socket = INVALID_SOCKET; } + +ssize_t ZeroSock::Send(const void*, size_t len, int) const { return len; } + +ssize_t ZeroSock::Recv(void* buf, size_t len, int flags) const +{ + memset(buf, 0x0, len); + return len; +} + +int ZeroSock::Connect(const sockaddr*, socklen_t) const { return 0; } + +int ZeroSock::Bind(const sockaddr*, socklen_t) const { return 0; } + +int ZeroSock::Listen(int) const { return 0; } + +std::unique_ptr ZeroSock::Accept(sockaddr* addr, socklen_t* addr_len) const +{ + 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(sizeof(sockaddr_in)); + if (*addr_len >= write_len) { + *addr_len = write_len; + sockaddr_in* addr_in = reinterpret_cast(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(); +} + +int ZeroSock::GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const +{ + std::memset(opt_val, 0x0, *opt_len); + return 0; +} + +int ZeroSock::SetSockOpt(int, int, const void*, socklen_t) const { return 0; } + +int ZeroSock::GetSockName(sockaddr* name, socklen_t* name_len) const +{ + std::memset(name, 0x0, *name_len); + return 0; +} + +bool ZeroSock::SetNonBlocking() const { return true; } + +bool ZeroSock::IsSelectable() const { return true; } + +bool ZeroSock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const +{ + if (occurred != nullptr) { + *occurred = requested; + } + return true; +} + +bool ZeroSock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const +{ + for (auto& [sock, events] : events_per_sock) { + (void)sock; + events.occurred = events.requested; + } + return true; +} + +ZeroSock& ZeroSock::operator=(Sock&& other) +{ + assert(false && "Move of Sock into ZeroSock not allowed."); + return *this; +} + +StaticContentsSock::StaticContentsSock(const std::string& contents) + : m_contents{contents} +{ +} + +ssize_t StaticContentsSock::Recv(void* buf, size_t len, int flags) const +{ + 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; +} + +StaticContentsSock& StaticContentsSock::operator=(Sock&& other) +{ + assert(false && "Move of Sock into StaticContentsSock not allowed."); + return *this; +} diff --git a/src/test/util/net.h b/src/test/util/net.h index 043e317bf080f8..dfea9dd44b83c3 100644 --- a/src/test/util/net.h +++ b/src/test/util/net.h @@ -133,99 +133,66 @@ constexpr auto ALL_NETWORKS = std::array{ Network::NET_INTERNAL, }; +std::vector GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context); + /** - * 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. + * A mocked Sock alternative that succeeds on all operations. + * Returns infinite amount of 0x0 bytes on reads. */ -class StaticContentsSock : public Sock +class ZeroSock : public Sock { public: - explicit StaticContentsSock(const std::string& contents) - : Sock{INVALID_SOCKET}, - m_contents{contents} - { - } + ZeroSock(); - ~StaticContentsSock() override { m_socket = INVALID_SOCKET; } + ~ZeroSock() override; - 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; - 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; - 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; - int Connect(const sockaddr*, socklen_t) const override { return 0; } + int Bind(const sockaddr*, socklen_t) const override; - int Bind(const sockaddr*, socklen_t) const override { return 0; } + int Listen(int) const override; - int Listen(int) const override { return 0; } + std::unique_ptr Accept(sockaddr* addr, socklen_t* addr_len) const override; - std::unique_ptr 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(sizeof(sockaddr_in)); - if (*addr_len >= write_len) { - *addr_len = write_len; - sockaddr_in* addr_in = reinterpret_cast(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(""); - }; - - 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; - } + int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override; - int SetSockOpt(int, int, const void*, socklen_t) const override { return 0; } + int SetSockOpt(int, int, const void*, socklen_t) const override; - int GetSockName(sockaddr* name, socklen_t* name_len) const override - { - std::memset(name, 0x0, *name_len); - return 0; - } + int GetSockName(sockaddr* name, socklen_t* name_len) const override; - bool SetNonBlocking() const override { return true; } + bool SetNonBlocking() const override; - bool IsSelectable() const override { return true; } + bool IsSelectable() const override; bool Wait(std::chrono::milliseconds timeout, Event requested, - Event* occurred = nullptr) const override - { - if (occurred != nullptr) { - *occurred = requested; - } - return true; - } + Event* occurred = nullptr) const override; - 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; - } + bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override; + +private: + ZeroSock& operator=(Sock&& other) override; +}; + +/** + * 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 ZeroSock +{ +public: + explicit StaticContentsSock(const std::string& contents); + + /** + * Return parts of the contents that was provided at construction until it is exhausted + * and then return 0 (EOF). + */ + ssize_t Recv(void* buf, size_t len, int flags) const override; bool IsConnected(std::string&) const override { @@ -233,10 +200,10 @@ class StaticContentsSock : public Sock } private: + StaticContentsSock& operator=(Sock&& other) override; + const std::string m_contents; mutable size_t m_consumed{0}; }; -std::vector GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context); - #endif // BITCOIN_TEST_UTIL_NET_H