rippled
Loading...
Searching...
No Matches
IPEndpoint_test.cpp
1// MODULES: ../impl/IPEndpoint.cpp ../impl/IPAddressV4.cpp
2// ../impl/IPAddressV6.cpp
3
4#include <test/beast/IPEndpointCommon.h>
5
6#include <xrpl/basics/random.h>
7#include <xrpl/beast/net/IPEndpoint.h>
8#include <xrpl/beast/unit_test.h>
9
10#include <boost/algorithm/string.hpp>
11#include <boost/asio/ip/address.hpp>
12#include <boost/predef.h>
13
14namespace beast {
15namespace IP {
16
17//------------------------------------------------------------------------------
18
20{
21public:
22 void
23 shouldParseAddrV4(std::string const& s, std::uint32_t value, std::string const& normal = "")
24 {
25 boost::system::error_code ec;
26 Address const result{boost::asio::ip::make_address(s, ec)};
27 if (!BEAST_EXPECTS(!ec, ec.message()))
28 return;
29 if (!BEAST_EXPECTS(result.is_v4(), s + " not v4"))
30 return;
31 if (!BEAST_EXPECTS(result.to_v4().to_uint() == value, s + " value mismatch"))
32 return;
33 BEAST_EXPECTS(result.to_string() == (normal.empty() ? s : normal), s + " as string");
34 }
35
36 void
38 {
39 boost::system::error_code ec;
40 auto a = boost::asio::ip::make_address(s, ec);
41 BEAST_EXPECTS(ec, s + " parses as " + a.to_string());
42 }
43
44 void
46 {
47 testcase("AddressV4");
48
49 BEAST_EXPECT(AddressV4{}.to_uint() == 0);
50 BEAST_EXPECT(is_unspecified(AddressV4{}));
51 BEAST_EXPECT(AddressV4{0x01020304}.to_uint() == 0x01020304);
52
53 {
54 AddressV4::bytes_type d = {{1, 2, 3, 4}};
55 BEAST_EXPECT(AddressV4{d}.to_uint() == 0x01020304);
56
58 }
59
60 AddressV4 const v1{1};
61 BEAST_EXPECT(AddressV4{v1}.to_uint() == 1);
62
63 {
64 AddressV4 v;
65 v = v1;
66 BEAST_EXPECT(v.to_uint() == v1.to_uint());
67 }
68
69 {
70 AddressV4 v;
71 auto d = v.to_bytes();
72 d[0] = 1;
73 d[1] = 2;
74 d[2] = 3;
75 d[3] = 4;
76 v = AddressV4{d};
77 BEAST_EXPECT(v.to_uint() == 0x01020304);
78 }
79
80 BEAST_EXPECT(AddressV4(0x01020304).to_string() == "1.2.3.4");
81
82 shouldParseAddrV4("1.2.3.4", 0x01020304);
83 shouldParseAddrV4("255.255.255.255", 0xffffffff);
84 shouldParseAddrV4("0.0.0.0", 0);
85
86 failParseAddr(".");
87 failParseAddr("..");
88 failParseAddr("...");
89 failParseAddr("....");
90#if BOOST_OS_WINDOWS
91 // WINDOWS bug in asio - I don't think these should parse
92 // at all, and in-fact they do not on mac/linux
93 shouldParseAddrV4("1", 0x00000001, "0.0.0.1");
94 shouldParseAddrV4("1.2", 0x01000002, "1.0.0.2");
95 shouldParseAddrV4("1.2.3", 0x01020003, "1.2.0.3");
96#else
97 failParseAddr("1");
98 failParseAddr("1.2");
99 failParseAddr("1.2.3");
100#endif
101 failParseAddr("1.");
102 failParseAddr("1.2.");
103 failParseAddr("1.2.3.");
104 failParseAddr("256.0.0.0");
105 failParseAddr("-1.2.3.4");
106 }
107
108 void
110 {
111 testcase("AddressV4::Bytes");
112
113 AddressV4::bytes_type d1 = {{10, 0, 0, 1}};
114 AddressV4 v4{d1};
115 BEAST_EXPECT(v4.to_bytes()[0] == 10);
116 BEAST_EXPECT(v4.to_bytes()[1] == 0);
117 BEAST_EXPECT(v4.to_bytes()[2] == 0);
118 BEAST_EXPECT(v4.to_bytes()[3] == 1);
119
120 BEAST_EXPECT((~((0xff) << 16)) == 0xff00ffff);
121
122 auto d2 = v4.to_bytes();
123 d2[1] = 10;
124 v4 = AddressV4{d2};
125 BEAST_EXPECT(v4.to_bytes()[0] == 10);
126 BEAST_EXPECT(v4.to_bytes()[1] == 10);
127 BEAST_EXPECT(v4.to_bytes()[2] == 0);
128 BEAST_EXPECT(v4.to_bytes()[3] == 1);
129 }
130
131 //--------------------------------------------------------------------------
132
133 void
135 {
136 testcase("Address");
137
138 boost::system::error_code ec;
139 Address result{boost::asio::ip::make_address("1.2.3.4", ec)};
140 AddressV4::bytes_type d = {{1, 2, 3, 4}};
141 BEAST_EXPECT(!ec);
142 BEAST_EXPECT(result.is_v4() && result.to_v4() == AddressV4{d});
143 }
144
145 //--------------------------------------------------------------------------
146
147 void
149 std::string const& s,
150 AddressV4::bytes_type const& value,
152 std::string const& normal = "")
153 {
154 auto const result = Endpoint::from_string_checked(s);
155 if (!BEAST_EXPECT(result))
156 return;
157 if (!BEAST_EXPECT(result->address().is_v4()))
158 return;
159 if (!BEAST_EXPECT(result->address().to_v4() == AddressV4{value}))
160 return;
161
162 BEAST_EXPECT(result->port() == p);
163 BEAST_EXPECT(to_string(*result) == (normal.empty() ? s : normal));
164 }
165
166 void
168 std::string const& s,
169 AddressV6::bytes_type const& value,
171 std::string const& normal = "")
172 {
173 auto result = Endpoint::from_string_checked(s);
174 if (!BEAST_EXPECT(result))
175 return;
176 if (!BEAST_EXPECT(result->address().is_v6()))
177 return;
178 if (!BEAST_EXPECT(result->address().to_v6() == AddressV6{value}))
179 return;
180
181 BEAST_EXPECT(result->port() == p);
182 BEAST_EXPECT(to_string(*result) == (normal.empty() ? s : normal));
183 }
184
185 void
187 {
188 auto a1 = Endpoint::from_string(s);
189 BEAST_EXPECTS(is_unspecified(a1), s + " parses as " + a1.to_string());
190
191 auto a2 = Endpoint::from_string(s);
192 BEAST_EXPECTS(is_unspecified(a2), s + " parses as " + a2.to_string());
193
194 boost::replace_last(s, ":", " ");
195 auto a3 = Endpoint::from_string(s);
196 BEAST_EXPECTS(is_unspecified(a3), s + " parses as " + a3.to_string());
197 }
198
199 void
201 {
202 testcase("Endpoint");
203
204 shouldParseEPV4("1.2.3.4", {{1, 2, 3, 4}}, 0);
205 shouldParseEPV4("1.2.3.4:5", {{1, 2, 3, 4}}, 5);
206 shouldParseEPV4("1.2.3.4 5", {{1, 2, 3, 4}}, 5, "1.2.3.4:5");
207 // leading, trailing space
208 shouldParseEPV4(" 1.2.3.4:5", {{1, 2, 3, 4}}, 5, "1.2.3.4:5");
209 shouldParseEPV4("1.2.3.4:5 ", {{1, 2, 3, 4}}, 5, "1.2.3.4:5");
210 shouldParseEPV4("1.2.3.4 ", {{1, 2, 3, 4}}, 0, "1.2.3.4");
211 shouldParseEPV4(" 1.2.3.4", {{1, 2, 3, 4}}, 0, "1.2.3.4");
212 shouldParseEPV6("2001:db8:a0b:12f0::1", {{32, 01, 13, 184, 10, 11, 18, 240, 0, 0, 0, 0, 0, 0, 0, 1}}, 0);
213 shouldParseEPV6("[2001:db8:a0b:12f0::1]:8", {{32, 01, 13, 184, 10, 11, 18, 240, 0, 0, 0, 0, 0, 0, 0, 1}}, 8);
215 "[2001:2002:2003:2004:2005:2006:2007:2008]:65535",
216 {{32, 1, 32, 2, 32, 3, 32, 4, 32, 5, 32, 6, 32, 7, 32, 8}},
217 65535);
219 "2001:2002:2003:2004:2005:2006:2007:2008 65535",
220 {{32, 1, 32, 2, 32, 3, 32, 4, 32, 5, 32, 6, 32, 7, 32, 8}},
221 65535,
222 "[2001:2002:2003:2004:2005:2006:2007:2008]:65535");
223
224 Endpoint ep;
225
226 AddressV4::bytes_type d = {{127, 0, 0, 1}};
227 ep = Endpoint(AddressV4{d}, 80);
228 BEAST_EXPECT(!is_unspecified(ep));
229 BEAST_EXPECT(!is_public(ep));
230 BEAST_EXPECT(is_private(ep));
231 BEAST_EXPECT(!is_multicast(ep));
232 BEAST_EXPECT(is_loopback(ep));
233 BEAST_EXPECT(to_string(ep) == "127.0.0.1:80");
234 // same address as v4 mapped in ipv6
235 ep = Endpoint(boost::asio::ip::make_address_v6(boost::asio::ip::v4_mapped, AddressV4{d}), 80);
236 BEAST_EXPECT(!is_unspecified(ep));
237 BEAST_EXPECT(!is_public(ep));
238 BEAST_EXPECT(is_private(ep));
239 BEAST_EXPECT(!is_multicast(ep));
240 BEAST_EXPECT(!is_loopback(ep)); // mapped loopback is not a loopback
241 BEAST_EXPECTS(to_string(ep) == "[::ffff:127.0.0.1]:80", to_string(ep));
242
243 d = {{10, 0, 0, 1}};
244 ep = Endpoint(AddressV4{d});
245 BEAST_EXPECT(get_class(ep.to_v4()) == 'A');
246 BEAST_EXPECT(!is_unspecified(ep));
247 BEAST_EXPECT(!is_public(ep));
248 BEAST_EXPECT(is_private(ep));
249 BEAST_EXPECT(!is_multicast(ep));
250 BEAST_EXPECT(!is_loopback(ep));
251 BEAST_EXPECT(to_string(ep) == "10.0.0.1");
252 // same address as v4 mapped in ipv6
253 ep = Endpoint(boost::asio::ip::make_address_v6(boost::asio::ip::v4_mapped, AddressV4{d}));
254 BEAST_EXPECT(get_class(boost::asio::ip::make_address_v4(boost::asio::ip::v4_mapped, ep.to_v6())) == 'A');
255 BEAST_EXPECT(!is_unspecified(ep));
256 BEAST_EXPECT(!is_public(ep));
257 BEAST_EXPECT(is_private(ep));
258 BEAST_EXPECT(!is_multicast(ep));
259 BEAST_EXPECT(!is_loopback(ep));
260 BEAST_EXPECTS(to_string(ep) == "::ffff:10.0.0.1", to_string(ep));
261
262 d = {{166, 78, 151, 147}};
263 ep = Endpoint(AddressV4{d});
264 BEAST_EXPECT(!is_unspecified(ep));
265 BEAST_EXPECT(is_public(ep));
266 BEAST_EXPECT(!is_private(ep));
267 BEAST_EXPECT(!is_multicast(ep));
268 BEAST_EXPECT(!is_loopback(ep));
269 BEAST_EXPECT(to_string(ep) == "166.78.151.147");
270 // same address as v4 mapped in ipv6
271 ep = Endpoint(boost::asio::ip::make_address_v6(boost::asio::ip::v4_mapped, AddressV4{d}));
272 BEAST_EXPECT(!is_unspecified(ep));
273 BEAST_EXPECT(is_public(ep));
274 BEAST_EXPECT(!is_private(ep));
275 BEAST_EXPECT(!is_multicast(ep));
276 BEAST_EXPECT(!is_loopback(ep));
277 BEAST_EXPECTS(to_string(ep) == "::ffff:166.78.151.147", to_string(ep));
278
279 // a private IPv6
280 AddressV6::bytes_type d2 = {{253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}};
281 ep = Endpoint(AddressV6{d2});
282 BEAST_EXPECT(!is_unspecified(ep));
283 BEAST_EXPECT(!is_public(ep));
284 BEAST_EXPECT(is_private(ep));
285 BEAST_EXPECT(!is_multicast(ep));
286 BEAST_EXPECT(!is_loopback(ep));
287 BEAST_EXPECTS(to_string(ep) == "fd00::1", to_string(ep));
288
289 {
290 ep = Endpoint::from_string("192.0.2.112");
291 BEAST_EXPECT(!is_unspecified(ep));
292 BEAST_EXPECT(ep == Endpoint::from_string("192.0.2.112"));
293
294 auto const ep1 = Endpoint::from_string("192.0.2.112:2016");
295 BEAST_EXPECT(!is_unspecified(ep1));
296 BEAST_EXPECT(ep.address() == ep1.address());
297 BEAST_EXPECT(ep1.port() == 2016);
298
299 auto const ep2 = Endpoint::from_string("192.0.2.112:2016");
300 BEAST_EXPECT(!is_unspecified(ep2));
301 BEAST_EXPECT(ep.address() == ep2.address());
302 BEAST_EXPECT(ep2.port() == 2016);
303 BEAST_EXPECT(ep1 == ep2);
304
305 auto const ep3 = Endpoint::from_string("192.0.2.112 2016");
306 BEAST_EXPECT(!is_unspecified(ep3));
307 BEAST_EXPECT(ep.address() == ep3.address());
308 BEAST_EXPECT(ep3.port() == 2016);
309 BEAST_EXPECT(ep2 == ep3);
310
311 auto const ep4 = Endpoint::from_string("192.0.2.112 2016");
312 BEAST_EXPECT(!is_unspecified(ep4));
313 BEAST_EXPECT(ep.address() == ep4.address());
314 BEAST_EXPECT(ep4.port() == 2016);
315 BEAST_EXPECT(ep3 == ep4);
316
317 BEAST_EXPECT(to_string(ep1) == to_string(ep2));
318 BEAST_EXPECT(to_string(ep1) == to_string(ep3));
319 BEAST_EXPECT(to_string(ep1) == to_string(ep4));
320 }
321
322 {
323 ep = Endpoint::from_string("[::]:2017");
324 BEAST_EXPECT(is_unspecified(ep));
325 BEAST_EXPECT(ep.port() == 2017);
326 BEAST_EXPECT(ep.address() == AddressV6{});
327 }
328
329 // Failures:
330 failParseEP("192.0.2.112:port");
331 failParseEP("ip:port");
332 failParseEP("");
333 failParseEP("1.2.3.256");
334
335#if BOOST_OS_WINDOWS
336 // windows asio bugs...false positives
337 shouldParseEPV4("255", {{ 0, 0, 0, 255 }}, 0, "0.0.0.255");
338 shouldParseEPV4("512", {{ 0, 0, 2, 0 }}, 0, "0.0.2.0");
339 shouldParseEPV4("1.2.3:80", {{ 1, 2, 0, 3 }}, 80, "1.2.0.3:80");
340#else
341 failParseEP("255");
342 failParseEP("512");
343 failParseEP("1.2.3:80");
344#endif
345
346 failParseEP("1.2.3.4:65536");
347 failParseEP("1.2.3.4:89119");
348 failParseEP("1.2.3:89119");
349 failParseEP("[::1]:89119");
350 failParseEP("[::az]:1");
351 failParseEP("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:1");
352 failParseEP("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:12345");
353 failParseEP("abcdef:12345");
354 failParseEP("[abcdef]:12345");
355 failParseEP("foo.org 12345");
356
357 // test with hashed container
359 constexpr auto items{100};
360 float max_lf{0};
361 for (auto i = 0; i < items; ++i)
362 {
363 eps.insert(randomEP(xrpl::rand_int(0, 1) == 1));
364 max_lf = std::max(max_lf, eps.load_factor());
365 }
366 BEAST_EXPECT(eps.bucket_count() >= items);
367 BEAST_EXPECT(max_lf > 0.90);
368 }
369
370 //--------------------------------------------------------------------------
371
372 template <typename T>
373 bool
374 parse(std::string const& text, T& t)
375 {
376 std::istringstream stream{text};
377 stream >> t;
378 return !stream.fail();
379 }
380
381 template <typename T>
382 void
383 shouldPass(std::string const& text, std::string const& normal = "")
384 {
385 using namespace std::literals;
386 T t;
387 BEAST_EXPECT(parse(text, t));
388 BEAST_EXPECTS(to_string(t) == (normal.empty() ? text : normal), "string mismatch for "s + text);
389 }
390
391 template <typename T>
392 void
394 {
395 T t;
396 unexpected(parse(text, t), text + " should not parse");
397 }
398
399 template <typename T>
400 void
401 testParse(char const* name)
402 {
403 testcase(name);
404
405 shouldPass<T>("0.0.0.0");
406 shouldPass<T>("192.168.0.1");
407 shouldPass<T>("168.127.149.132");
408 shouldPass<T>("168.127.149.132:80");
409 shouldPass<T>("168.127.149.132:54321");
410 shouldPass<T>("2001:db8:a0b:12f0::1");
411 shouldPass<T>("[2001:db8:a0b:12f0::1]:8");
412 shouldPass<T>("2001:db8:a0b:12f0::1 8", "[2001:db8:a0b:12f0::1]:8");
413 shouldPass<T>("[::1]:8");
414 shouldPass<T>("[2001:2002:2003:2004:2005:2006:2007:2008]:65535");
415
416 shouldFail<T>("1.2.3.256");
417 shouldFail<T>("");
418#if BOOST_OS_WINDOWS
419 // windows asio bugs...false positives
420 shouldPass<T>("512", "0.0.2.0");
421 shouldPass<T>("255", "0.0.0.255");
422 shouldPass<T>("1.2.3:80", "1.2.0.3:80");
423#else
424 shouldFail<T>("512");
425 shouldFail<T>("255");
426 shouldFail<T>("1.2.3:80");
427#endif
428 shouldFail<T>("1.2.3:65536");
429 shouldFail<T>("1.2.3:72131");
430 shouldFail<T>("[::1]:89119");
431 shouldFail<T>("[::az]:1");
432 shouldFail<T>("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:1");
433 shouldFail<T>("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:12345");
434 }
435
436 void
437 run() override
438 {
441 testAddress();
442 testEndpoint();
443 testParse<Endpoint>("Parse Endpoint");
444 }
445};
446
447BEAST_DEFINE_TESTSUITE(IPEndpoint, beast, beast);
448
449} // namespace IP
450} // namespace beast
T bucket_count(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
static std::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
static Endpoint from_string(std::string const &s)
void run() override
Runs the suite.
void shouldPass(std::string const &text, std::string const &normal="")
void shouldParseEPV4(std::string const &s, AddressV4::bytes_type const &value, std::uint16_t p, std::string const &normal="")
bool parse(std::string const &text, T &t)
void shouldParseAddrV4(std::string const &s, std::uint32_t value, std::string const &normal="")
void failParseAddr(std::string const &s)
void shouldFail(std::string const &text)
void testParse(char const *name)
void failParseEP(std::string s)
void shouldParseEPV6(std::string const &s, AddressV6::bytes_type const &value, std::uint16_t p, std::string const &normal="")
A testsuite class.
Definition suite.h:52
bool unexpected(Condition shouldBeFalse, String const &reason)
Definition suite.h:483
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:148
T insert(T... args)
T load_factor(T... args)
T max(T... args)
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition IPAddress.h:45
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition IPAddress.h:31
char get_class(AddressV4 const &address)
Returns the address class for the given address.
boost::asio::ip::address_v6 AddressV6
Definition IPAddressV6.h:11
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
Definition IPAddress.h:59
Endpoint randomEP(bool v4=true)
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition IPAddress.h:38
boost::asio::ip::address_v4 AddressV4
Definition IPAddressV4.h:11
boost::asio::ip::address Address
Definition IPAddress.h:20
bool is_private(Address const &addr)
Returns true if the address is a private unroutable address.
Definition IPAddress.h:52
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition IPAddress.h:24
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()