xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
rpc
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
15
namespace
xrpl::test
{
16
17
class
RPCHelpers_test
:
public
beast::unit_test::Suite
18
{
19
public
:
20
void
21
testChooseLedgerEntryType
()
22
{
23
testcase
(
"ChooseLedgerEntryType"
);
24
25
// Test no type.
26
json::Value
tx =
json::ValueType::Object
;
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
76
testOwnerDirNodeFieldsCoverage
()
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
105
testParseAccountMarker
()
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
149
run
()
override
150
{
151
testChooseLedgerEntryType
();
152
testOwnerDirNodeFieldsCoverage
();
153
testParseAccountMarker
();
154
}
155
};
156
157
BEAST_DEFINE_TESTSUITE
(RPCHelpers,
rpc
,
xrpl
);
158
159
}
// namespace xrpl::test
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
json::Value
Represents a JSON value.
Definition
json_value.h:121
xrpl::BaseUInt::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition
base_uint.h:526
xrpl::SField::getKnownCodeToField
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition
SField.h:291
xrpl::test::RPCHelpers_test
Definition
RPCHelpers_test.cpp:18
xrpl::test::RPCHelpers_test::run
void run() override
Runs the suite.
Definition
RPCHelpers_test.cpp:149
xrpl::test::RPCHelpers_test::testChooseLedgerEntryType
void testChooseLedgerEntryType()
Definition
RPCHelpers_test.cpp:21
xrpl::test::RPCHelpers_test::testParseAccountMarker
void testParseAccountMarker()
Definition
RPCHelpers_test.cpp:105
xrpl::test::RPCHelpers_test::testOwnerDirNodeFieldsCoverage
void testOwnerDirNodeFieldsCoverage()
Definition
RPCHelpers_test.cpp:76
std::set::contains
T contains(T... args)
cstdint
std::uint64_t
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:33
xrpl::rpc
The api_version numbers this server serves.
Definition
ApiVersion.h:26
xrpl::rpc::isOwnerDirNodeField
bool isOwnerDirNodeField(SField const &field)
Checks whether an SField is a UINT64 sf*Node owner-directory page-hint field probed by isRelatedToAcc...
Definition
RPCHelpers.cpp:91
xrpl::rpc::parseAccountMarker
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.
Definition
RPCHelpers.cpp:97
xrpl::rpc::chooseLedgerEntryType
std::pair<::rpc::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const ¶ms)
Chooses the ledger entry type based on RPC parameters.
Definition
RPCHelpers.cpp:415
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::RpcInvalidParams
@ RpcInvalidParams
Definition
ErrorCodes.h:67
xrpl::uint256
UInt256 uint256
Definition
base_uint.h:587
set
string
Generated by
1.17.0