1#include <gtest/gtest.h>
2
3#include <base/system.h>
4
5TEST(Net, Ipv4AndIpv6Work)
6{
7 NETADDR Bindaddr = {};
8 NETSOCKET Socket1;
9 NETSOCKET Socket2;
10
11 Bindaddr.type = NETTYPE_IPV4 | NETTYPE_IPV6;
12 Socket2 = net_udp_create(bindaddr: Bindaddr);
13 do
14 {
15 Bindaddr.port = secure_rand() % 64511 + 1024;
16 } while(!(Socket1 = net_udp_create(bindaddr: Bindaddr)));
17
18 NETADDR LocalhostV4;
19 NETADDR LocalhostV6;
20 NETADDR TargetV4;
21 NETADDR TargetV6;
22 ASSERT_FALSE(net_addr_from_str(&LocalhostV4, "127.0.0.1"));
23 ASSERT_FALSE(net_addr_from_str(&LocalhostV6, "[::1]"));
24 TargetV4 = LocalhostV4;
25 TargetV6 = LocalhostV6;
26 TargetV4.port = Bindaddr.port;
27 TargetV6.port = Bindaddr.port;
28
29 NETADDR Addr;
30 unsigned char *pData;
31
32 EXPECT_EQ(net_udp_send(Socket2, &TargetV4, "abc", 3), 3);
33
34 EXPECT_EQ(net_socket_read_wait(Socket1, 10000000), 1);
35 ASSERT_EQ(net_udp_recv(Socket1, &Addr, &pData), 3);
36 Addr.port = 0;
37 EXPECT_EQ(Addr, LocalhostV4);
38 EXPECT_EQ(mem_comp(pData, "abc", 3), 0);
39
40 EXPECT_EQ(net_udp_send(Socket2, &TargetV6, "def", 3), 3);
41
42 EXPECT_EQ(net_socket_read_wait(Socket1, 10000000), 1);
43 ASSERT_EQ(net_udp_recv(Socket1, &Addr, &pData), 3);
44 Addr.port = 0;
45 EXPECT_EQ(Addr, LocalhostV6);
46 EXPECT_EQ(mem_comp(pData, "def", 3), 0);
47
48 net_udp_close(sock: Socket1);
49 net_udp_close(sock: Socket2);
50}
51