forked from bitcoin-inquisition/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sock_tests.cpp
174 lines (136 loc) · 4.57 KB
/
sock_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2021-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <common/system.h>
#include <compat/compat.h>
#include <test/util/setup_common.h>
#include <util/sock.h>
#include <util/threadinterrupt.h>
#include <boost/test/unit_test.hpp>
#include <cassert>
#include <thread>
using namespace std::chrono_literals;
BOOST_FIXTURE_TEST_SUITE(sock_tests, BasicTestingSetup)
static bool SocketIsClosed(const SOCKET& s)
{
// Notice that if another thread is running and creates its own socket after `s` has been
// closed, it may be assigned the same file descriptor number. In this case, our test will
// wrongly pretend that the socket is not closed.
int type;
socklen_t len = sizeof(type);
return getsockopt(s, SOL_SOCKET, SO_TYPE, (sockopt_arg_type)&type, &len) == SOCKET_ERROR;
}
static SOCKET CreateSocket()
{
const SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
BOOST_REQUIRE(s != static_cast<SOCKET>(SOCKET_ERROR));
return s;
}
BOOST_AUTO_TEST_CASE(constructor_and_destructor)
{
const SOCKET s = CreateSocket();
Sock* sock = new Sock(s);
BOOST_CHECK(*sock == s);
BOOST_CHECK(!SocketIsClosed(s));
delete sock;
BOOST_CHECK(SocketIsClosed(s));
}
BOOST_AUTO_TEST_CASE(move_constructor)
{
const SOCKET s = CreateSocket();
Sock* sock1 = new Sock(s);
Sock* sock2 = new Sock(std::move(*sock1));
delete sock1;
BOOST_CHECK(!SocketIsClosed(s));
BOOST_CHECK(*sock2 == s);
delete sock2;
BOOST_CHECK(SocketIsClosed(s));
}
BOOST_AUTO_TEST_CASE(move_assignment)
{
const SOCKET s1 = CreateSocket();
const SOCKET s2 = CreateSocket();
Sock* sock1 = new Sock(s1);
Sock* sock2 = new Sock(s2);
BOOST_CHECK(!SocketIsClosed(s1));
BOOST_CHECK(!SocketIsClosed(s2));
*sock2 = std::move(*sock1);
BOOST_CHECK(!SocketIsClosed(s1));
BOOST_CHECK(SocketIsClosed(s2));
BOOST_CHECK(*sock2 == s1);
delete sock1;
BOOST_CHECK(!SocketIsClosed(s1));
BOOST_CHECK(SocketIsClosed(s2));
BOOST_CHECK(*sock2 == s1);
delete sock2;
BOOST_CHECK(SocketIsClosed(s1));
BOOST_CHECK(SocketIsClosed(s2));
}
#ifndef WIN32 // Windows does not have socketpair(2).
static void CreateSocketPair(int s[2])
{
BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, s), 0);
}
static void SendAndRecvMessage(const Sock& sender, const Sock& receiver)
{
const char* msg = "abcd";
constexpr ssize_t msg_len = 4;
char recv_buf[10];
BOOST_CHECK_EQUAL(sender.Send(msg, msg_len, 0), msg_len);
BOOST_CHECK_EQUAL(receiver.Recv(recv_buf, sizeof(recv_buf), 0), msg_len);
BOOST_CHECK_EQUAL(strncmp(msg, recv_buf, msg_len), 0);
}
BOOST_AUTO_TEST_CASE(send_and_receive)
{
int s[2];
CreateSocketPair(s);
Sock* sock0 = new Sock(s[0]);
Sock* sock1 = new Sock(s[1]);
SendAndRecvMessage(*sock0, *sock1);
Sock* sock0moved = new Sock(std::move(*sock0));
Sock* sock1moved = new Sock(INVALID_SOCKET);
*sock1moved = std::move(*sock1);
delete sock0;
delete sock1;
SendAndRecvMessage(*sock1moved, *sock0moved);
delete sock0moved;
delete sock1moved;
BOOST_CHECK(SocketIsClosed(s[0]));
BOOST_CHECK(SocketIsClosed(s[1]));
}
BOOST_AUTO_TEST_CASE(wait)
{
int s[2];
CreateSocketPair(s);
Sock sock0(s[0]);
Sock sock1(s[1]);
std::thread waiter([&sock0]() { (void)sock0.Wait(24h, Sock::RECV); });
BOOST_REQUIRE_EQUAL(sock1.Send("a", 1, 0), 1);
waiter.join();
}
BOOST_AUTO_TEST_CASE(recv_until_terminator_limit)
{
constexpr auto timeout = 1min; // High enough so that it is never hit.
CThreadInterrupt interrupt;
int s[2];
CreateSocketPair(s);
Sock sock_send(s[0]);
Sock sock_recv(s[1]);
std::thread receiver([&sock_recv, &timeout, &interrupt]() {
constexpr size_t max_data{10};
bool threw_as_expected{false};
// BOOST_CHECK_EXCEPTION() writes to some variables shared with the main thread which
// creates a data race. So mimic it manually.
try {
(void)sock_recv.RecvUntilTerminator('\n', timeout, interrupt, max_data);
} catch (const std::runtime_error& e) {
threw_as_expected = HasReason("too many bytes without a terminator")(e);
}
assert(threw_as_expected);
});
BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("1234567", timeout, interrupt));
BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("89a\n", timeout, interrupt));
receiver.join();
}
#endif /* WIN32 */
BOOST_AUTO_TEST_SUITE_END()