Support ipv6 for peer and RPC comms:

Fixes: RIPD-1574

Alias beast address classes to the asio equivalents. Adjust users of
address classes accordingly. Fix resolver class so that it can support
ipv6 addresses. Make unit tests use ipv6 localhost network. Extend
endpoint peer message to support string endpoint
representations while also supporting the existing fields (both are
optional/repeated types). Expand test for Livecache and Endpoint.
Workaround some false positive ipaddr tests on windows (asio bug?)
Replaced usage of address::from_string(deprecated) with free function
make_address. Identified a remaining use of v4 address type and
replaced with the more appropriate IPEndpoint type (rpc_ip cmdline
option). Add CLI flag for using ipv4 with unit tests.

Release Notes
-------------

The optional rpc_port command line flag is deprecated. The rpc_ip
parameter now works as documented and accepts ip and port combined.
This commit is contained in:
Mike Ellery
2017-11-08 10:10:24 -08:00
parent fd4636b056
commit 08382d866b
41 changed files with 968 additions and 1253 deletions

View File

@@ -172,21 +172,12 @@ private:
while (list2.size () < listSize)
list2.push_back (randomValidator());
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
// Use ports of 0 to allow OS selection
endpoint_type ep1{address_type::from_string("127.0.0.1"), 0};
endpoint_type ep2{address_type::from_string("127.0.0.1"), 0};
auto const sequence = 1;
auto const version = 1;
NetClock::time_point const expiration =
env.timeKeeper().now() + 3600s;
TrustedPublisherServer server1(
ep1,
env.app().getIOService(),
pubSigningKeys1,
manifest1,
@@ -196,7 +187,6 @@ private:
list1);
TrustedPublisherServer server2(
ep2,
env.app().getIOService(),
pubSigningKeys2,
manifest2,
@@ -205,14 +195,13 @@ private:
version,
list2);
std::uint16_t const port1 = server1.local_endpoint().port();
std::uint16_t const port2 = server2.local_endpoint().port();
std::stringstream url1, url2;
url1 << "http://" << server1.local_endpoint() << "/validators";
url2 << "http://" << server2.local_endpoint() << "/validators";
{
// fetch single site
std::vector<std::string> cfgSites(
{"http://127.0.0.1:" + std::to_string(port1) + "/validators"});
std::vector<std::string> cfgSites({ url1.str() });
auto sites = std::make_unique<ValidatorSite> (
env.app().getIOService(), env.app().validators(), journal);
@@ -229,9 +218,7 @@ private:
}
{
// fetch multiple sites
std::vector<std::string> cfgSites({
"http://127.0.0.1:" + std::to_string(port1) + "/validators",
"http://127.0.0.1:" + std::to_string(port2) + "/validators"});
std::vector<std::string> cfgSites({ url1.str(), url2.str() });
auto sites = std::make_unique<ValidatorSite> (
env.app().getIOService(), env.app().validators(), journal);

View File

@@ -77,6 +77,9 @@ public:
BEAST_EXPECT(parseUrl (pUrl, "Mixed://domain/path"));
BEAST_EXPECT(pUrl.scheme == "mixed");
BEAST_EXPECT(pUrl.path == "/path");
BEAST_EXPECT(parseUrl (pUrl, "scheme://[::1]:123/path"));
BEAST_EXPECT(*pUrl.port == 123);
BEAST_EXPECT(pUrl.domain == "::1");
}
void testToString ()

View File

@@ -0,0 +1,64 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2017 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/beast/net/IPEndpoint.h>
#include <ripple/basics/random.h>
namespace beast {
namespace IP {
inline Endpoint randomEP (bool v4 = true)
{
using namespace ripple;
auto dv4 = []() -> AddressV4::bytes_type {
return {{
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX))
}};
};
auto dv6 = []() -> AddressV6::bytes_type {
return {{
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX)),
static_cast<std::uint8_t>(rand_int<int>(1, UINT8_MAX))
}};
};
return Endpoint {
v4 ? Address { AddressV4 {dv4()} } : Address{ AddressV6 {dv6()} },
rand_int<std::uint16_t>(1, UINT16_MAX)
};
}
}
}

View File

@@ -23,10 +23,12 @@
#endif
#include <ripple/beast/net/IPEndpoint.h>
#include <ripple/beast/net/detail/Parse.h>
#include <ripple/beast/unit_test.h>
#include <ripple/basics/random.h>
#include <boost/algorithm/string.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/predef.h>
#include <test/beast/IPEndpointCommon.h>
#include <typeinfo>
namespace beast {
@@ -37,91 +39,111 @@ namespace IP {
class IPEndpoint_test : public unit_test::suite
{
public:
void shouldParseV4 (std::string const& s, std::uint32_t value)
void shouldParseAddrV4 (
std::string const& s,
std::uint32_t value,
std::string const& normal = "")
{
std::pair <AddressV4, bool> const result (
AddressV4::from_string (s));
if (BEAST_EXPECT(result.second))
{
if (BEAST_EXPECT(result.first.value == value))
{
BEAST_EXPECT(to_string (result.first) == s);
}
}
boost::system::error_code ec;
Address const result {Address::from_string (s, ec)};
if (! BEAST_EXPECTS(! ec, ec.message()))
return;
if (! BEAST_EXPECTS(result.is_v4(), s + " not v4"))
return;
if (! BEAST_EXPECTS(result.to_v4().to_ulong() == value,
s + " value mismatch"))
return;
BEAST_EXPECTS(result.to_string () == (normal.empty() ? s : normal),
s + " as string");
}
void failParseV4 (std::string const& s)
void failParseAddr (std::string const& s)
{
unexpected (AddressV4::from_string (s).second);
boost::system::error_code ec;
auto a = Address::from_string (s, ec);
BEAST_EXPECTS(ec, s + " parses as " + a.to_string());
}
void testAddressV4 ()
{
testcase ("AddressV4");
BEAST_EXPECT(AddressV4().value == 0);
BEAST_EXPECT(is_unspecified (AddressV4()));
BEAST_EXPECT(AddressV4(0x01020304).value == 0x01020304);
BEAST_EXPECT(AddressV4(1, 2, 3, 4).value == 0x01020304);
BEAST_EXPECT(AddressV4{}.to_ulong() == 0);
BEAST_EXPECT(is_unspecified (AddressV4{}));
BEAST_EXPECT(AddressV4{0x01020304}.to_ulong() == 0x01020304);
AddressV4::bytes_type d = {{1,2,3,4}};
BEAST_EXPECT(AddressV4{d}.to_ulong() == 0x01020304);
unexpected (is_unspecified (AddressV4(1, 2, 3, 4)));
unexpected (is_unspecified (AddressV4{d}));
AddressV4 const v1 (1);
BEAST_EXPECT(AddressV4(v1).value == 1);
AddressV4 const v1 {1};
BEAST_EXPECT(AddressV4{v1}.to_ulong() == 1);
{
AddressV4 v;
v = v1;
BEAST_EXPECT(v.value == v1.value);
BEAST_EXPECT(v.to_ulong() == v1.to_ulong());
}
{
AddressV4 v;
v [0] = 1;
v [1] = 2;
v [2] = 3;
v [3] = 4;
BEAST_EXPECT(v.value == 0x01020304);
auto d = v.to_bytes();
d[0] = 1;
d[1] = 2;
d[2] = 3;
d[3] = 4;
v = AddressV4{d};
BEAST_EXPECT(v.to_ulong() == 0x01020304);
}
BEAST_EXPECT(to_string (AddressV4(0x01020304)) == "1.2.3.4");
BEAST_EXPECT(AddressV4(0x01020304).to_string() == "1.2.3.4");
shouldParseV4 ("1.2.3.4", 0x01020304);
shouldParseV4 ("255.255.255.255", 0xffffffff);
shouldParseV4 ("0.0.0.0", 0);
shouldParseAddrV4 ("1.2.3.4", 0x01020304);
shouldParseAddrV4 ("255.255.255.255", 0xffffffff);
shouldParseAddrV4 ("0.0.0.0", 0);
failParseV4 (".");
failParseV4 ("..");
failParseV4 ("...");
failParseV4 ("....");
failParseV4 ("1");
failParseV4 ("1.");
failParseV4 ("1.2");
failParseV4 ("1.2.");
failParseV4 ("1.2.3");
failParseV4 ("1.2.3.");
failParseV4 ("256.0.0.0");
failParseV4 ("-1.2.3.4");
failParseAddr (".");
failParseAddr ("..");
failParseAddr ("...");
failParseAddr ("....");
#if BOOST_OS_WINDOWS
// WINDOWS bug in asio - I don't think these should parse
// at all, and in-fact they do not on mac/linux
shouldParseAddrV4 ("1", 0x00000001, "0.0.0.1");
shouldParseAddrV4 ("1.2", 0x01000002, "1.0.0.2");
shouldParseAddrV4 ("1.2.3", 0x01020003, "1.2.0.3");
#else
failParseAddr ("1");
failParseAddr ("1.2");
failParseAddr ("1.2.3");
#endif
failParseAddr ("1.");
failParseAddr ("1.2.");
failParseAddr ("1.2.3.");
failParseAddr ("256.0.0.0");
failParseAddr ("-1.2.3.4");
}
void testAddressV4Proxy ()
{
testcase ("AddressV4::Proxy");
testcase ("AddressV4::Bytes");
AddressV4 v4 (10, 0, 0, 1);
BEAST_EXPECT(v4[0]==10);
BEAST_EXPECT(v4[1]==0);
BEAST_EXPECT(v4[2]==0);
BEAST_EXPECT(v4[3]==1);
AddressV4::bytes_type d1 = {{10,0,0,1}};
AddressV4 v4 {d1};
BEAST_EXPECT(v4.to_bytes()[0]==10);
BEAST_EXPECT(v4.to_bytes()[1]==0);
BEAST_EXPECT(v4.to_bytes()[2]==0);
BEAST_EXPECT(v4.to_bytes()[3]==1);
BEAST_EXPECT((~((0xff)<<16)) == 0xff00ffff);
v4[1] = 10;
BEAST_EXPECT(v4[0]==10);
BEAST_EXPECT(v4[1]==10);
BEAST_EXPECT(v4[2]==0);
BEAST_EXPECT(v4[3]==1);
auto d2 = v4.to_bytes();
d2[1] = 10;
v4 = AddressV4{d2};
BEAST_EXPECT(v4.to_bytes()[0]==10);
BEAST_EXPECT(v4.to_bytes()[1]==10);
BEAST_EXPECT(v4.to_bytes()[2]==0);
BEAST_EXPECT(v4.to_bytes()[3]==1);
}
//--------------------------------------------------------------------------
@@ -130,76 +152,160 @@ public:
{
testcase ("Address");
std::pair <Address, bool> result (
Address::from_string ("1.2.3.4"));
BEAST_EXPECT(result.second);
if (BEAST_EXPECT(result.first.is_v4 ()))
BEAST_EXPECT(result.first.to_v4() == AddressV4 (1, 2, 3, 4));
boost::system::error_code ec;
Address result {Address::from_string ("1.2.3.4", ec)};
AddressV4::bytes_type d = {{1,2,3,4}};
BEAST_EXPECT(! ec);
BEAST_EXPECT(
result.is_v4 () &&
result.to_v4() == AddressV4{d});
}
//--------------------------------------------------------------------------
void shouldParseEPV4 (
std::string const& s,
AddressV4::bytes_type const& value,
std::uint16_t p,
std::string const& normal = "")
{
auto result {Endpoint::from_string_checked (s)};
if (! BEAST_EXPECT(result.second))
return;
if (! BEAST_EXPECT(result.first.address().is_v4 ()))
return;
if (! BEAST_EXPECT(result.first.address().to_v4() == AddressV4 {value}))
return;
BEAST_EXPECT(result.first.port() == p);
BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal));
}
void shouldParseEPV6 (
std::string const& s,
AddressV6::bytes_type const& value,
std::uint16_t p,
std::string const& normal = "")
{
auto result {Endpoint::from_string_checked (s)};
if (! BEAST_EXPECT(result.second))
return;
if (! BEAST_EXPECT(result.first.address().is_v6 ()))
return;
if (! BEAST_EXPECT(result.first.address().to_v6() == AddressV6 {value}))
return;
BEAST_EXPECT(result.first.port() == p);
BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal));
}
void failParseEP (std::string s)
{
auto a1 = Endpoint::from_string(s);
BEAST_EXPECTS(is_unspecified (a1), s + " parses as " + a1.to_string());
auto a2 = Endpoint::from_string(s);
BEAST_EXPECTS(is_unspecified (a2), s + " parses as " + a2.to_string());
boost::replace_last(s, ":", " ");
auto a3 = Endpoint::from_string(s);
BEAST_EXPECTS(is_unspecified (a3), s + " parses as " + a3.to_string());
}
void testEndpoint ()
{
testcase ("Endpoint");
{
std::pair <Endpoint, bool> result (
Endpoint::from_string_checked ("1.2.3.4"));
BEAST_EXPECT(result.second);
if (BEAST_EXPECT(result.first.address().is_v4 ()))
{
BEAST_EXPECT(result.first.address().to_v4() ==
AddressV4 (1, 2, 3, 4));
BEAST_EXPECT(result.first.port() == 0);
BEAST_EXPECT(to_string (result.first) == "1.2.3.4");
}
}
{
std::pair <Endpoint, bool> result (
Endpoint::from_string_checked ("1.2.3.4:5"));
BEAST_EXPECT(result.second);
if (BEAST_EXPECT(result.first.address().is_v4 ()))
{
BEAST_EXPECT(result.first.address().to_v4() ==
AddressV4 (1, 2, 3, 4));
BEAST_EXPECT(result.first.port() == 5);
BEAST_EXPECT(to_string (result.first) == "1.2.3.4:5");
}
}
shouldParseEPV4("1.2.3.4", {{1,2,3,4}}, 0);
shouldParseEPV4("1.2.3.4:5", {{1,2,3,4}}, 5);
shouldParseEPV4("1.2.3.4 5", {{1,2,3,4}}, 5, "1.2.3.4:5");
shouldParseEPV6(
"2001:db8:a0b:12f0::1",
{{32, 01, 13, 184, 10, 11, 18, 240, 0, 0, 0, 0, 0, 0, 0, 1}},
0);
shouldParseEPV6(
"[2001:db8:a0b:12f0::1]:8",
{{32, 01, 13, 184, 10, 11, 18, 240, 0, 0, 0, 0, 0, 0, 0, 1}},
8);
shouldParseEPV6(
"[2001:2002:2003:2004:2005:2006:2007:2008]:65535",
{{32, 1, 32, 2, 32, 3, 32, 4, 32, 5, 32, 6, 32, 7, 32, 8}},
65535);
shouldParseEPV6(
"2001:2002:2003:2004:2005:2006:2007:2008 65535",
{{32, 1, 32, 2, 32, 3, 32, 4, 32, 5, 32, 6, 32, 7, 32, 8}},
65535,
"[2001:2002:2003:2004:2005:2006:2007:2008]:65535");
Endpoint ep;
ep = Endpoint (AddressV4 (127,0,0,1), 80);
AddressV4::bytes_type d = {{127,0,0,1}};
ep = Endpoint (AddressV4 {d}, 80);
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT(! is_public (ep));
BEAST_EXPECT( is_private (ep));
BEAST_EXPECT(! is_multicast (ep));
BEAST_EXPECT( is_loopback (ep));
BEAST_EXPECT(to_string (ep) == "127.0.0.1:80");
// same address as v4 mapped in ipv6
ep = Endpoint (AddressV6::v4_mapped(AddressV4 {d}), 80);
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT(! is_public (ep));
BEAST_EXPECT( is_private (ep));
BEAST_EXPECT(! is_multicast (ep));
BEAST_EXPECT(! is_loopback (ep)); //mapped loopback is not a loopback
BEAST_EXPECTS(to_string (ep) == "[::ffff:127.0.0.1]:80", to_string (ep));
ep = Endpoint (AddressV4 (10,0,0,1));
BEAST_EXPECT(AddressV4::get_class (ep.to_v4()) == 'A');
d = {{10,0,0,1}};
ep = Endpoint (AddressV4 {d});
BEAST_EXPECT(get_class (ep.to_v4()) == 'A');
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT(! is_public (ep));
BEAST_EXPECT( is_private (ep));
BEAST_EXPECT(! is_multicast (ep));
BEAST_EXPECT(! is_loopback (ep));
BEAST_EXPECT(to_string (ep) == "10.0.0.1");
// same address as v4 mapped in ipv6
ep = Endpoint (AddressV6::v4_mapped(AddressV4 {d}));
BEAST_EXPECT(get_class (ep.to_v6().to_v4()) == 'A');
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT(! is_public (ep));
BEAST_EXPECT( is_private (ep));
BEAST_EXPECT(! is_multicast (ep));
BEAST_EXPECT(! is_loopback (ep));
BEAST_EXPECTS(to_string (ep) == "::ffff:10.0.0.1", to_string(ep));
ep = Endpoint (AddressV4 (166,78,151,147));
d = {{166,78,151,147}};
ep = Endpoint (AddressV4 {d});
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT( is_public (ep));
BEAST_EXPECT(! is_private (ep));
BEAST_EXPECT(! is_multicast (ep));
BEAST_EXPECT(! is_loopback (ep));
BEAST_EXPECT(to_string (ep) == "166.78.151.147");
// same address as v4 mapped in ipv6
ep = Endpoint (AddressV6::v4_mapped(AddressV4 {d}));
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT( is_public (ep));
BEAST_EXPECT(! is_private (ep));
BEAST_EXPECT(! is_multicast (ep));
BEAST_EXPECT(! is_loopback (ep));
BEAST_EXPECTS(to_string (ep) == "::ffff:166.78.151.147", to_string(ep));
// a private IPv6
AddressV6::bytes_type d2 = {{253,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
ep = Endpoint (AddressV6 {d2});
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT(! is_public (ep));
BEAST_EXPECT( is_private (ep));
BEAST_EXPECT(! is_multicast (ep));
BEAST_EXPECT(! is_loopback (ep));
BEAST_EXPECTS(to_string (ep) == "fd00::1", to_string(ep));
{
ep = Endpoint::from_string ("192.0.2.112");
BEAST_EXPECT(! is_unspecified (ep));
BEAST_EXPECT(ep == Endpoint::from_string_altform ("192.0.2.112"));
BEAST_EXPECT(ep == Endpoint::from_string ("192.0.2.112"));
auto const ep1 = Endpoint::from_string ("192.0.2.112:2016");
BEAST_EXPECT(! is_unspecified (ep1));
@@ -207,21 +313,21 @@ public:
BEAST_EXPECT(ep1.port() == 2016);
auto const ep2 =
Endpoint::from_string_altform ("192.0.2.112:2016");
Endpoint::from_string ("192.0.2.112:2016");
BEAST_EXPECT(! is_unspecified (ep2));
BEAST_EXPECT(ep.address() == ep2.address());
BEAST_EXPECT(ep2.port() == 2016);
BEAST_EXPECT(ep1 == ep2);
auto const ep3 =
Endpoint::from_string_altform ("192.0.2.112 2016");
Endpoint::from_string ("192.0.2.112 2016");
BEAST_EXPECT(! is_unspecified (ep3));
BEAST_EXPECT(ep.address() == ep3.address());
BEAST_EXPECT(ep3.port() == 2016);
BEAST_EXPECT(ep2 == ep3);
auto const ep4 =
Endpoint::from_string_altform ("192.0.2.112 2016");
Endpoint::from_string ("192.0.2.112 2016");
BEAST_EXPECT(! is_unspecified (ep4));
BEAST_EXPECT(ep.address() == ep4.address());
BEAST_EXPECT(ep4.port() == 2016);
@@ -232,87 +338,79 @@ public:
BEAST_EXPECT(to_string(ep1) == to_string(ep4));
}
{
ep = Endpoint::from_string("[::]:2017");
BEAST_EXPECT(is_unspecified (ep));
BEAST_EXPECT(ep.port() == 2017);
BEAST_EXPECT(ep.address() == AddressV6{});
}
// Failures:
BEAST_EXPECT(is_unspecified (
Endpoint::from_string ("192.0.2.112:port")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform ("192.0.2.112:port")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform ("192.0.2.112 port")));
failParseEP ("192.0.2.112:port");
failParseEP ("ip:port");
failParseEP ("");
failParseEP ("1.2.3.256");
BEAST_EXPECT(is_unspecified (
Endpoint::from_string ("ip:port")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform ("ip:port")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform ("ip port")));
#if BOOST_OS_WINDOWS
// windows asio bugs...false positives
shouldParseEPV4 ("255", {{0,0,0,255}}, 0, "0.0.0.255");
shouldParseEPV4 ("512", {{0,0,2,0}}, 0, "0.0.2.0");
shouldParseEPV4 ("1.2.3:80", {{1,2,0,3}}, 80, "1.2.0.3:80");
#else
failParseEP ("255");
failParseEP ("512");
failParseEP ("1.2.3:80");
#endif
BEAST_EXPECT(is_unspecified (
Endpoint::from_string("")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("")));
failParseEP ("1.2.3.4:65536");
failParseEP ("1.2.3.4:89119");
failParseEP ("1.2.3:89119");
failParseEP ("[::1]:89119");
failParseEP ("[::az]:1");
failParseEP ("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:1");
failParseEP ("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:12345");
failParseEP ("abcdef:12345");
failParseEP ("[abcdef]:12345");
failParseEP ("foo.org 12345");
BEAST_EXPECT(is_unspecified (
Endpoint::from_string("255")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("255")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string("512")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("512")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string("1.2.3.256")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("1.2.3.256")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string("1.2.3:80")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("1.2.3:80")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("1.2.3 80")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string("1.2.3.4:65536")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("1.2.3:65536")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("1.2.3 65536")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string("1.2.3.4:89119")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("1.2.3:89119")));
BEAST_EXPECT(is_unspecified (
Endpoint::from_string_altform("1.2.3 89119")));
// test with hashed container
std::unordered_set<Endpoint> eps;
constexpr auto items {100};
float max_lf {0};
for (auto i = 0; i < items; ++i)
{
eps.insert(randomEP(ripple::rand_int(0,1) == 1));
max_lf = std::max(max_lf, eps.load_factor());
}
BEAST_EXPECT(eps.bucket_count() >= items);
BEAST_EXPECT(max_lf > 0.90);
}
//--------------------------------------------------------------------------
template <typename T>
bool parse (char const* text, T& t)
bool parse (std::string const& text, T& t)
{
std::string input (text);
std::istringstream stream (input);
std::istringstream stream {text};
stream >> t;
return !stream.fail();
}
template <typename T>
void shouldPass (char const* text)
void shouldPass (std::string const& text, std::string const& normal="")
{
using namespace std::literals;
T t;
BEAST_EXPECT(parse (text, t));
BEAST_EXPECT(to_string (t) == std::string (text));
BEAST_EXPECTS(to_string (t) == (normal.empty() ? text : normal),
"string mismatch for "s + text);
}
template <typename T>
void shouldFail (char const* text)
void shouldFail (std::string const& text)
{
T t;
unexpected (parse (text, t));
unexpected (parse (text, t), text + " should not parse");
}
template <typename T>
@@ -325,14 +423,30 @@ public:
shouldPass <T> ("168.127.149.132");
shouldPass <T> ("168.127.149.132:80");
shouldPass <T> ("168.127.149.132:54321");
shouldPass <T> ("2001:db8:a0b:12f0::1");
shouldPass <T> ("[2001:db8:a0b:12f0::1]:8");
shouldPass <T> ("2001:db8:a0b:12f0::1 8", "[2001:db8:a0b:12f0::1]:8");
shouldPass <T> ("[::1]:8");
shouldPass <T> ("[2001:2002:2003:2004:2005:2006:2007:2008]:65535");
shouldFail <T> ("");
shouldFail <T> ("255");
shouldFail <T> ("512");
shouldFail <T> ("1.2.3.256");
shouldFail <T> ("");
#if BOOST_OS_WINDOWS
// windows asio bugs...false positives
shouldPass <T> ("512", "0.0.2.0");
shouldPass <T> ("255", "0.0.0.255");
shouldPass <T> ("1.2.3:80", "1.2.0.3:80");
#else
shouldFail <T> ("512");
shouldFail <T> ("255");
shouldFail <T> ("1.2.3:80");
#endif
shouldFail <T> ("1.2.3:65536");
shouldFail <T> ("1.2.3:72131");
shouldFail <T> ("[::1]:89119");
shouldFail <T> ("[::az]:1");
shouldFail <T> ("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:1");
shouldFail <T> ("[1234:5678:90ab:cdef:1234:5678:90ab:cdef:1111]:12345");
}
void run () override
@@ -341,7 +455,6 @@ public:
testAddressV4Proxy();
testAddress ();
testEndpoint ();
testParse <Endpoint> ("Parse Endpoint");
}
};

View File

@@ -23,6 +23,7 @@
#include <ripple/protocol/SecretKey.h>
#include <ripple/protocol/Sign.h>
#include <ripple/basics/strHex.h>
#include <test/jtx/envconfig.h>
#include <boost/asio.hpp>
#include <boost/beast/core/detail/base64.hpp>
#include <boost/beast/http.hpp>
@@ -55,7 +56,6 @@ public:
};
TrustedPublisherServer(
endpoint_type const& ep,
boost::asio::io_service& ios,
std::pair<PublicKey, SecretKey> keys,
std::string const& manifest,
@@ -65,6 +65,9 @@ public:
std::vector<Validator> const& validators)
: sock_(ios), acceptor_(ios)
{
endpoint_type const& ep {
beast::IP::Address::from_string (ripple::test::getEnvLocalhostAddr()),
0}; // 0 means let OS pick the port based on what's available
std::string data = "{\"sequence\":" + std::to_string(sequence) +
",\"expiration\":" +
std::to_string(expiration.time_since_epoch().count()) +

View File

@@ -25,6 +25,15 @@
namespace ripple {
namespace test {
extern std::atomic<bool> envUseIPv4;
inline
const char *
getEnvLocalhostAddr()
{
return envUseIPv4 ? "127.0.0.1" : "::1";
}
/// @brief initializes a config object for use with jtx::Env
///
/// @param config the configuration object to be initialized

View File

@@ -49,9 +49,9 @@ class JSONRPCClient : public AbstractClient
parse_Port(pp, cfg[name], log);
if(pp.protocol.count("http") == 0)
continue;
using boost::asio::ip::address_v4;
if(*pp.ip == address_v4{0x00000000})
*pp.ip = address_v4{0x7f000001};
using namespace boost::asio::ip;
if(pp.ip && pp.ip->is_unspecified())
*pp.ip = pp.ip->is_v6() ? address{address_v6::loopback()} : address{address_v4::loopback()};
return { *pp.ip, *pp.port };
}
Throw<std::runtime_error>("Missing HTTP port");
@@ -83,7 +83,7 @@ public:
: ep_(getEndpoint(cfg))
, stream_(ios_)
, rpc_version_(rpc_version)
{
{
stream_.connect(ep_);
}

View File

@@ -64,9 +64,9 @@ class WSClientImpl : public WSClient
parse_Port(pp, cfg[name], log);
if(pp.protocol.count(ps) == 0)
continue;
using boost::asio::ip::address_v4;
if(*pp.ip == address_v4{0x00000000})
*pp.ip = address_v4{0x7f000001};
using namespace boost::asio::ip;
if(pp.ip && pp.ip->is_unspecified())
*pp.ip = pp.ip->is_v6() ? address{address_v6::loopback()} : address{address_v4::loopback()};
return { *pp.ip, *pp.port };
}
Throw<std::runtime_error>("Missing WebSocket port");

View File

@@ -30,6 +30,8 @@ void incPorts()
port_base += 3;
}
std::atomic<bool> envUseIPv4 {false};
void
setupConfigForUnitTests (Config& cfg)
{
@@ -43,19 +45,21 @@ setupConfigForUnitTests (Config& cfg)
cfg.legacy("database_path", "");
cfg.setupControl(true, true, true);
cfg["server"].append("port_peer");
cfg["port_peer"].set("ip", "127.0.0.1");
cfg["port_peer"].set("ip", getEnvLocalhostAddr());
cfg["port_peer"].set("port", port_peer);
cfg["port_peer"].set("protocol", "peer");
cfg["server"].append("port_rpc");
cfg["port_rpc"].set("ip", "127.0.0.1");
cfg["port_rpc"].set("ip", getEnvLocalhostAddr());
cfg["port_rpc"].set("admin", getEnvLocalhostAddr());
cfg["port_rpc"].set("port", port_rpc);
cfg["port_rpc"].set("protocol", "http,ws2");
cfg["port_rpc"].set("admin", "127.0.0.1");
cfg["server"].append("port_ws");
cfg["port_ws"].set("ip", "127.0.0.1");
cfg["port_ws"].set("ip", getEnvLocalhostAddr());
cfg["port_ws"].set("admin", getEnvLocalhostAddr());
cfg["port_ws"].set("port", port_ws);
cfg["port_ws"].set("protocol", "ws");
cfg["port_ws"].set("admin", "127.0.0.1");
}
namespace jtx {

View File

@@ -20,6 +20,7 @@
#include <ripple/basics/make_SSLContext.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/beast/unit_test.h>
#include <test/jtx/envconfig.h>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/optional.hpp>
@@ -183,7 +184,8 @@ private:
, server_(server)
, test_(server_.test_)
, acceptor_(test_.io_service_,
endpoint_type(address_type::from_string("127.0.0.1"), 0))
endpoint_type(beast::IP::Address::from_string(
test::getEnvLocalhostAddr()), 0))
, socket_(test_.io_service_)
, strand_(socket_.get_io_service())
{

View File

@@ -21,10 +21,18 @@
#include <ripple/peerfinder/impl/Livecache.h>
#include <ripple/beast/unit_test.h>
#include <ripple/beast/clock/manual_clock.h>
#include <test/beast/IPEndpointCommon.h>
#include <boost/algorithm/string.hpp>
namespace ripple {
namespace PeerFinder {
bool operator== (Endpoint const& a, Endpoint const& b)
{
return (a.hops == b.hops &&
a.address == b.address);
}
class Livecache_test : public beast::unit_test::suite
{
public:
@@ -32,38 +40,178 @@ public:
// Add the address as an endpoint
template <class C>
void add (std::uint32_t index, std::uint16_t port, C& c)
inline void add (beast::IP::Endpoint ep, C& c, int hops = 0)
{
Endpoint ep;
ep.hops = 0;
ep.address = beast::IP::Endpoint (
beast::IP::AddressV4 (index), port);
c.insert (ep);
Endpoint cep {ep, hops};
c.insert (cep);
}
void testFetch ()
void testBasicInsert ()
{
testcase ("Basic Insert");
Livecache <> c (m_clock, beast::Journal());
BEAST_EXPECT(c.empty());
for (auto i = 0; i < 10; ++i)
add(beast::IP::randomEP(true), c);
BEAST_EXPECT(! c.empty());
BEAST_EXPECT(c.size() == 10);
for (auto i = 0; i < 10; ++i)
add(beast::IP::randomEP(false), c);
BEAST_EXPECT(! c.empty());
BEAST_EXPECT(c.size() == 20);
}
void testInsertUpdate ()
{
testcase ("Insert/Update");
Livecache <> c (m_clock, beast::Journal());
add (1, 1, c);
add (2, 1, c);
add (3, 1, c);
add (4, 1, c);
add (4, 2, c);
add (4, 3, c);
add (5, 1, c);
add (6, 1, c);
add (6, 2, c);
add (7, 1, c);
auto ep1 = Endpoint {beast::IP::randomEP(), 2};
c.insert(ep1);
BEAST_EXPECT(c.size() == 1);
// third position list will contain the entry
BEAST_EXPECT((c.hops.begin()+2)->begin()->hops == 2);
// VFALCO TODO!
auto ep2 = Endpoint {ep1.address, 4};
// this will not change the entry has higher hops
c.insert(ep2);
BEAST_EXPECT(c.size() == 1);
// still in third position list
BEAST_EXPECT((c.hops.begin()+2)->begin()->hops == 2);
pass();
auto ep3 = Endpoint {ep1.address, 2};
// this will not change the entry has the same hops as existing
c.insert(ep3);
BEAST_EXPECT(c.size() == 1);
// still in third position list
BEAST_EXPECT((c.hops.begin()+2)->begin()->hops == 2);
auto ep4 = Endpoint {ep1.address, 1};
c.insert(ep4);
BEAST_EXPECT(c.size() == 1);
// now at second position list
BEAST_EXPECT((c.hops.begin()+1)->begin()->hops == 1);
}
void testExpire ()
{
testcase ("Expire");
using namespace std::chrono_literals;
Livecache <> c (m_clock, beast::Journal());
auto ep1 = Endpoint {beast::IP::randomEP(), 1};
c.insert(ep1);
BEAST_EXPECT(c.size() == 1);
c.expire();
BEAST_EXPECT(c.size() == 1);
// verify that advancing to 1 sec before expiration
// leaves our entry intact
m_clock.advance(Tuning::liveCacheSecondsToLive - 1s);
c.expire();
BEAST_EXPECT(c.size() == 1);
// now advance to the point of expiration
m_clock.advance(1s);
c.expire();
BEAST_EXPECT(c.empty());
}
void testHistogram ()
{
testcase ("Histogram");
constexpr auto num_eps = 40;
Livecache <> c (m_clock, beast::Journal());
for (auto i = 0; i < num_eps; ++i)
add(
beast::IP::randomEP(true),
c,
ripple::rand_int(0, static_cast<int>(Tuning::maxHops + 1)));
auto h = c.hops.histogram();
if(! BEAST_EXPECT(! h.empty()))
return;
std::vector <std::string> v;
boost::split (v, h, boost::algorithm::is_any_of (","));
auto sum = 0;
for (auto const& n : v)
{
auto val = boost::lexical_cast<int>(boost::trim_copy(n));
sum += val;
BEAST_EXPECT(val >= 0);
}
BEAST_EXPECT(sum == num_eps);
}
void testShuffle ()
{
testcase ("Shuffle");
Livecache <> c (m_clock, beast::Journal());
for (auto i = 0; i < 100; ++i)
add(
beast::IP::randomEP(true),
c,
ripple::rand_int(0, static_cast<int>(Tuning::maxHops + 1)));
using at_hop = std::vector <ripple::PeerFinder::Endpoint>;
using all_hops = std::array <at_hop, 1 + Tuning::maxHops + 1>;
auto cmp_EP = [](Endpoint const& a, Endpoint const& b) {
return (b.hops < a.hops || (b.hops == a.hops && b.address < a.address));
};
all_hops before;
all_hops before_sorted;
for (auto i = std::make_pair(0, c.hops.begin());
i.second != c.hops.end(); ++i.first, ++i.second)
{
std::copy ((*i.second).begin(), (*i.second).end(),
std::back_inserter (before[i.first]));
std::copy ((*i.second).begin(), (*i.second).end(),
std::back_inserter (before_sorted[i.first]));
std::sort(
before_sorted[i.first].begin(),
before_sorted[i.first].end(),
cmp_EP);
}
c.hops.shuffle();
all_hops after;
all_hops after_sorted;
for (auto i = std::make_pair(0, c.hops.begin());
i.second != c.hops.end(); ++i.first, ++i.second)
{
std::copy ((*i.second).begin(), (*i.second).end(),
std::back_inserter (after[i.first]));
std::copy ((*i.second).begin(), (*i.second).end(),
std::back_inserter (after_sorted[i.first]));
std::sort(
after_sorted[i.first].begin(),
after_sorted[i.first].end(),
cmp_EP);
}
// each hop bucket should contain the same items
// before and after sort, albeit in different order
bool all_match = true;
for (auto i = 0; i < before.size(); ++i)
{
BEAST_EXPECT(before[i].size() == after[i].size());
all_match = all_match && (before[i] == after[i]);
BEAST_EXPECT(before_sorted[i] == after_sorted[i]);
}
BEAST_EXPECT(! all_match);
}
void run () override
{
testFetch ();
testBasicInsert ();
testInsertUpdate ();
testExpire ();
testHistogram ();
testShuffle ();
}
};

View File

@@ -63,15 +63,16 @@ public:
void createGossip (Gossip& gossip)
{
int const v (10 + rand_int(9));
int const n (10 + rand_int(9));
std::uint8_t const v (10 + rand_int(9));
std::uint8_t const n (10 + rand_int(9));
gossip.items.reserve (n);
for (int i = 0; i < n; ++i)
for (std::uint8_t i = 0; i < n; ++i)
{
Gossip::Item item;
item.balance = 100 + rand_int(499);
item.address = beast::IP::Endpoint (
beast::IP::AddressV4 (192, 0, 2, v + i));
beast::IP::AddressV4::bytes_type d =
{{192,0,2,static_cast<std::uint8_t>(v + i)}};
item.address = beast::IP::Endpoint { beast::IP::AddressV4 {d} };
gossip.items.push_back (item);
}
}
@@ -193,8 +194,8 @@ public:
Gossip g;
Gossip::Item item;
item.balance = 100;
item.address = beast::IP::Endpoint (
beast::IP::AddressV4 (192, 0, 2, 1));
beast::IP::AddressV4::bytes_type d = {{192, 0, 2, 1}};
item.address = beast::IP::Endpoint { beast::IP::AddressV4 {d} };
g.items.push_back (item);
logic.importConsumers ("g", g);

View File

@@ -21,6 +21,7 @@
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/JsonFields.h>
#include <test/jtx.h>
#include <test/jtx/envconfig.h>
#include <boost/algorithm/string/predicate.hpp>
#include <ripple/beast/utility/temp_dir.h>
#include <ripple/resource/ResourceManager.h>
@@ -265,7 +266,7 @@ class NoRippleCheckLimits_test : public beast::unit_test::suite
using namespace std::chrono;
using namespace beast::IP;
auto c = env.app().getResourceManager()
.newInboundEndpoint (Endpoint::from_string ("127.0.0.1"));
.newInboundEndpoint (Endpoint::from_string (test::getEnvLocalhostAddr()));
if (dropThreshold - c.balance() <= 20)
{
using clock_type = beast::abstract_clock <steady_clock>;

View File

@@ -178,8 +178,6 @@ public:
testDynamicUNL()
{
using namespace test::jtx;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
auto toStr = [](PublicKey const& publicKey) {
return toBase58(TokenType::NodePublic, publicKey);
@@ -208,7 +206,9 @@ public:
// Publisher list site unavailable
{
// Publisher site information
std::string siteURI = "http://127.0.0.1:1234/validators";
using namespace std::string_literals;
std::string siteURI =
"http://"s + getEnvLocalhostAddr() + ":1234/validators";
Env env{
*this,
@@ -271,9 +271,6 @@ public:
{
NetClock::time_point const expiration{3600s};
// 0 port means to use OS port selection
endpoint_type ep{address_type::from_string("127.0.0.1"), 0};
// Manage single thread io_service for server
struct Worker : BasicApp
{
@@ -282,7 +279,6 @@ public:
Worker w;
TrustedPublisherServer server(
ep,
w.get_io_service(),
publisherSigningKeys,
manifest,
@@ -291,9 +287,9 @@ public:
1,
validators);
endpoint_type const & local_ep = server.local_endpoint();
std::string siteURI = "http://127.0.0.1:" +
std::to_string(local_ep.port()) + "/validators";
std::stringstream uri;
uri << "http://" << server.local_endpoint() << "/validators";
auto siteURI = uri.str();
Env env{
*this,

View File

@@ -73,10 +73,10 @@ class ServerStatus_test :
// which requires an http endpoint to talk to. In the connection
// failure test, this endpoint should never be used
(*p)["server"].append("port_alt");
(*p)["port_alt"].set("ip", "127.0.0.1");
(*p)["port_alt"].set("ip", getEnvLocalhostAddr());
(*p)["port_alt"].set("port", "8099");
(*p)["port_alt"].set("protocol", "http");
(*p)["port_alt"].set("admin", "127.0.0.1");
(*p)["port_alt"].set("admin", getEnvLocalhostAddr());
}
return p;

View File

@@ -283,7 +283,7 @@ public:
thread.get_io_service(), journal);
std::vector<Port> serverPort(1);
serverPort.back().ip =
boost::asio::ip::address::from_string ("127.0.0.1"),
beast::IP::Address::from_string (getEnvLocalhostAddr()),
serverPort.back().port = 0;
serverPort.back().protocol.insert("http");
auto eps = s->ports (serverPort);
@@ -355,7 +355,7 @@ public:
thread.get_io_service(), {});
std::vector<Port> serverPort(1);
serverPort.back().ip =
boost::asio::ip::address::from_string ("127.0.0.1"),
beast::IP::Address::from_string (getEnvLocalhostAddr()),
serverPort.back().port = 0;
serverPort.back().protocol.insert("http");
s->ports (serverPort);
@@ -441,7 +441,7 @@ public:
Env env {*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg).deprecatedClearSection("port_rpc");
(*cfg)["port_rpc"].set("ip", "127.0.0.1");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
return cfg;
}),
std::make_unique<CaptureLogs>(messages)};
@@ -455,7 +455,7 @@ public:
Env env {*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg).deprecatedClearSection("port_rpc");
(*cfg)["port_rpc"].set("ip", "127.0.0.1");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_rpc"].set("port", "0");
return cfg;
}),
@@ -470,7 +470,7 @@ public:
Env env {*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg).deprecatedClearSection("port_rpc");
(*cfg)["port_rpc"].set("ip", "127.0.0.1");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_rpc"].set("port", "8081");
(*cfg)["port_rpc"].set("protocol", "");
return cfg;
@@ -495,17 +495,17 @@ public:
ConfigSection::importNodeDatabase ());
cfg->legacy("database_path", "");
cfg->setupControl(true, true, true);
(*cfg)["port_peer"].set("ip", "127.0.0.1");
(*cfg)["port_peer"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_peer"].set("port", "8080");
(*cfg)["port_peer"].set("protocol", "peer");
(*cfg)["port_rpc"].set("ip", "127.0.0.1");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_rpc"].set("port", "8081");
(*cfg)["port_rpc"].set("protocol", "http,ws2");
(*cfg)["port_rpc"].set("admin", "127.0.0.1");
(*cfg)["port_ws"].set("ip", "127.0.0.1");
(*cfg)["port_rpc"].set("admin", getEnvLocalhostAddr());
(*cfg)["port_ws"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_ws"].set("port", "8082");
(*cfg)["port_ws"].set("protocol", "ws");
(*cfg)["port_ws"].set("admin", "127.0.0.1");
(*cfg)["port_ws"].set("admin", getEnvLocalhostAddr());
return cfg;
}),
std::make_unique<CaptureLogs>(messages)};