LCOV - code coverage report
Current view: top level - src/test - sock_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 111 111 100.0 %
Date: 2022-04-21 14:51:19 Functions: 14 14 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 0 -

           Branch data     Line data    Source code
#       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 <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                 :            : 
#      18                 :            : BOOST_FIXTURE_TEST_SUITE(sock_tests, BasicTestingSetup)
#      19                 :            : 
#      20                 :            : static bool SocketIsClosed(const SOCKET& s)
#      21                 :         20 : {
#      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                 :         20 :     int type;
#      26                 :         20 :     socklen_t len = sizeof(type);
#      27                 :         20 :     return getsockopt(s, SOL_SOCKET, SO_TYPE, (sockopt_arg_type)&type, &len) == SOCKET_ERROR;
#      28                 :         20 : }
#      29                 :            : 
#      30                 :            : static SOCKET CreateSocket()
#      31                 :         10 : {
#      32                 :         10 :     const SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#      33                 :         10 :     BOOST_REQUIRE(s != static_cast<SOCKET>(SOCKET_ERROR));
#      34                 :         10 :     return s;
#      35                 :         10 : }
#      36                 :            : 
#      37                 :            : BOOST_AUTO_TEST_CASE(constructor_and_destructor)
#      38                 :          2 : {
#      39                 :          2 :     const SOCKET s = CreateSocket();
#      40                 :          2 :     Sock* sock = new Sock(s);
#      41                 :          2 :     BOOST_CHECK_EQUAL(sock->Get(), s);
#      42                 :          2 :     BOOST_CHECK(!SocketIsClosed(s));
#      43                 :          2 :     delete sock;
#      44                 :          2 :     BOOST_CHECK(SocketIsClosed(s));
#      45                 :          2 : }
#      46                 :            : 
#      47                 :            : BOOST_AUTO_TEST_CASE(move_constructor)
#      48                 :          2 : {
#      49                 :          2 :     const SOCKET s = CreateSocket();
#      50                 :          2 :     Sock* sock1 = new Sock(s);
#      51                 :          2 :     Sock* sock2 = new Sock(std::move(*sock1));
#      52                 :          2 :     delete sock1;
#      53                 :          2 :     BOOST_CHECK(!SocketIsClosed(s));
#      54                 :          2 :     BOOST_CHECK_EQUAL(sock2->Get(), s);
#      55                 :          2 :     delete sock2;
#      56                 :          2 :     BOOST_CHECK(SocketIsClosed(s));
#      57                 :          2 : }
#      58                 :            : 
#      59                 :            : BOOST_AUTO_TEST_CASE(move_assignment)
#      60                 :          2 : {
#      61                 :          2 :     const SOCKET s = CreateSocket();
#      62                 :          2 :     Sock* sock1 = new Sock(s);
#      63                 :          2 :     Sock* sock2 = new Sock();
#      64                 :          2 :     *sock2 = std::move(*sock1);
#      65                 :          2 :     delete sock1;
#      66                 :          2 :     BOOST_CHECK(!SocketIsClosed(s));
#      67                 :          2 :     BOOST_CHECK_EQUAL(sock2->Get(), s);
#      68                 :          2 :     delete sock2;
#      69                 :          2 :     BOOST_CHECK(SocketIsClosed(s));
#      70                 :          2 : }
#      71                 :            : 
#      72                 :            : BOOST_AUTO_TEST_CASE(release)
#      73                 :          2 : {
#      74                 :          2 :     SOCKET s = CreateSocket();
#      75                 :          2 :     Sock* sock = new Sock(s);
#      76                 :          2 :     BOOST_CHECK_EQUAL(sock->Release(), s);
#      77                 :          2 :     delete sock;
#      78                 :          2 :     BOOST_CHECK(!SocketIsClosed(s));
#      79                 :          2 :     BOOST_REQUIRE(CloseSocket(s));
#      80                 :          2 : }
#      81                 :            : 
#      82                 :            : BOOST_AUTO_TEST_CASE(reset)
#      83                 :          2 : {
#      84                 :          2 :     const SOCKET s = CreateSocket();
#      85                 :          2 :     Sock sock(s);
#      86                 :          2 :     sock.Reset();
#      87                 :          2 :     BOOST_CHECK(SocketIsClosed(s));
#      88                 :          2 : }
#      89                 :            : 
#      90                 :            : #ifndef WIN32 // Windows does not have socketpair(2).
#      91                 :            : 
#      92                 :            : static void CreateSocketPair(int s[2])
#      93                 :          6 : {
#      94                 :          6 :     BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, s), 0);
#      95                 :          6 : }
#      96                 :            : 
#      97                 :            : static void SendAndRecvMessage(const Sock& sender, const Sock& receiver)
#      98                 :          4 : {
#      99                 :          4 :     const char* msg = "abcd";
#     100                 :          4 :     constexpr ssize_t msg_len = 4;
#     101                 :          4 :     char recv_buf[10];
#     102                 :            : 
#     103                 :          4 :     BOOST_CHECK_EQUAL(sender.Send(msg, msg_len, 0), msg_len);
#     104                 :          4 :     BOOST_CHECK_EQUAL(receiver.Recv(recv_buf, sizeof(recv_buf), 0), msg_len);
#     105                 :          4 :     BOOST_CHECK_EQUAL(strncmp(msg, recv_buf, msg_len), 0);
#     106                 :          4 : }
#     107                 :            : 
#     108                 :            : BOOST_AUTO_TEST_CASE(send_and_receive)
#     109                 :          2 : {
#     110                 :          2 :     int s[2];
#     111                 :          2 :     CreateSocketPair(s);
#     112                 :            : 
#     113                 :          2 :     Sock* sock0 = new Sock(s[0]);
#     114                 :          2 :     Sock* sock1 = new Sock(s[1]);
#     115                 :            : 
#     116                 :          2 :     SendAndRecvMessage(*sock0, *sock1);
#     117                 :            : 
#     118                 :          2 :     Sock* sock0moved = new Sock(std::move(*sock0));
#     119                 :          2 :     Sock* sock1moved = new Sock();
#     120                 :          2 :     *sock1moved = std::move(*sock1);
#     121                 :            : 
#     122                 :          2 :     delete sock0;
#     123                 :          2 :     delete sock1;
#     124                 :            : 
#     125                 :          2 :     SendAndRecvMessage(*sock1moved, *sock0moved);
#     126                 :            : 
#     127                 :          2 :     delete sock0moved;
#     128                 :          2 :     delete sock1moved;
#     129                 :            : 
#     130                 :          2 :     BOOST_CHECK(SocketIsClosed(s[0]));
#     131                 :          2 :     BOOST_CHECK(SocketIsClosed(s[1]));
#     132                 :          2 : }
#     133                 :            : 
#     134                 :            : BOOST_AUTO_TEST_CASE(wait)
#     135                 :          2 : {
#     136                 :          2 :     int s[2];
#     137                 :          2 :     CreateSocketPair(s);
#     138                 :            : 
#     139                 :          2 :     Sock sock0(s[0]);
#     140                 :          2 :     Sock sock1(s[1]);
#     141                 :            : 
#     142                 :          2 :     std::thread waiter([&sock0]() { (void)sock0.Wait(24h, Sock::RECV); });
#     143                 :            : 
#     144                 :          2 :     BOOST_REQUIRE_EQUAL(sock1.Send("a", 1, 0), 1);
#     145                 :            : 
#     146                 :          2 :     waiter.join();
#     147                 :          2 : }
#     148                 :            : 
#     149                 :            : BOOST_AUTO_TEST_CASE(recv_until_terminator_limit)
#     150                 :          2 : {
#     151                 :          2 :     constexpr auto timeout = 1min; // High enough so that it is never hit.
#     152                 :          2 :     CThreadInterrupt interrupt;
#     153                 :          2 :     int s[2];
#     154                 :          2 :     CreateSocketPair(s);
#     155                 :            : 
#     156                 :          2 :     Sock sock_send(s[0]);
#     157                 :          2 :     Sock sock_recv(s[1]);
#     158                 :            : 
#     159                 :          2 :     std::thread receiver([&sock_recv, &timeout, &interrupt]() {
#     160                 :          2 :         constexpr size_t max_data{10};
#     161                 :          2 :         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                 :          2 :         try {
#     165                 :          2 :             (void)sock_recv.RecvUntilTerminator('\n', timeout, interrupt, max_data);
#     166                 :          2 :         } catch (const std::runtime_error& e) {
#     167                 :          2 :             threw_as_expected = HasReason("too many bytes without a terminator")(e);
#     168                 :          2 :         }
#     169                 :          2 :         assert(threw_as_expected);
#     170                 :          2 :     });
#     171                 :            : 
#     172                 :          2 :     BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("1234567", timeout, interrupt));
#     173                 :          2 :     BOOST_REQUIRE_NO_THROW(sock_send.SendComplete("89a\n", timeout, interrupt));
#     174                 :            : 
#     175                 :          2 :     receiver.join();
#     176                 :          2 : }
#     177                 :            : 
#     178                 :            : #endif /* WIN32 */
#     179                 :            : 
#     180                 :            : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a