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#include <xrpld/app/misc/detail/WorkSSL.h>
22#include <xrpl/basics/StringUtilities.h>
23
24#include <condition_variable>
25#include <memory>
26
27namespace ripple {
28namespace test {
29
31{
32 using endpoint_type = boost::asio::ip::tcp::endpoint;
33 using error_code = boost::system::error_code;
42
43public:
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()]++;
58 };
59
60 auto sp = std::make_shared<ripple::detail::WorkSSL>(
62 pUrl_.path,
63 port_,
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));
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 {
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.
119 for (int i = 0; i < 4; ++i)
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
129BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(DNS, ripple_data, ripple, 20);
130
131} // namespace test
132} // namespace ripple
T begin(T... args)
A testsuite class.
Definition: suite.h:53
std::string const & arg() const
Return the argument associated with the runner.
Definition: suite.h:286
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:34
void makeRequest(endpoint_type const &lastEndpoint, bool lastStatus)
Definition: DNS_test.cpp:49
void run() override
Runs the suite.
Definition: DNS_test.cpp:102
std::map< std::string, int > resolved_
Definition: DNS_test.cpp:39
boost::asio::ip::tcp::endpoint endpoint_type
Definition: DNS_test.cpp:32
endpoint_type lastEndpoint_
Definition: DNS_test.cpp:35
boost::system::error_code error_code
Definition: DNS_test.cpp:33
std::condition_variable cv_
Definition: DNS_test.cpp:41
A transaction testing environment.
Definition: Env.h:117
Application & app()
Definition: Env.h:255
beast::Journal const journal
Definition: Env.h:158
T clear(T... args)
boost::beast::http::response< boost::beast::http::string_body > response_type
Definition: Work.h:35
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)