rippled
Loading...
Searching...
No Matches
InnerObjectFormats_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx.h>
21
22#include <xrpl/basics/contract.h>
23#include <xrpl/beast/unit_test.h>
24#include <xrpl/json/json_reader.h> // Json::Reader
25#include <xrpl/protocol/ErrorCodes.h> // RPC::containsError
26#include <xrpl/protocol/STParsedJSON.h> // STParsedJSONObject
27
28namespace ripple {
29
30namespace InnerObjectFormatsUnitTestDetail {
31
33{
35 bool const expectFail;
36};
37
38static TestJSONTxt const testArray[] = {
39
40 // Valid SignerEntry
41 {R"({
42 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
43 "SignerEntries" :
44 [
45 {
46 "SignerEntry" :
47 {
48 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
49 "SignerWeight" : 4
50 }
51 },
52 {
53 "SignerEntry" :
54 {
55 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
56 "SignerWeight" : 3
57 }
58 }
59 ],
60 "SignerQuorum" : 7,
61 "TransactionType" : "SignerListSet"
62})",
63 false},
64
65 // SignerEntry missing Account
66 {R"({
67 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
68 "SignerEntries" :
69 [
70 {
71 "SignerEntry" :
72 {
73 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
74 "SignerWeight" : 4
75 }
76 },
77 {
78 "SignerEntry" :
79 {
80 "SignerWeight" : 3
81 }
82 }
83 ],
84 "SignerQuorum" : 7,
85 "TransactionType" : "SignerListSet"
86})",
87 true},
88
89 // SignerEntry missing SignerWeight
90 {R"({
91 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
92 "SignerEntries" :
93 [
94 {
95 "SignerEntry" :
96 {
97 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
98 "SignerWeight" : 4
99 }
100 },
101 {
102 "SignerEntry" :
103 {
104 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
105 }
106 }
107 ],
108 "SignerQuorum" : 7,
109 "TransactionType" : "SignerListSet"
110})",
111 true},
112
113 // SignerEntry with unexpected Amount
114 {R"({
115 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
116 "SignerEntries" :
117 [
118 {
119 "SignerEntry" :
120 {
121 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
122 "SignerWeight" : 4
123 }
124 },
125 {
126 "SignerEntry" :
127 {
128 "Amount" : "1000000",
129 "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
130 "SignerWeight" : 3
131 }
132 }
133 ],
134 "SignerQuorum" : 7,
135 "TransactionType" : "SignerListSet"
136})",
137 true},
138
139 // SignerEntry with no Account and unexpected Amount
140 {R"({
141 "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
142 "SignerEntries" :
143 [
144 {
145 "SignerEntry" :
146 {
147 "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
148 "SignerWeight" : 4
149 }
150 },
151 {
152 "SignerEntry" :
153 {
154 "Amount" : "10000000",
155 "SignerWeight" : 3
156 }
157 }
158 ],
159 "SignerQuorum" : 7,
160 "TransactionType" : "SignerListSet"
161})",
162 true},
163
164};
165
166} // namespace InnerObjectFormatsUnitTestDetail
167
169{
170public:
171 void
172 run() override
173 {
174 using namespace InnerObjectFormatsUnitTestDetail;
175
176 // Instantiate a jtx::Env so debugLog writes are exercised.
177 test::jtx::Env env(*this);
178
179 for (auto const& test : testArray)
180 {
181 Json::Value req;
182 Json::Reader().parse(test.txt, req);
183 if (RPC::contains_error(req))
184 {
185 Throw<std::runtime_error>(
186 "Internal InnerObjectFormatsParsedJSON error. Bad JSON.");
187 }
188 STParsedJSONObject parsed("request", req);
189 bool const noObj = !parsed.object.has_value();
190 if (noObj == test.expectFail)
191 {
192 pass();
193 }
194 else
195 {
196 std::string errStr("Unexpected STParsedJSON result on:\n");
197 errStr += test.txt;
198 fail(errStr);
199 }
200 }
201 }
202};
203
204BEAST_DEFINE_TESTSUITE(InnerObjectFormatsParsedJSON, ripple_app, ripple);
205
206} // namespace ripple
Unserialize a JSON document into a Value.
Definition: json_reader.h:39
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Definition: json_reader.cpp:78
Represents a JSON value.
Definition: json_value.h:150
A testsuite class.
Definition: suite.h:55
void pass()
Record a successful test condition.
Definition: suite.h:511
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition: suite.h:533
Holds the serialized result of parsing an input JSON object.
Definition: STParsedJSON.h:33
std::optional< STObject > object
The STObject if the parse was successful.
Definition: STParsedJSON.h:51
A transaction testing environment.
Definition: Env.h:121
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Definition: ErrorCodes.cpp:204
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26