xrpld
Loading...
Searching...
No Matches
RPCHelpers_test.cpp
1#include <xrpld/rpc/Status.h>
2#include <xrpld/rpc/detail/RPCHelpers.h>
3
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 <set>
12#include <string>
13
14namespace xrpl::test {
15
17{
18public:
19 void
21 {
22 testcase("ChooseLedgerEntryType");
23
24 // Test no type.
26 auto result = rpc::chooseLedgerEntryType(tx);
27 BEAST_EXPECT(result.first == rpc::Status::kOK);
28 BEAST_EXPECT(result.second == 0);
29
30 // Test empty type.
31 tx[jss::type] = "";
32 result = rpc::chooseLedgerEntryType(tx);
33 BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams});
34 BEAST_EXPECT(result.second == 0);
35
36 // Test type using canonical name in mixedcase.
37 tx[jss::type] = "MPTokenIssuance";
38 result = rpc::chooseLedgerEntryType(tx);
39 BEAST_EXPECT(result.first == rpc::Status::kOK);
40 BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);
41
42 // Test type using canonical name in lowercase.
43 tx[jss::type] = "mptokenissuance";
44 result = rpc::chooseLedgerEntryType(tx);
45 BEAST_EXPECT(result.first == rpc::Status::kOK);
46 BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);
47
48 // Test type using RPC name with exact match.
49 tx[jss::type] = "mpt_issuance";
50 result = rpc::chooseLedgerEntryType(tx);
51 BEAST_EXPECT(result.first == rpc::Status::kOK);
52 BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE);
53
54 // Test type using RPC name with inexact match.
55 tx[jss::type] = "MPT_Issuance";
56 result = rpc::chooseLedgerEntryType(tx);
57 BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams});
58 BEAST_EXPECT(result.second == 0);
59
60 // Test invalid type.
61 tx[jss::type] = 1234;
62 result = rpc::chooseLedgerEntryType(tx);
63 BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams});
64 BEAST_EXPECT(result.second == 0);
65
66 // Test unknown type.
67 tx[jss::type] = "unknown";
68 result = rpc::chooseLedgerEntryType(tx);
69 BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams});
70 BEAST_EXPECT(result.second == 0);
71 }
72
73 void
75 {
76 // Every UINT64 sf*Node field must be classified: either it is an
77 // owner-directory page hint (rpc::isOwnerDirNodeField()) or it
78 // belongs to a book directory (exclusion set below). Adding a new
79 // sf*Node UINT64 without updating one of these lists trips this
80 // test.
81 testcase("Owner-directory Node fields coverage");
82
83 // Book-directory page hints, not owner-directory. sfBookNode: offer
84 // book. sfNFTokenOfferNode: NFT bid/ask book.
85 std::set<SField const*> const nonOwnerDirNodes{
86 &sfBookNode,
87 &sfNFTokenOfferNode,
88 };
89
90 for (auto const& [_, sf] : SField::getKnownCodeToField())
91 {
92 if (sf->fieldType != STI_UINT64 || !sf->getName().ends_with("Node"))
93 continue;
94 BEAST_EXPECTS(
95 rpc::isOwnerDirNodeField(*sf) || nonOwnerDirNodes.contains(sf),
96 "sf" + sf->getName() +
97 ": add to kOwnerDirNodeFields (owner-dir page hint) "
98 "or to nonOwnerDirNodes (book-dir).");
99 }
100 }
101
102 void
108};
109
111
112} // 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:117
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:292
void run() override
Runs the suite.
T contains(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
API version numbers used in later API versions.
Definition ApiVersion.h:36
std::pair< rpc::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const &params)
Chooses the ledger entry type based on RPC parameters.
bool isOwnerDirNodeField(SField const &field)
Checks whether an SField is a UINT64 sf*Node owner-directory page-hint field probed by isRelatedToAcc...
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Status represents the results of an operation that might fail.
Definition Status.h:27
static constexpr Code kOK
Definition Status.h:33