xrpld
Loading...
Searching...
No Matches
DNS_test.cpp
1
2#include <test/jtx/Env.h>
3
4#include <xrpld/app/misc/detail/Work.h>
5#include <xrpld/app/misc/detail/WorkSSL.h>
6
7#include <xrpl/basics/StringUtilities.h>
8#include <xrpl/beast/unit_test/suite.h>
9
10#include <boost/asio/ip/tcp.hpp>
11#include <boost/system/detail/error_code.hpp>
12
13#include <condition_variable>
14#include <map>
15#include <memory>
16#include <mutex>
17#include <string>
18
19namespace xrpl::test {
20
22{
23 using endpoint_type = boost::asio::ip::tcp::endpoint;
24 using error_code = boost::system::error_code;
33
34public:
35 DNS_test() : env_(*this)
36 {
37 }
38
39 void
40 makeRequest(endpoint_type const& lastEndpoint, bool lastStatus)
41 {
42 auto onFetch = [&](error_code const& errorCode,
43 endpoint_type const& endpoint,
44 xrpl::detail::response_type const& resp) {
45 BEAST_EXPECT(!errorCode);
46 lastEndpoint_ = endpoint;
47 resolved_[endpoint.address().to_string()]++;
48 cv_.notify_all();
49 };
50
52 pUrl_.domain,
53 pUrl_.path,
54 port_,
55 env_.app().getIOContext(),
56 env_.journal,
57 env_.app().config(),
58 lastEndpoint,
59 lastStatus,
60 onFetch);
61 work_ = sp;
62 sp->run();
63
65 cv_.wait(l);
66 }
67
68 bool
70 {
71 using boost::asio::ip::tcp;
72 tcp::resolver resolver(env_.app().getIOContext());
73 std::string const port = pUrl_.port ? std::to_string(*pUrl_.port) : "443";
74 auto results = resolver.resolve(pUrl_.domain, port);
75 auto it = results.begin();
76 auto end = results.end();
77 int n = 0;
78 for (; it != end; ++it)
79 ++n;
80 return n > 1;
81 }
82
83 void
85 {
86 std::string url = arg();
87 if (url.empty())
88 url = "https://vl.ripple.com";
89 BEAST_EXPECT(parseUrl(pUrl_, url));
90 port_ = pUrl_.port ? std::to_string(*pUrl_.port) : "443";
91 }
92
93 void
94 run() override
95 {
96 parse();
97 // First endpoint is random. Next three
98 // should resolve to the same endpoint. Run a few times
99 // to verify we are not selecting by chance the same endpoint.
100 for (int i = 1; i <= 4; ++i)
101 {
103 BEAST_EXPECT(resolved_.size() == 1 && resolved_.begin()->second == i);
104 }
105 if (!isMultipleEndpoints())
106 return;
107 // Run with the "failed" status. In this case endpoints are selected at
108 // random but last endpoint is not selected.
109 resolved_.clear();
110 for (int i = 0; i < 4; ++i)
112 // Should have more than one but some endpoints can repeat since
113 // selected at random. We'll never have four identical endpoints
114 // here because on failure we randomly select an endpoint different
115 // from the last endpoint.
116 BEAST_EXPECT(resolved_.size() > 1);
117 }
118};
119
121
122} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
std::string const & arg() const
Return the argument associated with the runner.
Definition suite.h:278
std::condition_variable cv_
Definition DNS_test.cpp:32
std::map< std::string, int > resolved_
Definition DNS_test.cpp:30
boost::asio::ip::tcp::endpoint endpoint_type
Definition DNS_test.cpp:23
std::string port_
Definition DNS_test.cpp:28
boost::system::error_code error_code
Definition DNS_test.cpp:24
void makeRequest(endpoint_type const &lastEndpoint, bool lastStatus)
Definition DNS_test.cpp:40
void run() override
Runs the suite.
Definition DNS_test.cpp:94
std::weak_ptr< xrpl::detail::Work > work_
Definition DNS_test.cpp:25
endpoint_type lastEndpoint_
Definition DNS_test.cpp:26
A transaction testing environment.
Definition Env.h:143
T empty(T... args)
T make_shared(T... args)
boost::beast::http::response< boost::beast::http::string_body > response_type
Definition Work.h:8
BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimits, app, xrpl, 10)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool parseUrl(ParsedUrl &pUrl, std::string const &strUrl)
T to_string(T... args)