xrpld
Loading...
Searching...
No Matches
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
8namespace xrpl {
9
11{
12private:
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
32public:
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(
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
85
86} // namespace xrpl
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
void run() override
Runs the suite.
void check(std::string const &s, std::string const &answer)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::vector< ProtocolVersion > parseProtocolVersions(boost::beast::string_view const &value)
Parse a set of protocol versions.
std::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
Stream & join(Stream &s, Iter iter, Iter end, std::string_view delimiter)
Definition join.h:12
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
constexpr ProtocolVersion makeProtocol(std::uint16_t major, std::uint16_t minor)
std::pair< std::uint16_t, std::uint16_t > ProtocolVersion
Represents a particular version of the peer-to-peer protocol.
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)