blob: 4256e426572b7b5ae22dbfda403aaed1e3695a95 [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 III751a6cc2023-06-16 15:41:05 -07006#include <string_view>
7
William A. Kennington III9330c872023-01-04 15:41:53 -08008#include <gtest/gtest.h>
9
William A. Kennington III751a6cc2023-06-16 15:41:05 -070010using std::literals::string_view_literals::operator""sv;
11
William A. Kennington III9330c872023-01-04 15:41:53 -080012namespace stdplus
13{
14
15TEST(EqualOperator, In4Addr)
16{
17 EXPECT_EQ((In4Addr{255, 0, 255, 0}), (in_addr{hton(0xff00ff00)}));
18 EXPECT_EQ((in_addr{}), (In4Addr{}));
19 EXPECT_NE((In4Addr{1}), (In4Addr{in_addr{}}));
20 std::hash<In4Addr>{}(In4Addr{});
21}
22
William A. Kennington IIIdf1e4732023-06-14 17:29:31 -070023TEST(FromStr, In4Addr)
24{
25 EXPECT_THROW(fromStr<In4Addr>(""), std::invalid_argument);
26 EXPECT_THROW(fromStr<In4Addr>("0"), std::invalid_argument);
27 EXPECT_THROW(fromStr<In4Addr>("0.0.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.0.0"), std::invalid_argument);
31 EXPECT_THROW(fromStr<In4Addr>("x.0.0.0"), std::invalid_argument);
32 EXPECT_THROW(fromStr<In4Addr>("ff.0.0.0"), std::invalid_argument);
33 EXPECT_THROW(fromStr<In4Addr>("256.0.0.0"), std::overflow_error);
34
35 EXPECT_EQ((In4Addr{}), fromStr<In4Addr>("0.0.0.0"));
36 EXPECT_EQ((In4Addr{192, 168, 1, 1}), fromStr<In4Addr>("192.168.001.1"));
William A. Kennington IIIfa0b5142023-06-27 00:01:34 -070037
38 constexpr bool tv = detail::CompileIn4Addr("192.0.0.0").valid;
39 EXPECT_TRUE(tv);
40 constexpr bool t = "192.0.0.0"_ip4 == (In4Addr{192});
41 EXPECT_TRUE(t);
William A. Kennington IIIdf1e4732023-06-14 17:29:31 -070042}
43
William A. Kennington III7c9365c2023-06-14 17:41:13 -070044TEST(ToStr, In4Addr)
45{
46 ToStrHandle<ToStr<In4Addr>> tsh;
47 EXPECT_EQ("255.255.255.255", tsh(in_addr{0xffffffff}));
48 EXPECT_EQ("1.15.3.4", tsh(In4Addr{1, 15, 3, 4}));
49 EXPECT_EQ("0.0.0.0", tsh(In4Addr{}));
50 EXPECT_EQ("a 1.15.3.4 b", fmt::format("a {} b", In4Addr{1, 15, 3, 4}));
51}
52
William A. Kennington III9330c872023-01-04 15:41:53 -080053TEST(EqualOperator, In6Addr)
54{
55 EXPECT_EQ((In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}),
56 (in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
57 EXPECT_EQ((in6_addr{}), (In6Addr{}));
58 EXPECT_NE((In6Addr{1}), (In6Addr{in6_addr{}}));
59 std::hash<In6Addr>{}(In6Addr{});
60}
61
William A. Kennington III751a6cc2023-06-16 15:41:05 -070062TEST(FromStr, In6Addr)
63{
64 constexpr FromStr<In6Addr> fs;
65 EXPECT_THROW(fs(""sv), std::invalid_argument);
66 EXPECT_THROW(fs("0"sv), std::invalid_argument);
67 EXPECT_THROW(fs("0:0"sv), std::invalid_argument);
68 EXPECT_THROW(fs("0::0:"sv), std::invalid_argument);
69 EXPECT_THROW(fs("0:::"sv), std::invalid_argument);
70 EXPECT_THROW(fs(":::0"sv), std::invalid_argument);
71 EXPECT_THROW(fs("0:::0"sv), std::invalid_argument);
72 EXPECT_THROW(fs("0::0::0"sv), std::invalid_argument);
73 EXPECT_THROW(fs("1::0.0.0."sv), std::invalid_argument);
74 EXPECT_THROW(fs("1::.0.0.0"sv), std::invalid_argument);
75 EXPECT_THROW(fs("x::0"sv), std::invalid_argument);
76 EXPECT_THROW(fs("g::0"sv), std::invalid_argument);
77 EXPECT_THROW(fs("0:1:2:3:4::5:6:7"sv), std::invalid_argument);
78 EXPECT_THROW(fs("::0:1:2:3:4:5:6:7"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:8"sv), std::invalid_argument);
81 EXPECT_THROW(fs("0:1:2:3:4:5:6:0.0.0.0"sv), std::invalid_argument);
82 EXPECT_THROW(fs("0:1:2:3:4:5::0.0.0.0"sv), std::invalid_argument);
83 EXPECT_THROW(fs("ffff0::0"sv), std::overflow_error);
84
85 EXPECT_EQ((In6Addr{}), fs("::"sv));
86 EXPECT_EQ((In6Addr{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
87 255, 255, 255, 255, 255}),
88 fs("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"sv));
89 EXPECT_EQ((In6Addr{}), fs("0:0:0:0:0:0:0:0"sv));
William A. Kennington IIIfa0b5142023-06-27 00:01:34 -070090 EXPECT_EQ((In6Addr{0, 1}), fs("1::"sv));
William A. Kennington III751a6cc2023-06-16 15:41:05 -070091 EXPECT_EQ((In6Addr{0, 0xff}), fs("ff::"sv));
92 EXPECT_EQ((In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}),
93 fs("::ff"sv));
94 EXPECT_EQ((In6Addr{0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}),
95 fs("0:0:ff::ff"sv));
96 EXPECT_EQ((In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 6, 0, 7, 0, 8}),
97 fs("1:2:3:4::6:7:8"sv));
98 EXPECT_EQ((In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0}),
99 fs("1:2:3:4:5:6:7::"sv));
100 EXPECT_EQ((In6Addr{0, 0, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8}),
101 fs("::2:3:4:5:6:7:8"sv));
102 EXPECT_EQ(
103 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}),
104 fs("::ffff:192.168.0.1"sv));
105 EXPECT_EQ((In6Addr{0, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 168, 0, 1}),
106 fs("ff::255.168.0.1"sv));
107 EXPECT_EQ((In6Addr{0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 255, 168, 0, 1}),
108 fs("0:1:2:3:4:5:255.168.0.1"sv));
William A. Kennington IIIfa0b5142023-06-27 00:01:34 -0700109
110 constexpr bool tv = detail::CompileIn6Addr("1::").valid;
111 EXPECT_TRUE(tv);
112 constexpr bool t = "ff02::"_ip6 == In6Addr{0xff, 2};
113 EXPECT_TRUE(t);
114 EXPECT_EQ("1::"_ip6, (In6Addr{0, 1}));
115 EXPECT_EQ("100::"_ip6, (In6Addr{1}));
116 EXPECT_EQ("2::"_ip6, (In6Addr{0, 2}));
117 EXPECT_EQ("ff::"_ip6, (In6Addr{0, 0xff}));
118 EXPECT_EQ("::100"_ip6,
119 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}));
120 EXPECT_EQ("::1"_ip6,
121 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
122 EXPECT_EQ("::2"_ip6,
123 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}));
124 EXPECT_EQ("::ff"_ip6,
125 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
126 EXPECT_EQ("1::1"_ip6,
127 (In6Addr{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));
128 EXPECT_EQ("5::b"_ip6,
129 (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 -0700130}
131
William A. Kennington IIIf53953b2023-06-16 16:05:52 -0700132TEST(ToStr, In6Addr)
133{
134 ToStrHandle<ToStr<In6Addr>> tsh;
135 EXPECT_EQ("::", tsh(In6Addr{}));
136 EXPECT_EQ("ff::", tsh(In6Addr{0, 0xff}));
137 EXPECT_EQ("::ff",
138 tsh(In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
139 EXPECT_EQ("0:0:ff::ff", tsh(In6Addr{0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0, 0, 0,
140 0, 0, 0, 0xff}));
141 EXPECT_EQ("::100:0:ff",
142 tsh(In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0xff}));
143 EXPECT_EQ("ff00::", tsh(In6Addr{0xff}));
144 EXPECT_EQ("1:2:3:4:5:6:7:8",
145 tsh(In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8}));
146 EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
147 tsh(In6Addr{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
148 255, 255, 255, 255, 255}));
149 // rfc5952 4.2.2
150 EXPECT_EQ("1:2:3:4:0:6:7:8",
151 tsh(In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 6, 0, 7, 0, 8}));
152 // rfc5952 4.2.3
153 EXPECT_EQ("1::4:0:0:7:8",
154 tsh(In6Addr{0, 1, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 7, 0, 8}));
155 // rfc5952 5
156 EXPECT_EQ("::ffff:192.168.0.1", tsh(In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157 0xff, 0xff, 192, 168, 0, 1}));
158
159 EXPECT_EQ("a ff00:: b", fmt::format("a {} b", In6Addr{0xff}));
160}
161
William A. Kennington IIIea3aeb72023-01-05 04:22:19 -0800162TEST(EqualOperator, InAnyAddr)
163{
164 EXPECT_EQ(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0xff}),
166 (In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}));
167 EXPECT_NE(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 0xff}),
169 (in_addr{}));
170 EXPECT_EQ((In6Addr{}), InAnyAddr(In6Addr{}));
171 EXPECT_NE((In4Addr{}), InAnyAddr(In6Addr{}));
172 EXPECT_EQ((In4Addr{}), InAnyAddr(In4Addr{}));
173 EXPECT_NE((In6Addr{}), InAnyAddr(In4Addr{}));
174 EXPECT_EQ(InAnyAddr(In6Addr{}), (in6_addr{}));
175 EXPECT_EQ(InAnyAddr(In4Addr{}), (in_addr{}));
176 EXPECT_NE(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{}));
177 EXPECT_EQ(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{1}));
178
179 std::hash<InAnyAddr>{}(In4Addr{});
180 std::hash<InAnyAddr>{}(In6Addr{});
181}
182
William A. Kennington IIIb26141d2023-06-16 16:13:20 -0700183TEST(FromStr, InAnyAddr)
184{
185 constexpr FromStr<InAnyAddr> fs;
186 EXPECT_EQ((In4Addr{}), fs("0.0.0.0"sv));
187 EXPECT_EQ((In6Addr{}), fs("::"sv));
188 EXPECT_EQ(
189 (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}),
190 fs("::ffff:192.168.0.1"sv));
191}
192
William A. Kennington III5ee09cf2023-06-16 16:17:35 -0700193TEST(ToStr, InAnyAddr)
194{
195 ToStrHandle<ToStr<InAnyAddr>> tsh;
196 EXPECT_EQ("1.15.3.4", tsh(In4Addr{1, 15, 3, 4}));
197 EXPECT_EQ("ff::", tsh(In6Addr{0, 0xff}));
198
199 EXPECT_EQ("a ff00:: b", fmt::format("a {} b", InAnyAddr{In6Addr{0xff}}));
200}
201
William A. Kennington III9330c872023-01-04 15:41:53 -0800202} // namespace stdplus