rippled
Loading...
Searching...
No Matches
ProtocolVersion_test.cpp
1#include <xrpld/overlay/detail/ProtocolVersion.h>
2
3#include <xrpl/beast/unit_test.h>
4
5namespace ripple {
6
8{
9private:
10 void
11 check(std::string const& s, std::string const& answer)
12 {
13 auto join = [](auto first, auto last) {
14 std::string result;
15 if (first != last)
16 {
17 result = to_string(*first++);
18
19 while (first != last)
20 result += "," + to_string(*first++);
21 }
22 return result;
23 };
24
25 auto const result = parseProtocolVersions(s);
26 BEAST_EXPECT(join(result.begin(), result.end()) == answer);
27 }
28
29public:
30 void
31 run() override
32 {
33 testcase("Convert protocol version to string");
34 BEAST_EXPECT(to_string(make_protocol(1, 3)) == "XRPL/1.3");
35 BEAST_EXPECT(to_string(make_protocol(2, 0)) == "XRPL/2.0");
36 BEAST_EXPECT(to_string(make_protocol(2, 1)) == "XRPL/2.1");
37 BEAST_EXPECT(to_string(make_protocol(10, 10)) == "XRPL/10.10");
38
39 {
40 testcase("Convert strings to protocol versions");
41
42 // Empty string
43 check("", "");
44
45 // clang-format off
46 check(
47 "RTXP/1.1,RTXP/1.2,RTXP/1.3,XRPL/2.1,XRPL/2.0,/XRPL/3.0",
48 "XRPL/2.0,XRPL/2.1");
49 check(
50 "RTXP/0.9,RTXP/1.01,XRPL/0.3,XRPL/2.01,websocket",
51 "");
52 check(
53 "XRPL/2.0,XRPL/2.0,XRPL/19.4,XRPL/7.89,XRPL/XRPL/3.0,XRPL/2.01",
54 "XRPL/2.0,XRPL/7.89,XRPL/19.4");
55 check(
56 "XRPL/2.0,XRPL/3.0,XRPL/4,XRPL/,XRPL,OPT XRPL/2.2,XRPL/5.67",
57 "XRPL/2.0,XRPL/3.0,XRPL/5.67");
58 // clang-format on
59 }
60
61 {
62 testcase("Protocol version negotiation");
63
64 BEAST_EXPECT(negotiateProtocolVersion("RTXP/1.2") == std::nullopt);
65 BEAST_EXPECT(
66 negotiateProtocolVersion("RTXP/1.2, XRPL/2.0, XRPL/2.1") ==
67 make_protocol(2, 1));
68 BEAST_EXPECT(
69 negotiateProtocolVersion("XRPL/2.2") == make_protocol(2, 2));
70 BEAST_EXPECT(
72 "RTXP/1.2, XRPL/2.2, XRPL/2.3, XRPL/999.999") ==
73 make_protocol(2, 2));
74 BEAST_EXPECT(
75 negotiateProtocolVersion("XRPL/999.999, WebSocket/1.0") ==
77 BEAST_EXPECT(negotiateProtocolVersion("") == std::nullopt);
78 }
79 }
80};
81
82BEAST_DEFINE_TESTSUITE(ProtocolVersion, overlay, ripple);
83
84} // namespace ripple
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
void check(std::string const &s, std::string const &answer)
void run() override
Runs the suite.
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
std::vector< ProtocolVersion > parseProtocolVersions(boost::beast::string_view const &value)
Parse a set of protocol versions.
constexpr ProtocolVersion make_protocol(std::uint16_t major, std::uint16_t minor)
Stream & join(Stream &s, Iter iter, Iter end, std::string const &delimiter)
Definition join.h:10
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611