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 testcase("Convert protocol version to string");
37 BEAST_EXPECT(to_string(makeProtocol(1, 3)) == "XRPL/1.3");
38 BEAST_EXPECT(to_string(makeProtocol(2, 0)) == "XRPL/2.0");
39 BEAST_EXPECT(to_string(makeProtocol(2, 1)) == "XRPL/2.1");
40 BEAST_EXPECT(to_string(makeProtocol(10, 10)) == "XRPL/10.10");
41
42 {
43 testcase("Convert strings to protocol versions");
44
45 // Empty string
46 check("", "");
47
48 check("RTXP/1.1,RTXP/1.2,RTXP/1.3,XRPL/2.1,XRPL/2.0,/XRPL/3.0", "XRPL/2.0,XRPL/2.1");
49 check("RTXP/0.9,RTXP/1.01,XRPL/0.3,XRPL/2.01,websocket", "");
50 check(
51 "XRPL/2.0,XRPL/2.0,XRPL/19.4,XRPL/7.89,XRPL/XRPL/3.0,XRPL/2.01",
52 "XRPL/2.0,XRPL/7.89,XRPL/19.4");
53 check(
54 "XRPL/2.0,XRPL/3.0,XRPL/4,XRPL/,XRPL,OPT XRPL/2.2,XRPL/5.67",
55 "XRPL/2.0,XRPL/3.0,XRPL/5.67");
56 }
57
58 {
59 testcase("Protocol version negotiation");
60
61 BEAST_EXPECT(negotiateProtocolVersion("RTXP/1.2") == std::nullopt);
62 BEAST_EXPECT(
63 negotiateProtocolVersion("RTXP/1.2, XRPL/2.0, XRPL/2.1") == makeProtocol(2, 1));
64 BEAST_EXPECT(negotiateProtocolVersion("XRPL/2.2") == makeProtocol(2, 2));
65 BEAST_EXPECT(
66 negotiateProtocolVersion("RTXP/1.2, XRPL/2.2, XRPL/2.3, XRPL/999.999") ==
67 makeProtocol(2, 2));
68 BEAST_EXPECT(negotiateProtocolVersion("XRPL/999.999, WebSocket/1.0") == std::nullopt);
69 BEAST_EXPECT(negotiateProtocolVersion("") == std::nullopt);
70 }
71 }
72};
73
75
76} // namespace xrpl
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
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:10
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
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)