blob: 5fef26ac050994304feac6405f28481b98e4cf35 [file] [log] [blame]
William A. Kennington III7c9365c2023-06-14 17:41:13 -07001#include <fmt/format.h>
2
William A. Kennington III9330c872023-01-04 15:41:53 -08003#include <stdplus/net/addr/ip.hpp>
4#include <stdplus/numeric/endian.hpp>
5
William A. Kennington III5b20b0a2023-07-18 13:38:37 -07006#include <format>
William A. Kennington III751a6cc2023-06-16 15:41:05 -07007#include <string_view>
8
William A. Kennington III9330c872023-01-04 15:41:53 -08009#include <gtest/gtest.h>
10
William A. Kennington III751a6cc2023-06-16 15:41:05 -070011using std::literals::string_view_literals::operator""sv;
12
William A. Kennington III9330c872023-01-04 15:41:53 -080013namespace stdplus
14{
15
16TEST(EqualOperator, In4Addr)
17{
18 EXPECT_EQ((In4Addr{255, 0, 255, 0}), (in_addr{hton(0xff00ff00)}));
19 EXPECT_EQ((in_addr{}), (In4Addr{}));
20 EXPECT_NE((In4Addr{1}), (In4Addr{in_addr{}}));
21 std::hash<In4Addr>{}(In4Addr{});
22}
23
William A. Kennington IIIdf1e4732023-06-14 17:29:31 -070024TEST(FromStr, In4Addr)
25{
26 EXPECT_THROW(fromStr<In4Addr>(""), std::invalid_argument);
27 EXPECT_THROW(fromStr<In4Addr>("0"), std::invalid_argument);
28 EXPECT_THROW(fromStr<In4Addr>("0.0.0"), std::invalid_argument);
29 EXPECT_THROW(fromStr<In4Addr>("0.0.0."), std::invalid_argument);
30 EXPECT_THROW(fromStr<In4Addr>(".0.0.0"), std::invalid_argument);
31 EXPECT_THROW(fromStr<In4Addr>("0.0.0.0.0"), std::invalid_argument);
32 EXPECT_THROW(fromStr<In4Addr>("x.0.0.0"), std::invalid_argument);
33 EXPECT_THROW(fromStr<In4Addr>("ff.0.0.0"), std::invalid_argument);
34 EXPECT_THROW(fromStr<In4Addr>("256.0.0.0"), std::overflow_error);
35
36 EXPECT_EQ((In4Addr{}), fromStr<In4Addr>("0.0.0.0"));
37 EXPECT_EQ((In4Addr{192, 168, 1, 1}), fromStr<In4Addr>("192.168.001.1"));
William A. Kennington IIIfa0b5142023-06-27 00:01:34 -070038
39 constexpr bool tv = detail::CompileIn4Addr("192.0.0.0").valid;
40 EXPECT_TRUE(tv);
41 constexpr bool t = "192.0.0.0"_ip4 == (In4Addr{192});
42 EXPECT_TRUE(t);
William A. Kennington IIIdf1e4732023-06-14 17:29:31 -070043}
44
William A. Kennington III7c9365c2023-06-14 17:41:13 -070045TEST(ToStr, In4Addr)
46{
47 ToStrHandle<ToStr<In4Addr>> tsh;
48 EXPECT_EQ("255.255.255.255", tsh(in_addr{0xffffffff}));
49 EXPECT_EQ("1.15.3.4", tsh(In4Addr{1, 15, 3, 4}));
50 EXPECT_EQ("0.0.0.0", tsh(In4Addr{}));
51 EXPECT_EQ("a 1.15.3.4 b", fmt::format("a {} b", In4Addr{1, 15, 3, 4}));
William A. Kennington III5b20b0a2023-07-18 13:38:37 -070052 EXPECT_EQ("a 1.15.3.4 b", std::format("a {} b", In4Addr{1, 15, 3, 4}));
William A. Kennington III7c9365c2023-06-14 17:41:13 -070053}
54
William A. Kennington III9330c872023-01-04 15:41:53 -080055TEST(EqualOperator, In6Addr)
56{
57 EXPECT_EQ((In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}),
58 (in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
59 EXPECT_EQ((in6_addr{}), (In6Addr{}));
60 EXPECT_NE((In6Addr{1}), (In6Addr{in6_addr{}}));
61 std::hash<In6Addr>{}(In6Addr{});
62}
63
William A. Kennington III751a6cc2023-06-16 15:41:05 -070064TEST(FromStr, In6Addr)
65{
66 constexpr FromStr<In6Addr> fs;
67 EXPECT_THROW(fs(""sv), std::invalid_argument);
68 EXPECT_THROW(fs("0"sv), std::invalid_argument);
69 EXPECT_THROW(fs("0:0"sv), std::invalid_argument);
70 EXPECT_THROW(fs("0::0:"sv), std::invalid_argument);
71 EXPECT_THROW(fs("0:::"sv), std::invalid_argument);
72 EXPECT_THROW(fs(":::0"sv), std::invalid_argument);
73 EXPECT_THROW(fs("0:::0"sv), std::invalid_argument);
74 EXPECT_THROW(fs("0::0::0"sv), std::invalid_argument);
75 EXPECT_THROW(fs("1::0.0.0."sv), std::invalid_argument);
76 EXPECT_THROW(fs("1::.0.0.0"sv), std::invalid_argument);
77 EXPECT_THROW(fs("x::0"sv), std::invalid_argument);
78 EXPECT_THROW(fs("g::0"sv), std::invalid_argument);
79 EXPECT_THROW(fs("0:1:2:3:4::5:6:7"sv), std::invalid_argument);
80 EXPECT_THROW(fs("::0:1:2:3:4:5:6:7"sv), std::invalid_argument);
81 EXPECT_THROW(fs("0:1:2:3:4:5:6:7::"sv), std::invalid_argument);
82 EXPECT_THROW(fs("0:1:2:3:4:5:6:7:8"sv), std::invalid_argument);
83 EXPECT_THROW(fs("0:1:2:3:4:5:6:0.0.0.0"sv), std::invalid_argument);
84 EXPECT_THROW(fs("0:1:2:3:4:5::0.0.0.0"sv), std::invalid_argument);
85 EXPECT_THROW(fs("ffff0::0"sv), std::overflow_error);
86
87 EXPECT_EQ((In6Addr{}), fs("::"sv));
88 EXPECT_EQ((In6Addr{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
89 255, 255, 255, 255, 255}),
90 fs("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"sv));
91 EXPECT_EQ((In6Addr{}), fs("0:0:0:0:0:0:0:0"sv));
William A. Kennington IIIfa0b5142023-06-27 00:01:34 -070092 EXPECT_EQ((In6Addr{0, 1}), fs("1::"sv));
William A. Kennington III751a6cc2023-06-16 15:41:05 -070093 EXPECT_EQ((In6Addr{0, 0xff}), fs("ff::"sv));
94 EXPECT_EQ((In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}),
95 fs("::ff"sv));
96 EXPECT_EQ((In6Addr{0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}),
97 fs("0:0:ff::ff"sv));
98 EXPECT_EQ((In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 6, 0, 7, 0, 8}),
99 fs("1:2:3:4::6:7:8"sv));
100 EXPECT_EQ((In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0}),
101 fs("1:2:3:4:5:6:7::"sv));
102 EXPECT_EQ((In6Addr{0, 0, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8}),
103 fs("::2:3:4:5:6:7:8"sv));
104 EXPECT_EQ(
105 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}),
106 fs("::ffff:192.168.0.1"sv));
107 EXPECT_EQ((In6Addr{0, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 168, 0, 1}),
108 fs("ff::255.168.0.1"sv));
109 EXPECT_EQ((In6Addr{0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 255, 168, 0, 1}),
110 fs("0:1:2:3:4:5:255.168.0.1"sv));
William A. Kennington IIIfa0b5142023-06-27 00:01:34 -0700111
112 constexpr bool tv = detail::CompileIn6Addr("1::").valid;
113 EXPECT_TRUE(tv);
114 constexpr bool t = "ff02::"_ip6 == In6Addr{0xff, 2};
115 EXPECT_TRUE(t);
116 EXPECT_EQ("1::"_ip6, (In6Addr{0, 1}));
117 EXPECT_EQ("100::"_ip6, (In6Addr{1}));
118 EXPECT_EQ("2::"_ip6, (In6Addr{0, 2}));
119 EXPECT_EQ("ff::"_ip6, (In6Addr{0, 0xff}));
120 EXPECT_EQ("::100"_ip6,
121 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}));
122 EXPECT_EQ("::1"_ip6,
123 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
124 EXPECT_EQ("::2"_ip6,
125 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}));
126 EXPECT_EQ("::ff"_ip6,
127 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
128 EXPECT_EQ("1::1"_ip6,
129 (In6Addr{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
130 EXPECT_EQ("5::b"_ip6,
131 (In6Addr{0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xb}));
William A. Kennington III751a6cc2023-06-16 15:41:05 -0700132}
133
William A. Kennington IIIf53953b2023-06-16 16:05:52 -0700134TEST(ToStr, In6Addr)
135{
136 ToStrHandle<ToStr<In6Addr>> tsh;
137 EXPECT_EQ("::", tsh(In6Addr{}));
138 EXPECT_EQ("ff::", tsh(In6Addr{0, 0xff}));
139 EXPECT_EQ("::ff",
140 tsh(In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
141 EXPECT_EQ("0:0:ff::ff", tsh(In6Addr{0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0, 0, 0,
142 0, 0, 0, 0xff}));
143 EXPECT_EQ("::100:0:ff",
144 tsh(In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0xff}));
145 EXPECT_EQ("ff00::", tsh(In6Addr{0xff}));
146 EXPECT_EQ("1:2:3:4:5:6:7:8",
147 tsh(In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8}));
148 EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
149 tsh(In6Addr{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
150 255, 255, 255, 255, 255}));
151 // rfc5952 4.2.2
152 EXPECT_EQ("1:2:3:4:0:6:7:8",
153 tsh(In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 6, 0, 7, 0, 8}));
154 // rfc5952 4.2.3
155 EXPECT_EQ("1::4:0:0:7:8",
156 tsh(In6Addr{0, 1, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 7, 0, 8}));
157 // rfc5952 5
158 EXPECT_EQ("::ffff:192.168.0.1", tsh(In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0xff, 0xff, 192, 168, 0, 1}));
160
161 EXPECT_EQ("a ff00:: b", fmt::format("a {} b", In6Addr{0xff}));
William A. Kennington III5b20b0a2023-07-18 13:38:37 -0700162 EXPECT_EQ("a ff00:: b", std::format("a {} b", In6Addr{0xff}));
William A. Kennington IIIf53953b2023-06-16 16:05:52 -0700163}
164
William A. Kennington IIIea3aeb72023-01-05 04:22:19 -0800165TEST(EqualOperator, InAnyAddr)
166{
167 EXPECT_EQ(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 0xff}),
169 (In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
170 EXPECT_NE(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0xff}),
172 (in_addr{}));
173 EXPECT_EQ((In6Addr{}), InAnyAddr(In6Addr{}));
174 EXPECT_NE((In4Addr{}), InAnyAddr(In6Addr{}));
175 EXPECT_EQ((In4Addr{}), InAnyAddr(In4Addr{}));
176 EXPECT_NE((In6Addr{}), InAnyAddr(In4Addr{}));
177 EXPECT_EQ(InAnyAddr(In6Addr{}), (in6_addr{}));
178 EXPECT_EQ(InAnyAddr(In4Addr{}), (in_addr{}));
179 EXPECT_NE(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{}));
180 EXPECT_EQ(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{1}));
181
182 std::hash<InAnyAddr>{}(In4Addr{});
183 std::hash<InAnyAddr>{}(In6Addr{});
184}
185
William A. Kennington IIIb26141d2023-06-16 16:13:20 -0700186TEST(FromStr, InAnyAddr)
187{
188 constexpr FromStr<InAnyAddr> fs;
189 EXPECT_EQ((In4Addr{}), fs("0.0.0.0"sv));
190 EXPECT_EQ((In6Addr{}), fs("::"sv));
191 EXPECT_EQ(
192 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}),
193 fs("::ffff:192.168.0.1"sv));
William A. Kennington IIIb396b8c2023-06-27 16:40:19 -0700194
195 constexpr bool tv = detail::CompileInAnyAddr("192.0.0.0").valid;
196 EXPECT_TRUE(tv);
197 constexpr bool tv2 = detail::CompileInAnyAddr("::").valid;
198 EXPECT_TRUE(tv2);
199 EXPECT_EQ((In4Addr{}), "0.0.0.0"_ip);
200 EXPECT_EQ((In6Addr{}), "::"_ip);
201 EXPECT_EQ("ff02::"_ip, (In6Addr{0xff, 2}));
202 EXPECT_EQ("1::"_ip, (In6Addr{0, 1}));
203 EXPECT_EQ("100::"_ip, (In6Addr{1}));
204 EXPECT_EQ("2::"_ip, (In6Addr{0, 2}));
205 EXPECT_EQ("ff::"_ip, (In6Addr{0, 0xff}));
206 EXPECT_EQ("::100"_ip,
207 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}));
208 EXPECT_EQ("::1"_ip,
209 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
210 EXPECT_EQ("::2"_ip,
211 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}));
212 EXPECT_EQ("::ff"_ip,
213 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
214 EXPECT_EQ("1::1"_ip,
215 (In6Addr{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
216 EXPECT_EQ("5::b"_ip,
217 (In6Addr{0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xb}));
William A. Kennington IIIb26141d2023-06-16 16:13:20 -0700218}
219
William A. Kennington III5ee09cf2023-06-16 16:17:35 -0700220TEST(ToStr, InAnyAddr)
221{
222 ToStrHandle<ToStr<InAnyAddr>> tsh;
223 EXPECT_EQ("1.15.3.4", tsh(In4Addr{1, 15, 3, 4}));
224 EXPECT_EQ("ff::", tsh(In6Addr{0, 0xff}));
225
226 EXPECT_EQ("a ff00:: b", fmt::format("a {} b", InAnyAddr{In6Addr{0xff}}));
William A. Kennington III5b20b0a2023-07-18 13:38:37 -0700227 EXPECT_EQ("a ff00:: b", std::format("a {} b", InAnyAddr{In6Addr{0xff}}));
William A. Kennington IIIe38e42a2023-06-29 02:27:01 -0700228 EXPECT_NE("0.0.0.0"_ip, std::optional<InAnyAddr>());
229 EXPECT_EQ("0.0.0.0"_ip, std::optional<InAnyAddr>(In4Addr{}));
William A. Kennington III5ee09cf2023-06-16 16:17:35 -0700230}
231
William A. Kennington IIIa1323d22023-06-27 00:42:43 -0700232TEST(Loopback, In4Addr)
233{
234 constexpr bool t = "127.0.0.0"_ip4.isLoopback();
235 EXPECT_TRUE(t);
236 EXPECT_TRUE("127.0.0.1"_ip4.isLoopback());
237 EXPECT_TRUE("127.0.0.83"_ip4.isLoopback());
238 EXPECT_TRUE("127.253.0.0"_ip4.isLoopback());
239 EXPECT_TRUE("127.255.255.255"_ip4.isLoopback());
240
241 EXPECT_FALSE("0.0.0.0"_ip4.isLoopback());
242 EXPECT_FALSE("0.255.255.255"_ip4.isLoopback());
243 EXPECT_FALSE("126.255.255.255"_ip4.isLoopback());
244 EXPECT_FALSE("128.0.0.0"_ip4.isLoopback());
245 EXPECT_FALSE("224.0.0.0"_ip4.isLoopback());
246 EXPECT_FALSE("227.30.10.50"_ip4.isLoopback());
247 EXPECT_FALSE("239.255.255.255"_ip4.isLoopback());
248 EXPECT_FALSE("255.255.255.255"_ip4.isLoopback());
249}
250
251TEST(Loopback, In6Addr)
252{
253 constexpr bool t = "::1"_ip6.isLoopback();
254 EXPECT_TRUE(t);
255
256 EXPECT_FALSE("::2"_ip6.isLoopback());
257 EXPECT_FALSE("1::"_ip6.isLoopback());
258 EXPECT_FALSE("2001:5938::fd98"_ip6.isLoopback());
259 EXPECT_FALSE("fe80::1"_ip6.isLoopback());
260 EXPECT_FALSE("feff:ffff:ffff:ffff::"_ip6.isLoopback());
261}
262
263TEST(Unicast, In4Addr)
264{
265 constexpr bool t = "1.1.1.1"_ip4.isUnicast();
266 EXPECT_TRUE(t);
267 EXPECT_TRUE("8.8.4.4"_ip4.isUnicast());
268 EXPECT_TRUE("10.30.0.1"_ip4.isUnicast());
269 EXPECT_TRUE("127.0.0.0"_ip4.isUnicast());
270 EXPECT_TRUE("127.0.0.1"_ip4.isUnicast());
271 EXPECT_TRUE("127.0.0.83"_ip4.isUnicast());
272 EXPECT_TRUE("127.253.0.0"_ip4.isUnicast());
273 EXPECT_TRUE("127.255.255.255"_ip4.isUnicast());
274 EXPECT_TRUE("169.253.255.255"_ip4.isUnicast());
275 EXPECT_TRUE("169.254.0.1"_ip4.isUnicast());
276 EXPECT_TRUE("169.254.255.255"_ip4.isUnicast());
277 EXPECT_TRUE("169.255.0.0"_ip4.isUnicast());
278 EXPECT_TRUE("192.168.1.0"_ip4.isUnicast());
279 EXPECT_TRUE("240.0.0.0"_ip4.isUnicast());
280 EXPECT_TRUE("255.255.255.1"_ip4.isUnicast());
281
282 EXPECT_FALSE("0.0.0.0"_ip4.isUnicast());
283 EXPECT_FALSE("0.255.255.255"_ip4.isUnicast());
284 EXPECT_FALSE("224.0.0.0"_ip4.isUnicast());
285 EXPECT_FALSE("227.30.10.50"_ip4.isUnicast());
286 EXPECT_FALSE("239.255.255.255"_ip4.isUnicast());
287 EXPECT_FALSE("255.255.255.255"_ip4.isUnicast());
288}
289
290TEST(Unicast, In6Addr)
291{
292 constexpr bool t = "::2"_ip6.isUnicast();
293 EXPECT_TRUE(t);
294 EXPECT_TRUE("::1"_ip6.isUnicast());
295 EXPECT_TRUE("1::"_ip6.isUnicast());
296 EXPECT_TRUE("2001:5938::fd98"_ip6.isUnicast());
297 EXPECT_TRUE("fe80::1"_ip6.isUnicast());
298 EXPECT_TRUE("feff:ffff:ffff:ffff::"_ip6.isUnicast());
299
300 EXPECT_FALSE("::"_ip6.isUnicast());
301 EXPECT_FALSE("ff00::"_ip6.isUnicast());
302 EXPECT_FALSE("ffff:ffff:ffff:ffff:ffff::"_ip6.isUnicast());
303}
304
William A. Kennington III9330c872023-01-04 15:41:53 -0800305} // namespace stdplus