rippled
SetRegularKey_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 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/protocol/Feature.h>
21 #include <ripple/protocol/jss.h>
22 #include <test/jtx.h>
23 
24 namespace ripple {
25 
26 class SetRegularKey_test : public beast::unit_test::suite
27 {
28 public:
29  void
31  {
32  using namespace test::jtx;
33 
34  testcase("Set regular key");
35  Env env{*this, supported_amendments() - fixMasterKeyAsRegularKey};
36  Account const alice("alice");
37  Account const bob("bob");
38  env.fund(XRP(10000), alice, bob);
39 
40  env(regkey(alice, bob));
41  auto const ar = env.le(alice);
42  BEAST_EXPECT(
43  ar->isFieldPresent(sfRegularKey) &&
44  (ar->getAccountID(sfRegularKey) == bob.id()));
45 
46  env(noop(alice), sig(bob));
47  env(noop(alice), sig(alice));
48 
49  testcase("Disable master key");
50  env(fset(alice, asfDisableMaster), sig(alice));
51  env(noop(alice), sig(bob));
52  env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
53 
54  testcase("Re-enable master key");
55  env(fclear(alice, asfDisableMaster),
56  sig(alice),
57  ter(tefMASTER_DISABLED));
58 
59  env(fclear(alice, asfDisableMaster), sig(bob));
60  env(noop(alice), sig(bob));
61  env(noop(alice), sig(alice));
62 
63  testcase("Revoke regular key");
64  env(regkey(alice, disabled));
65  env(noop(alice), sig(bob), ter(tefBAD_AUTH_MASTER));
66  env(noop(alice), sig(alice));
67  }
68 
69  void
71  {
72  using namespace test::jtx;
73 
74  testcase("Set regular key");
75  Env env{*this, supported_amendments() | fixMasterKeyAsRegularKey};
76  Account const alice("alice");
77  Account const bob("bob");
78  env.fund(XRP(10000), alice, bob);
79 
80  env(regkey(alice, bob));
81  env(noop(alice), sig(bob));
82  env(noop(alice), sig(alice));
83 
84  testcase("Disable master key");
85  env(fset(alice, asfDisableMaster), sig(alice));
86  env(noop(alice), sig(bob));
87  env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
88 
89  testcase("Re-enable master key");
90  env(fclear(alice, asfDisableMaster),
91  sig(alice),
92  ter(tefMASTER_DISABLED));
93 
94  env(fclear(alice, asfDisableMaster), sig(bob));
95  env(noop(alice), sig(bob));
96  env(noop(alice), sig(alice));
97 
98  testcase("Revoke regular key");
99  env(regkey(alice, disabled));
100  env(noop(alice), sig(bob), ter(tefBAD_AUTH));
101  env(noop(alice), sig(alice));
102  }
103 
104  void
106  {
107  using namespace test::jtx;
108 
109  // See https://ripplelabs.atlassian.net/browse/RIPD-1721.
110  testcase(
111  "Set regular key to master key (before fixMasterKeyAsRegularKey)");
112  Env env{*this, supported_amendments() - fixMasterKeyAsRegularKey};
113  Account const alice("alice");
114  env.fund(XRP(10000), alice);
115 
116  // Must be possible unless amendment `fixMasterKeyAsRegularKey` enabled.
117  env(regkey(alice, alice), sig(alice));
118  env(fset(alice, asfDisableMaster), sig(alice));
119 
120  // No way to sign...
121  env(noop(alice), ter(tefMASTER_DISABLED));
122  env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
123 
124  // ... until now.
125  env.enableFeature(fixMasterKeyAsRegularKey);
126  env(noop(alice));
127  env(noop(alice), sig(alice));
128 
129  env(regkey(alice, disabled), ter(tecNO_ALTERNATIVE_KEY));
130  env(fclear(alice, asfDisableMaster));
131  env(regkey(alice, disabled));
132  env(fset(alice, asfDisableMaster), ter(tecNO_ALTERNATIVE_KEY));
133  }
134 
135  void
137  {
138  using namespace test::jtx;
139 
140  testcase(
141  "Set regular key to master key (after fixMasterKeyAsRegularKey)");
142  Env env{*this, supported_amendments() | fixMasterKeyAsRegularKey};
143  Account const alice("alice");
144  env.fund(XRP(10000), alice);
145 
146  // Must be possible unless amendment `fixMasterKeyAsRegularKey` enabled.
147  env(regkey(alice, alice), ter(temBAD_REGKEY));
148  }
149 
150  void
152  {
153  using namespace test::jtx;
154 
155  testcase("Password spent");
156  Env env(*this);
157  Account const alice("alice");
158  Account const bob("bob");
159  env.fund(XRP(10000), alice, bob);
160 
161  auto ar = env.le(alice);
162  BEAST_EXPECT(
163  ar->isFieldPresent(sfFlags) &&
164  ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) == 0));
165 
166  env(regkey(alice, bob), sig(alice), fee(0));
167 
168  ar = env.le(alice);
169  BEAST_EXPECT(
170  ar->isFieldPresent(sfFlags) &&
171  ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) ==
173 
174  // The second SetRegularKey transaction with Fee=0 should fail.
175  env(regkey(alice, bob), sig(alice), fee(0), ter(telINSUF_FEE_P));
176 
177  env.trust(bob["USD"](1), alice);
178  env(pay(bob, alice, bob["USD"](1)));
179  ar = env.le(alice);
180  BEAST_EXPECT(
181  ar->isFieldPresent(sfFlags) &&
182  ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) == 0));
183  }
184 
185  void
187  {
188  using namespace test::jtx;
189 
190  testcase("Universal mask");
191  Env env(*this);
192  Account const alice("alice");
193  Account const bob("bob");
194  env.fund(XRP(10000), alice, bob);
195 
196  auto jv = regkey(alice, bob);
198  env(jv, ter(temINVALID_FLAG));
199  }
200 
201  void
202  run() override
203  {
210  }
211 };
212 
213 BEAST_DEFINE_TESTSUITE(SetRegularKey, app, ripple);
214 
215 } // namespace ripple
ripple::sfRegularKey
const SF_Account sfRegularKey(access, STI_ACCOUNT, 8, "RegularKey")
Definition: SField.h:487
ripple::lsfPasswordSpent
@ lsfPasswordSpent
Definition: LedgerFormats.h:103
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::SetRegularKey_test::run
void run() override
Definition: SetRegularKey_test.cpp:202
ripple::SField::fieldName
const std::string fieldName
Definition: SField.h:129
ripple::temBAD_REGKEY
@ temBAD_REGKEY
Definition: TER.h:93
ripple::SetRegularKey_test::testUniversalMask
void testUniversalMask()
Definition: SetRegularKey_test.cpp:186
ripple::sfFlags
const SF_U32 sfFlags(access, STI_UINT32, 2, "Flags")
Definition: SField.h:354
ripple::tefBAD_AUTH
@ tefBAD_AUTH
Definition: TER.h:145
ripple::SetRegularKey_test::testDisableMasterKey
void testDisableMasterKey()
Definition: SetRegularKey_test.cpp:30
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:106
ripple::tecNO_ALTERNATIVE_KEY
@ tecNO_ALTERNATIVE_KEY
Definition: TER.h:254
ripple::tefMASTER_DISABLED
@ tefMASTER_DISABLED
Definition: TER.h:153
ripple::SetRegularKey_test::testDisabledRegularKey
void testDisabledRegularKey()
Definition: SetRegularKey_test.cpp:105
ripple::telINSUF_FEE_P
@ telINSUF_FEE_P
Definition: TER.h:56
ripple::SetRegularKey_test::testDisableRegularKeyAfterFix
void testDisableRegularKeyAfterFix()
Definition: SetRegularKey_test.cpp:136
ripple::fixMasterKeyAsRegularKey
const uint256 fixMasterKeyAsRegularKey
Definition: Feature.cpp:180
ripple::SetRegularKey_test
Definition: SetRegularKey_test.cpp:26
ripple::asfDisableMaster
const std::uint32_t asfDisableMaster
Definition: TxFlags.h:68
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::tefBAD_AUTH_MASTER
@ tefBAD_AUTH_MASTER
Definition: TER.h:158
ripple::tfUniversalMask
const std::uint32_t tfUniversalMask
Definition: TxFlags.h:50
ripple::SetRegularKey_test::testDisableMasterKeyAfterFix
void testDisableMasterKeyAfterFix()
Definition: SetRegularKey_test.cpp:70
ripple::SetRegularKey_test::testPasswordSpent
void testPasswordSpent()
Definition: SetRegularKey_test.cpp:151