rippled
ServerInfo_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2016 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/app/misc/NetworkOPs.h>
21 #include <ripple/beast/unit_test.h>
22 #include <ripple/protocol/jss.h>
23 #include <test/jtx.h>
24 
25 #include <boost/format.hpp>
26 
27 namespace ripple {
28 
29 namespace test {
30 
31 namespace validator_data {
32 static auto const public_key =
33  "nHBt9fsb4849WmZiCds4r5TXyBeQjqnH5kzPtqgMAQMgi39YZRPa";
34 
35 static auto const token =
36  "eyJ2YWxpZGF0aW9uX3NlY3JldF9rZXkiOiI5ZWQ0NWY4NjYyNDFjYzE4YTI3NDdiNT\n"
37  "QzODdjMDYyNTkwNzk3MmY0ZTcxOTAyMzFmYWE5Mzc0NTdmYTlkYWY2IiwibWFuaWZl\n"
38  "c3QiOiJKQUFBQUFGeEllMUZ0d21pbXZHdEgyaUNjTUpxQzlnVkZLaWxHZncxL3ZDeE\n"
39  "hYWExwbGMyR25NaEFrRTFhZ3FYeEJ3RHdEYklENk9NU1l1TTBGREFscEFnTms4U0tG\n"
40  "bjdNTzJmZGtjd1JRSWhBT25ndTlzQUtxWFlvdUorbDJWMFcrc0FPa1ZCK1pSUzZQU2\n"
41  "hsSkFmVXNYZkFpQnNWSkdlc2FhZE9KYy9hQVpva1MxdnltR21WcmxIUEtXWDNZeXd1\n"
42  "NmluOEhBU1FLUHVnQkQ2N2tNYVJGR3ZtcEFUSGxHS0pkdkRGbFdQWXk1QXFEZWRGdj\n"
43  "VUSmEydzBpMjFlcTNNWXl3TFZKWm5GT3I3QzBrdzJBaVR6U0NqSXpkaXRROD0ifQ==\n";
44 } // namespace validator_data
45 
46 class ServerInfo_test : public beast::unit_test::suite
47 {
48 public:
51  {
52  auto p = std::make_unique<Config>();
53  boost::format toLoad(R"rippleConfig(
54 [validator_token]
55 %1%
56 
57 [validators]
58 %2%
59 
60 [port_grpc]
61 ip = 0.0.0.0
62 port = 50051
63 
64 [port_admin]
65 ip = 0.0.0.0
66 port = 50052
67 protocol = wss2
68 admin = 127.0.0.1
69 )rippleConfig");
70 
71  p->loadFromString(boost::str(
73 
75 
76  return p;
77  }
78 
79  void
81  {
82  testcase("server_info");
83 
84  using namespace test::jtx;
85 
86  {
87  Env env(*this);
88  auto const result = env.rpc("server_info");
89  BEAST_EXPECT(!result[jss::result].isMember(jss::error));
90  BEAST_EXPECT(result[jss::result][jss::status] == "success");
91  BEAST_EXPECT(result[jss::result].isMember(jss::info));
92  }
93 
94  {
95  Env env(*this);
96 
97  // Call NetworkOPs directly and set the admin flag to false.
98  // Expect that the admin ports are not included in the result.
99  auto const result =
100  env.app().getOPs().getServerInfo(true, false, 0);
101  auto const& ports = result[jss::ports];
102  BEAST_EXPECT(ports.isArray() && ports.size() == 0);
103  }
104 
105  {
106  auto config = makeValidatorConfig();
107  auto const rpc_port =
108  (*config)["port_rpc"].get<unsigned int>("port");
109  auto const grpc_port =
110  (*config)["port_grpc"].get<unsigned int>("port");
111  auto const ws_port = (*config)["port_ws"].get<unsigned int>("port");
112  BEAST_EXPECT(grpc_port);
113  BEAST_EXPECT(rpc_port);
114  BEAST_EXPECT(ws_port);
115 
116  Env env(*this, std::move(config));
117  auto const result = env.rpc("server_info");
118  BEAST_EXPECT(!result[jss::result].isMember(jss::error));
119  BEAST_EXPECT(result[jss::result][jss::status] == "success");
120  BEAST_EXPECT(result[jss::result].isMember(jss::info));
121  BEAST_EXPECT(
122  result[jss::result][jss::info][jss::pubkey_validator] ==
124 
125  auto const& ports = result[jss::result][jss::info][jss::ports];
126  BEAST_EXPECT(ports.isArray() && ports.size() == 3);
127  for (auto const& port : ports)
128  {
129  auto const& proto = port[jss::protocol];
130  BEAST_EXPECT(proto.isArray());
131  auto const p = port[jss::port].asUInt();
132  BEAST_EXPECT(p == rpc_port || p == ws_port || p == grpc_port);
133  if (p == grpc_port)
134  {
135  BEAST_EXPECT(proto.size() == 1);
136  BEAST_EXPECT(proto[0u].asString() == "grpc");
137  }
138  if (p == rpc_port)
139  {
140  BEAST_EXPECT(proto.size() == 2);
141  BEAST_EXPECT(proto[0u].asString() == "http");
142  BEAST_EXPECT(proto[1u].asString() == "ws2");
143  }
144  if (p == ws_port)
145  {
146  BEAST_EXPECT(proto.size() == 1);
147  BEAST_EXPECT(proto[0u].asString() == "ws");
148  }
149  }
150  }
151  }
152 
153  void
155  {
156  testcase("server_definitions");
157 
158  using namespace test::jtx;
159 
160  {
161  Env env(*this);
162  auto const result = env.rpc("server_definitions");
163  BEAST_EXPECT(!result[jss::result].isMember(jss::error));
164  BEAST_EXPECT(result[jss::result][jss::status] == "success");
165  BEAST_EXPECT(result[jss::result].isMember(jss::FIELDS));
166  BEAST_EXPECT(result[jss::result].isMember(jss::LEDGER_ENTRY_TYPES));
167  BEAST_EXPECT(
168  result[jss::result].isMember(jss::TRANSACTION_RESULTS));
169  BEAST_EXPECT(result[jss::result].isMember(jss::TRANSACTION_TYPES));
170  BEAST_EXPECT(result[jss::result].isMember(jss::TYPES));
171  BEAST_EXPECT(result[jss::result].isMember(jss::hash));
172 
173  // test a random element of each result
174  // (testing the whole output would be difficult to maintain)
175 
176  {
177  auto const firstField = result[jss::result][jss::FIELDS][0u];
178  BEAST_EXPECT(firstField[0u].asString() == "Generic");
179  BEAST_EXPECT(
180  firstField[1][jss::isSerialized].asBool() == false);
181  BEAST_EXPECT(
182  firstField[1][jss::isSigningField].asBool() == false);
183  BEAST_EXPECT(firstField[1][jss::isVLEncoded].asBool() == false);
184  BEAST_EXPECT(firstField[1][jss::nth].asUInt() == 0);
185  BEAST_EXPECT(firstField[1][jss::type].asString() == "Unknown");
186  }
187 
188  BEAST_EXPECT(
189  result[jss::result][jss::LEDGER_ENTRY_TYPES]["AccountRoot"]
190  .asUInt() == 97);
191  BEAST_EXPECT(
192  result[jss::result][jss::TRANSACTION_RESULTS]["tecDIR_FULL"]
193  .asUInt() == 121);
194  BEAST_EXPECT(
195  result[jss::result][jss::TRANSACTION_TYPES]["Payment"]
196  .asUInt() == 0);
197  BEAST_EXPECT(
198  result[jss::result][jss::TYPES]["AccountID"].asUInt() == 8);
199  }
200 
201  // test providing the same hash
202  {
203  Env env(*this);
204  auto const firstResult = env.rpc("server_definitions");
205  auto const hash = firstResult[jss::result][jss::hash].asString();
206  auto const hashParam =
207  std::string("{ ") + "\"hash\": \"" + hash + "\"}";
208 
209  auto const result =
210  env.rpc("json", "server_definitions", hashParam);
211  BEAST_EXPECT(!result[jss::result].isMember(jss::error));
212  BEAST_EXPECT(result[jss::result][jss::status] == "success");
213  BEAST_EXPECT(!result[jss::result].isMember(jss::FIELDS));
214  BEAST_EXPECT(
215  !result[jss::result].isMember(jss::LEDGER_ENTRY_TYPES));
216  BEAST_EXPECT(
217  !result[jss::result].isMember(jss::TRANSACTION_RESULTS));
218  BEAST_EXPECT(!result[jss::result].isMember(jss::TRANSACTION_TYPES));
219  BEAST_EXPECT(!result[jss::result].isMember(jss::TYPES));
220  BEAST_EXPECT(result[jss::result].isMember(jss::hash));
221  }
222 
223  // test providing a different hash
224  {
225  Env env(*this);
226  std::string const hash =
227  "54296160385A27154BFA70A239DD8E8FD4CC2DB7BA32D970BA3A5B132CF749"
228  "D1";
229  auto const hashParam =
230  std::string("{ ") + "\"hash\": \"" + hash + "\"}";
231 
232  auto const result =
233  env.rpc("json", "server_definitions", hashParam);
234  BEAST_EXPECT(!result[jss::result].isMember(jss::error));
235  BEAST_EXPECT(result[jss::result][jss::status] == "success");
236  BEAST_EXPECT(result[jss::result].isMember(jss::FIELDS));
237  BEAST_EXPECT(result[jss::result].isMember(jss::LEDGER_ENTRY_TYPES));
238  BEAST_EXPECT(
239  result[jss::result].isMember(jss::TRANSACTION_RESULTS));
240  BEAST_EXPECT(result[jss::result].isMember(jss::TRANSACTION_TYPES));
241  BEAST_EXPECT(result[jss::result].isMember(jss::TYPES));
242  BEAST_EXPECT(result[jss::result].isMember(jss::hash));
243  }
244  }
245 
246  void
247  run() override
248  {
249  testServerInfo();
251  }
252 };
253 
254 BEAST_DEFINE_TESTSUITE(ServerInfo, app, ripple);
255 
256 } // namespace test
257 } // namespace ripple
ripple::test::ServerInfo_test::testServerDefinitions
void testServerDefinitions()
Definition: ServerInfo_test.cpp:154
ripple::NetworkOPs::getServerInfo
virtual Json::Value getServerInfo(bool human, bool admin, bool counters)=0
std::string
STL class.
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:242
ripple::test::validator_data::public_key
static const auto public_key
Definition: ServerInfo_test.cpp:32
ripple::test::validator_data::token
static const auto token
Definition: ServerInfo_test.cpp:35
ripple::test::ServerInfo_test::makeValidatorConfig
static std::unique_ptr< Config > makeValidatorConfig()
Definition: ServerInfo_test.cpp:50
ripple::Application::getOPs
virtual NetworkOPs & getOPs()=0
ripple::test::ServerInfo_test::run
void run() override
Definition: ServerInfo_test.cpp:247
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::ServerInfo_test
Definition: ServerInfo_test.cpp:46
std::unique_ptr
STL class.
ripple::test::setupConfigForUnitTests
void setupConfigForUnitTests(Config &config)
initializes a config object for use with jtx::Env
Definition: envconfig.cpp:39
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117
ripple::test::ServerInfo_test::testServerInfo
void testServerInfo()
Definition: ServerInfo_test.cpp:80
ripple::test::jtx::Env::rpc
Json::Value rpc(std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition: Env.h:700
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)