xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
app
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
19
namespace
xrpl::test
{
20
21
class
DNS_test
:
public
beast::unit_test::Suite
22
{
23
using
EndpointType
= boost::asio::ip::tcp::endpoint;
24
using
ErrorCode
= boost::system::error_code;
25
std::weak_ptr<xrpl::detail::Work>
work_
;
26
EndpointType
lastEndpoint_
;
27
ParsedUrl
pUrl_
;
28
std::string
port_
;
29
jtx::Env
env_
;
30
std::map<std::string, int>
resolved_
;
31
std::mutex
mutex_
;
32
std::condition_variable
cv_
;
33
34
public
:
35
DNS_test
() :
env_
(*this)
36
{
37
}
38
39
void
40
makeRequest
(
EndpointType
const
& lastEndpoint,
bool
lastStatus)
41
{
42
auto
onFetch = [&](
ErrorCode
const
& errorCode,
43
EndpointType
const
& endpoint,
44
xrpl::detail::ResponseType
const
& resp) {
45
BEAST_EXPECT(!errorCode);
46
lastEndpoint_
= endpoint;
47
resolved_
[endpoint.address().to_string()]++;
48
cv_
.notify_all();
49
};
50
51
auto
sp =
std::make_shared<xrpl::detail::WorkSSL>
(
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
64
std::unique_lock
l(
mutex_
);
65
cv_
.wait(l);
66
}
67
68
bool
69
isMultipleEndpoints
()
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
84
parse
()
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
{
102
makeRequest
(
lastEndpoint_
,
true
);
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)
111
makeRequest
(
lastEndpoint_
,
false
);
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
120
BEAST_DEFINE_TESTSUITE_MANUAL_PRIO
(DNS, app,
xrpl
, 20);
121
122
}
// namespace xrpl::test
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::arg
std::string const & arg() const
Return the argument associated with the runner.
Definition
suite.h:292
xrpl::test::DNS_test::cv_
std::condition_variable cv_
Definition
DNS_test.cpp:32
xrpl::test::DNS_test::makeRequest
void makeRequest(EndpointType const &lastEndpoint, bool lastStatus)
Definition
DNS_test.cpp:40
xrpl::test::DNS_test::mutex_
std::mutex mutex_
Definition
DNS_test.cpp:31
xrpl::test::DNS_test::resolved_
std::map< std::string, int > resolved_
Definition
DNS_test.cpp:30
xrpl::test::DNS_test::EndpointType
boost::asio::ip::tcp::endpoint EndpointType
Definition
DNS_test.cpp:23
xrpl::test::DNS_test::port_
std::string port_
Definition
DNS_test.cpp:28
xrpl::test::DNS_test::isMultipleEndpoints
bool isMultipleEndpoints()
Definition
DNS_test.cpp:69
xrpl::test::DNS_test::env_
jtx::Env env_
Definition
DNS_test.cpp:29
xrpl::test::DNS_test::ErrorCode
boost::system::error_code ErrorCode
Definition
DNS_test.cpp:24
xrpl::test::DNS_test::parse
void parse()
Definition
DNS_test.cpp:84
xrpl::test::DNS_test::DNS_test
DNS_test()
Definition
DNS_test.cpp:35
xrpl::test::DNS_test::lastEndpoint_
EndpointType lastEndpoint_
Definition
DNS_test.cpp:26
xrpl::test::DNS_test::run
void run() override
Runs the suite.
Definition
DNS_test.cpp:94
xrpl::test::DNS_test::work_
std::weak_ptr< xrpl::detail::Work > work_
Definition
DNS_test.cpp:25
xrpl::test::DNS_test::pUrl_
ParsedUrl pUrl_
Definition
DNS_test.cpp:27
xrpl::test::jtx::Env
A transaction testing environment.
Definition
Env.h:161
condition_variable
std::string::empty
T empty(T... args)
std::make_shared
T make_shared(T... args)
map
memory
mutex
xrpl::detail::ResponseType
boost::beast::http::response< boost::beast::http::string_body > ResponseType
Definition
Work.h:8
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::BEAST_DEFINE_TESTSUITE_MANUAL_PRIO
BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimits, app, xrpl, 10)
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::parseUrl
bool parseUrl(ParsedUrl &pUrl, std::string const &strUrl)
Definition
libxrpl/basics/StringUtilities.cpp:37
string
xrpl::ParsedUrl
Definition
StringUtilities.h:106
std::to_string
T to_string(T... args)
std::unique_lock
std::weak_ptr
Generated by
1.17.0