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  env(jv, fee(1000), sig(env.master));
72  }
73 
74  env(jv, fee(1000), ter(expectedOutcome));
75  env.close();
76  };
77 
78  // test mainnet
79  {
80  test::jtx::Env env{*this, makeNetworkConfig(0)};
81  BEAST_EXPECT(env.app().config().NETWORK_ID == 0);
82 
83  // try to submit a txn without network id, this should work
84  Json::Value jv;
85  jv[jss::Account] = alice.human();
86  jv[jss::TransactionType] = jss::AccountSet;
87  runTx(env, jv, tesSUCCESS);
88 
89  // try to submit a txn with NetworkID present against a mainnet
90  // node, this will fail
91  jv[jss::NetworkID] = 0;
93 
94  // change network id to something else, should still return same
95  // error
96  jv[jss::NetworkID] = 10000;
98  }
99 
100  // any network up to and including networkid 1024 cannot support
101  // NetworkID
102  {
103  test::jtx::Env env{*this, makeNetworkConfig(1024)};
104  BEAST_EXPECT(env.app().config().NETWORK_ID == 1024);
105 
106  // try to submit a txn without network id, this should work
107  Json::Value jv;
108  jv[jss::Account] = alice.human();
109  jv[jss::TransactionType] = jss::AccountSet;
110  runTx(env, jv, tesSUCCESS);
111 
112  // now submit with a network id, this will fail
113  jv[jss::NetworkID] = 1024;
114  runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL);
115 
116  jv[jss::NetworkID] = 1000;
117  runTx(env, jv, telNETWORK_ID_MAKES_TX_NON_CANONICAL);
118  }
119 
120  // any network above networkid 1024 will produce an error if fed a txn
121  // absent networkid
122  {
123  test::jtx::Env env{*this, makeNetworkConfig(1025)};
124  BEAST_EXPECT(env.app().config().NETWORK_ID == 1025);
125  {
126  env.fund(XRP(200), alice);
127  // try to submit a txn without network id, this should not work
128  Json::Value jvn;
129  jvn[jss::Account] = alice.human();
130  jvn[jss::TransactionType] = jss::AccountSet;
131  jvn[jss::Fee] = to_string(env.current()->fees().base);
132  jvn[jss::Sequence] = env.seq(alice);
133  jvn[jss::LastLedgerSequence] = env.current()->info().seq + 2;
134  auto jt = env.jtnofill(jvn);
135  Serializer s;
136  jt.stx->add(s);
137  BEAST_EXPECT(
138  env.rpc(
139  "submit",
140  strHex(s.slice()))[jss::result][jss::engine_result] ==
141  "telREQUIRES_NETWORK_ID");
142  env.close();
143  }
144 
145  Json::Value jv;
146  jv[jss::Account] = alice.human();
147  jv[jss::TransactionType] = jss::AccountSet;
148 
149  // try to submit with wrong network id
150  jv[jss::NetworkID] = 0;
151  runTx(env, jv, telWRONG_NETWORK);
152 
153  jv[jss::NetworkID] = 1024;
154  runTx(env, jv, telWRONG_NETWORK);
155 
156  // submit the correct network id
157  jv[jss::NetworkID] = 1025;
158  runTx(env, jv, tesSUCCESS);
159  }
160  }
161 };
162 
163 BEAST_DEFINE_TESTSUITE(NetworkID, app, ripple);
164 
165 } // namespace test
166 } // namespace ripple
ripple::test::NetworkID_test::run
void run() override
Definition: NetworkID_test.cpp:33
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
ripple::test::jtx::Env::rpc
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition: Env.h:712
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:67
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:242
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 >
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::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:64
ripple::test::jtx::sig
Set the regular signature on a JTx.
Definition: sig.h:34
ripple::test::jtx::Env::seq
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition: Env.cpp:207
ripple::test::jtx::fee
Set the fee on a JTx.
Definition: fee.h:35
ripple::Serializer
Definition: Serializer.h:40
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:166
ripple::test::jtx::Env::fund
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition: Env.cpp:228
ripple::test::jtx::Env::master
Account const & master
Definition: Env.h:122
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
ripple::telWRONG_NETWORK
@ telWRONG_NETWORK
Definition: TER.h:65
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::test::jtx::Env::jtnofill
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition: Env.h:463
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:236
ripple::test::jtx::Env::current
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:312
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117
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)