sock_tests.cpp
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> 8 #include <test/util/setup_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 17 using namespace std::chrono_literals; 18 19 BOOST_FIXTURE_TEST_SUITE(sock_tests, BasicTestingSetup) 20 21 static 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 31 static SOCKET CreateSocket() 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 38 BOOST_AUTO_TEST_CASE(constructor_and_destructor) 39 { 40 const SOCKET s = CreateSocket(); 41 Sock* sock = new Sock(s); 42 BOOST_CHECK(*sock == s); 43 BOOST_CHECK(!SocketIsClosed(s)); 44 delete sock; 45 BOOST_CHECK(SocketIsClosed(s)); 46 } 47 48 BOOST_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; 54 BOOST_CHECK(!SocketIsClosed(s)); 55 BOOST_CHECK(*sock2 == s); 56 delete sock2; 57 BOOST_CHECK(SocketIsClosed(s)); 58 } 59 60 BOOST_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 67 BOOST_CHECK(!SocketIsClosed(s1)); 68 BOOST_CHECK(!SocketIsClosed(s2)); 69 70 *sock2 = std::move(*sock1); 71 BOOST_CHECK(!SocketIsClosed(s1)); 72 BOOST_CHECK(SocketIsClosed(s2)); 73 BOOST_CHECK(*sock2 == s1); 74 75 delete sock1; 76 BOOST_CHECK(!SocketIsClosed(s1)); 77 BOOST_CHECK(SocketIsClosed(s2)); 78 BOOST_CHECK(*sock2 == s1); 79 80 delete sock2; 81 BOOST_CHECK(SocketIsClosed(s1)); 82 BOOST_CHECK(SocketIsClosed(s2)); 83 } 84 85 #ifndef WIN32 // Windows does not have socketpair(2). 86 87 static void CreateSocketPair(int s[2]) 88 { 89 BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, s), 0); 90 } 91 92 static 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 103 BOOST_AUTO_TEST_CASE(send_and_receive) 104 { 105 int s[2]; 106 CreateSocketPair(s); 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 125 BOOST_CHECK(SocketIsClosed(s[0])); 126 BOOST_CHECK(SocketIsClosed(s[1])); 127 } 128 129 BOOST_AUTO_TEST_CASE(wait) 130 { 131 int s[2]; 132 CreateSocketPair(s); 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 144 BOOST_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]; 149 CreateSocketPair(s); 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 175 BOOST_AUTO_TEST_SUITE_END()