rippled
RPCOverload_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  Permission to use, copy, modify, and/or distribute this software for any
6  purpose with or without fee is hereby granted, provided that the above
7  copyright notice and this permission notice appear in all copies.
8  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 //==============================================================================
17 
18 #include <ripple/beast/unit_test.h>
19 #include <ripple/core/ConfigSections.h>
20 #include <ripple/protocol/jss.h>
21 #include <test/jtx.h>
22 #include <test/jtx/JSONRPCClient.h>
23 #include <test/jtx/WSClient.h>
24 
25 namespace ripple {
26 namespace test {
27 
28 class RPCOverload_test : public beast::unit_test::suite
29 {
30 public:
31  void
32  testOverload(bool useWS)
33  {
34  testcase << "Overload " << (useWS ? "WS" : "HTTP") << " RPC client";
35  using namespace jtx;
36  Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
37  cfg->loadFromString("[" SECTION_SIGNING_SUPPORT "]\ntrue");
38  return no_admin(std::move(cfg));
39  })};
40 
41  Account const alice{"alice"};
42  Account const bob{"bob"};
43  env.fund(XRP(10000), alice, bob);
44 
45  std::unique_ptr<AbstractClient> client = useWS
46  ? makeWSClient(env.app().config())
47  : makeJSONRPCClient(env.app().config());
48 
50  tx[jss::tx_json] = pay(alice, bob, XRP(1));
51  tx[jss::secret] = toBase58(generateSeed("alice"));
52 
53  // Ask the server to repeatedly sign this transaction
54  // Signing is a resource heavy transaction, so we want the server
55  // to warn and eventually boot us.
56  bool warned = false, booted = false;
57  for (int i = 0; i < 500 && !booted; ++i)
58  {
59  auto jv = client->invoke("sign", tx);
60  if (!useWS)
61  jv = jv[jss::result];
62  // When booted, we just get a null json response
63  if (jv.isNull())
64  booted = true;
65  else if (!(jv.isMember(jss::status) &&
66  (jv[jss::status] == "success")))
67  {
68  // Don't use BEAST_EXPECT above b/c it will be called a
69  // non-deterministic number of times and the number of tests run
70  // should be deterministic
71  fail("", __FILE__, __LINE__);
72  }
73 
74  if (jv.isMember(jss::warning))
75  warned = jv[jss::warning] == jss::load;
76  }
77  BEAST_EXPECT(warned && booted);
78  }
79 
80  void
81  run() override
82  {
83  testOverload(false /* http */);
84  testOverload(true /* ws */);
85  }
86 };
87 
88 BEAST_DEFINE_TESTSUITE(RPCOverload, app, ripple);
89 
90 } // namespace test
91 } // namespace ripple
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::test::makeJSONRPCClient
std::unique_ptr< AbstractClient > makeJSONRPCClient(Config const &cfg, unsigned rpc_version)
Returns a client using JSON-RPC over HTTP/S.
Definition: JSONRPCClient.cpp:155
ripple::test::jtx::envconfig
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:49
ripple::test::RPCOverload_test::run
void run() override
Definition: RPCOverload_test.cpp:81
ripple::test::jtx::no_admin
std::unique_ptr< Config > no_admin(std::unique_ptr< Config >)
adjust config so no admin ports are enabled
Definition: envconfig.cpp:79
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::test::RPCOverload_test::testOverload
void testOverload(bool useWS)
Definition: RPCOverload_test.cpp:32
ripple::generateSeed
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition: Seed.cpp:69
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::RPCOverload_test
Definition: RPCOverload_test.cpp:28
ripple::test::jtx::pay
Json::Value pay(Account const &account, Account const &to, AnyAmount amount)
Create a payment.
Definition: pay.cpp:29
ripple::test::makeWSClient
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpc_version, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Definition: WSClient.cpp:300
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
std::unique_ptr
STL class.
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)