xrpld
Loading...
Searching...
No Matches
InnerObjectFormats_test.cpp
1
2#include <test/jtx/Env.h>
3
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/unit_test/suite.h>
6#include <xrpl/json/json_reader.h> // json::Reader
7#include <xrpl/json/json_value.h>
8#include <xrpl/protocol/ErrorCodes.h> // RPC::containsError
9#include <xrpl/protocol/STParsedJSON.h> // STParsedJSONObject
10
11#include <stdexcept>
12#include <string>
13
14namespace xrpl {
15
17
19{
21 bool const expectFail;
22};
23
24static TestJSONTxt const kTestArray[] = {
25
26 // Valid SignerEntry
27 {.txt = R"({
28 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
29 "SignerEntries" :
30 [
31 {
32 "SignerEntry" :
33 {
34 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
35 "SignerWeight" : 4
36 }
37 },
38 {
39 "SignerEntry" :
40 {
41 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
42 "SignerWeight" : 3
43 }
44 }
45 ],
46 "SignerQuorum" : 7,
47 "TransactionType" : "SignerListSet"
48})",
49 .expectFail = false},
50
51 // SignerEntry missing Account
52 {.txt = R"({
53 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
54 "SignerEntries" :
55 [
56 {
57 "SignerEntry" :
58 {
59 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
60 "SignerWeight" : 4
61 }
62 },
63 {
64 "SignerEntry" :
65 {
66 "SignerWeight" : 3
67 }
68 }
69 ],
70 "SignerQuorum" : 7,
71 "TransactionType" : "SignerListSet"
72})",
73 .expectFail = true},
74
75 // SignerEntry missing SignerWeight
76 {.txt = R"({
77 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
78 "SignerEntries" :
79 [
80 {
81 "SignerEntry" :
82 {
83 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
84 "SignerWeight" : 4
85 }
86 },
87 {
88 "SignerEntry" :
89 {
90 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
91 }
92 }
93 ],
94 "SignerQuorum" : 7,
95 "TransactionType" : "SignerListSet"
96})",
97 .expectFail = true},
98
99 // SignerEntry with unexpected Amount
100 {.txt = R"({
101 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
102 "SignerEntries" :
103 [
104 {
105 "SignerEntry" :
106 {
107 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
108 "SignerWeight" : 4
109 }
110 },
111 {
112 "SignerEntry" :
113 {
114 "Amount" : "1000000",
115 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
116 "SignerWeight" : 3
117 }
118 }
119 ],
120 "SignerQuorum" : 7,
121 "TransactionType" : "SignerListSet"
122})",
123 .expectFail = true},
124
125 // SignerEntry with no Account and unexpected Amount
126 {.txt = R"({
127 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
128 "SignerEntries" :
129 [
130 {
131 "SignerEntry" :
132 {
133 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
134 "SignerWeight" : 4
135 }
136 },
137 {
138 "SignerEntry" :
139 {
140 "Amount" : "10000000",
141 "SignerWeight" : 3
142 }
143 }
144 ],
145 "SignerQuorum" : 7,
146 "TransactionType" : "SignerListSet"
147})",
148 .expectFail = true},
149
150};
151
152} // namespace InnerObjectFormatsUnitTestDetail
153
155{
156public:
157 void
158 run() override
159 {
160 using namespace InnerObjectFormatsUnitTestDetail;
161
162 // Instantiate a jtx::Env so debugLog writes are exercised.
163 test::jtx::Env const env(*this);
164
165 for (auto const& test : kTestArray)
166 {
167 json::Value req;
168 json::Reader().parse(test.txt, req);
169 if (RPC::containsError(req))
170 {
172 "Internal InnerObjectFormatsParsedJSON error. Bad JSON.");
173 }
174 STParsedJSONObject const parsed("request", req);
175 bool const noObj = !parsed.object.has_value();
176 if (noObj == test.expectFail)
177 {
178 pass();
179 }
180 else
181 {
182 std::string errStr("Unexpected STParsedJSON result on:\n");
183 errStr += test.txt;
184 fail(errStr);
185 }
186 }
187 }
188};
189
190BEAST_DEFINE_TESTSUITE(InnerObjectFormatsParsedJSON, protocol, xrpl);
191
192} // namespace xrpl
A testsuite class.
Definition suite.h:50
void pass()
Record a successful test condition.
Definition suite.h:500
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:522
Unserialize a JSON document into a Value.
Definition json_reader.h:17
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Represents a JSON value.
Definition json_value.h:130
Holds the serialized result of parsing an input JSON object.
std::optional< STObject > object
The STObject if the parse was successful.
A transaction testing environment.
Definition Env.h:143
bool containsError(json::Value const &json)
Returns true if the json contains an rpc error specification.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49