-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet_tests.cpp
274 lines (223 loc) · 10.5 KB
/
wallet_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include <eosio/utilities/key_conversion.hpp>
#include <eosio/utilities/rand.hpp>
#include <eosio/chain/genesis_state.hpp>
#include <eosio/wallet_plugin/wallet.hpp>
#include <eosio/wallet_plugin/wallet_manager.hpp>
#include <boost/test/unit_test.hpp>
#include <eosio/chain/authority.hpp>
#include <eosio/chain/exceptions.hpp>
namespace eosio {
BOOST_AUTO_TEST_SUITE(wallet_tests)
/// Test creating the wallet
BOOST_AUTO_TEST_CASE(wallet_test)
{ try {
using namespace eosio::wallet;
using namespace eosio::utilities;
wallet_data d;
soft_wallet wallet(d);
BOOST_CHECK(wallet.is_locked());
wallet.set_password("pass");
BOOST_CHECK(wallet.is_locked());
wallet.unlock("pass");
BOOST_CHECK(!wallet.is_locked());
wallet.set_wallet_filename("test");
BOOST_CHECK_EQUAL("test", wallet.get_wallet_filename());
BOOST_CHECK_EQUAL(0, wallet.list_keys().size());
auto priv = fc::crypto::private_key::generate();
auto pub = priv.get_public_key();
auto wif = (std::string)priv;
wallet.import_key(wif);
BOOST_CHECK_EQUAL(1, wallet.list_keys().size());
auto privCopy = wallet.get_private_key(pub);
BOOST_CHECK_EQUAL(wif, (std::string)privCopy);
wallet.lock();
BOOST_CHECK(wallet.is_locked());
wallet.unlock("pass");
BOOST_CHECK_EQUAL(1, wallet.list_keys().size());
wallet.save_wallet_file("wallet_test.json");
BOOST_CHECK(fc::exists("wallet_test.json"));
wallet_data d2;
soft_wallet wallet2(d2);
BOOST_CHECK(wallet2.is_locked());
wallet2.load_wallet_file("wallet_test.json");
BOOST_CHECK(wallet2.is_locked());
wallet2.unlock("pass");
BOOST_CHECK_EQUAL(1, wallet2.list_keys().size());
auto privCopy2 = wallet2.get_private_key(pub);
BOOST_CHECK_EQUAL(wif, (std::string)privCopy2);
fc::remove("wallet_test.json");
} FC_LOG_AND_RETHROW() }
/// Test wallet manager
BOOST_AUTO_TEST_CASE(wallet_manager_test)
{ try {
using namespace eosio::wallet;
if (fc::exists("test.wallet")) fc::remove("test.wallet");
if (fc::exists("test2.wallet")) fc::remove("test2.wallet");
if (fc::exists("testgen.wallet")) fc::remove("testgen.wallet");
constexpr auto key1 = "5JktVNHnRX48BUdtewU7N1CyL4Z886c42x7wYW7XhNWkDQRhdcS";
constexpr auto key2 = "5Ju5RTcVDo35ndtzHioPMgebvBM6LkJ6tvuU6LTNQv8yaz3ggZr";
constexpr auto key3 = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3";
wallet_manager wm;
BOOST_CHECK_EQUAL(0, wm.list_wallets().size());
BOOST_CHECK_THROW(wm.get_public_keys(), wallet_not_available_exception);
BOOST_CHECK_NO_THROW(wm.lock_all());
BOOST_CHECK_THROW(wm.lock("test"), fc::exception);
BOOST_CHECK_THROW(wm.unlock("test", "pw"), fc::exception);
BOOST_CHECK_THROW(wm.import_key("test", "pw"), fc::exception);
auto pw = wm.create("test");
BOOST_CHECK(!pw.empty());
BOOST_CHECK_EQUAL(0, pw.find("PW")); // starts with PW
BOOST_CHECK_EQUAL(1, wm.list_wallets().size());
// wallet has no keys when it is created
BOOST_CHECK_EQUAL(0, wm.get_public_keys().size());
BOOST_CHECK_EQUAL(0, wm.list_keys("test", pw).size());
BOOST_CHECK(wm.list_wallets().at(0).find("*") != std::string::npos);
wm.lock("test");
BOOST_CHECK(wm.list_wallets().at(0).find("*") == std::string::npos);
wm.unlock("test", pw);
BOOST_CHECK_THROW(wm.unlock("test", pw), chain::wallet_unlocked_exception);
BOOST_CHECK(wm.list_wallets().at(0).find("*") != std::string::npos);
wm.import_key("test", key1);
BOOST_CHECK_EQUAL(1, wm.get_public_keys().size());
auto keys = wm.list_keys("test", pw);
auto pub_pri_pair = [](const char *key) -> auto {
private_key_type prikey = private_key_type(std::string(key));
return std::pair<const public_key_type, private_key_type>(prikey.get_public_key(), prikey);
};
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key1)) != keys.cend());
wm.import_key("test", key2);
keys = wm.list_keys("test", pw);
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key1)) != keys.cend());
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key2)) != keys.cend());
// key3 was not automatically imported
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key3)) == keys.cend());
wm.remove_key("test", pw, string(pub_pri_pair(key2).first));
BOOST_CHECK_EQUAL(1, wm.get_public_keys().size());
keys = wm.list_keys("test", pw);
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key2)) == keys.cend());
wm.import_key("test", key2);
BOOST_CHECK_EQUAL(2, wm.get_public_keys().size());
keys = wm.list_keys("test", pw);
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key2)) != keys.cend());
BOOST_CHECK_THROW(wm.remove_key("test", pw, string(pub_pri_pair(key3).first)), fc::exception);
BOOST_CHECK_EQUAL(2, wm.get_public_keys().size());
BOOST_CHECK_THROW(wm.remove_key("test", "PWnogood", string(pub_pri_pair(key2).first)), wallet_invalid_password_exception);
BOOST_CHECK_EQUAL(2, wm.get_public_keys().size());
wm.lock("test");
BOOST_CHECK_THROW(wm.list_keys("test", pw), wallet_locked_exception);
BOOST_CHECK_THROW(wm.get_public_keys(), wallet_locked_exception);
wm.unlock("test", pw);
BOOST_CHECK_EQUAL(2, wm.get_public_keys().size());
BOOST_CHECK_EQUAL(2, wm.list_keys("test", pw).size());
wm.lock_all();
BOOST_CHECK_THROW(wm.get_public_keys(), wallet_locked_exception);
BOOST_CHECK(wm.list_wallets().at(0).find("*") == std::string::npos);
auto pw2 = wm.create("test2");
BOOST_CHECK_EQUAL(2, wm.list_wallets().size());
// wallet has no keys when it is created
BOOST_CHECK_EQUAL(0, wm.get_public_keys().size());
wm.import_key("test2", key3);
BOOST_CHECK_EQUAL(1, wm.get_public_keys().size());
BOOST_CHECK_THROW(wm.import_key("test2", key3), fc::exception);
keys = wm.list_keys("test2", pw2);
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key1)) == keys.cend());
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key2)) == keys.cend());
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key3)) != keys.cend());
wm.unlock("test", pw);
keys = wm.list_keys("test", pw);
auto keys2 = wm.list_keys("test2", pw2);
keys.insert(keys2.begin(), keys2.end());
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key1)) != keys.cend());
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key2)) != keys.cend());
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key3)) != keys.cend());
BOOST_CHECK_EQUAL(3, keys.size());
BOOST_CHECK_THROW(wm.list_keys("test2", "PWnogood"), wallet_invalid_password_exception);
private_key_type pkey1{std::string(key1)};
private_key_type pkey2{std::string(key2)};
chain::signed_transaction trx;
auto chain_id = genesis_state().compute_chain_id();
flat_set<public_key_type> pubkeys;
pubkeys.emplace(pkey1.get_public_key());
pubkeys.emplace(pkey2.get_public_key());
trx = wm.sign_transaction(trx, pubkeys, chain_id );
const auto& pks = trx.get_signature_keys(chain_id);
BOOST_CHECK_EQUAL(2, pks.size());
BOOST_CHECK(find(pks.cbegin(), pks.cend(), pkey1.get_public_key()) != pks.cend());
BOOST_CHECK(find(pks.cbegin(), pks.cend(), pkey2.get_public_key()) != pks.cend());
BOOST_CHECK_EQUAL(3, wm.get_public_keys().size());
wm.set_timeout(chrono::seconds(0));
BOOST_CHECK_THROW(wm.get_public_keys(), wallet_locked_exception);
BOOST_CHECK_THROW(wm.list_keys("test", pw), wallet_locked_exception);
wm.set_timeout(chrono::seconds(15));
wm.create("testgen");
BOOST_CHECK_THROW(wm.create_key("testgen", "xxx"), chain::wallet_exception);
wm.lock("testgen");
fc::remove("testgen.wallet");
const string test_key_create_types[] = {"K1", "R1", "k1", ""};
for(const string& key_type_to_create : test_key_create_types) {
string pw = wm.create("testgen");
//check that the public key returned looks legit through a string conversion
// (would throw otherwise)
public_key_type create_key_pub(wm.create_key("testgen", key_type_to_create));
//now pluck out the private key from the wallet and see if the public key of said
// private key matches what was returned earlier from the create_key() call
private_key_type create_key_priv(wm.list_keys("testgen", pw).cbegin()->second);
BOOST_CHECK_EQUAL((string)create_key_pub, (string)create_key_priv.get_public_key());
wm.lock("testgen");
BOOST_CHECK(fc::exists("testgen.wallet"));
fc::remove("testgen.wallet");
}
BOOST_CHECK(fc::exists("test.wallet"));
BOOST_CHECK(fc::exists("test2.wallet"));
fc::remove("test.wallet");
fc::remove("test2.wallet");
} FC_LOG_AND_RETHROW() }
/// Test wallet manager
BOOST_AUTO_TEST_CASE(wallet_manager_create_test) {
try {
using namespace eosio::wallet;
if (fc::exists("test.wallet")) fc::remove("test.wallet");
wallet_manager wm;
wm.create("test");
constexpr auto key1 = "5JktVNHnRX48BUdtewU7N1CyL4Z886c42x7wYW7XhNWkDQRhdcS";
wm.import_key("test", key1);
BOOST_CHECK_THROW(wm.create("test"), wallet_exist_exception);
BOOST_CHECK_THROW(wm.create("./test"), wallet_exception);
BOOST_CHECK_THROW(wm.create("../../test"), wallet_exception);
BOOST_CHECK_THROW(wm.create("/tmp/test"), wallet_exception);
BOOST_CHECK_THROW(wm.create("/tmp/"), wallet_exception);
BOOST_CHECK_THROW(wm.create("/"), wallet_exception);
BOOST_CHECK_THROW(wm.create(",/"), wallet_exception);
BOOST_CHECK_THROW(wm.create(","), wallet_exception);
BOOST_CHECK_THROW(wm.create("<<"), wallet_exception);
BOOST_CHECK_THROW(wm.create("<"), wallet_exception);
BOOST_CHECK_THROW(wm.create(",<"), wallet_exception);
BOOST_CHECK_THROW(wm.create(",<<"), wallet_exception);
BOOST_CHECK_THROW(wm.create(""), wallet_exception);
fc::remove("test.wallet");
wm.create(".test");
BOOST_CHECK(fc::exists(".test.wallet"));
fc::remove(".test.wallet");
wm.create("..test");
BOOST_CHECK(fc::exists("..test.wallet"));
fc::remove("..test.wallet");
wm.create("...test");
BOOST_CHECK(fc::exists("...test.wallet"));
fc::remove("...test.wallet");
wm.create(".");
BOOST_CHECK(fc::exists("..wallet"));
fc::remove("..wallet");
wm.create("__test_test");
BOOST_CHECK(fc::exists("__test_test.wallet"));
fc::remove("__test_test.wallet");
wm.create("t-t");
BOOST_CHECK(fc::exists("t-t.wallet"));
fc::remove("t-t.wallet");
} FC_LOG_AND_RETHROW()
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace eos