Coverage Report

Created: 2026-10-24 08:16

/work/testing/support/doubles/fake_network_udp_test.cc
Line
Count
Source (jump to first uncovered line)
1
#include <gtest/gtest.h>
2
3
#include <cstddef>
4
5
#include "fake_network_stack.hh"
6
#include "network_universe.hh"
7
8
#ifdef _WIN32
9
#define WIN32_LEAN_AND_MEAN
10
#define NOMINMAX
11
#include <winsock2.h>
12
#else
13
#include <netinet/in.h>
14
#include <sys/socket.h>
15
#endif
16
17
namespace tox::test {
18
namespace {
19
20
    class FakeNetworkUdpTest : public ::testing::Test {
21
    public:
22
        FakeNetworkUdpTest()
23
            : ip1(make_ip(0x0A000001))  // 10.0.0.1
24
            , ip2(make_ip(0x0A000002))  // 10.0.0.2
25
            , stack1{universe, ip1}
26
            , stack2{universe, ip2}
27
0
        {
28
0
        }
29
30
    protected:
31
        NetworkUniverse universe;
32
        IP ip1, ip2;
33
        FakeNetworkStack stack1;
34
        FakeNetworkStack stack2;
35
    };
36
37
    TEST_F(FakeNetworkUdpTest, UdpExchange)
38
0
    {
39
0
        Socket sock1 = stack1.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
40
0
        Socket sock2 = stack2.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
41
42
0
        IP_Port addr1;
43
0
        addr1.ip = ip1;
44
0
        addr1.port = net_htons(1234);
45
0
        ASSERT_EQ(stack1.bind(sock1, &addr1), 0);
46
47
0
        IP_Port addr2;
48
0
        addr2.ip = ip2;
49
0
        addr2.port = net_htons(5678);
50
0
        ASSERT_EQ(stack2.bind(sock2, &addr2), 0);
51
52
0
        const char *msg = "Hello UDP";
53
0
        std::size_t msg_len = strlen(msg) + 1;
54
55
        // Send from 1 to 2
56
0
        ASSERT_EQ(stack1.sendto(sock1, reinterpret_cast<const uint8_t *>(msg), msg_len, &addr2),
57
0
            static_cast<int>(msg_len));
58
59
        // Delivery
60
0
        universe.process_events(10);  // With some time offset
61
62
        // Receive at 2
63
0
        uint8_t buffer[1024];
64
0
        IP_Port from_addr;
65
0
        int recv_len = stack2.recvfrom(sock2, buffer, sizeof(buffer), &from_addr);
66
67
0
        ASSERT_EQ(recv_len, static_cast<int>(msg_len));
68
0
        EXPECT_STREQ(reinterpret_cast<const char *>(buffer), msg);
69
0
        EXPECT_EQ(net_ntohl(from_addr.ip.ip.v4.uint32), net_ntohl(ip1.ip.v4.uint32));
70
0
        EXPECT_EQ(net_ntohs(from_addr.port), 1234);
71
72
0
        stack1.close(sock1);
73
0
        stack2.close(sock2);
74
0
    }
75
76
}  // namespace
77
}  // namespace tox::test