Bitcoin Core  22.99.0
P2P Digital Currency
sock_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2021 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.h>
7 #include <threadinterrupt.h>
8 #include <util/sock.h>
9 #include <util/system.h>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 #include <cassert>
14 #include <thread>
15 
16 using namespace std::chrono_literals;
17 
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 
73 {
74  SOCKET s = CreateSocket();
75  Sock* sock = new Sock(s);
76  BOOST_CHECK_EQUAL(sock->Release(), s);
77  delete sock;
79  BOOST_REQUIRE(CloseSocket(s));
80 }
81 
83 {
84  const SOCKET s = CreateSocket();
85  Sock sock(s);
86  sock.Reset();
88 }
89 
90 #ifndef WIN32 // Windows does not have socketpair(2).
91 
92 static void CreateSocketPair(int s[2])
93 {
94  BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, s), 0);
95 }
96 
97 static void SendAndRecvMessage(const Sock& sender, const Sock& receiver)
98 {
99  const char* msg = "abcd";
100  constexpr ssize_t msg_len = 4;
101  char recv_buf[10];
102 
103  BOOST_CHECK_EQUAL(sender.Send(msg, msg_len, 0), msg_len);
104  BOOST_CHECK_EQUAL(receiver.Recv(recv_buf, sizeof(recv_buf), 0), msg_len);
105  BOOST_CHECK_EQUAL(strncmp(msg, recv_buf, msg_len), 0);
106 }
107 
108 BOOST_AUTO_TEST_CASE(send_and_receive)
109 {
110  int s[2];
111  CreateSocketPair(s);
112 
113  Sock* sock0 = new Sock(s[0]);
114  Sock* sock1 = new Sock(s[1]);
115 
116  SendAndRecvMessage(*sock0, *sock1);
117 
118  Sock* sock0moved = new Sock(std::move(*sock0));
119  Sock* sock1moved = new Sock();
120  *sock1moved = std::move(*sock1);
121 
122  delete sock0;
123  delete sock1;
124 
125  SendAndRecvMessage(*sock1moved, *sock0moved);
126 
127  delete sock0moved;
128  delete sock1moved;
129 
132 }
133 
135 {
136  int s[2];
137  CreateSocketPair(s);
138 
139  Sock sock0(s[0]);
140  Sock sock1(s[1]);
141 
142  std::thread waiter([&sock0]() { (void)sock0.Wait(24h, Sock::RECV); });
143 
144  BOOST_REQUIRE_EQUAL(sock1.Send("a", 1, 0), 1);
145 
146  waiter.join();
147 }
148 
149 BOOST_AUTO_TEST_CASE(recv_until_terminator_limit)
150 {
151  constexpr auto timeout = 1min; // High enough so that it is never hit.
152  CThreadInterrupt interrupt;
153  int s[2];
154  CreateSocketPair(s);
155 
156  Sock sock_send(s[0]);
157  Sock sock_recv(s[1]);
158 
159  std::thread receiver([&sock_recv, &timeout, &interrupt]() {
160  constexpr size_t max_data{10};
161  bool threw_as_expected{false};
162  // BOOST_CHECK_EXCEPTION() writes to some variables shared with the main thread which
163  // creates a data race. So mimic it manually.
164  try {
165  (void)sock_recv.RecvUntilTerminator('\n', timeout, interrupt, max_data);
166  } catch (const std::runtime_error& e) {
167  threw_as_expected = HasReason("too many bytes without a terminator")(e);
168  }
169  assert(threw_as_expected);
170  });
171 
172  BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("1234567", timeout, interrupt));
173  BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("89a\n", timeout, interrupt));
174 
175  receiver.join();
176 }
177 
178 #endif /* WIN32 */
179 
threadinterrupt.h
HasReason
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:217
assert
assert(!tx.IsCoinBase())
setup_common.h
Sock::SendComplete
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:147
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
SocketIsClosed
static bool SocketIsClosed(const SOCKET &s)
Definition: sock_tests.cpp:20
Sock::Reset
virtual void Reset()
Close if non-empty.
Definition: sock.cpp:59
CloseSocket
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: sock.cpp:331
Sock
RAII helper class that manages a socket.
Definition: sock.h:25
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(constructor_and_destructor)
Definition: sock_tests.cpp:37
Sock::RecvUntilTerminator
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:188
CreateSocket
static SOCKET CreateSocket()
Definition: sock_tests.cpp:30
CreateSocketPair
static void CreateSocketPair(int s[2])
Definition: sock_tests.cpp:92
compat.h
Sock::Recv
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:66
Sock::Wait
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:81
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
Sock::Send
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:61
SOCKET_ERROR
#define SOCKET_ERROR
Definition: compat.h:54
SendAndRecvMessage
static void SendAndRecvMessage(const Sock &sender, const Sock &receiver)
Definition: sock_tests.cpp:97
system.h
Sock::Get
virtual SOCKET Get() const
Get the value of the contained socket.
Definition: sock.cpp:50
sockopt_arg_type
void * sockopt_arg_type
Definition: compat.h:88
Sock::Release
virtual SOCKET Release()
Get the value of the contained socket and drop ownership.
Definition: sock.cpp:52
Sock::RECV
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:114
CThreadInterrupt
Definition: threadinterrupt.h:19
sock.h
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
SOCKET
unsigned int SOCKET
Definition: compat.h:41