aboutsummaryrefslogtreecommitdiff
path: root/src/test/util/net.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/util/net.h')
-rw-r--r--src/test/util/net.h88
1 files changed, 78 insertions, 10 deletions
diff --git a/src/test/util/net.h b/src/test/util/net.h
index e25036be26..71685d437a 100644
--- a/src/test/util/net.h
+++ b/src/test/util/net.h
@@ -5,7 +5,13 @@
#ifndef BITCOIN_TEST_UTIL_NET_H
#define BITCOIN_TEST_UTIL_NET_H
+#include <compat.h>
#include <net.h>
+#include <util/sock.h>
+
+#include <cassert>
+#include <cstring>
+#include <string>
struct ConnmanTestMsg : public CConnman {
using CConnman::CConnman;
@@ -40,16 +46,16 @@ constexpr ServiceFlags ALL_SERVICE_FLAGS[]{
};
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]{
- NetPermissionFlags::PF_NONE,
- NetPermissionFlags::PF_BLOOMFILTER,
- NetPermissionFlags::PF_RELAY,
- NetPermissionFlags::PF_FORCERELAY,
- NetPermissionFlags::PF_NOBAN,
- NetPermissionFlags::PF_MEMPOOL,
- NetPermissionFlags::PF_ADDR,
- NetPermissionFlags::PF_DOWNLOAD,
- NetPermissionFlags::PF_ISIMPLICIT,
- NetPermissionFlags::PF_ALL,
+ NetPermissionFlags::None,
+ NetPermissionFlags::BloomFilter,
+ NetPermissionFlags::Relay,
+ NetPermissionFlags::ForceRelay,
+ NetPermissionFlags::NoBan,
+ NetPermissionFlags::Mempool,
+ NetPermissionFlags::Addr,
+ NetPermissionFlags::Download,
+ NetPermissionFlags::Implicit,
+ NetPermissionFlags::All,
};
constexpr ConnectionType ALL_CONNECTION_TYPES[]{
@@ -61,4 +67,66 @@ constexpr ConnectionType ALL_CONNECTION_TYPES[]{
ConnectionType::ADDR_FETCH,
};
+/**
+ * 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.
+ m_socket = INVALID_SOCKET - 1;
+ }
+
+ ~StaticContentsSock() override { Reset(); }
+
+ StaticContentsSock& operator=(Sock&& other) override
+ {
+ assert(false && "Move of Sock into MockSock not allowed.");
+ return *this;
+ }
+
+ void Reset() override
+ {
+ m_socket = INVALID_SOCKET;
+ }
+
+ 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; }
+
+ 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;
+ }
+
+ bool Wait(std::chrono::milliseconds timeout,
+ Event requested,
+ Event* occurred = nullptr) const override
+ {
+ if (occurred != nullptr) {
+ *occurred = requested;
+ }
+ return true;
+ }
+
+private:
+ const std::string m_contents;
+ mutable size_t m_consumed;
+};
+
#endif // BITCOIN_TEST_UTIL_NET_H