rippled
DNS_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright 2020 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/app/misc/detail/WorkSSL.h>
21 #include <ripple/basics/StringUtilities.h>
22 #include <test/jtx.h>
23 
24 #include <condition_variable>
25 #include <memory>
26 
27 namespace ripple {
28 namespace test {
29 
30 class DNS_test : public beast::unit_test::suite
31 {
32  using endpoint_type = boost::asio::ip::tcp::endpoint;
33  using error_code = boost::system::error_code;
42 
43 public:
44  DNS_test() : env_(*this)
45  {
46  }
47 
48  void
49  makeRequest(endpoint_type const& lastEndpoint, bool lastStatus)
50  {
51  auto onFetch = [&](error_code const& errorCode,
52  endpoint_type const& endpoint,
54  BEAST_EXPECT(!errorCode);
55  lastEndpoint_ = endpoint;
56  resolved_[endpoint.address().to_string()]++;
57  cv_.notify_all();
58  };
59 
60  auto sp = std::make_shared<ripple::detail::WorkSSL>(
61  pUrl_.domain,
62  pUrl_.path,
63  port_,
64  env_.app().getIOService(),
65  env_.journal,
66  env_.app().config(),
67  lastEndpoint,
68  lastStatus,
69  onFetch);
70  work_ = sp;
71  sp->run();
72 
74  cv_.wait(l);
75  }
76 
77  bool
79  {
80  using boost::asio::ip::tcp;
81  tcp::resolver resolver(env_.app().getIOService());
82  std::string port = pUrl_.port ? std::to_string(*pUrl_.port) : "443";
83  tcp::resolver::iterator it = resolver.resolve(pUrl_.domain, port);
84  tcp::resolver::iterator end;
85  int n = 0;
86  for (; it != end; ++it)
87  ++n;
88  return n > 1;
89  }
90 
91  void
93  {
94  std::string url = arg();
95  if (url == "")
96  url = "https://vl.ripple.com";
97  BEAST_EXPECT(parseUrl(pUrl_, url));
98  port_ = pUrl_.port ? std::to_string(*pUrl_.port) : "443";
99  }
100 
101  void
102  run() override
103  {
104  parse();
105  // First endpoint is random. Next three
106  // should resolve to the same endpoint. Run a few times
107  // to verify we are not selecting by chance the same endpoint.
108  for (int i = 1; i <= 4; ++i)
109  {
110  makeRequest(lastEndpoint_, true);
111  BEAST_EXPECT(
112  resolved_.size() == 1 && resolved_.begin()->second == i);
113  }
114  if (!isMultipleEndpoints())
115  return;
116  // Run with the "failed" status. In this case endpoints are selected at
117  // random but last endpoint is not selected.
118  resolved_.clear();
119  for (int i = 0; i < 4; ++i)
120  makeRequest(lastEndpoint_, false);
121  // Should have more than one but some endpoints can repeat since
122  // selected at random. We'll never have four identical endpoints
123  // here because on failure we randomly select an endpoint different
124  // from the last endpoint.
125  BEAST_EXPECT(resolved_.size() > 1);
126  }
127 };
128 
129 BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(DNS, ripple_data, ripple, 20);
130 
131 } // namespace test
132 } // namespace ripple
ripple::test::DNS_test::resolved_
std::map< std::string, int > resolved_
Definition: DNS_test.cpp:39
std::string
STL class.
ripple::test::DNS_test::env_
jtx::Env env_
Definition: DNS_test.cpp:38
ripple::parsedURL
Definition: StringUtilities.h:116
ripple::test::DNS_test::makeRequest
void makeRequest(endpoint_type const &lastEndpoint, bool lastStatus)
Definition: DNS_test.cpp:49
ripple::test::DNS_test::cv_
std::condition_variable cv_
Definition: DNS_test.cpp:41
std::map::size
T size(T... args)
ripple::test::jtx::Env::journal
const beast::Journal journal
Definition: Env.h:144
ripple::test::DNS_test::error_code
boost::system::error_code error_code
Definition: DNS_test.cpp:33
ripple::detail::Work::run
virtual void run()=0
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:241
ripple::parsedURL::path
std::string path
Definition: StringUtilities.h:125
ripple::test::DNS_test::DNS_test
DNS_test()
Definition: DNS_test.cpp:44
std::map::clear
T clear(T... args)
ripple::detail::response_type
boost::beast::http::response< boost::beast::http::string_body > response_type
Definition: Work.h:31
ripple::test::BEAST_DEFINE_TESTSUITE_MANUAL_PRIO
BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimits, tx, ripple, 10)
ripple::test::DNS_test::endpoint_type
boost::asio::ip::tcp::endpoint endpoint_type
Definition: DNS_test.cpp:32
ripple::parsedURL::port
std::optional< std::uint16_t > port
Definition: StringUtilities.h:124
ripple::Application::config
virtual Config & config()=0
std::unique_lock
STL class.
std::to_string
T to_string(T... args)
ripple::parseUrl
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
Definition: StringUtilities.cpp:47
ripple::test::DNS_test::pUrl_
parsedURL pUrl_
Definition: DNS_test.cpp:36
ripple::test::DNS_test::port_
std::string port_
Definition: DNS_test.cpp:37
std::condition_variable::wait
T wait(T... args)
std::map< std::string, int >
ripple::test::DNS_test::mutex_
std::mutex mutex_
Definition: DNS_test.cpp:40
memory
ripple::Application::getIOService
virtual boost::asio::io_service & getIOService()=0
std::weak_ptr< ripple::detail::Work >
ripple::test::DNS_test::isMultipleEndpoints
bool isMultipleEndpoints()
Definition: DNS_test.cpp:78
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::DNS_test
Definition: DNS_test.cpp:30
std::map::begin
T begin(T... args)
condition_variable
std::mutex
STL class.
ripple::test::DNS_test::work_
std::weak_ptr< ripple::detail::Work > work_
Definition: DNS_test.cpp:34
ripple::parsedURL::domain
std::string domain
Definition: StringUtilities.h:123
ripple::test::DNS_test::lastEndpoint_
endpoint_type lastEndpoint_
Definition: DNS_test.cpp:35
std::condition_variable::notify_all
T notify_all(T... args)
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
ripple::test::DNS_test::run
void run() override
Definition: DNS_test.cpp:102
ripple::test::DNS_test::parse
void parse()
Definition: DNS_test.cpp:92