Bitcoin Core 31.99.0
P2P Digital Currency
sock_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-present 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#include <common/system.h>
6#include <compat/compat.h>
7#include <test/util/common.h>
9#include <util/sock.h>
10#include <util/threadinterrupt.h>
11
12#include <boost/test/unit_test.hpp>
13
14#include <cassert>
15#include <thread>
16
17using namespace std::chrono_literals;
18
20
21static bool SocketIsClosed(const SOCKET& s)
22{
23 // Notice that if another thread is running and creates its own socket after `s` has been
24 // closed, it may be assigned the same file descriptor number. In this case, our test will
25 // wrongly pretend that the socket is not closed.
26 int type;
27 socklen_t len = sizeof(type);
28 return getsockopt(s, SOL_SOCKET, SO_TYPE, reinterpret_cast<char*>(&type), &len) == SOCKET_ERROR;
29}
30
32{
33 const SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
34 BOOST_REQUIRE(s != static_cast<SOCKET>(SOCKET_ERROR));
35 return s;
36}
37
38BOOST_AUTO_TEST_CASE(constructor_and_destructor)
39{
40 const SOCKET s = CreateSocket();
41 Sock* sock = new Sock(s);
42 BOOST_CHECK(*sock == s);
44 delete sock;
46}
47
48BOOST_AUTO_TEST_CASE(move_constructor)
49{
50 const SOCKET s = CreateSocket();
51 Sock* sock1 = new Sock(s);
52 Sock* sock2 = new Sock(std::move(*sock1));
53 delete sock1;
55 BOOST_CHECK(*sock2 == s);
56 delete sock2;
58}
59
60BOOST_AUTO_TEST_CASE(move_assignment)
61{
62 const SOCKET s1 = CreateSocket();
63 const SOCKET s2 = CreateSocket();
64 Sock* sock1 = new Sock(s1);
65 Sock* sock2 = new Sock(s2);
66
69
70 *sock2 = std::move(*sock1);
73 BOOST_CHECK(*sock2 == s1);
74
75 delete sock1;
78 BOOST_CHECK(*sock2 == s1);
79
80 delete sock2;
83}
84
85#ifndef WIN32 // Windows does not have socketpair(2).
86
87static void CreateSocketPair(int s[2])
88{
89 BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, s), 0);
90}
91
92static void SendAndRecvMessage(const Sock& sender, const Sock& receiver)
93{
94 const char* msg = "abcd";
95 constexpr ssize_t msg_len = 4;
96 char recv_buf[10];
97
98 BOOST_CHECK_EQUAL(sender.Send(msg, msg_len, 0), msg_len);
99 BOOST_CHECK_EQUAL(receiver.Recv(recv_buf, sizeof(recv_buf), 0), msg_len);
100 BOOST_CHECK_EQUAL(strncmp(msg, recv_buf, msg_len), 0);
101}
102
103BOOST_AUTO_TEST_CASE(send_and_receive)
104{
105 int s[2];
107
108 Sock* sock0 = new Sock(s[0]);
109 Sock* sock1 = new Sock(s[1]);
110
111 SendAndRecvMessage(*sock0, *sock1);
112
113 Sock* sock0moved = new Sock(std::move(*sock0));
114 Sock* sock1moved = new Sock(INVALID_SOCKET);
115 *sock1moved = std::move(*sock1);
116
117 delete sock0;
118 delete sock1;
119
120 SendAndRecvMessage(*sock1moved, *sock0moved);
121
122 delete sock0moved;
123 delete sock1moved;
124
127}
128
130{
131 int s[2];
133
134 Sock sock0(s[0]);
135 Sock sock1(s[1]);
136
137 std::thread waiter([&sock0]() { (void)sock0.Wait(24h, Sock::RECV); });
138
139 BOOST_REQUIRE_EQUAL(sock1.Send("a", 1, 0), 1);
140
141 waiter.join();
142}
143
144BOOST_AUTO_TEST_CASE(recv_until_terminator_limit)
145{
146 constexpr auto timeout = 1min; // High enough so that it is never hit.
147 CThreadInterrupt interrupt;
148 int s[2];
150
151 Sock sock_send(s[0]);
152 Sock sock_recv(s[1]);
153
154 std::thread receiver([&sock_recv, &timeout, &interrupt]() {
155 constexpr size_t max_data{10};
156 bool threw_as_expected{false};
157 // BOOST_CHECK_EXCEPTION() writes to some variables shared with the main thread which
158 // creates a data race. So mimic it manually.
159 try {
160 (void)sock_recv.RecvUntilTerminator('\n', timeout, interrupt, max_data);
161 } catch (const std::runtime_error& e) {
162 threw_as_expected = HasReason("too many bytes without a terminator")(e);
163 }
164 assert(threw_as_expected);
165 });
166
167 BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("1234567", timeout, interrupt));
168 BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("89a\n", timeout, interrupt));
169
170 receiver.join();
171}
172
173#endif /* WIN32 */
174
A helper class for interruptible sleeps.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: common.h:18
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
Definition: sock.h:28
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:47
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
Definition: sock.cpp:141
virtual void SendComplete(std::span< const unsigned char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Send the given data, retrying on transient errors.
Definition: sock.cpp:249
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:144
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:52
virtual std::string RecvUntilTerminator(uint8_t terminator, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt, size_t max_data) const
Read from socket until a terminator character is encountered.
Definition: sock.cpp:297
#define INVALID_SOCKET
Definition: compat.h:67
#define SOCKET_ERROR
Definition: compat.h:68
unsigned int SOCKET
Definition: compat.h:57
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
static void CreateSocketPair(int s[2])
Definition: sock_tests.cpp:87
static bool SocketIsClosed(const SOCKET &s)
Definition: sock_tests.cpp:21
BOOST_AUTO_TEST_CASE(constructor_and_destructor)
Definition: sock_tests.cpp:38
static void SendAndRecvMessage(const Sock &sender, const Sock &receiver)
Definition: sock_tests.cpp:92
static SOCKET CreateSocket()
Definition: sock_tests.cpp:31
Basic testing setup.
Definition: setup_common.h:64
assert(!tx.IsCoinBase())