Skip to content

Commit 615ba0e

Browse files
committed
test: add Sock unit tests
1 parent 7bd21ce commit 615ba0e

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ BITCOIN_TESTS =\
114114
test/sighash_tests.cpp \
115115
test/sigopcount_tests.cpp \
116116
test/skiplist_tests.cpp \
117+
test/sock_tests.cpp \
117118
test/streams_tests.cpp \
118119
test/sync_tests.cpp \
119120
test/system_tests.cpp \

src/test/sock_tests.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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>
6+
#include <test/util/setup_common.h>
7+
#include <util/sock.h>
8+
#include <util/system.h>
9+
10+
#include <boost/test/unit_test.hpp>
11+
12+
#include <thread>
13+
14+
using namespace std::chrono_literals;
15+
16+
BOOST_FIXTURE_TEST_SUITE(sock_tests, BasicTestingSetup)
17+
18+
static bool SocketIsClosed(const SOCKET& s)
19+
{
20+
// Notice that if another thread is running and creates its own socket after `s` has been
21+
// closed, it may be assigned the same file descriptor number. In this case, our test will
22+
// wrongly pretend that the socket is not closed.
23+
int type;
24+
socklen_t len = sizeof(type);
25+
return getsockopt(s, SOL_SOCKET, SO_TYPE, (sockopt_arg_type)&type, &len) == SOCKET_ERROR;
26+
}
27+
28+
static SOCKET CreateSocket()
29+
{
30+
const SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
31+
BOOST_REQUIRE(s != static_cast<SOCKET>(SOCKET_ERROR));
32+
return s;
33+
}
34+
35+
BOOST_AUTO_TEST_CASE(constructor_and_destructor)
36+
{
37+
const SOCKET s = CreateSocket();
38+
Sock* sock = new Sock(s);
39+
BOOST_CHECK_EQUAL(sock->Get(), s);
40+
BOOST_CHECK(!SocketIsClosed(s));
41+
delete sock;
42+
BOOST_CHECK(SocketIsClosed(s));
43+
}
44+
45+
BOOST_AUTO_TEST_CASE(move_constructor)
46+
{
47+
const SOCKET s = CreateSocket();
48+
Sock* sock1 = new Sock(s);
49+
Sock* sock2 = new Sock(std::move(*sock1));
50+
delete sock1;
51+
BOOST_CHECK(!SocketIsClosed(s));
52+
BOOST_CHECK_EQUAL(sock2->Get(), s);
53+
delete sock2;
54+
BOOST_CHECK(SocketIsClosed(s));
55+
}
56+
57+
BOOST_AUTO_TEST_CASE(move_assignment)
58+
{
59+
const SOCKET s = CreateSocket();
60+
Sock* sock1 = new Sock(s);
61+
Sock* sock2 = new Sock();
62+
*sock2 = std::move(*sock1);
63+
delete sock1;
64+
BOOST_CHECK(!SocketIsClosed(s));
65+
BOOST_CHECK_EQUAL(sock2->Get(), s);
66+
delete sock2;
67+
BOOST_CHECK(SocketIsClosed(s));
68+
}
69+
70+
BOOST_AUTO_TEST_CASE(release)
71+
{
72+
SOCKET s = CreateSocket();
73+
Sock* sock = new Sock(s);
74+
BOOST_CHECK_EQUAL(sock->Release(), s);
75+
delete sock;
76+
BOOST_CHECK(!SocketIsClosed(s));
77+
BOOST_REQUIRE(CloseSocket(s));
78+
}
79+
80+
BOOST_AUTO_TEST_CASE(reset)
81+
{
82+
const SOCKET s = CreateSocket();
83+
Sock sock(s);
84+
sock.Reset();
85+
BOOST_CHECK(SocketIsClosed(s));
86+
}
87+
88+
#ifndef WIN32 // Windows does not have socketpair(2).
89+
90+
static void CreateSocketPair(int s[2])
91+
{
92+
BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, s), 0);
93+
}
94+
95+
static void SendAndRecvMessage(const Sock& sender, const Sock& receiver)
96+
{
97+
const char* msg = "abcd";
98+
constexpr size_t msg_len = 4;
99+
char recv_buf[10];
100+
101+
BOOST_CHECK_EQUAL(sender.Send(msg, msg_len, 0), msg_len);
102+
BOOST_CHECK_EQUAL(receiver.Recv(recv_buf, sizeof(recv_buf), 0), msg_len);
103+
BOOST_CHECK_EQUAL(strncmp(msg, recv_buf, msg_len), 0);
104+
}
105+
106+
BOOST_AUTO_TEST_CASE(send_and_receive)
107+
{
108+
int s[2];
109+
CreateSocketPair(s);
110+
111+
Sock* sock0 = new Sock(s[0]);
112+
Sock* sock1 = new Sock(s[1]);
113+
114+
SendAndRecvMessage(*sock0, *sock1);
115+
116+
Sock* sock0moved = new Sock(std::move(*sock0));
117+
Sock* sock1moved = new Sock();
118+
*sock1moved = std::move(*sock1);
119+
120+
delete sock0;
121+
delete sock1;
122+
123+
SendAndRecvMessage(*sock1moved, *sock0moved);
124+
125+
delete sock0moved;
126+
delete sock1moved;
127+
128+
BOOST_CHECK(SocketIsClosed(s[0]));
129+
BOOST_CHECK(SocketIsClosed(s[1]));
130+
}
131+
132+
BOOST_AUTO_TEST_CASE(wait)
133+
{
134+
int s[2];
135+
CreateSocketPair(s);
136+
137+
Sock sock0(s[0]);
138+
Sock sock1(s[1]);
139+
140+
std::thread waiter([&sock0]() { sock0.Wait(24h, Sock::RECV); });
141+
142+
BOOST_REQUIRE_EQUAL(sock1.Send("a", 1, 0), 1);
143+
144+
waiter.join();
145+
}
146+
147+
#endif /* WIN32 */
148+
149+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)