1#include <test/unit_test/SuiteJournal.h>
3#include <xrpld/core/Config.h>
4#include <xrpld/peerfinder/PeerfinderManager.h>
5#include <xrpld/peerfinder/detail/Logic.h>
7#include <xrpl/basics/chrono.h>
8#include <xrpl/beast/unit_test/suite.h>
9#include <xrpl/protocol/PublicKey.h>
10#include <xrpl/protocol/SecretKey.h>
50 template <
class Handler>
54 boost::system::error_code ec;
62 auto const seconds = 10000;
82 BEAST_EXPECT(list.size() == 1);
100 auto const seconds = 10000;
123 BEAST_EXPECT(list.size() == 1);
131 PeerFinder::Result::success))
140 BEAST_EXPECT(n <= (seconds + 59) / 60);
162 BEAST_EXPECT(slot1 !=
nullptr);
163 BEAST_EXPECT(r == Result::success);
169 BEAST_EXPECT(r2 == Result::duplicatePeer);
171 if (!BEAST_EXPECT(slot2 ==
nullptr))
198 BEAST_EXPECT(slot1 !=
nullptr);
199 BEAST_EXPECT(r == Result::success);
203 BEAST_EXPECT(r2 == Result::duplicatePeer);
205 if (!BEAST_EXPECT(slot2 ==
nullptr))
229 BEAST_EXPECT(slot !=
nullptr);
230 BEAST_EXPECT(r == Result::success);
234 BEAST_EXPECT(slot1 !=
nullptr);
235 BEAST_EXPECT(r1 == Result::success);
239 BEAST_EXPECT(r2 == Result::ipLimitExceeded);
241 if (!BEAST_EXPECT(slot2 ==
nullptr))
250 testcase(
"test activate duplicate peer");
269 BEAST_EXPECT(slot !=
nullptr);
270 BEAST_EXPECT(rSlot == Result::success);
274 BEAST_EXPECT(slot2 !=
nullptr);
275 BEAST_EXPECT(r2Slot == Result::success);
280 BEAST_EXPECT(logic.
activate(slot, pk1,
false) == Result::success);
284 logic.
activate(slot2, pk1,
false) == Result::duplicatePeer);
289 BEAST_EXPECT(logic.
activate(slot2, pk1,
false) == Result::success);
296 testcase(
"test activate inbound disabled");
314 BEAST_EXPECT(slot !=
nullptr);
315 BEAST_EXPECT(rSlot == Result::success);
318 logic.
activate(slot, pk1,
false) == Result::inboundDisabled);
329 BEAST_EXPECT(logic.
activate(slot, pk1,
false) == Result::success);
334 BEAST_EXPECT(slot2 !=
nullptr);
335 BEAST_EXPECT(r2Slot == Result::success);
340 BEAST_EXPECT(logic.
activate(slot2, pk2,
false) == Result::full);
349 testcase(
"test addFixedPeer no port");
358 fail(
"invalid endpoint successfully added");
369 testcase(
"test onConnected self connection");
377 BEAST_EXPECT(slot !=
nullptr);
378 BEAST_EXPECT(r == Result::success);
406 max = maxPeers.value();
409 "\n" +
"[peers_out_max]\n" +
412 else if (maxIn && maxOut)
429 counts.
out_max() == expectOut && counts.
in_max() == expectIn &&
430 config.
ipLimit == expectIpLimit);
438 BEAST_EXPECT(logic.
config() == config);
452 run(
"legacy no config", {}, {}, {}, 4000, 10, 11, 2);
453 run(
"legacy max_peers 0", 0, 100, 10, 4000, 10, 11, 2);
454 run(
"legacy max_peers 5", 5, 100, 10, 4000, 10, 0, 1);
455 run(
"legacy max_peers 20", 20, 100, 10, 4000, 10, 10, 2);
456 run(
"legacy max_peers 100", 100, 100, 10, 4000, 15, 85, 6);
457 run(
"legacy max_peers 20, private", 20, 100, 10, 0, 20, 0, 1);
460 run(
"new in 100/out 10", {}, 100, 10, 4000, 10, 100, 6);
461 run(
"new in 0/out 10", {}, 0, 10, 4000, 10, 0, 1);
462 run(
"new in 100/out 10, private", {}, 100, 10, 0, 10, 0, 6);
527BEAST_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