1 | #include <gtest/gtest.h> |
2 | |
3 | #include <base/system.h> |
4 | |
5 | TEST(NetAddr, FromUrlString) |
6 | { |
7 | NETADDR Addr; |
8 | char aBuf1[NETADDR_MAXSTRSIZE]; |
9 | char aBuf2[NETADDR_MAXSTRSIZE]; |
10 | |
11 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://127.0" , nullptr, 0), -1); // invalid ip |
12 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://ddnet.org" , nullptr, 0), -1); // invalid ip |
13 | EXPECT_EQ(net_addr_from_url(&Addr, "127.0.0.1" , nullptr, 0), 1); // not a URL |
14 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.9+udp://127.0.0.1" , nullptr, 0), 1); // invalid tw protocol |
15 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.7+udp://127.0.0.1" , nullptr, 0), 1); // unsupported tw protocol |
16 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+tcp://127.0.0.1" , nullptr, 0), 1); // invalid internet protocol |
17 | |
18 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://127.0.0.1" , nullptr, 0), 0); |
19 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
20 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
21 | EXPECT_STREQ(aBuf1, "127.0.0.1:0" ); |
22 | EXPECT_STREQ(aBuf2, "127.0.0.1" ); |
23 | |
24 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://127.0.0.1" , nullptr, 0), 0); |
25 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
26 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
27 | EXPECT_STREQ(aBuf1, "127.0.0.1:0" ); |
28 | EXPECT_STREQ(aBuf2, "127.0.0.1" ); |
29 | |
30 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://[0123:4567:89ab:cdef:1:2:3:4]:5678" , nullptr, 0), 0); |
31 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
32 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
33 | EXPECT_STREQ(aBuf1, "[123:4567:89ab:cdef:1:2:3:4]:5678" ); |
34 | EXPECT_STREQ(aBuf2, "[123:4567:89ab:cdef:1:2:3:4]" ); |
35 | |
36 | char aHost[128]; |
37 | EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://ger10.ddnet.org:5678" , aHost, sizeof(aHost)), -1); |
38 | EXPECT_STREQ(aHost, "ger10.ddnet.org:5678" ); |
39 | } |
40 | |
41 | TEST(NetAddr, FromStr) |
42 | { |
43 | NETADDR Addr; |
44 | char aBuf1[NETADDR_MAXSTRSIZE]; |
45 | char aBuf2[NETADDR_MAXSTRSIZE]; |
46 | |
47 | EXPECT_FALSE(net_addr_from_str(&Addr, "127.0.0.1" )); |
48 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
49 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
50 | EXPECT_STREQ(aBuf1, "127.0.0.1:0" ); |
51 | EXPECT_STREQ(aBuf2, "127.0.0.1" ); |
52 | |
53 | EXPECT_FALSE(net_addr_from_str(&Addr, "1.2.3.4:5678" )); |
54 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
55 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
56 | EXPECT_STREQ(aBuf1, "1.2.3.4:5678" ); |
57 | EXPECT_STREQ(aBuf2, "1.2.3.4" ); |
58 | |
59 | EXPECT_FALSE(net_addr_from_str(&Addr, "[::1]" )); |
60 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
61 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
62 | EXPECT_STREQ(aBuf1, "[::1]:0" ); |
63 | EXPECT_STREQ(aBuf2, "[::1]" ); |
64 | |
65 | EXPECT_FALSE(net_addr_from_str(&Addr, "[0123:4567:89ab:cdef:1:2:3:4]:5678" )); |
66 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
67 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
68 | EXPECT_STREQ(aBuf1, "[123:4567:89ab:cdef:1:2:3:4]:5678" ); |
69 | EXPECT_STREQ(aBuf2, "[123:4567:89ab:cdef:1:2:3:4]" ); |
70 | } |
71 | |
72 | TEST(NetAddr, StrV6) |
73 | { |
74 | NETADDR Addr; |
75 | char aBuf1[NETADDR_MAXSTRSIZE]; |
76 | char aBuf2[NETADDR_MAXSTRSIZE]; |
77 | |
78 | // Test vectors from RFC 5952 section 4: |
79 | // https://tools.ietf.org/html/rfc5952#section-4 |
80 | // 4.1 Handling Leading Zeros in a 16-Bit Field |
81 | EXPECT_FALSE(net_addr_from_str(&Addr, "[2001:0db8::0001]:1" )); |
82 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
83 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
84 | EXPECT_STREQ(aBuf1, "[2001:db8::1]:1" ); |
85 | EXPECT_STREQ(aBuf2, "[2001:db8::1]" ); |
86 | |
87 | // 4.2.1 Shorten as Much as Possible |
88 | EXPECT_FALSE(net_addr_from_str(&Addr, "[2001:db8:0:0:0:0:2:1]" )); |
89 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
90 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
91 | EXPECT_STREQ(aBuf1, "[2001:db8::2:1]:0" ); |
92 | EXPECT_STREQ(aBuf2, "[2001:db8::2:1]" ); |
93 | |
94 | EXPECT_FALSE(net_addr_from_str(&Addr, "[2001:db8::0:1]" )); |
95 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
96 | EXPECT_STREQ(aBuf2, "[2001:db8::1]" ); |
97 | |
98 | // 4.2.2 Handling One 16-Bit 0 Field |
99 | EXPECT_FALSE(net_addr_from_str(&Addr, "[2001:db8::1:1:1:1:1]" )); |
100 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
101 | EXPECT_STREQ(aBuf2, "[2001:db8:0:1:1:1:1:1]" ); |
102 | |
103 | // 4.2.3 Choice in Placement of "::" |
104 | EXPECT_FALSE(net_addr_from_str(&Addr, "[2001:0:0:1:0:0:0:1]" )); |
105 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
106 | EXPECT_STREQ(aBuf2, "[2001:0:0:1::1]" ); |
107 | |
108 | EXPECT_FALSE(net_addr_from_str(&Addr, "[2001:db8:0:0:1:0:0:1]" )); |
109 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
110 | EXPECT_STREQ(aBuf2, "[2001:db8::1:0:0:1]" ); |
111 | } |
112 | |
113 | TEST(NetAddr, FromStrInvalid) |
114 | { |
115 | NETADDR Addr; |
116 | EXPECT_TRUE(net_addr_from_str(&Addr, "127.0.0." )); |
117 | EXPECT_TRUE(net_addr_from_str(&Addr, "127.0.0.1a" )); |
118 | EXPECT_TRUE(net_addr_from_str(&Addr, "1.1" )); |
119 | EXPECT_TRUE(net_addr_from_str(&Addr, "[::1" )); |
120 | EXPECT_TRUE(net_addr_from_str(&Addr, "[::" )); |
121 | EXPECT_TRUE(net_addr_from_str(&Addr, "127.0.0.1:" )); |
122 | EXPECT_TRUE(net_addr_from_str(&Addr, "[::]:" )); |
123 | EXPECT_TRUE(net_addr_from_str(&Addr, "127.0.0.1:1a" )); |
124 | EXPECT_TRUE(net_addr_from_str(&Addr, "[::]:c" )); |
125 | } |
126 | |
127 | TEST(NetAddr, StrInvalid) |
128 | { |
129 | NETADDR Addr = {.type: 0}; |
130 | char aBuf1[NETADDR_MAXSTRSIZE]; |
131 | char aBuf2[NETADDR_MAXSTRSIZE]; |
132 | net_addr_str(addr: &Addr, string: aBuf1, max_length: sizeof(aBuf1), add_port: true); |
133 | EXPECT_STREQ(aBuf1, "unknown type 0" ); |
134 | net_addr_str(addr: &Addr, string: aBuf2, max_length: sizeof(aBuf2), add_port: false); |
135 | EXPECT_STREQ(aBuf2, "unknown type 0" ); |
136 | } |
137 | |