20#include <test/unit_test/SuiteJournal.h>
22#include <xrpld/core/Config.h>
23#include <xrpld/peerfinder/PeerfinderManager.h>
24#include <xrpld/peerfinder/detail/Logic.h>
26#include <xrpl/basics/chrono.h>
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/protocol/PublicKey.h>
29#include <xrpl/protocol/SecretKey.h>
69 template <
class Handler>
73 boost::system::error_code ec;
81 auto const seconds = 10000;
101 BEAST_EXPECT(list.size() == 1);
112 BEAST_EXPECT(n < 20);
119 auto const seconds = 10000;
142 BEAST_EXPECT(list.size() == 1);
150 PeerFinder::Result::success))
159 BEAST_EXPECT(n <= (seconds + 59) / 60);
181 BEAST_EXPECT(slot1 !=
nullptr);
182 BEAST_EXPECT(r == Result::success);
188 BEAST_EXPECT(r2 == Result::duplicatePeer);
190 if (!BEAST_EXPECT(slot2 ==
nullptr))
217 BEAST_EXPECT(slot1 !=
nullptr);
218 BEAST_EXPECT(r == Result::success);
222 BEAST_EXPECT(r2 == Result::duplicatePeer);
224 if (!BEAST_EXPECT(slot2 ==
nullptr))
248 BEAST_EXPECT(slot !=
nullptr);
249 BEAST_EXPECT(r == Result::success);
253 BEAST_EXPECT(slot1 !=
nullptr);
254 BEAST_EXPECT(r1 == Result::success);
258 BEAST_EXPECT(r2 == Result::ipLimitExceeded);
260 if (!BEAST_EXPECT(slot2 ==
nullptr))
269 testcase(
"test activate duplicate peer");
288 BEAST_EXPECT(slot !=
nullptr);
289 BEAST_EXPECT(rSlot == Result::success);
293 BEAST_EXPECT(slot2 !=
nullptr);
294 BEAST_EXPECT(r2Slot == Result::success);
299 BEAST_EXPECT(logic.
activate(slot, pk1,
false) == Result::success);
303 logic.
activate(slot2, pk1,
false) == Result::duplicatePeer);
308 BEAST_EXPECT(logic.
activate(slot2, pk1,
false) == Result::success);
315 testcase(
"test activate inbound disabled");
333 BEAST_EXPECT(slot !=
nullptr);
334 BEAST_EXPECT(rSlot == Result::success);
337 logic.
activate(slot, pk1,
false) == Result::inboundDisabled);
348 BEAST_EXPECT(logic.
activate(slot, pk1,
false) == Result::success);
353 BEAST_EXPECT(slot2 !=
nullptr);
354 BEAST_EXPECT(r2Slot == Result::success);
359 BEAST_EXPECT(logic.
activate(slot2, pk2,
false) == Result::full);
368 testcase(
"test addFixedPeer no port");
377 fail(
"invalid endpoint successfully added");
388 testcase(
"test onConnected self connection");
396 BEAST_EXPECT(slot !=
nullptr);
397 BEAST_EXPECT(r == Result::success);
425 max = maxPeers.value();
428 "\n" +
"[peers_out_max]\n" +
431 else if (maxIn && maxOut)
448 counts.
out_max() == expectOut && counts.
in_max() == expectIn &&
449 config.
ipLimit == expectIpLimit);
457 BEAST_EXPECT(logic.
config() == config);
471 run(
"legacy no config", {}, {}, {}, 4000, 10, 11, 2);
472 run(
"legacy max_peers 0", 0, 100, 10, 4000, 10, 11, 2);
473 run(
"legacy max_peers 5", 5, 100, 10, 4000, 10, 0, 1);
474 run(
"legacy max_peers 20", 20, 100, 10, 4000, 10, 10, 2);
475 run(
"legacy max_peers 100", 100, 100, 10, 4000, 15, 85, 6);
476 run(
"legacy max_peers 20, private", 20, 100, 10, 0, 20, 0, 1);
479 run(
"new in 100/out 10", {}, 100, 10, 4000, 10, 100, 6);
480 run(
"new in 0/out 10", {}, 0, 10, 4000, 10, 0, 1);
481 run(
"new in 100/out 10, private", {}, 100, 10, 0, 10, 0, 6);
546BEAST_DEFINE_TESTSUITE(PeerFinder, peerfinder,
ripple);
A version-independent IP address and port combination.
static Endpoint from_string(std::string const &s)
void pass()
Record a successful test condition.
testcase_t testcase
Memberspace for declaring test cases.
void fail(String const &reason, char const *file, int line)
Record a failure.
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
std::size_t PEERS_OUT_MAX
Manages the count of available connections for the various slots.
int out_max() const
Returns the total number of outbound slots.
int in_max() const
Returns the total number of inbound slots.
void onConfig(Config const &config)
Called when the config is set or changed.
The Logic for maintaining the list of Slot addresses.
bool onConnected(SlotImp::ptr const &slot, beast::IP::Endpoint const &local_endpoint)
void on_closed(SlotImp::ptr const &slot)
Result activate(SlotImp::ptr const &slot, PublicKey const &key, bool reserved)
std::pair< SlotImp::ptr, Result > new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint)
std::multiset< beast::IP::Address > connectedAddresses_
std::vector< beast::IP::Endpoint > autoconnect()
Create new outbound connection attempts as needed.
std::pair< SlotImp::ptr, Result > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)
void addFixedPeer(std::string const &name, beast::IP::Endpoint const &ep)
void config(Config const &c)
test::SuiteJournal journal_
void test_duplicateInOut()
void test_invalid_config()
void test_onConnected_self_connection()
void test_addFixedPeer_no_port()
void test_duplicateOutIn()
void test_peerLimitExceeded()
void run() override
Runs the suite.
void test_activate_inbound_disabled()
void test_activate_duplicate_peer()
Abstract persistence for PeerFinder data.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PeerFinder configuration settings.
bool autoConnect
true if we want to establish connections automatically
int ipLimit
Limit how many incoming connections we allow per IP.
std::size_t inPeers
The number of automatic inbound connections to maintain.
static Config makeConfig(ripple::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit)
Make PeerFinder::Config from configuration parameters.
std::uint16_t listeningPort
The listening port number.
void async_connect(beast::IP::Endpoint const &ep, Handler &&handler)
void save(std::vector< Entry > const &) override
std::size_t load(load_callback const &cb) override