Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/network_test_util.cc
Line
Count
Source (jump to first uncovered line)
1
#include "network_test_util.hh"
2
3
#include <iomanip>
4
5
Network_Funcs const Network_Class::vtable = {
6
    Method<net_close_cb, Network_Class>::invoke<&Network_Class::close>,
7
    Method<net_accept_cb, Network_Class>::invoke<&Network_Class::accept>,
8
    Method<net_bind_cb, Network_Class>::invoke<&Network_Class::bind>,
9
    Method<net_listen_cb, Network_Class>::invoke<&Network_Class::listen>,
10
    Method<net_recvbuf_cb, Network_Class>::invoke<&Network_Class::recvbuf>,
11
    Method<net_recv_cb, Network_Class>::invoke<&Network_Class::recv>,
12
    Method<net_recvfrom_cb, Network_Class>::invoke<&Network_Class::recvfrom>,
13
    Method<net_send_cb, Network_Class>::invoke<&Network_Class::send>,
14
    Method<net_sendto_cb, Network_Class>::invoke<&Network_Class::sendto>,
15
    Method<net_socket_cb, Network_Class>::invoke<&Network_Class::socket>,
16
    Method<net_socket_nonblock_cb, Network_Class>::invoke<&Network_Class::socket_nonblock>,
17
    Method<net_getsockopt_cb, Network_Class>::invoke<&Network_Class::getsockopt>,
18
    Method<net_setsockopt_cb, Network_Class>::invoke<&Network_Class::setsockopt>,
19
    Method<net_getaddrinfo_cb, Network_Class>::invoke<&Network_Class::getaddrinfo>,
20
    Method<net_freeaddrinfo_cb, Network_Class>::invoke<&Network_Class::freeaddrinfo>,
21
};
22
23
0
int Test_Network::close(void *obj, int sock) { return net->funcs->close(net->obj, sock); }
24
0
int Test_Network::accept(void *obj, int sock) { return net->funcs->accept(net->obj, sock); }
25
int Test_Network::bind(void *obj, int sock, const Network_Addr *addr)
26
0
{
27
0
    return net->funcs->bind(net->obj, sock, addr);
28
0
}
29
int Test_Network::listen(void *obj, int sock, int backlog)
30
0
{
31
0
    return net->funcs->listen(net->obj, sock, backlog);
32
0
}
33
0
int Test_Network::recvbuf(void *obj, int sock) { return net->funcs->recvbuf(net->obj, sock); }
34
int Test_Network::recv(void *obj, int sock, uint8_t *buf, size_t len)
35
0
{
36
0
    return net->funcs->recv(net->obj, sock, buf, len);
37
0
}
38
int Test_Network::recvfrom(void *obj, int sock, uint8_t *buf, size_t len, Network_Addr *addr)
39
0
{
40
0
    return net->funcs->recvfrom(net->obj, sock, buf, len, addr);
41
0
}
42
int Test_Network::send(void *obj, int sock, const uint8_t *buf, size_t len)
43
0
{
44
0
    return net->funcs->send(net->obj, sock, buf, len);
45
0
}
46
int Test_Network::sendto(
47
    void *obj, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr)
48
0
{
49
0
    return net->funcs->sendto(net->obj, sock, buf, len, addr);
50
0
}
51
int Test_Network::socket(void *obj, int domain, int type, int proto)
52
0
{
53
0
    return net->funcs->socket(net->obj, domain, type, proto);
54
0
}
55
int Test_Network::socket_nonblock(void *obj, int sock, bool nonblock)
56
0
{
57
0
    return net->funcs->socket_nonblock(net->obj, sock, nonblock);
58
0
}
59
int Test_Network::getsockopt(
60
    void *obj, int sock, int level, int optname, void *optval, size_t *optlen)
61
0
{
62
0
    return net->funcs->getsockopt(net->obj, sock, level, optname, optval, optlen);
63
0
}
64
int Test_Network::setsockopt(
65
    void *obj, int sock, int level, int optname, const void *optval, size_t optlen)
66
0
{
67
0
    return net->funcs->setsockopt(net->obj, sock, level, optname, optval, optlen);
68
0
}
69
int Test_Network::getaddrinfo(void *obj, int family, Network_Addr **addrs)
70
0
{
71
0
    return net->funcs->getaddrinfo(net->obj, family, addrs);
72
0
}
73
int Test_Network::freeaddrinfo(void *obj, Network_Addr *addrs)
74
0
{
75
0
    return net->funcs->freeaddrinfo(net->obj, addrs);
76
0
}
77
78
1
Network_Class::~Network_Class() = default;
79
80
IP_Port increasing_ip_port::operator()()
81
20
{
82
20
    IP_Port ip_port;
83
20
    ip_port.ip.family = net_family_ipv4();
84
20
    ip_port.ip.ip.v4.uint8[0] = 192;
85
20
    ip_port.ip.ip.v4.uint8[1] = 168;
86
20
    ip_port.ip.ip.v4.uint8[2] = 0;
87
20
    ip_port.ip.ip.v4.uint8[3] = start_;
88
20
    ip_port.port = random_u16(rng_);
89
20
    ++start_;
90
20
    return ip_port;
91
20
}
92
93
IP_Port random_ip_port(const Random *rng)
94
48
{
95
48
    IP_Port ip_port;
96
48
    ip_port.ip.family = net_family_ipv4();
97
48
    ip_port.ip.ip.v4.uint8[0] = 192;
98
48
    ip_port.ip.ip.v4.uint8[1] = 168;
99
48
    ip_port.ip.ip.v4.uint8[2] = 0;
100
48
    ip_port.ip.ip.v4.uint8[3] = random_u08(rng);
101
48
    ip_port.port = random_u16(rng);
102
48
    return ip_port;
103
48
}
104
105
208
bool operator==(Family const &a, Family const &b) { return a.value == b.value; }
106
107
125
bool operator==(IP4 const &a, IP4 const &b) { return a.uint32 == b.uint32; }
108
109
bool operator==(IP6 const &a, IP6 const &b)
110
83
{
111
83
    return a.uint64[0] == b.uint64[0] && a.uint64[1] == b.uint64[1];
112
83
}
113
114
bool operator==(IP const &a, IP const &b)
115
208
{
116
208
    if (!(a.family == b.family)) {
117
0
        return false;
118
0
    }
119
120
208
    if (net_family_is_ipv4(a.family)) {
121
125
        return a.ip.v4 == b.ip.v4;
122
125
    } else {
123
83
        return a.ip.v6 == b.ip.v6;
124
83
    }
125
208
}
126
127
208
bool operator==(IP_Port const &a, IP_Port const &b) { return a.ip == b.ip && a.port == b.port; }
128
129
std::ostream &operator<<(std::ostream &out, IP const &v)
130
0
{
131
0
    Ip_Ntoa ip_str;
132
0
    out << '"' << net_ip_ntoa(&v, &ip_str) << '"';
133
0
    return out;
134
0
}
135
136
std::ostream &operator<<(std::ostream &out, IP_Port const &v)
137
0
{
138
0
    return out << "IP_Port{\n"
139
0
               << "        ip = " << v.ip << ",\n"
140
0
               << "        port = " << std::dec << std::setw(0) << v.port << " }";
141
0
}