rippled
Roles_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2019 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/beast/unit_test.h>
21 #include <ripple/protocol/ErrorCodes.h>
22 #include <ripple/protocol/jss.h>
23 #include <string>
24 #include <test/jtx.h>
25 #include <test/jtx/WSClient.h>
26 #include <unordered_map>
27 
28 namespace ripple {
29 
30 namespace test {
31 
32 class Roles_test : public beast::unit_test::suite
33 {
34  void
36  {
37  using namespace test::jtx;
38 
39  {
40  Env env(*this);
41 
42  BEAST_EXPECT(env.rpc("ping")["result"]["role"] == "admin");
43  BEAST_EXPECT(makeWSClient(env.app().config())
44  ->invoke("ping")["result"]["unlimited"]
45  .asBool());
46  }
47  {
48  Env env{*this, envconfig(no_admin)};
49 
50  BEAST_EXPECT(!env.rpc("ping")["result"].isMember("role"));
51  auto wsRes =
52  makeWSClient(env.app().config())->invoke("ping")["result"];
53  BEAST_EXPECT(
54  !wsRes.isMember("unlimited") || !wsRes["unlimited"].asBool());
55  }
56  {
57  Env env{*this, envconfig(secure_gateway)};
58 
59  BEAST_EXPECT(env.rpc("ping")["result"]["role"] == "proxied");
60  auto wsRes =
61  makeWSClient(env.app().config())->invoke("ping")["result"];
62  BEAST_EXPECT(
63  !wsRes.isMember("unlimited") || !wsRes["unlimited"].asBool());
64 
66  headers["X-Forwarded-For"] = "12.34.56.78";
67  auto rpcRes = env.rpc(headers, "ping")["result"];
68  BEAST_EXPECT(rpcRes["role"] == "proxied");
69  BEAST_EXPECT(rpcRes["ip"] == "12.34.56.78");
70 
71  headers["X-Forwarded-For"] = "87.65.43.21, 44.33.22.11";
72  rpcRes = env.rpc(headers, "ping")["result"];
73  BEAST_EXPECT(rpcRes["ip"] == "87.65.43.21");
74  headers.erase("X-Forwarded-For");
75 
76  headers["Forwarded"] = "for=88.77.66.55";
77  rpcRes = env.rpc(headers, "ping")["result"];
78  BEAST_EXPECT(rpcRes["ip"] == "88.77.66.55");
79 
80  headers["Forwarded"] =
81  "what=where;for=55.66.77.88;for=nobody;"
82  "who=3";
83  rpcRes = env.rpc(headers, "ping")["result"];
84  BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");
85 
86  headers["Forwarded"] =
87  "what=where;for=55.66.77.88, 99.00.11.22;"
88  "who=3";
89  rpcRes = env.rpc(headers, "ping")["result"];
90  BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");
91 
92  wsRes = makeWSClient(env.app().config(), true, 2, headers)
93  ->invoke("ping")["result"];
94  BEAST_EXPECT(
95  !wsRes.isMember("unlimited") || !wsRes["unlimited"].asBool());
96 
97  std::string const name = "xrposhi";
98  headers["X-User"] = name;
99  rpcRes = env.rpc(headers, "ping")["result"];
100  BEAST_EXPECT(rpcRes["role"] == "identified");
101  BEAST_EXPECT(rpcRes["username"] == name);
102  BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");
103  wsRes = makeWSClient(env.app().config(), true, 2, headers)
104  ->invoke("ping")["result"];
105  BEAST_EXPECT(wsRes["unlimited"].asBool());
106  }
107  }
108 
109 public:
110  void
111  run() override
112  {
113  testRoles();
114  }
115 };
116 
117 BEAST_DEFINE_TESTSUITE(Roles, app, ripple);
118 
119 } // namespace test
120 
121 } // namespace ripple
ripple::test::jtx::secure_gateway
std::unique_ptr< Config > secure_gateway(std::unique_ptr< Config >)
Definition: envconfig.cpp:78
std::string
STL class.
ripple::test::Roles_test::run
void run() override
Definition: Roles_test.cpp:111
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:240
ripple::test::jtx::envconfig
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:49
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:70
ripple::test::Roles_test
Definition: Roles_test.cpp:32
ripple::Application::config
virtual Config & config()=0
std::unordered_map::erase
T erase(T... args)
ripple::test::Roles_test::testRoles
void testRoles()
Definition: Roles_test.cpp:35
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h: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
unordered_map
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:115
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:683
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)
string