rippled
PeerFinder_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/Slice.h>
21 #include <ripple/basics/chrono.h>
22 #include <ripple/beast/unit_test.h>
23 #include <ripple/peerfinder/impl/Logic.h>
24 #include <ripple/protocol/PublicKey.h>
25 #include <ripple/protocol/SecretKey.h>
26 #include <test/unit_test/SuiteJournal.h>
27 
28 namespace ripple {
29 namespace PeerFinder {
30 
31 class PeerFinder_test : public beast::unit_test::suite
32 {
34 
35 public:
36  PeerFinder_test() : journal_("PeerFinder_test", *this)
37  {
38  }
39 
40  struct TestStore : Store
41  {
43  load(load_callback const& cb) override
44  {
45  return 0;
46  }
47 
48  void
49  save(std::vector<Entry> const&) override
50  {
51  }
52  };
53 
54  struct TestChecker
55  {
56  void
57  stop()
58  {
59  }
60 
61  void
62  wait()
63  {
64  }
65 
66  template <class Handler>
67  void
68  async_connect(beast::IP::Endpoint const& ep, Handler&& handler)
69  {
70  boost::system::error_code ec;
71  handler(ep, ep, ec);
72  }
73  };
74 
75  void
77  {
78  auto const seconds = 10000;
79  testcase("backoff 1");
80  TestStore store;
81  TestChecker checker;
82  TestStopwatch clock;
83  Logic<TestChecker> logic(clock, store, checker, journal_);
84  logic.addFixedPeer(
85  "test", beast::IP::Endpoint::from_string("65.0.0.1:5"));
86  {
87  Config c;
88  c.autoConnect = false;
89  c.listeningPort = 1024;
90  logic.config(c);
91  }
92  std::size_t n = 0;
93  for (std::size_t i = 0; i < seconds; ++i)
94  {
95  auto const list = logic.autoconnect();
96  if (!list.empty())
97  {
98  BEAST_EXPECT(list.size() == 1);
99  auto const slot = logic.new_outbound_slot(list.front());
100  BEAST_EXPECT(logic.onConnected(
101  slot, beast::IP::Endpoint::from_string("65.0.0.2:5")));
102  logic.on_closed(slot);
103  ++n;
104  }
105  clock.advance(std::chrono::seconds(1));
106  logic.once_per_second();
107  }
108  // Less than 20 attempts
109  BEAST_EXPECT(n < 20);
110  }
111 
112  // with activate
113  void
115  {
116  auto const seconds = 10000;
117  testcase("backoff 2");
118  TestStore store;
119  TestChecker checker;
120  TestStopwatch clock;
121  Logic<TestChecker> logic(clock, store, checker, journal_);
122  logic.addFixedPeer(
123  "test", beast::IP::Endpoint::from_string("65.0.0.1:5"));
124  {
125  Config c;
126  c.autoConnect = false;
127  c.listeningPort = 1024;
128  logic.config(c);
129  }
130 
131  PublicKey const pk(randomKeyPair(KeyType::secp256k1).first);
132  std::size_t n = 0;
133 
134  for (std::size_t i = 0; i < seconds; ++i)
135  {
136  auto const list = logic.autoconnect();
137  if (!list.empty())
138  {
139  BEAST_EXPECT(list.size() == 1);
140  auto const slot = logic.new_outbound_slot(list.front());
141  if (!BEAST_EXPECT(logic.onConnected(
142  slot, beast::IP::Endpoint::from_string("65.0.0.2:5"))))
143  return;
144  std::string s = ".";
145  if (!BEAST_EXPECT(
146  logic.activate(slot, pk, false) ==
148  return;
149  logic.on_closed(slot);
150  ++n;
151  }
152  clock.advance(std::chrono::seconds(1));
153  logic.once_per_second();
154  }
155  // No more often than once per minute
156  BEAST_EXPECT(n <= (seconds + 59) / 60);
157  }
158 
159  void
160  run() override
161  {
162  test_backoff1();
163  test_backoff2();
164  }
165 };
166 
167 BEAST_DEFINE_TESTSUITE(PeerFinder, PeerFinder, ripple);
168 
169 } // namespace PeerFinder
170 } // namespace ripple
ripple::PeerFinder::PeerFinder_test::TestStore::save
void save(std::vector< Entry > const &) override
Definition: PeerFinder_test.cpp:49
std::string
STL class.
ripple::PeerFinder::Logic
The Logic for maintaining the list of Slot addresses.
Definition: peerfinder/impl/Logic.h:54
ripple::PeerFinder::PeerFinder_test
Definition: PeerFinder_test.cpp:31
ripple::PeerFinder::Logic::new_outbound_slot
SlotImp::ptr new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)
Definition: peerfinder/impl/Logic.h:311
std::vector
STL class.
std::chrono::seconds
ripple::PeerFinder::PeerFinder_test::journal_
test::SuiteJournal journal_
Definition: PeerFinder_test.cpp:33
ripple::PeerFinder::PeerFinder_test::TestChecker
Definition: PeerFinder_test.cpp:54
ripple::PeerFinder::PeerFinder_test::TestChecker::wait
void wait()
Definition: PeerFinder_test.cpp:62
std::function
ripple::PeerFinder::Store
Abstract persistence for PeerFinder data.
Definition: Store.h:27
ripple::PeerFinder::PeerFinder_test::TestStore
Definition: PeerFinder_test.cpp:40
ripple::PeerFinder::Result::success
@ success
ripple::PeerFinder::Logic::onConnected
bool onConnected(SlotImp::ptr const &slot, beast::IP::Endpoint const &local_endpoint)
Definition: peerfinder/impl/Logic.h:346
ripple::PeerFinder::PeerFinder_test::TestChecker::stop
void stop()
Definition: PeerFinder_test.cpp:57
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::PeerFinder::PeerFinder_test::PeerFinder_test
PeerFinder_test()
Definition: PeerFinder_test.cpp:36
ripple::PeerFinder::PeerFinder_test::test_backoff2
void test_backoff2()
Definition: PeerFinder_test.cpp:114
ripple::PeerFinder::Logic::autoconnect
std::vector< beast::IP::Endpoint > autoconnect()
Create new outbound connection attempts as needed.
Definition: peerfinder/impl/Logic.h:467
ripple::PeerFinder::Logic::addFixedPeer
void addFixedPeer(std::string const &name, beast::IP::Endpoint const &ep)
Definition: peerfinder/impl/Logic.h:174
ripple::PeerFinder::Logic::activate
Result activate(SlotImp::ptr const &slot, PublicKey const &key, bool reserved)
Definition: peerfinder/impl/Logic.h:383
ripple::PeerFinder::PeerFinder_test::TestChecker::async_connect
void async_connect(beast::IP::Endpoint const &ep, Handler &&handler)
Definition: PeerFinder_test.cpp:68
ripple::test::SuiteJournal
Definition: SuiteJournal.h:88
ripple::KeyType::secp256k1
@ secp256k1
ripple::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:260
ripple::PeerFinder::Logic::once_per_second
void once_per_second()
Definition: peerfinder/impl/Logic.h:672
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Config::listeningPort
std::uint16_t listeningPort
The listening port number.
Definition: PeerfinderManager.h:70
ripple::PeerFinder::PeerFinder_test::run
void run() override
Definition: PeerFinder_test.cpp:160
beast::IP::Endpoint::from_string
static Endpoint from_string(std::string const &s)
Definition: IPEndpoint.cpp:46
ripple::PeerFinder::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(Livecache, peerfinder, ripple)
ripple::PeerFinder::PeerFinder_test::test_backoff1
void test_backoff1()
Definition: PeerFinder_test.cpp:76
std::size_t
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
ripple::PeerFinder::Config::autoConnect
bool autoConnect
true if we want to establish connections automatically
Definition: PeerfinderManager.h:67
ripple::PeerFinder::Config
PeerFinder configuration settings.
Definition: PeerfinderManager.h:40
ripple::PeerFinder::Logic::on_closed
void on_closed(SlotImp::ptr const &slot)
Definition: peerfinder/impl/Logic.h:876
beast::manual_clock< std::chrono::steady_clock >
ripple::PeerFinder::Logic::config
void config(Config const &c)
Definition: peerfinder/impl/Logic.h:159
ripple::PeerFinder::PeerFinder_test::TestStore::load
std::size_t load(load_callback const &cb) override
Definition: PeerFinder_test.cpp:43