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 <test/jtx.h>
24 #include <test/jtx/WSClient.h>
25 #include <string>
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(
44  env.app().config())->invoke(
45  "ping")["result"]["unlimited"].asBool());
46  }
47  {
48  Env env { *this, envconfig(no_admin) };
49 
50  BEAST_EXPECT(! env.rpc("ping")["result"].isMember("role"));
51  auto wsRes = makeWSClient(
52  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 = makeWSClient(
61  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"] = "what=where;for=55.66.77.88;for=nobody;"
81  "who=3";
82  rpcRes = env.rpc(headers, "ping")["result"];
83  BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");
84 
85  headers["Forwarded"] = "what=where;for=55.66.77.88, 99.00.11.22;"
86  "who=3";
87  rpcRes = env.rpc(headers, "ping")["result"];
88  BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");
89 
90  wsRes = makeWSClient(
91  env.app().config(), true, 2, headers)->invoke("ping")["result"];
92  BEAST_EXPECT(
93  !wsRes.isMember("unlimited") || !wsRes["unlimited"].asBool());
94 
95  std::string const name = "xrposhi";
96  headers["X-User"] = name;
97  rpcRes = env.rpc(headers, "ping")["result"];
98  BEAST_EXPECT(rpcRes["role"] == "identified");
99  BEAST_EXPECT(rpcRes["username"] == name);
100  BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");
101  wsRes = makeWSClient(
102  env.app().config(), true, 2, headers)->invoke("ping")["result"];
103  BEAST_EXPECT(wsRes["unlimited"].asBool());
104  }
105  }
106 
107 public:
108  void
109  run() override
110  {
111  testRoles();
112  }
113 };
114 
115 BEAST_DEFINE_TESTSUITE(Roles, app, ripple);
116 
117 } // namespace test
118 
119 } // namespace ripple
ripple::test::jtx::secure_gateway
std::unique_ptr< Config > secure_gateway(std::unique_ptr< Config >)
Definition: envconfig.cpp:77
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
std::string
STL class.
ripple::test::Roles_test::run
void run() override
Definition: Roles_test.cpp:109
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:237
ripple::test::jtx::envconfig
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:52
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:69
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:307
unordered_map
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117
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:688
string