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 {
28 .txt = R"({
29 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
30 "SignerEntries" :
31 [
32 {
33 "SignerEntry" :
34 {
35 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
36 "SignerWeight" : 4
37 }
38 },
39 {
40 "SignerEntry" :
41 {
42 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
43 "SignerWeight" : 3
44 }
45 }
46 ],
47 "SignerQuorum" : 7,
48 "TransactionType" : "SignerListSet"
49})",
50 .expectFail = false,
51 },
52
53 // SignerEntry missing Account
54 {
55 .txt = R"({
56 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
57 "SignerEntries" :
58 [
59 {
60 "SignerEntry" :
61 {
62 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
63 "SignerWeight" : 4
64 }
65 },
66 {
67 "SignerEntry" :
68 {
69 "SignerWeight" : 3
70 }
71 }
72 ],
73 "SignerQuorum" : 7,
74 "TransactionType" : "SignerListSet"
75})",
76 .expectFail = true,
77 },
78
79 // SignerEntry missing SignerWeight
80 {
81 .txt = R"({
82 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
83 "SignerEntries" :
84 [
85 {
86 "SignerEntry" :
87 {
88 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
89 "SignerWeight" : 4
90 }
91 },
92 {
93 "SignerEntry" :
94 {
95 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
96 }
97 }
98 ],
99 "SignerQuorum" : 7,
100 "TransactionType" : "SignerListSet"
101})",
102 .expectFail = true,
103 },
104
105 // SignerEntry with unexpected Amount
106 {
107 .txt = R"({
108 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
109 "SignerEntries" :
110 [
111 {
112 "SignerEntry" :
113 {
114 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
115 "SignerWeight" : 4
116 }
117 },
118 {
119 "SignerEntry" :
120 {
121 "Amount" : "1000000",
122 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
123 "SignerWeight" : 3
124 }
125 }
126 ],
127 "SignerQuorum" : 7,
128 "TransactionType" : "SignerListSet"
129})",
130 .expectFail = true,
131 },
132
133 // SignerEntry with no Account and unexpected Amount
134 {
135 .txt = R"({
136 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
137 "SignerEntries" :
138 [
139 {
140 "SignerEntry" :
141 {
142 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
143 "SignerWeight" : 4
144 }
145 },
146 {
147 "SignerEntry" :
148 {
149 "Amount" : "10000000",
150 "SignerWeight" : 3
151 }
152 }
153 ],
154 "SignerQuorum" : 7,
155 "TransactionType" : "SignerListSet"
156})",
157 .expectFail = true,
158 },
159
160};
161
162} // namespace inner_object_formats_unit_test_detail
163
165{
166public:
167 void
168 run() override
169 {
171
172 // Instantiate a jtx::Env so debugLog writes are exercised.
173 test::jtx::Env const env(*this);
174
175 for (auto const& test : kTestArray)
176 {
177 json::Value req;
178 json::Reader().parse(test.txt, req);
179 if (rpc::containsError(req))
180 {
182 "Internal InnerObjectFormatsParsedJSON error. Bad JSON.");
183 }
184 STParsedJSONObject const parsed("request", req);
185 bool const noObj = !parsed.object.has_value();
186 if (noObj == test.expectFail)
187 {
188 pass();
189 }
190 else
191 {
192 std::string errStr("Unexpected STParsedJSON result on:\n");
193 errStr += test.txt;
194 fail(errStr);
195 }
196 }
197 }
198};
199
200BEAST_DEFINE_TESTSUITE(InnerObjectFormatsParsedJSON, protocol, xrpl);
201
202} // namespace xrpl
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:532
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
Unserialize a JSON document into a Value.
Definition json_reader.h:20
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Represents a JSON value.
Definition json_value.h:121
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:161
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:52