rippled
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 <ripple/basics/contract.h>
21 #include <ripple/beast/unit_test.h>
22 #include <ripple/json/json_reader.h> // Json::Reader
23 #include <ripple/protocol/ErrorCodes.h> // RPC::containsError
24 #include <ripple/protocol/InnerObjectFormats.h>
25 #include <ripple/protocol/STParsedJSON.h> // STParsedJSONObject
26 #include <test/jtx.h>
27 
28 namespace ripple {
29 
30 namespace InnerObjectFormatsUnitTestDetail {
31 
33 {
35  bool const expectFail;
36 };
37 
38 static 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 
168 class InnerObjectFormatsParsedJSON_test : public beast::unit_test::suite
169 {
170 public:
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 
204 BEAST_DEFINE_TESTSUITE(InnerObjectFormatsParsedJSON, ripple_app, ripple);
205 
206 } // namespace ripple
std::string
STL class.
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::STParsedJSONObject
Holds the serialized result of parsing an input JSON object.
Definition: STParsedJSON.h:31
Json::Reader
Unserialize a JSON document into a Value.
Definition: json_reader.h:36
ripple::STParsedJSONObject::object
std::optional< STObject > object
The STObject if the parse was successful.
Definition: STParsedJSON.h:50
ripple::InnerObjectFormatsUnitTestDetail::TestJSONTxt
Definition: InnerObjectFormats_test.cpp:32
ripple::RPC::contains_error
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Definition: ErrorCodes.cpp:194
ripple::InnerObjectFormatsUnitTestDetail::TestJSONTxt::expectFail
const bool expectFail
Definition: InnerObjectFormats_test.cpp:35
ripple::InnerObjectFormatsParsedJSON_test::run
void run() override
Definition: InnerObjectFormats_test.cpp:172
ripple::InnerObjectFormatsParsedJSON_test
Definition: InnerObjectFormats_test.cpp:168
ripple::InnerObjectFormatsUnitTestDetail::TestJSONTxt::txt
const std::string txt
Definition: InnerObjectFormats_test.cpp:34
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
Json::Reader::parse
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Definition: json_reader.cpp:74
ripple::InnerObjectFormatsUnitTestDetail::testArray
static const TestJSONTxt testArray[]
Definition: InnerObjectFormats_test.cpp:38
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
Json::Value
Represents a JSON value.
Definition: json_value.h:145