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/protocol/InnerObjectFormats.h>
22 #include <ripple/protocol/ErrorCodes.h> // RPC::containsError
23 #include <ripple/json/json_reader.h> // Json::Reader
24 #include <ripple/protocol/STParsedJSON.h> // STParsedJSONObject
25 #include <ripple/beast/unit_test.h>
26 #include <test/jtx.h>
27 
28 namespace ripple {
29 
30 namespace InnerObjectFormatsUnitTestDetail
31 {
32 
34 {
36  bool const expectFail;
37 };
38 
39 static TestJSONTxt const testArray[] =
40 {
41 
42 // Valid SignerEntry
43 {R"({
44  "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
45  "SignerEntries" :
46  [
47  {
48  "SignerEntry" :
49  {
50  "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
51  "SignerWeight" : 4
52  }
53  },
54  {
55  "SignerEntry" :
56  {
57  "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
58  "SignerWeight" : 3
59  }
60  }
61  ],
62  "SignerQuorum" : 7,
63  "TransactionType" : "SignerListSet"
64 })", false
65 },
66 
67 // SignerEntry missing Account
68 {R"({
69  "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
70  "SignerEntries" :
71  [
72  {
73  "SignerEntry" :
74  {
75  "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
76  "SignerWeight" : 4
77  }
78  },
79  {
80  "SignerEntry" :
81  {
82  "SignerWeight" : 3
83  }
84  }
85  ],
86  "SignerQuorum" : 7,
87  "TransactionType" : "SignerListSet"
88 })", true
89 },
90 
91 // SignerEntry missing SignerWeight
92 {R"({
93  "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
94  "SignerEntries" :
95  [
96  {
97  "SignerEntry" :
98  {
99  "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
100  "SignerWeight" : 4
101  }
102  },
103  {
104  "SignerEntry" :
105  {
106  "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
107  }
108  }
109  ],
110  "SignerQuorum" : 7,
111  "TransactionType" : "SignerListSet"
112 })", true
113 },
114 
115 // SignerEntry with unexpected Amount
116 {R"({
117  "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
118  "SignerEntries" :
119  [
120  {
121  "SignerEntry" :
122  {
123  "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
124  "SignerWeight" : 4
125  }
126  },
127  {
128  "SignerEntry" :
129  {
130  "Amount" : "1000000",
131  "Account" : "rPcNzota6B8YBokhYtcTNqQVCngtbnWfux",
132  "SignerWeight" : 3
133  }
134  }
135  ],
136  "SignerQuorum" : 7,
137  "TransactionType" : "SignerListSet"
138 })", true
139 },
140 
141 // SignerEntry with no Account and unexpected Amount
142 {R"({
143  "Account" : "rDg53Haik2475DJx8bjMDSDPj4VX7htaMd",
144  "SignerEntries" :
145  [
146  {
147  "SignerEntry" :
148  {
149  "Account" : "rnUy2SHTrB9DubsPmkJZUXTf5FcNDGrYEA",
150  "SignerWeight" : 4
151  }
152  },
153  {
154  "SignerEntry" :
155  {
156  "Amount" : "10000000",
157  "SignerWeight" : 3
158  }
159  }
160  ],
161  "SignerQuorum" : 7,
162  "TransactionType" : "SignerListSet"
163 })", true
164 },
165 
166 };
167 
168 } // namespace InnerObjectFormatsUnitTestDetail
169 
170 
171 class InnerObjectFormatsParsedJSON_test : public beast::unit_test::suite
172 {
173 public:
174  void run() override
175  {
176  using namespace InnerObjectFormatsUnitTestDetail;
177 
178  // Instantiate a jtx::Env so debugLog writes are exercised.
179  test::jtx::Env env (*this);
180 
181  for (auto const& test : testArray)
182  {
183  Json::Value req;
184  Json::Reader ().parse (test.txt, req);
185  if (RPC::contains_error (req))
186  {
187  Throw<std::runtime_error> (
188  "Internal InnerObjectFormatsParsedJSON error. Bad JSON.");
189  }
190  STParsedJSONObject parsed ("request", req);
191  bool const noObj = parsed.object == boost::none;
192  if ( noObj == test.expectFail )
193  {
194  pass ();
195  }
196  else
197  {
198  std::string errStr ("Unexpected STParsedJSON result on:\n");
199  errStr += test.txt;
200  fail (errStr);
201  }
202  }
203  }
204 };
205 
206 BEAST_DEFINE_TESTSUITE(InnerObjectFormatsParsedJSON,ripple_app,ripple);
207 
208 } // 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::InnerObjectFormatsUnitTestDetail::TestJSONTxt
Definition: InnerObjectFormats_test.cpp:33
ripple::RPC::contains_error
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Definition: ErrorCodes.cpp:196
ripple::InnerObjectFormatsUnitTestDetail::TestJSONTxt::expectFail
const bool expectFail
Definition: InnerObjectFormats_test.cpp:36
ripple::InnerObjectFormatsParsedJSON_test::run
void run() override
Definition: InnerObjectFormats_test.cpp:174
ripple::InnerObjectFormatsParsedJSON_test
Definition: InnerObjectFormats_test.cpp:171
ripple::STParsedJSONObject::object
boost::optional< STObject > object
The STObject if the parse was successful.
Definition: STParsedJSON.h:49
ripple::InnerObjectFormatsUnitTestDetail::TestJSONTxt::txt
const std::string txt
Definition: InnerObjectFormats_test.cpp:35
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:76
ripple::InnerObjectFormatsUnitTestDetail::testArray
static const TestJSONTxt testArray[]
Definition: InnerObjectFormats_test.cpp:39
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117
Json::Value
Represents a JSON value.
Definition: json_value.h:141