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