xrpld
Loading...
Searching...
No Matches
RPCHelpers_test.cpp
1#include <xrpld/rpc/detail/RPCHelpers.h>
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/beast/unit_test/suite.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/protocol/ErrorCodes.h>
7#include <xrpl/protocol/LedgerFormats.h>
8#include <xrpl/protocol/SField.h>
9#include <xrpl/protocol/jss.h>
10
11#include <cstdint>
12#include <set>
13#include <string>
14
15namespace xrpl::test {
16
18{
19public:
20 void
22 {
23 testcase("ChooseLedgerEntryType");
24
25 // Test no type.
27 auto result = rpc::chooseLedgerEntryType(tx);
28 BEAST_EXPECT(result.first == ::rpc::Status::kOK);
29 BEAST_EXPECT(result.second == 0);
30
31 // Test empty type.
32 tx[jss::type] = "";
33 result = rpc::chooseLedgerEntryType(tx);
34 BEAST_EXPECT(result.first == ::rpc::Status(RpcInvalidParams, "Invalid field 'type'."));
35 BEAST_EXPECT(result.second == 0);
36
37 // Test type using canonical name in mixedcase.
38 tx[jss::type] = "MPTokenIssuance";
39 result = rpc::chooseLedgerEntryType(tx);
40 BEAST_EXPECT(result.first == ::rpc::Status::kOK);
41 BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);
42
43 // Test type using canonical name in lowercase.
44 tx[jss::type] = "mptokenissuance";
45 result = rpc::chooseLedgerEntryType(tx);
46 BEAST_EXPECT(result.first == ::rpc::Status::kOK);
47 BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);
48
49 // Test type using RPC name with exact match.
50 tx[jss::type] = "mpt_issuance";
51 result = rpc::chooseLedgerEntryType(tx);
52 BEAST_EXPECT(result.first == ::rpc::Status::kOK);
53 BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);
54
55 // Test type using RPC name with inexact match.
56 tx[jss::type] = "MPT_Issuance";
57 result = rpc::chooseLedgerEntryType(tx);
58 BEAST_EXPECT(result.first == ::rpc::Status(RpcInvalidParams, "Invalid field 'type'."));
59 BEAST_EXPECT(result.second == 0);
60
61 // Test invalid type.
62 tx[jss::type] = 1234;
63 result = rpc::chooseLedgerEntryType(tx);
64 BEAST_EXPECT(
65 result.first == ::rpc::Status(RpcInvalidParams, "Invalid field 'type', not string."));
66 BEAST_EXPECT(result.second == 0);
67
68 // Test unknown type.
69 tx[jss::type] = "unknown";
70 result = rpc::chooseLedgerEntryType(tx);
71 BEAST_EXPECT(result.first == ::rpc::Status(RpcInvalidParams, "Invalid field 'type'."));
72 BEAST_EXPECT(result.second == 0);
73 }
74
75 void
77 {
78 // Every UINT64 sf*Node field must be classified: either it is an
79 // owner-directory page hint (rpc::isOwnerDirNodeField()) or it
80 // belongs to a book directory (exclusion set below). Adding a new
81 // sf*Node UINT64 without updating one of these lists trips this
82 // test.
83 testcase("Owner-directory Node fields coverage");
84
85 // Book-directory page hints, not owner-directory. sfBookNode: offer
86 // book. sfNFTokenOfferNode: NFT bid/ask book.
87 std::set<SField const*> const nonOwnerDirNodes{
88 &sfBookNode,
89 &sfNFTokenOfferNode,
90 };
91
92 for (auto const& [_, sf] : SField::getKnownCodeToField())
93 {
94 if (sf->fieldType != STI_UINT64 || !sf->getName().ends_with("Node"))
95 continue;
96 BEAST_EXPECTS(
97 rpc::isOwnerDirNodeField(*sf) || nonOwnerDirNodes.contains(sf),
98 "sf" + sf->getName() +
99 ": add to kOwnerDirNodeFields (owner-dir page hint) "
100 "or to nonOwnerDirNodes (book-dir).");
101 }
102 }
103
104 void
106 {
107 testcase("ParseAccountMarker");
108
109 auto const index =
110 std::string{"1B8590C01B0006EDFA9ED60296DD052DC5E90F99659B25014D08E1BC983515BC"};
111 uint256 expected;
112 BEAST_EXPECT(expected.parseHex(index));
113
114 auto const expectParsed = [&](std::string const& marker, std::uint64_t hint) {
115 auto const parsed = rpc::parseAccountMarker(marker);
116 BEAST_EXPECTS(parsed.has_value(), marker);
117 if (parsed.has_value())
118 {
119 BEAST_EXPECTS(parsed->first == expected, marker);
120 BEAST_EXPECTS(parsed->second == hint, marker);
121 }
122 };
123
124 expectParsed(index + ",7", 7);
125 expectParsed(index + ",0", 0);
126 expectParsed(index + ",+7", 7);
127 expectParsed(index + ",7,ignored", 7);
128 expectParsed(index + ",18446744073709551615", 18446744073709551615u);
129
130 for (auto const& marker : {
131 std::string{},
132 index,
133 index + ",",
134 index + ",abc",
135 index + ",7x",
136 index + ",-1",
137 index + ",+",
138 index + ",++7",
139 index + ",18446744073709551616",
140 std::string{"NOTHEX,7"},
141 std::string{",7"},
142 })
143 {
144 BEAST_EXPECTS(!rpc::parseAccountMarker(marker).has_value(), marker);
145 }
146 }
147
148 void
155};
156
158
159} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:526
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:291
void run() override
Runs the suite.
T contains(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
The api_version numbers this server serves.
Definition ApiVersion.h:26
bool isOwnerDirNodeField(SField const &field)
Checks whether an SField is a UINT64 sf*Node owner-directory page-hint field probed by isRelatedToAcc...
std::optional< std::pair< uint256, std::uint64_t > > parseAccountMarker(std::string_view marker)
Splits an account marker into the entry to start after and its start hint.
std::pair<::rpc::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const &params)
Chooses the ledger entry type based on RPC parameters.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInvalidParams
Definition ErrorCodes.h:67
UInt256 uint256
Definition base_uint.h:587