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 
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(ar->isFieldPresent(sfRegularKey) && (ar->getAccountID(sfRegularKey) == bob.id()));
43 
44  env(noop(alice), sig(bob));
45  env(noop(alice), sig(alice));
46 
47  testcase("Disable master key");
48  env(fset(alice, asfDisableMaster), sig(alice));
49  env(noop(alice), sig(bob));
50  env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
51 
52  testcase("Re-enable master key");
53  env(fclear(alice, asfDisableMaster), sig(alice), ter(tefMASTER_DISABLED));
54 
55  env(fclear(alice, asfDisableMaster), sig(bob));
56  env(noop(alice), sig(bob));
57  env(noop(alice), sig(alice));
58 
59  testcase("Revoke regular key");
60  env(regkey(alice, disabled));
61  env(noop(alice), sig(bob), ter(tefBAD_AUTH_MASTER));
62  env(noop(alice), sig(alice));
63  }
64 
66  {
67  using namespace test::jtx;
68 
69  testcase("Set regular key");
70  Env env{*this, supported_amendments() | fixMasterKeyAsRegularKey};
71  Account const alice("alice");
72  Account const bob("bob");
73  env.fund(XRP(10000), alice, bob);
74 
75  env(regkey(alice, bob));
76  env(noop(alice), sig(bob));
77  env(noop(alice), sig(alice));
78 
79  testcase("Disable master key");
80  env(fset(alice, asfDisableMaster), sig(alice));
81  env(noop(alice), sig(bob));
82  env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
83 
84  testcase("Re-enable master key");
85  env(
86  fclear(alice, asfDisableMaster),
87  sig(alice),
89  );
90 
91  env(fclear(alice, asfDisableMaster), sig(bob));
92  env(noop(alice), sig(bob));
93  env(noop(alice), sig(alice));
94 
95  testcase("Revoke regular key");
96  env(regkey(alice, disabled));
97  env(noop(alice), sig(bob), ter(tefBAD_AUTH));
98  env(noop(alice), sig(alice));
99  }
100 
102  {
103  using namespace test::jtx;
104 
105  // See https://ripplelabs.atlassian.net/browse/RIPD-1721.
106  testcase("Set regular key to master key (before fixMasterKeyAsRegularKey)");
107  Env env {*this, supported_amendments() - fixMasterKeyAsRegularKey};
108  Account const alice("alice");
109  env.fund(XRP(10000), alice);
110 
111  // Must be possible unless amendment `fixMasterKeyAsRegularKey` enabled.
112  env(regkey(alice, alice), sig(alice));
113  env(fset(alice, asfDisableMaster), sig(alice));
114 
115  // No way to sign...
116  env(noop(alice), ter(tefMASTER_DISABLED));
117  env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
118 
119  // ... until now.
120  env.enableFeature(fixMasterKeyAsRegularKey);
121  env(noop(alice));
122  env(noop(alice), sig(alice));
123 
124  env(regkey(alice, disabled), ter(tecNO_ALTERNATIVE_KEY));
125  env(fclear(alice, asfDisableMaster));
126  env(regkey(alice, disabled));
127  env(fset(alice, asfDisableMaster), ter(tecNO_ALTERNATIVE_KEY));
128  }
129 
131  {
132  using namespace test::jtx;
133 
134  testcase("Set regular key to master key (after fixMasterKeyAsRegularKey)");
135  Env env {*this, supported_amendments() | fixMasterKeyAsRegularKey};
136  Account const alice("alice");
137  env.fund(XRP(10000), alice);
138 
139  // Must be possible unless amendment `fixMasterKeyAsRegularKey` enabled.
140  env(regkey(alice, alice), ter(temBAD_REGKEY));
141  }
142 
144  {
145  using namespace test::jtx;
146 
147  testcase("Password spent");
148  Env env(*this);
149  Account const alice("alice");
150  Account const bob("bob");
151  env.fund(XRP(10000), alice, bob);
152 
153  auto ar = env.le(alice);
154  BEAST_EXPECT(ar->isFieldPresent(sfFlags) && ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) == 0));
155 
156  env(regkey(alice, bob), sig(alice), fee(0));
157 
158  ar = env.le(alice);
159  BEAST_EXPECT(ar->isFieldPresent(sfFlags) && ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) == lsfPasswordSpent));
160 
161  // The second SetRegularKey transaction with Fee=0 should fail.
162  env(regkey(alice, bob), sig(alice), fee(0), ter(telINSUF_FEE_P));
163 
164  env.trust(bob["USD"](1), alice);
165  env(pay(bob, alice, bob["USD"](1)));
166  ar = env.le(alice);
167  BEAST_EXPECT(ar->isFieldPresent(sfFlags) && ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) == 0));
168  }
169 
171  {
172  using namespace test::jtx;
173 
174  testcase("Universal mask");
175  Env env(*this);
176  Account const alice("alice");
177  Account const bob("bob");
178  env.fund(XRP(10000), alice, bob);
179 
180  auto jv = regkey(alice, bob);
182  env(jv, ter(temINVALID_FLAG));
183  }
184 
185  void run() override
186  {
193  }
194 
195 };
196 
197 BEAST_DEFINE_TESTSUITE(SetRegularKey,app,ripple);
198 
199 }
200 
ripple::sfRegularKey
const SF_Account sfRegularKey(access, STI_ACCOUNT, 8, "RegularKey")
Definition: SField.h:467
ripple::lsfPasswordSpent
@ lsfPasswordSpent
Definition: LedgerFormats.h:132
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::SetRegularKey_test::run
void run() override
Definition: SetRegularKey_test.cpp:185
ripple::SField::fieldName
const std::string fieldName
Definition: SField.h:136
ripple::temBAD_REGKEY
@ temBAD_REGKEY
Definition: TER.h:96
ripple::SetRegularKey_test::testUniversalMask
void testUniversalMask()
Definition: SetRegularKey_test.cpp:170
ripple::sfFlags
const SF_U32 sfFlags(access, STI_UINT32, 2, "Flags")
Definition: SField.h:338
ripple::tefBAD_AUTH
@ tefBAD_AUTH
Definition: TER.h:149
ripple::SetRegularKey_test::testDisableMasterKey
void testDisableMasterKey()
Definition: SetRegularKey_test.cpp:30
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:109
ripple::tecNO_ALTERNATIVE_KEY
@ tecNO_ALTERNATIVE_KEY
Definition: TER.h:261
ripple::tefMASTER_DISABLED
@ tefMASTER_DISABLED
Definition: TER.h:157
ripple::SetRegularKey_test::testDisabledRegularKey
void testDisabledRegularKey()
Definition: SetRegularKey_test.cpp:101
ripple::telINSUF_FEE_P
@ telINSUF_FEE_P
Definition: TER.h:58
ripple::SetRegularKey_test::testDisableRegularKeyAfterFix
void testDisableRegularKeyAfterFix()
Definition: SetRegularKey_test.cpp:130
ripple::fixMasterKeyAsRegularKey
const uint256 fixMasterKeyAsRegularKey
Definition: Feature.cpp:174
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:162
ripple::tfUniversalMask
const std::uint32_t tfUniversalMask
Definition: TxFlags.h:50
ripple::SetRegularKey_test::testDisableMasterKeyAfterFix
void testDisableMasterKeyAfterFix()
Definition: SetRegularKey_test.cpp:65
ripple::SetRegularKey_test::testPasswordSpent
void testPasswordSpent()
Definition: SetRegularKey_test.cpp:143