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  using namespace test::jtx;
83 
84  {
85  Env env(*this);
86  auto const result = env.rpc("server_info");
87  BEAST_EXPECT(!result[jss::result].isMember(jss::error));
88  BEAST_EXPECT(result[jss::result][jss::status] == "success");
89  BEAST_EXPECT(result[jss::result].isMember(jss::info));
90  }
91 
92  {
93  Env env(*this);
94 
95  // Call NetworkOPs directly and set the admin flag to false.
96  // Expect that the admin ports are not included in the result.
97  auto const result =
98  env.app().getOPs().getServerInfo(true, false, 0);
99  auto const& ports = result[jss::ports];
100  BEAST_EXPECT(ports.isArray() && ports.size() == 0);
101  }
102 
103  {
104  auto config = makeValidatorConfig();
105  auto const rpc_port =
106  (*config)["port_rpc"].get<unsigned int>("port");
107  auto const grpc_port =
108  (*config)["port_grpc"].get<unsigned int>("port");
109  auto const ws_port = (*config)["port_ws"].get<unsigned int>("port");
110  BEAST_EXPECT(grpc_port);
111  BEAST_EXPECT(rpc_port);
112  BEAST_EXPECT(ws_port);
113 
114  Env env(*this, std::move(config));
115  auto const result = env.rpc("server_info");
116  BEAST_EXPECT(!result[jss::result].isMember(jss::error));
117  BEAST_EXPECT(result[jss::result][jss::status] == "success");
118  BEAST_EXPECT(result[jss::result].isMember(jss::info));
119  BEAST_EXPECT(
120  result[jss::result][jss::info][jss::pubkey_validator] ==
122 
123  auto const& ports = result[jss::result][jss::info][jss::ports];
124  BEAST_EXPECT(ports.isArray() && ports.size() == 3);
125  for (auto const& port : ports)
126  {
127  auto const& proto = port[jss::protocol];
128  BEAST_EXPECT(proto.isArray());
129  auto const p = port[jss::port].asUInt();
130  BEAST_EXPECT(p == rpc_port || p == ws_port || p == grpc_port);
131  if (p == grpc_port)
132  {
133  BEAST_EXPECT(proto.size() == 1);
134  BEAST_EXPECT(proto[0u].asString() == "grpc");
135  }
136  if (p == rpc_port)
137  {
138  BEAST_EXPECT(proto.size() == 2);
139  BEAST_EXPECT(proto[0u].asString() == "http");
140  BEAST_EXPECT(proto[1u].asString() == "ws2");
141  }
142  if (p == ws_port)
143  {
144  BEAST_EXPECT(proto.size() == 1);
145  BEAST_EXPECT(proto[0u].asString() == "ws");
146  }
147  }
148  }
149  }
150 
151  void
152  run() override
153  {
154  testServerInfo();
155  }
156 };
157 
158 BEAST_DEFINE_TESTSUITE(ServerInfo, app, ripple);
159 
160 } // namespace test
161 } // namespace ripple
ripple::NetworkOPs::getServerInfo
virtual Json::Value getServerInfo(bool human, bool admin, bool counters)=0
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:241
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:152
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:116
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:699
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)