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/chrono.h>
21 #include <ripple/basics/Slice.h>
22 #include <ripple/protocol/PublicKey.h>
23 #include <ripple/protocol/SecretKey.h>
24 #include <ripple/peerfinder/impl/Logic.h>
25 #include <ripple/beast/unit_test.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:
37  : journal_ ("PeerFinder_test", *this)
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
69  Handler&& handler)
70  {
71  boost::system::error_code ec;
72  handler (ep, ep, ec);
73  }
74  };
75 
76  void
78  {
79  auto const seconds = 10000;
80  testcase("backoff 1");
81  TestStore store;
82  TestChecker checker;
83  TestStopwatch clock;
84  Logic<TestChecker> logic (clock, store, checker, journal_);
85  logic.addFixedPeer ("test",
86  beast::IP::Endpoint::from_string("65.0.0.1:5"));
87  {
88  Config c;
89  c.autoConnect = false;
90  c.listeningPort = 1024;
91  logic.config(c);
92  }
93  std::size_t n = 0;
94  for (std::size_t i = 0; i < seconds; ++i)
95  {
96  auto const list = logic.autoconnect();
97  if (! list.empty())
98  {
99  BEAST_EXPECT(list.size() == 1);
100  auto const slot = logic.new_outbound_slot(list.front());
101  BEAST_EXPECT(logic.onConnected(slot,
102  beast::IP::Endpoint::from_string("65.0.0.2:5")));
103  logic.on_closed(slot);
104  ++n;
105  }
106  clock.advance(std::chrono::seconds(1));
107  logic.once_per_second();
108  }
109  // Less than 20 attempts
110  BEAST_EXPECT(n < 20);
111  }
112 
113  // with activate
114  void
116  {
117  auto const seconds = 10000;
118  testcase("backoff 2");
119  TestStore store;
120  TestChecker checker;
121  TestStopwatch clock;
122  Logic<TestChecker> logic (clock, store, checker, journal_);
123  logic.addFixedPeer ("test",
124  beast::IP::Endpoint::from_string("65.0.0.1:5"));
125  {
126  Config c;
127  c.autoConnect = false;
128  c.listeningPort = 1024;
129  logic.config(c);
130  }
131 
132  PublicKey const pk (randomKeyPair(KeyType::secp256k1).first);
133  std::size_t n = 0;
134 
135  for (std::size_t i = 0; i < seconds; ++i)
136  {
137  auto const list = logic.autoconnect();
138  if (! list.empty())
139  {
140  BEAST_EXPECT(list.size() == 1);
141  auto const slot = logic.new_outbound_slot(list.front());
142  if (! BEAST_EXPECT(logic.onConnected(slot,
143  beast::IP::Endpoint::from_string("65.0.0.2:5"))))
144  return;
145  std::string s = ".";
146  if (! BEAST_EXPECT(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 run () override
160  {
161  test_backoff1();
162  test_backoff2();
163  }
164 };
165 
166 BEAST_DEFINE_TESTSUITE(PeerFinder,PeerFinder,ripple);
167 
168 }
169 }
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:300
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:338
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:115
ripple::PeerFinder::Logic::autoconnect
std::vector< beast::IP::Endpoint > autoconnect()
Create new outbound connection attempts as needed.
Definition: peerfinder/impl/Logic.h:464
ripple::PeerFinder::Logic::addFixedPeer
void addFixedPeer(std::string const &name, beast::IP::Endpoint const &ep)
Definition: peerfinder/impl/Logic.h:170
ripple::PeerFinder::Logic::activate
Result activate(SlotImp::ptr const &slot, PublicKey const &key, bool reserved)
Definition: peerfinder/impl/Logic.h:375
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:81
ripple::KeyType::secp256k1
@ secp256k1
ripple::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:286
ripple::PeerFinder::Logic::once_per_second
void once_per_second()
Definition: peerfinder/impl/Logic.h:664
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:159
beast::IP::Endpoint::from_string
static Endpoint from_string(std::string const &s)
Definition: IPEndpoint.cpp:47
ripple::PeerFinder::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(Livecache, peerfinder, ripple)
ripple::PeerFinder::PeerFinder_test::test_backoff1
void test_backoff1()
Definition: PeerFinder_test.cpp:77
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:861
beast::manual_clock< std::chrono::steady_clock >
ripple::PeerFinder::Logic::config
void config(Config const &c)
Definition: peerfinder/impl/Logic.h:155
ripple::PeerFinder::PeerFinder_test::TestStore::load
std::size_t load(load_callback const &cb) override
Definition: PeerFinder_test.cpp:43