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