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
203  {
204  using namespace test::jtx;
205 
206  testcase("Ticket regular key");
207  Env env{*this};
208  Account const alice{"alice", KeyType::ed25519};
209  env.fund(XRP(1000), alice);
210  env.close();
211 
212  // alice makes herself some tickets.
213  env(ticket::create(alice, 4));
214  env.close();
215  std::uint32_t ticketSeq{env.seq(alice)};
216 
217  // Make sure we can give a regular key using a ticket.
218  Account const alie{"alie", KeyType::secp256k1};
219  env(regkey(alice, alie), ticket::use(--ticketSeq));
220  env.close();
221 
222  // Disable alice's master key using a ticket.
223  env(fset(alice, asfDisableMaster),
224  sig(alice),
225  ticket::use(--ticketSeq));
226  env.close();
227 
228  // alice should be able to sign using the regular key but not the
229  // master key.
230  std::uint32_t const aliceSeq{env.seq(alice)};
231  env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
232  env(noop(alice), sig(alie), ter(tesSUCCESS));
233  env.close();
234  BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
235 
236  // Re-enable the master key using a ticket.
237  env(fclear(alice, asfDisableMaster),
238  sig(alie),
239  ticket::use(--ticketSeq));
240  env.close();
241 
242  // Disable the regular key using a ticket.
243  env(regkey(alice, disabled), sig(alie), ticket::use(--ticketSeq));
244  env.close();
245 
246  // alice should be able to sign using the master key but not the
247  // regular key.
248  env(noop(alice), sig(alice), ter(tesSUCCESS));
249  env(noop(alice), sig(alie), ter(tefBAD_AUTH));
250  env.close();
251  }
252 
253  void
254  run() override
255  {
263  }
264 };
265 
266 BEAST_DEFINE_TESTSUITE(SetRegularKey, app, ripple);
267 
268 } // namespace ripple
ripple::lsfPasswordSpent
@ lsfPasswordSpent
Definition: LedgerFormats.h:223
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::SetRegularKey_test::run
void run() override
Definition: SetRegularKey_test.cpp:254
ripple::sfRegularKey
const SF_ACCOUNT sfRegularKey
ripple::SField::fieldName
const std::string fieldName
Definition: SField.h:132
ripple::temBAD_REGKEY
@ temBAD_REGKEY
Definition: TER.h:96
ripple::SetRegularKey_test::testUniversalMask
void testUniversalMask()
Definition: SetRegularKey_test.cpp:186
ripple::tefBAD_AUTH
@ tefBAD_AUTH
Definition: TER.h:151
ripple::SetRegularKey_test::testDisableMasterKey
void testDisableMasterKey()
Definition: SetRegularKey_test.cpp:30
ripple::KeyType::ed25519
@ ed25519
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:109
ripple::asfDisableMaster
constexpr std::uint32_t asfDisableMaster
Definition: TxFlags.h:77
ripple::tecNO_ALTERNATIVE_KEY
@ tecNO_ALTERNATIVE_KEY
Definition: TER.h:263
ripple::tefMASTER_DISABLED
@ tefMASTER_DISABLED
Definition: TER.h:159
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
std::uint32_t
ripple::SetRegularKey_test
Definition: SetRegularKey_test.cpp:26
ripple::KeyType::secp256k1
@ secp256k1
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sfFlags
const SF_UINT32 sfFlags
ripple::tefBAD_AUTH_MASTER
@ tefBAD_AUTH_MASTER
Definition: TER.h:164
ripple::tfUniversalMask
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:60
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:222
ripple::SetRegularKey_test::testDisableMasterKeyAfterFix
void testDisableMasterKeyAfterFix()
Definition: SetRegularKey_test.cpp:70
ripple::SetRegularKey_test::testPasswordSpent
void testPasswordSpent()
Definition: SetRegularKey_test.cpp:151
ripple::SetRegularKey_test::testTicketRegularKey
void testTicketRegularKey()
Definition: SetRegularKey_test.cpp:202