xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
overlay
ProtocolVersion_test.cpp
1
#include <xrpld/overlay/detail/ProtocolVersion.h>
2
3
#include <xrpl/beast/unit_test/suite.h>
4
5
#include <
optional
>
6
#include <
string
>
7
8
namespace
xrpl
{
9
10
class
ProtocolVersion_test
:
public
beast::unit_test::Suite
11
{
12
private
:
13
void
14
check
(
std::string
const
& s,
std::string
const
& answer)
15
{
16
auto
join
= [](
auto
first,
auto
last) {
17
std::string
result;
18
if
(first != last)
19
{
20
result =
to_string
(*first++);
21
22
while
(first != last)
23
result +=
","
+
to_string
(*first++);
24
}
25
return
result;
26
};
27
28
auto
const
result =
parseProtocolVersions
(s);
29
BEAST_EXPECT(
join
(result.begin(), result.end()) == answer);
30
}
31
32
public
:
33
void
34
run
()
override
35
{
36
{
37
testcase
(
"Convert protocol version to string"
);
38
39
BEAST_EXPECT(
to_string
(
makeProtocol
(0, 0)) ==
"XRPL/0.0"
);
40
BEAST_EXPECT(
to_string
(
makeProtocol
(0, 1)) ==
"XRPL/0.1"
);
41
BEAST_EXPECT(
to_string
(
makeProtocol
(1, 3)) ==
"XRPL/1.3"
);
42
BEAST_EXPECT(
to_string
(
makeProtocol
(2, 0)) ==
"XRPL/2.0"
);
43
BEAST_EXPECT(
to_string
(
makeProtocol
(2, 1)) ==
"XRPL/2.1"
);
44
BEAST_EXPECT(
to_string
(
makeProtocol
(10, 10)) ==
"XRPL/10.10"
);
45
BEAST_EXPECT(
to_string
(
makeProtocol
(65535, 65535)) ==
"XRPL/65535.65535"
);
46
}
47
48
{
49
testcase
(
"Convert strings to protocol versions"
);
50
51
// Invalid versions, either they do not parse as XRPL/N.M or are unsupported.
52
check
(
""
,
""
);
53
check
(
"RTXP/1.1,RTXP/1.2,RTXP/1.3"
,
""
);
54
check
(
"XRPL/-2.1,XRPL/0.3,XRPL/2,XRPL/2.01,websocket"
,
""
);
55
56
// Mixture of valid, duplicate, and invalid versions.
57
check
(
"RTXP/1.3,XRPL/2.1,XRPL/2.0,/XRPL/3.0"
,
"XRPL/2.0,XRPL/2.1"
);
58
check
(
59
"XRPL/2.0,XRPL/2.0,XRPL/19.4,XRPL/7.89,XRPL/XRPL/3.0,XRPL/2.01,XRPL/-65535.65535"
,
60
"XRPL/2.0,XRPL/7.89,XRPL/19.4"
);
61
check
(
62
"XRPL/2.0,XRPL/3.0,XRPL/4,XRPL/,XRPL,OPT XRPL/2.2,XRPL/5.67"
,
63
"XRPL/2.0,XRPL/3.0,XRPL/5.67"
);
64
}
65
66
{
67
testcase
(
"Protocol version negotiation"
);
68
69
// Only the highest supported protocol version, if any, is returned.
70
BEAST_EXPECT(
negotiateProtocolVersion
(
""
) == std::nullopt);
71
BEAST_EXPECT(
negotiateProtocolVersion
(
"XRPL/0.0"
) == std::nullopt);
72
BEAST_EXPECT(
negotiateProtocolVersion
(
"RTXP/1.2,XRPL/0.1"
) == std::nullopt);
73
BEAST_EXPECT(
74
negotiateProtocolVersion
(
"XRPL/999.999, XRPL/-2.2,WebSocket/1.0"
) == std::nullopt);
75
BEAST_EXPECT(
negotiateProtocolVersion
(
"XRPL/2.2"
) ==
makeProtocol
(2, 2));
76
BEAST_EXPECT(
77
negotiateProtocolVersion
(
78
"RTXP/1.2, XRPL/2.1, XRPL/2.2, XRPL/2.3, XRPL/2.4, XRPL/999.999"
) ==
79
makeProtocol
(2, 3));
80
}
81
}
82
};
83
84
BEAST_DEFINE_TESTSUITE
(
ProtocolVersion
, overlay,
xrpl
);
85
86
}
// namespace xrpl
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
xrpl::ProtocolVersion_test
Definition
ProtocolVersion_test.cpp:11
xrpl::ProtocolVersion_test::run
void run() override
Runs the suite.
Definition
ProtocolVersion_test.cpp:34
xrpl::ProtocolVersion_test::check
void check(std::string const &s, std::string const &answer)
Definition
ProtocolVersion_test.cpp:14
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::negotiateProtocolVersion
std::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
Definition
ProtocolVersion.cpp:104
xrpl::join
Stream & join(Stream &s, Iter iter, Iter end, std::string_view delimiter)
Definition
join.h:12
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::makeProtocol
constexpr ProtocolVersion makeProtocol(std::uint16_t major, std::uint16_t minor)
Definition
ProtocolVersion.h:21
xrpl::ProtocolVersion
std::pair< std::uint16_t, std::uint16_t > ProtocolVersion
Represents a particular version of the peer-to-peer protocol.
Definition
ProtocolVersion.h:18
xrpl::parseProtocolVersions
std::vector< ProtocolVersion > parseProtocolVersions(std::string_view value)
Parse a set of protocol versions.
Definition
ProtocolVersion.cpp:56
xrpl::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
optional
string
Generated by
1.17.0