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
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().getIOContext());
84 std::string port = pUrl_.port ? std::to_string(*pUrl_.port) : "443";
85 auto results = resolver.resolve(pUrl_.domain, port);
86 auto it = results.begin();
87 auto end = results.end();
88 int n = 0;
89 for (; it != end; ++it)
90 ++n;
91 return n > 1;
92 }
93
94 void
96 {
97 std::string url = arg();
98 if (url == "")
99 url = "https://vl.ripple.com";
100 BEAST_EXPECT(parseUrl(pUrl_, url));
101 port_ = pUrl_.port ? std::to_string(*pUrl_.port) : "443";
102 }
103
104 void
105 run() override
106 {
107 parse();
108 // First endpoint is random. Next three
109 // should resolve to the same endpoint. Run a few times
110 // to verify we are not selecting by chance the same endpoint.
111 for (int i = 1; i <= 4; ++i)
112 {
114 BEAST_EXPECT(
115 resolved_.size() == 1 && resolved_.begin()->second == i);
116 }
117 if (!isMultipleEndpoints())
118 return;
119 // Run with the "failed" status. In this case endpoints are selected at
120 // random but last endpoint is not selected.
122 for (int i = 0; i < 4; ++i)
124 // Should have more than one but some endpoints can repeat since
125 // selected at random. We'll never have four identical endpoints
126 // here because on failure we randomly select an endpoint different
127 // from the last endpoint.
128 BEAST_EXPECT(resolved_.size() > 1);
129 }
130};
131
132BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(DNS, app, ripple, 20);
133
134} // namespace test
135} // 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_context & getIOContext()=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:105
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:121
Application & app()
Definition Env.h:261
beast::Journal const journal
Definition Env.h:162
T clear(T... args)
T is_same_v
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:25
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
T size(T... args)
std::optional< std::uint16_t > port
T to_string(T... args)