rippled
NetworkID_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 Dev Null Productions
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/basics/BasicConfig.h>
21 #include <ripple/core/ConfigSections.h>
22 #include <ripple/protocol/jss.h>
23 #include <test/jtx.h>
24 #include <test/jtx/Env.h>
25 
26 namespace ripple {
27 namespace test {
28 
29 class NetworkID_test : public beast::unit_test::suite
30 {
31 public:
32  void
33  run() override
34  {
35  testNetworkID();
36  }
37 
39  makeNetworkConfig(uint32_t networkID)
40  {
41  using namespace jtx;
42  return envconfig([&](std::unique_ptr<Config> cfg) {
43  cfg->NETWORK_ID = networkID;
44  return cfg;
45  });
46  }
47 
48  void
50  {
51  testcase(
52  "Require txn NetworkID to be specified (or not) depending on the "
53  "network ID of the node");
54  using namespace jtx;
55 
56  auto const alice = Account{"alice"};
57 
58  auto const runTx = [&](test::jtx::Env& env,
59  Json::Value const& jv,
60  TER expectedOutcome) {
61  env.memoize(env.master);
62  env.memoize(alice);
63 
64  // fund alice
65  {
66  Json::Value jv;
67  jv[jss::Account] = env.master.human();
68  jv[jss::Destination] = alice.human();
69  jv[jss::TransactionType] = "Payment";
70  jv[jss::Amount] = "10000000000";
71  if (env.app().config().NETWORK_ID > 1024)
72  jv[jss::NetworkID] =
74 
75  env(jv, fee(1000), sig(env.master));
76  }
77 
78  // run tx
79  env(jv, fee(1000), ter(expectedOutcome));
80  env.close();
81  };
82 
83  // test mainnet
84  {
85  test::jtx::Env env{*this, makeNetworkConfig(0)};
86  BEAST_EXPECT(env.app().config().NETWORK_ID == 0);
87 
88  // try to submit a txn without network id, this should work
89  Json::Value jv;
90  jv[jss::Account] = alice.human();
91  jv[jss::TransactionType] = jss::AccountSet;
92  runTx(env, jv, tesSUCCESS);
93 
94  // try to submit a txn with NetworkID present against a mainnet
95  // node, this will fail
96  jv[jss::NetworkID] = 0;
98 
99  // change network id to something else, should still return same
100  // error
101  jv[jss::NetworkID] = 10000;
102  runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL);
103  }
104 
105  // any network up to and including networkid 1024 cannot support
106  // NetworkID
107  {
108  test::jtx::Env env{*this, makeNetworkConfig(1024)};
109  BEAST_EXPECT(env.app().config().NETWORK_ID == 1024);
110 
111  // try to submit a txn without network id, this should work
112  Json::Value jv;
113  jv[jss::Account] = alice.human();
114  jv[jss::TransactionType] = jss::AccountSet;
115  runTx(env, jv, tesSUCCESS);
116 
117  // now submit with a network id, this will fail
118  jv[jss::NetworkID] = 1024;
119  runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL);
120 
121  jv[jss::NetworkID] = 1000;
122  runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL);
123  }
124 
125  // any network above networkid 1024 will produce an error if fed a txn
126  // absent networkid
127  {
128  test::jtx::Env env{*this, makeNetworkConfig(1025)};
129  BEAST_EXPECT(env.app().config().NETWORK_ID == 1025);
130 
131  // try to submit a txn without network id, this should not work
132  Json::Value jv;
133  jv[jss::Account] = alice.human();
134  jv[jss::TransactionType] = jss::AccountSet;
135  runTx(env, jv, telREQUIRES_NETWORK_ID);
136 
137  // try to submit with wrong network id
138  jv[jss::NetworkID] = 0;
139  runTx(env, jv, telWRONG_NETWORK);
140 
141  jv[jss::NetworkID] = 1024;
142  runTx(env, jv, telWRONG_NETWORK);
143 
144  // submit the correct network id
145  jv[jss::NetworkID] = 1025;
146  runTx(env, jv, tesSUCCESS);
147  }
148  }
149 };
150 
151 BEAST_DEFINE_TESTSUITE(NetworkID, app, ripple);
152 
153 } // namespace test
154 } // namespace ripple
ripple::test::NetworkID_test::run
void run() override
Definition: NetworkID_test.cpp:33
ripple::test::jtx::ter
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: ter.h:33
ripple::test::jtx::Account::human
std::string const & human() const
Returns the human readable public key.
Definition: Account.h:113
ripple::telNETWORK_ID_MAKES_TX_NON_CANONICAL
@ telNETWORK_ID_MAKES_TX_NON_CANONICAL
Definition: TER.h:66
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:241
ripple::test::jtx::envconfig
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:49
ripple::Application::config
virtual Config & config()=0
ripple::TERSubset< CanCvtToTER >
std::to_string
T to_string(T... args)
ripple::test::jtx::Env::close
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition: Env.cpp:121
ripple::telREQUIRES_NETWORK_ID
@ telREQUIRES_NETWORK_ID
Definition: TER.h:65
ripple::test::jtx::sig
Set the regular signature on a JTx.
Definition: sig.h:34
ripple::test::jtx::fee
Set the fee on a JTx.
Definition: fee.h:35
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Config::NETWORK_ID
uint32_t NETWORK_ID
Definition: Config.h:161
ripple::test::jtx::Env::master
Account const & master
Definition: Env.h:121
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::telWRONG_NETWORK
@ telWRONG_NETWORK
Definition: TER.h:64
ripple::test::NetworkID_test
Definition: NetworkID_test.cpp:29
ripple::test::jtx::Env::memoize
void memoize(Account const &account)
Associate AccountID with account.
Definition: Env.cpp:156
ripple::test::NetworkID_test::makeNetworkConfig
std::unique_ptr< Config > makeNetworkConfig(uint32_t networkID)
Definition: NetworkID_test.cpp:39
std::unique_ptr
STL class.
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:222
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::NetworkID_test::testNetworkID
void testNetworkID()
Definition: NetworkID_test.cpp:49
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)