rippled
Status_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/rpc/Status.h>
23 #include <algorithm>
24 
25 namespace ripple {
26 namespace RPC {
27 
28 class codeString_test : public beast::unit_test::suite
29 {
30 private:
31  template <typename Type>
33  codeString(Type t)
34  {
35  return Status(t).codeString();
36  }
37 
38  void
40  {
41  testcase("OK");
42  {
43  auto s = codeString(Status());
44  expect(s.empty(), "String for OK status");
45  }
46 
47  {
48  auto s = codeString(Status::OK);
49  expect(s.empty(), "String for OK status");
50  }
51 
52  {
53  auto s = codeString(0);
54  expect(s.empty(), "String for 0 status");
55  }
56 
57  {
58  auto s = codeString(tesSUCCESS);
59  expect(s.empty(), "String for tesSUCCESS");
60  }
61 
62  {
63  auto s = codeString(rpcSUCCESS);
64  expect(s.empty(), "String for rpcSUCCESS");
65  }
66  }
67 
68  void
70  {
71  testcase("error");
72  {
73  auto s = codeString(23);
74  expect(s == "23", s);
75  }
76 
77  {
78  auto s = codeString(temBAD_AMOUNT);
79  expect(s == "temBAD_AMOUNT: Can only send positive amounts.", s);
80  }
81 
82  {
83  auto s = codeString(rpcBAD_SYNTAX);
84  expect(s == "badSyntax: Syntax error.", s);
85  }
86  }
87 
88 public:
89  void
90  run() override
91  {
92  test_OK();
93  test_error();
94  }
95 };
96 
97 BEAST_DEFINE_TESTSUITE(codeString, Status, RPC);
98 
99 class fillJson_test : public beast::unit_test::suite
100 {
101 private:
103 
104  template <typename Type>
105  void
106  fillJson(Type t)
107  {
108  value_.clear();
109  Status(t).fillJson(value_);
110  }
111 
112  void
114  {
115  testcase("OK");
116  fillJson(Status());
117  expect(!value_, "Value for empty status");
118 
119  fillJson(0);
120  expect(!value_, "Value for 0 status");
121 
123  expect(!value_, "Value for OK status");
124 
126  expect(!value_, "Value for tesSUCCESS");
127 
129  expect(!value_, "Value for rpcSUCCESS");
130  }
131 
132  template <typename Type>
133  void
135  std::string const& label,
136  Type status,
137  Status::Strings messages,
138  std::string const& message)
139  {
140  value_.clear();
141  fillJson(Status(status, messages));
142 
143  auto prefix = label + ": ";
144  expect(bool(value_), prefix + "No value");
145 
146  auto error = value_[jss::error];
147  expect(bool(error), prefix + "No error.");
148 
149  auto code = error[jss::code].asInt();
150  expect(
151  status == code,
152  prefix + "Wrong status " + std::to_string(code) +
153  " != " + std::to_string(status));
154 
155  auto m = error[jss::message].asString();
156  expect(m == message, m + " != " + message);
157 
158  auto d = error[jss::data];
159  size_t s1 = d.size(), s2 = messages.size();
160  expect(
161  s1 == s2,
162  prefix + "Data sizes differ " + std::to_string(s1) +
163  " != " + std::to_string(s2));
164  for (auto i = 0; i < std::min(s1, s2); ++i)
165  {
166  auto ds = d[i].asString();
167  expect(ds == messages[i], prefix + ds + " != " + messages[i]);
168  }
169  }
170 
171  void
173  {
174  testcase("error");
175  expectFill(
176  "temBAD_AMOUNT",
178  {},
179  "temBAD_AMOUNT: Can only send positive amounts.");
180 
181  expectFill(
182  "rpcBAD_SYNTAX",
184  {"An error.", "Another error."},
185  "badSyntax: Syntax error.");
186 
187  expectFill("integer message", 23, {"Stuff."}, "23");
188  }
189 
190  void
192  {
193  testcase("throw");
194  try
195  {
196  Throw<Status>(Status(temBAD_PATH, {"path=sdcdfd"}));
197  }
198  catch (Status const& s)
199  {
200  expect(s.toTER() == temBAD_PATH, "temBAD_PATH wasn't thrown");
201  auto msgs = s.messages();
202  expect(msgs.size() == 1, "Wrong number of messages");
203  expect(msgs[0] == "path=sdcdfd", msgs[0]);
204  }
205  catch (std::exception const&)
206  {
207  expect(false, "Didn't catch a Status");
208  }
209  }
210 
211 public:
212  void
213  run() override
214  {
215  test_OK();
216  test_error();
217  test_throw();
218  }
219 };
220 
221 BEAST_DEFINE_TESTSUITE(fillJson, Status, RPC);
222 
223 } // namespace RPC
224 } // namespace ripple
ripple::RPC::codeString_test::test_OK
void test_OK()
Definition: Status_test.cpp:39
ripple::RPC::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountLinesRPC, app, ripple)
ripple::RPC::fillJson_test::fillJson
void fillJson(Type t)
Definition: Status_test.cpp:106
std::string
STL class.
std::exception
STL class.
std::vector< std::string >
std::vector::size
T size(T... args)
ripple::RPC::Status::codeString
std::string codeString() const
Definition: Status.cpp:27
ripple::RPC::fillJson_test::test_error
void test_error()
Definition: Status_test.cpp:172
ripple::RPC::fillJson_test::test_throw
void test_throw()
Definition: Status_test.cpp:191
ripple::RPC::Status::messages
Strings const & messages() const
Definition: Status.h:127
ripple::temBAD_PATH
@ temBAD_PATH
Definition: TER.h:94
algorithm
ripple::RPC::codeString_test
Definition: Status_test.cpp:28
ripple::RPC::Status::OK
static constexpr Code OK
Definition: Status.h:46
ripple::rpcSUCCESS
@ rpcSUCCESS
Definition: ErrorCodes.h:44
ripple::RPC::fillJson_test
Definition: Status_test.cpp:99
ripple::RPC::fillJson_test::value_
Json::Value value_
Definition: Status_test.cpp:102
ripple::RPC::codeString_test::codeString
std::string codeString(Type t)
Definition: Status_test.cpp:33
std::to_string
T to_string(T... args)
ripple::temBAD_AMOUNT
@ temBAD_AMOUNT
Definition: TER.h:87
ripple::RPC::fillJson_test::expectFill
void expectFill(std::string const &label, Type status, Status::Strings messages, std::string const &message)
Definition: Status_test.cpp:134
ripple::RPC::Status
Status represents the results of an operation that might fail.
Definition: Status.h:39
std::min
T min(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::Status::toTER
TER toTER() const
Returns the Status as a TER.
Definition: Status.h:96
ripple::rpcBAD_SYNTAX
@ rpcBAD_SYNTAX
Definition: ErrorCodes.h:46
Json::Value::clear
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:753
ripple::RPC::fillJson_test::test_OK
void test_OK()
Definition: Status_test.cpp:113
ripple::RPC::codeString_test::test_error
void test_error()
Definition: Status_test.cpp:69
ripple::RPC::codeString_test::run
void run() override
Definition: Status_test.cpp:90
ripple::RPC::Status::fillJson
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition: Status.cpp:59
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:222
ripple::RPC::fillJson_test::run
void run() override
Definition: Status_test.cpp:213
Json::Value
Represents a JSON value.
Definition: json_value.h:145