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