]> Git Repo - VerusCoin.git/blame - src/test/rpc_wallet_tests.cpp
fix rpcmining/getblocktemplate univalue transition logic error
[VerusCoin.git] / src / test / rpc_wallet_tests.cpp
CommitLineData
3fc68461 1// Copyright (c) 2013-2014 The Bitcoin Core developers
78253fcb 2// Distributed under the MIT software license, see the accompanying
3fc68461
WL
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5094f8d4
WL
5#include "rpcserver.h"
6#include "rpcclient.h"
7
8#include "base58.h"
8a893c94 9#include "main.h"
50c72f23 10#include "wallet/wallet.h"
5094f8d4 11
51598b26
PW
12#include "test/test_bitcoin.h"
13
60f762a5
S
14#include "zcash/Address.hpp"
15
b922924d
S
16#include "rpcserver.h"
17#include "asyncrpcqueue.h"
18#include "asyncrpcoperation.h"
19#include "wallet/asyncrpcoperation_sendmany.h"
fc4b127e 20#include "rpcprotocol.h"
7b79275e 21#include "init.h"
b922924d
S
22
23#include <chrono>
24#include <thread>
25
60f762a5
S
26#include <fstream>
27#include <unordered_set>
28
5094f8d4
WL
29#include <boost/algorithm/string.hpp>
30#include <boost/test/unit_test.hpp>
60f762a5 31#include <boost/format.hpp>
6be367ea 32#include <boost/filesystem.hpp>
5094f8d4 33
d014114d
JS
34#include "univalue/univalue.h"
35
5094f8d4 36using namespace std;
5094f8d4 37
851f58f9 38extern UniValue createArgs(int nRequired, const char* address1 = NULL, const char* address2 = NULL);
d014114d 39extern UniValue CallRPC(string args);
5094f8d4 40
fd67424c
GA
41extern CWallet* pwalletMain;
42
da5e7e51
S
43bool find_error(const Object& objError, const std::string& expected) {
44 return find_value(objError, "message").get_str().find(expected) != string::npos;
45}
46
51598b26 47BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
5094f8d4
WL
48
49BOOST_AUTO_TEST_CASE(rpc_addmultisig)
50{
fd67424c
GA
51 LOCK(pwalletMain->cs_wallet);
52
5094f8d4
WL
53 rpcfn_type addmultisig = tableRPC["addmultisigaddress"]->actor;
54
55 // old, 65-byte-long:
56 const char address1Hex[] = "0434e3e09f49ea168c5bbf53f877ff4206923858aab7c7e1df25bc263978107c95e35065a27ef6f1b27222db0ec97e0e895eaca603d3ee0d4c060ce3d8a00286c8";
57 // new, compressed:
58 const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4";
59
d014114d 60 UniValue v;
5094f8d4
WL
61 CBitcoinAddress address;
62 BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false));
63 address.SetString(v.get_str());
64 BOOST_CHECK(address.IsValid() && address.IsScript());
65
66 BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false));
67 address.SetString(v.get_str());
68 BOOST_CHECK(address.IsValid() && address.IsScript());
69
70 BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false));
71 address.SetString(v.get_str());
72 BOOST_CHECK(address.IsValid() && address.IsScript());
73
74 BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error);
75 BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error);
76 BOOST_CHECK_THROW(addmultisig(createArgs(2, address1Hex), false), runtime_error);
77
78 BOOST_CHECK_THROW(addmultisig(createArgs(1, ""), false), runtime_error);
79 BOOST_CHECK_THROW(addmultisig(createArgs(1, "NotAValidPubkey"), false), runtime_error);
80
bc470c43 81 string short1(address1Hex, address1Hex + sizeof(address1Hex) - 2); // last byte missing
5094f8d4
WL
82 BOOST_CHECK_THROW(addmultisig(createArgs(2, short1.c_str()), false), runtime_error);
83
bc470c43 84 string short2(address1Hex + 1, address1Hex + sizeof(address1Hex)); // first byte missing
5094f8d4
WL
85 BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error);
86}
87
88BOOST_AUTO_TEST_CASE(rpc_wallet)
89{
90 // Test RPC calls for various wallet statistics
d014114d 91 UniValue r;
5094f8d4 92
ed671005 93 LOCK2(cs_main, pwalletMain->cs_wallet);
fd67424c 94
75ebced4 95 CPubKey demoPubkey = pwalletMain->GenerateNewKey();
bc470c43 96 CBitcoinAddress demoAddress = CBitcoinAddress(CTxDestination(demoPubkey.GetID()));
d014114d 97 UniValue retValue;
b6f100cf 98 string strAccount = "";
bc470c43
ES
99 string strPurpose = "receive";
100 BOOST_CHECK_NO_THROW({ /*Initialize Wallet with an account */
101 CWalletDB walletdb(pwalletMain->strWalletFile);
102 CAccount account;
103 account.vchPubKey = demoPubkey;
104 pwalletMain->SetAddressBook(account.vchPubKey.GetID(), strAccount, strPurpose);
105 walletdb.WriteAccount(strAccount, account);
106 });
75ebced4 107
31d6390f 108 CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey();
bc470c43
ES
109 CBitcoinAddress setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID()));
110
111 /*********************************
112 * setaccount
113 *********************************/
b6f100cf
JG
114 BOOST_CHECK_NO_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " \"\""));
115 /* Accounts are disabled */
116 BOOST_CHECK_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " nullaccount"), runtime_error);
ee64b5e7
JG
117 /* t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV is not owned by the test wallet. */
118 BOOST_CHECK_THROW(CallRPC("setaccount t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV nullaccount"), runtime_error);
bc470c43 119 BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error);
ee64b5e7
JG
120 /* t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg (34 chars) is an illegal address (should be 35 chars) */
121 BOOST_CHECK_THROW(CallRPC("setaccount t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg nullaccount"), runtime_error);
75ebced4 122
7c5dd603
EF
123
124 /*********************************
125 * getbalance
126 *********************************/
127 BOOST_CHECK_NO_THROW(CallRPC("getbalance"));
b6f100cf 128 BOOST_CHECK_THROW(CallRPC("getbalance " + demoAddress.ToString()), runtime_error);
7c5dd603 129
75ebced4
AM
130 /*********************************
131 * listunspent
132 *********************************/
5094f8d4
WL
133 BOOST_CHECK_NO_THROW(CallRPC("listunspent"));
134 BOOST_CHECK_THROW(CallRPC("listunspent string"), runtime_error);
135 BOOST_CHECK_THROW(CallRPC("listunspent 0 string"), runtime_error);
136 BOOST_CHECK_THROW(CallRPC("listunspent 0 1 not_array"), runtime_error);
137 BOOST_CHECK_THROW(CallRPC("listunspent 0 1 [] extra"), runtime_error);
bc470c43 138 BOOST_CHECK_NO_THROW(r = CallRPC("listunspent 0 1 []"));
5094f8d4
WL
139 BOOST_CHECK(r.get_array().empty());
140
75ebced4 141 /*********************************
bc470c43
ES
142 * listreceivedbyaddress
143 *********************************/
5094f8d4
WL
144 BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress"));
145 BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0"));
146 BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress not_int"), runtime_error);
147 BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 not_bool"), runtime_error);
148 BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0 true"));
149 BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 true extra"), runtime_error);
150
75ebced4 151 /*********************************
bc470c43
ES
152 * listreceivedbyaccount
153 *********************************/
5094f8d4
WL
154 BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount"));
155 BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0"));
156 BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount not_int"), runtime_error);
157 BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 not_bool"), runtime_error);
158 BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0 true"));
159 BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 true extra"), runtime_error);
75ebced4 160
7c5dd603
EF
161 /*********************************
162 * listsinceblock
163 *********************************/
164 BOOST_CHECK_NO_THROW(CallRPC("listsinceblock"));
165
166 /*********************************
167 * listtransactions
168 *********************************/
169 BOOST_CHECK_NO_THROW(CallRPC("listtransactions"));
170 BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString()));
171 BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20"));
172 BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20 0"));
173 BOOST_CHECK_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " not_int"), runtime_error);
174
175 /*********************************
176 * listlockunspent
177 *********************************/
178 BOOST_CHECK_NO_THROW(CallRPC("listlockunspent"));
179
180 /*********************************
181 * listaccounts
182 *********************************/
183 BOOST_CHECK_NO_THROW(CallRPC("listaccounts"));
184
185 /*********************************
186 * listaddressgroupings
187 *********************************/
188 BOOST_CHECK_NO_THROW(CallRPC("listaddressgroupings"));
189
75ebced4 190 /*********************************
bc470c43
ES
191 * getrawchangeaddress
192 *********************************/
75ebced4
AM
193 BOOST_CHECK_NO_THROW(CallRPC("getrawchangeaddress"));
194
195 /*********************************
bc470c43
ES
196 * getnewaddress
197 *********************************/
75ebced4 198 BOOST_CHECK_NO_THROW(CallRPC("getnewaddress"));
b6f100cf
JG
199 BOOST_CHECK_NO_THROW(CallRPC("getnewaddress \"\""));
200 /* Accounts are deprecated */
201 BOOST_CHECK_THROW(CallRPC("getnewaddress getnewaddress_demoaccount"), runtime_error);
75ebced4
AM
202
203 /*********************************
bc470c43
ES
204 * getaccountaddress
205 *********************************/
75ebced4 206 BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress \"\""));
b6f100cf
JG
207 /* Accounts are deprecated */
208 BOOST_CHECK_THROW(CallRPC("getaccountaddress accountThatDoesntExists"), runtime_error);
bc470c43
ES
209 BOOST_CHECK_NO_THROW(retValue = CallRPC("getaccountaddress " + strAccount));
210 BOOST_CHECK(CBitcoinAddress(retValue.get_str()).Get() == demoAddress.Get());
75ebced4 211
bc470c43
ES
212 /*********************************
213 * getaccount
214 *********************************/
215 BOOST_CHECK_THROW(CallRPC("getaccount"), runtime_error);
216 BOOST_CHECK_NO_THROW(CallRPC("getaccount " + demoAddress.ToString()));
217
218 /*********************************
219 * signmessage + verifymessage
220 *********************************/
221 BOOST_CHECK_NO_THROW(retValue = CallRPC("signmessage " + demoAddress.ToString() + " mymessage"));
222 BOOST_CHECK_THROW(CallRPC("signmessage"), runtime_error);
223 /* Should throw error because this address is not loaded in the wallet */
ee64b5e7 224 BOOST_CHECK_THROW(CallRPC("signmessage t1h8SqgtM3QM5e2M8EzhhT1yL2PXXtA6oqe mymessage"), runtime_error);
bc470c43
ES
225
226 /* missing arguments */
227 BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString()), runtime_error);
228 BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str()), runtime_error);
229 /* Illegal address */
ee64b5e7 230 BOOST_CHECK_THROW(CallRPC("verifymessage t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg " + retValue.get_str() + " mymessage"), runtime_error);
bc470c43 231 /* wrong address */
ee64b5e7 232 BOOST_CHECK(CallRPC("verifymessage t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV " + retValue.get_str() + " mymessage").get_bool() == false);
bc470c43
ES
233 /* Correct address and signature but wrong message */
234 BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " wrongmessage").get_bool() == false);
235 /* Correct address, message and signature*/
236 BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " mymessage").get_bool() == true);
237
238 /*********************************
239 * getaddressesbyaccount
240 *********************************/
241 BOOST_CHECK_THROW(CallRPC("getaddressesbyaccount"), runtime_error);
242 BOOST_CHECK_NO_THROW(retValue = CallRPC("getaddressesbyaccount " + strAccount));
851f58f9 243 UniValue arr = retValue.get_array();
b6f100cf
JG
244 BOOST_CHECK_EQUAL(4, arr.size());
245 bool notFound = true;
246 for (auto a : arr) {
247 notFound &= CBitcoinAddress(a.get_str()).Get() != demoAddress.Get();
248 }
249 BOOST_CHECK(!notFound);
5d50130b
S
250
251 /*
252 * getblocksubsidy
253 */
254 BOOST_CHECK_THROW(CallRPC("getblocksubsidy too many args"), runtime_error);
255 BOOST_CHECK_THROW(CallRPC("getblocksubsidy -1"), runtime_error);
256 BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 50000"));
257 Object obj = retValue.get_obj();
258 BOOST_CHECK(find_value(obj, "miner") == 10.0);
259 BOOST_CHECK(find_value(obj, "founders") == 2.5);
260 BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 1000000"));
261 obj = retValue.get_obj();
262 BOOST_CHECK(find_value(obj, "miner") == 6.25);
263 BOOST_CHECK(find_value(obj, "founders") == 0.0);
264 BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 2000000"));
265 obj = retValue.get_obj();
266 BOOST_CHECK(find_value(obj, "miner") == 3.125);
267 BOOST_CHECK(find_value(obj, "founders") == 0.0);
5094f8d4
WL
268}
269
e883ffef
S
270BOOST_AUTO_TEST_CASE(rpc_wallet_getbalance)
271{
272 SelectParams(CBaseChainParams::TESTNET);
273
274 LOCK(pwalletMain->cs_wallet);
275
276
277 BOOST_CHECK_THROW(CallRPC("z_getbalance too many args"), runtime_error);
278 BOOST_CHECK_THROW(CallRPC("z_getbalance invalidaddress"), runtime_error);
ee64b5e7
JG
279 BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab"));
280 BOOST_CHECK_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
281 BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab 0"));
e883ffef
S
282 BOOST_CHECK_THROW(CallRPC("z_getbalance tnRZ8bPq2pff3xBWhTJhNkVUkm2uhzksDeW5PvEa7aFKGT9Qi3YgTALZfjaY4jU3HLVKBtHdSXxoPoLA3naMPcHBcY88FcF 1"), runtime_error);
283
284
285 BOOST_CHECK_THROW(CallRPC("z_gettotalbalance too manyargs"), runtime_error);
286 BOOST_CHECK_THROW(CallRPC("z_gettotalbalance -1"), runtime_error);
287 BOOST_CHECK_NO_THROW(CallRPC("z_gettotalbalance 0"));
288
289
290 BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress too many args"), runtime_error);
291 // negative minconf not allowed
ee64b5e7 292 BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
e883ffef 293 // invalid zaddr, taddr not allowed
ee64b5e7 294 BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab 0"), runtime_error);
e883ffef
S
295 // don't have the spending key
296 BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tnRZ8bPq2pff3xBWhTJhNkVUkm2uhzksDeW5PvEa7aFKGT9Qi3YgTALZfjaY4jU3HLVKBtHdSXxoPoLA3naMPcHBcY88FcF 1"), runtime_error);
297}
298
4e16a724
S
299/**
300 * This test covers RPC command z_validateaddress
301 */
302BOOST_AUTO_TEST_CASE(rpc_wallet_z_validateaddress)
303{
304 SelectParams(CBaseChainParams::MAIN);
305
306 LOCK2(cs_main, pwalletMain->cs_wallet);
307
308 Value retValue;
309
310 // Check number of args
311 BOOST_CHECK_THROW(CallRPC("z_validateaddress"), runtime_error);
312 BOOST_CHECK_THROW(CallRPC("z_validateaddress toomany args"), runtime_error);
313
314 // Wallet should be empty
315 std::set<libzcash::PaymentAddress> addrs;
316 pwalletMain->GetPaymentAddresses(addrs);
317 BOOST_CHECK(addrs.size()==0);
318
319 // This address is not valid, it belongs to another network
320 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_validateaddress ztaaga95QAPyp1kSQ1hD2kguCpzyMHjxWZqaYDEkzbvo7uYQYAw2S8X4Kx98AvhhofMtQL8PAXKHuZsmhRcanavKRKmdCzk"));
321 Object resultObj = retValue.get_obj();
322 bool b = find_value(resultObj, "isvalid").get_bool();
323 BOOST_CHECK_EQUAL(b, false);
324
325 // This address is valid, but the spending key is not in this wallet
326 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_validateaddress zcfA19SDAKRYHLoRDoShcoz4nPohqWxuHcqg8WAxsiB2jFrrs6k7oSvst3UZvMYqpMNSRBkxBsnyjjngX5L55FxMzLKach8"));
327 resultObj = retValue.get_obj();
328 b = find_value(resultObj, "isvalid").get_bool();
329 BOOST_CHECK_EQUAL(b, true);
330 b = find_value(resultObj, "ismine").get_bool();
331 BOOST_CHECK_EQUAL(b, false);
332
333 // Let's import a spending key to the wallet and validate its payment address
334 BOOST_CHECK_NO_THROW(CallRPC("z_importkey SKxoWv77WGwFnUJitQKNEcD636bL4X5Gd6wWmgaA4Q9x8jZBPJXT"));
335 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_validateaddress zcWsmqT4X2V4jgxbgiCzyrAfRT1vi1F4sn7M5Pkh66izzw8Uk7LBGAH3DtcSMJeUb2pi3W4SQF8LMKkU2cUuVP68yAGcomL"));
336 resultObj = retValue.get_obj();
337 b = find_value(resultObj, "isvalid").get_bool();
338 BOOST_CHECK_EQUAL(b, true);
339 b = find_value(resultObj, "ismine").get_bool();
340 BOOST_CHECK_EQUAL(b, true);
341 BOOST_CHECK_EQUAL(find_value(resultObj, "payingkey").get_str(), "f5bb3c888ccc9831e3f6ba06e7528e26a312eec3acc1823be8918b6a3a5e20ad");
342 BOOST_CHECK_EQUAL(find_value(resultObj, "transmissionkey").get_str(), "7a58c7132446564e6b810cf895c20537b3528357dc00150a8e201f491efa9c1a");
343}
344
60f762a5
S
345/*
346 * This test covers RPC command z_exportwallet
347 */
348BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet)
349{
350 LOCK2(cs_main, pwalletMain->cs_wallet);
4e16a724 351
4b2e5571 352 // wallet should be empty
60f762a5
S
353 std::set<libzcash::PaymentAddress> addrs;
354 pwalletMain->GetPaymentAddresses(addrs);
355 BOOST_CHECK(addrs.size()==0);
356
357 // wallet should have one key
358 CZCPaymentAddress paymentAddress = pwalletMain->GenerateNewZKey();
359 pwalletMain->GetPaymentAddresses(addrs);
360 BOOST_CHECK(addrs.size()==1);
361
9064d73b
S
362 // Set up paths
363 boost::filesystem::path tmppath = boost::filesystem::temp_directory_path();
364 boost::filesystem::path tmpfilename = boost::filesystem::unique_path("%%%%%%%%");
365 boost::filesystem::path exportfilepath = tmppath / tmpfilename;
366
367 // export will fail since exportdir is not set
368 BOOST_CHECK_THROW(CallRPC(string("z_exportwallet ") + tmpfilename.string()), runtime_error);
369
370 // set exportdir
371 mapArgs["-exportdir"] = tmppath.native();
372
373 // run some tests
60f762a5 374 BOOST_CHECK_THROW(CallRPC("z_exportwallet"), runtime_error);
e346a0b3
S
375
376 BOOST_CHECK_THROW(CallRPC("z_exportwallet toomany args"), runtime_error);
377
9064d73b
S
378 BOOST_CHECK_THROW(CallRPC(string("z_exportwallet invalid!*/_chars.txt")), runtime_error);
379
380 BOOST_CHECK_NO_THROW(CallRPC(string("z_exportwallet ") + tmpfilename.string()));
60f762a5 381
60f762a5
S
382
383 auto addr = paymentAddress.Get();
384 libzcash::SpendingKey key;
385 BOOST_CHECK(pwalletMain->GetSpendingKey(addr, key));
386
387 std::string s1 = paymentAddress.ToString();
388 std::string s2 = CZCSpendingKey(key).ToString();
389
390 // There's no way to really delete a private key so we will read in the
391 // exported wallet file and search for the spending key and payment address.
392
393 EnsureWalletIsUnlocked();
394
395 ifstream file;
9064d73b 396 file.open(exportfilepath.string().c_str(), std::ios::in | std::ios::ate);
60f762a5
S
397 BOOST_CHECK(file.is_open());
398 bool fVerified = false;
399 int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
400 file.seekg(0, file.beg);
401 while (file.good()) {
402 std::string line;
403 std::getline(file, line);
404 if (line.empty() || line[0] == '#')
405 continue;
406 if (line.find(s1) != std::string::npos && line.find(s2) != std::string::npos) {
407 fVerified = true;
408 break;
409 }
410 }
411 BOOST_CHECK(fVerified);
412}
413
414
415/*
416 * This test covers RPC command z_importwallet
417 */
418BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
419{
420 LOCK2(cs_main, pwalletMain->cs_wallet);
421
422 // error if no args
423 BOOST_CHECK_THROW(CallRPC("z_importwallet"), runtime_error);
424
e346a0b3
S
425 // error if too many args
426 BOOST_CHECK_THROW(CallRPC("z_importwallet toomany args"), runtime_error);
427
60f762a5
S
428 // create a random key locally
429 auto testSpendingKey = libzcash::SpendingKey::random();
430 auto testPaymentAddress = testSpendingKey.address();
431 std::string testAddr = CZCPaymentAddress(testPaymentAddress).ToString();
432 std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
60f762a5
S
433
434 // create test data using the random key
435 std::string format_str = "# Wallet dump created by Zcash v0.11.2.0.z8-9155cc6-dirty (2016-08-11 11:37:00 -0700)\n"
436 "# * Created on 2016-08-12T21:55:36Z\n"
437 "# * Best block at time of backup was 0 (0de0a3851fef2d433b9b4f51d4342bdd24c5ddd793eb8fba57189f07e9235d52),\n"
438 "# mined on 2009-01-03T18:15:05Z\n"
439 "\n"
440 "# Zkeys\n"
441 "\n"
442 "%s 2016-08-12T21:55:36Z # zaddr=%s\n"
443 "\n"
444 "\n# End of dump";
445
446 boost::format formatobject(format_str);
447 std::string testWalletDump = (formatobject % testKey % testAddr).str();
60f762a5
S
448
449 // write test data to file
450 boost::filesystem::path temp = boost::filesystem::temp_directory_path() /
451 boost::filesystem::unique_path();
452 const std::string path = temp.native();
453 std::ofstream file(path);
454 file << testWalletDump;
455 file << std::flush;
456
457 // wallet should currently be empty
458 std::set<libzcash::PaymentAddress> addrs;
459 pwalletMain->GetPaymentAddresses(addrs);
460 BOOST_CHECK(addrs.size()==0);
461
462 // import test data from file into wallet
463 BOOST_CHECK_NO_THROW(CallRPC(string("z_importwallet ") + path));
464
465 // wallet should now have one zkey
466 pwalletMain->GetPaymentAddresses(addrs);
467 BOOST_CHECK(addrs.size()==1);
468
469 // check that we have the spending key for the address
470 CZCPaymentAddress address(testAddr);
471 auto addr = address.Get();
472 BOOST_CHECK(pwalletMain->HaveSpendingKey(addr));
473
4b2e5571 474 // Verify the spending key is the same as the test data
60f762a5
S
475 libzcash::SpendingKey k;
476 BOOST_CHECK(pwalletMain->GetSpendingKey(addr, k));
477 CZCSpendingKey spendingkey(k);
478 BOOST_CHECK_EQUAL(testKey, spendingkey.ToString());
60f762a5
S
479}
480
481
482/*
4b2e5571 483 * This test covers RPC commands z_listaddresses, z_importkey, z_exportkey
60f762a5
S
484 */
485BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
486{
487 LOCK2(cs_main, pwalletMain->cs_wallet);
488 Value retValue;
489 int n1 = 1000; // number of times to import/export
490 int n2 = 1000; // number of addresses to create and list
491
492 // error if no args
493 BOOST_CHECK_THROW(CallRPC("z_importkey"), runtime_error);
494 BOOST_CHECK_THROW(CallRPC("z_exportkey"), runtime_error);
495
e346a0b3
S
496 // error if too many args
497 BOOST_CHECK_THROW(CallRPC("z_importkey too many args"), runtime_error);
498 BOOST_CHECK_THROW(CallRPC("z_exportkey toomany args"), runtime_error);
499
60f762a5
S
500 // wallet should currently be empty
501 std::set<libzcash::PaymentAddress> addrs;
502 pwalletMain->GetPaymentAddresses(addrs);
503 BOOST_CHECK(addrs.size()==0);
504
505 // verify import and export key
506 for (int i = 0; i < n1; i++) {
507 // create a random key locally
508 auto testSpendingKey = libzcash::SpendingKey::random();
509 auto testPaymentAddress = testSpendingKey.address();
510 std::string testAddr = CZCPaymentAddress(testPaymentAddress).ToString();
511 std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
512 BOOST_CHECK_NO_THROW(CallRPC(string("z_importkey ") + testKey));
513 BOOST_CHECK_NO_THROW(retValue = CallRPC(string("z_exportkey ") + testAddr));
514 BOOST_CHECK_EQUAL(retValue.get_str(), testKey);
515 }
516
517 // Verify we can list the keys imported
518 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
519 Array arr = retValue.get_array();
520 BOOST_CHECK(arr.size() == n1);
521
522 // Put addresses into a set
523 std::unordered_set<std::string> myaddrs;
524 for (Value element : arr) {
525 myaddrs.insert(element.get_str());
526 }
527
528 // Make new addresses for the set
529 for (int i=0; i<n2; i++) {
530 myaddrs.insert((pwalletMain->GenerateNewZKey()).ToString());
531 }
532
533 // Verify number of addresses stored in wallet is n1+n2
534 int numAddrs = myaddrs.size();
535 BOOST_CHECK(numAddrs == n1+n2);
536 pwalletMain->GetPaymentAddresses(addrs);
537 BOOST_CHECK(addrs.size()==numAddrs);
538
539 // Ask wallet to list addresses
540 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
541 arr = retValue.get_array();
542 BOOST_CHECK(arr.size() == numAddrs);
543
4b2e5571 544 // Create a set from them
60f762a5
S
545 std::unordered_set<std::string> listaddrs;
546 for (Value element : arr) {
547 listaddrs.insert(element.get_str());
548 }
549
550 // Verify the two sets of addresses are the same
551 BOOST_CHECK(listaddrs.size() == numAddrs);
552 BOOST_CHECK(myaddrs == listaddrs);
badb9a9c
S
553
554 // Add one more address
555 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getnewaddress"));
556 std::string newaddress = retValue.get_str();
557 CZCPaymentAddress pa(newaddress);
558 auto newAddr = pa.Get();
559 BOOST_CHECK(pwalletMain->HaveSpendingKey(newAddr));
e346a0b3
S
560
561 // Check if too many args
562 BOOST_CHECK_THROW(CallRPC("z_getnewaddress toomanyargs"), runtime_error);
60f762a5 563}
b922924d
S
564
565
566
567/**
fc4b127e 568 * Test Async RPC operations.
b922924d
S
569 * Tip: Create mock operations by subclassing AsyncRPCOperation.
570 */
fc4b127e 571
b922924d
S
572class MockSleepOperation : public AsyncRPCOperation {
573public:
574 std::chrono::milliseconds naptime;
575 MockSleepOperation(int t=1000) {
576 this->naptime = std::chrono::milliseconds(t);
577 }
578 virtual ~MockSleepOperation() {
579 }
580 virtual void main() {
581 set_state(OperationStatus::EXECUTING);
582 start_execution_clock();
583 std::this_thread::sleep_for(std::chrono::milliseconds(naptime));
584 stop_execution_clock();
585 set_result(Value("done"));
586 set_state(OperationStatus::SUCCESS);
587 }
588};
589
590
591/*
592 * Test Aysnc RPC queue and operations.
593 */
594BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations)
595{
596 std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
597 BOOST_CHECK(q->getNumberOfWorkers() == 0);
598 std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
599 BOOST_CHECK(ids.size()==0);
600
601 std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
602 q->addOperation(op1);
603 BOOST_CHECK(q->getOperationCount() == 1);
604
605 OperationStatus status = op1->getState();
606 BOOST_CHECK(status == OperationStatus::READY);
607
608 AsyncRPCOperationId id1 = op1->getId();
609 int64_t creationTime1 = op1->getCreationTime();
610
611 q->addWorker();
612 BOOST_CHECK(q->getNumberOfWorkers() == 1);
613
614 // an AsyncRPCOperation doesn't do anything so will finish immediately
615 std::this_thread::sleep_for(std::chrono::seconds(1));
616 BOOST_CHECK(q->getOperationCount() == 0);
617
618 // operation should be a success
619 BOOST_CHECK_EQUAL(op1->isCancelled(), false);
620 BOOST_CHECK_EQUAL(op1->isExecuting(), false);
621 BOOST_CHECK_EQUAL(op1->isReady(), false);
622 BOOST_CHECK_EQUAL(op1->isFailed(), false);
623 BOOST_CHECK_EQUAL(op1->isSuccess(), true);
624 BOOST_CHECK(op1->getError() == Value::null );
625 BOOST_CHECK(op1->getResult().is_null() == false );
626 BOOST_CHECK_EQUAL(op1->getStateAsString(), "success");
627 BOOST_CHECK_NE(op1->getStateAsString(), "executing");
628
629 // Create a second operation which just sleeps
630 std::shared_ptr<AsyncRPCOperation> op2(new MockSleepOperation(2500));
631 AsyncRPCOperationId id2 = op2->getId();
632 int64_t creationTime2 = op2->getCreationTime();
633
634 // it's different from the previous operation
635 BOOST_CHECK_NE(op1.get(), op2.get());
636 BOOST_CHECK_NE(id1, id2);
637 BOOST_CHECK_NE(creationTime1, creationTime2);
638
639 // Only the first operation has been added to the queue
640 std::vector<AsyncRPCOperationId> v = q->getAllOperationIds();
641 std::set<AsyncRPCOperationId> opids(v.begin(), v.end());
642 BOOST_CHECK(opids.size() == 1);
643 BOOST_CHECK(opids.count(id1)==1);
644 BOOST_CHECK(opids.count(id2)==0);
645 std::shared_ptr<AsyncRPCOperation> p1 = q->getOperationForId(id1);
646 BOOST_CHECK_EQUAL(p1.get(), op1.get());
647 std::shared_ptr<AsyncRPCOperation> p2 = q->getOperationForId(id2);
648 BOOST_CHECK(!p2); // null ptr as not added to queue yet
649
650 // Add operation 2 and 3 to the queue
651 q->addOperation(op2);
652 std::shared_ptr<AsyncRPCOperation> op3(new MockSleepOperation(1000));
653 q->addOperation(op3);
654 std::this_thread::sleep_for(std::chrono::milliseconds(500));
655 BOOST_CHECK_EQUAL(op2->isExecuting(), true);
656 op2->cancel(); // too late, already executing
657 op3->cancel();
658 std::this_thread::sleep_for(std::chrono::milliseconds(3000));
659 BOOST_CHECK_EQUAL(op2->isSuccess(), true);
660 BOOST_CHECK_EQUAL(op2->isCancelled(), false);
661 BOOST_CHECK_EQUAL(op3->isCancelled(), true);
662
663
664 v = q->getAllOperationIds();
665 std::copy( v.begin(), v.end(), std::inserter( opids, opids.end() ) );
666 BOOST_CHECK(opids.size() == 3);
667 BOOST_CHECK(opids.count(id1)==1);
668 BOOST_CHECK(opids.count(id2)==1);
669 BOOST_CHECK(opids.count(op3->getId())==1);
670 q->finishAndWait();
671}
672
673
674// The CountOperation will increment this global
675std::atomic<int64_t> gCounter(0);
676
677class CountOperation : public AsyncRPCOperation {
678public:
679 CountOperation() {}
680 virtual ~CountOperation() {}
681 virtual void main() {
682 set_state(OperationStatus::EXECUTING);
683 gCounter++;
684 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
685 set_state(OperationStatus::SUCCESS);
686 }
687};
688
689// This tests the queue waiting for multiple workers to finish
690BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_wait)
691{
692 gCounter = 0;
693
694 std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
695 q->addWorker();
696 q->addWorker();
697 q->addWorker();
698 q->addWorker();
699 BOOST_CHECK(q->getNumberOfWorkers() == 4);
700
701 int64_t numOperations = 10; // 10 * 1000ms / 4 = 2.5 secs to finish
702 for (int i=0; i<numOperations; i++) {
703 std::shared_ptr<AsyncRPCOperation> op(new CountOperation());
704 q->addOperation(op);
705 }
706
707 std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
708 BOOST_CHECK(ids.size()==numOperations);
709 q->finishAndWait();
710 BOOST_CHECK_EQUAL(q->isFinishing(), true);
711 BOOST_CHECK_EQUAL(numOperations, gCounter.load());
712}
713
714// This tests the queue shutting down immediately
715BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_cancel)
716{
717 gCounter = 0;
718
719 std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
720 q->addWorker();
721 q->addWorker();
722 BOOST_CHECK(q->getNumberOfWorkers() == 2);
723
fc4b127e 724 int numOperations = 10000; // 10000 seconds to complete
b922924d
S
725 for (int i=0; i<numOperations; i++) {
726 std::shared_ptr<AsyncRPCOperation> op(new CountOperation());
727 q->addOperation(op);
728 }
729 std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
730 BOOST_CHECK(ids.size()==numOperations);
731 q->closeAndWait();
b922924d
S
732
733 int numSuccess = 0;
734 int numCancelled = 0;
735 for (auto & id : ids) {
736 std::shared_ptr<AsyncRPCOperation> ptr = q->popOperationForId(id);
737 if (ptr->isCancelled()) {
738 numCancelled++;
739 } else if (ptr->isSuccess()) {
740 numSuccess++;
741 }
742 }
60f762a5 743
b922924d
S
744 BOOST_CHECK_EQUAL(numOperations, numSuccess+numCancelled);
745 BOOST_CHECK_EQUAL(gCounter.load(), numSuccess);
746 BOOST_CHECK(q->getOperationCount() == 0);
747 ids = q->getAllOperationIds();
748 BOOST_CHECK(ids.size()==0);
749}
750
fc4b127e
S
751// This tests z_getoperationstatus, z_getoperationresult, z_listoperationids
752BOOST_AUTO_TEST_CASE(rpc_z_getoperations)
753{
754 std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
755 std::shared_ptr<AsyncRPCQueue> sharedInstance = AsyncRPCQueue::sharedInstance();
756 BOOST_CHECK(q == sharedInstance);
757
758 BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus"));
759 BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus []"));
760 BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus [\"opid-1234\"]"));
761 BOOST_CHECK_THROW(CallRPC("z_getoperationstatus [] toomanyargs"), runtime_error);
762 BOOST_CHECK_THROW(CallRPC("z_getoperationstatus not_an_array"), runtime_error);
763
764 BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult"));
765 BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult []"));
766 BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult [\"opid-1234\"]"));
767 BOOST_CHECK_THROW(CallRPC("z_getoperationresult [] toomanyargs"), runtime_error);
768 BOOST_CHECK_THROW(CallRPC("z_getoperationresult not_an_array"), runtime_error);
769
770 std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
771 q->addOperation(op1);
772 std::shared_ptr<AsyncRPCOperation> op2 = std::make_shared<AsyncRPCOperation>();
773 q->addOperation(op2);
774
775 BOOST_CHECK(q->getOperationCount() == 2);
776 BOOST_CHECK(q->getNumberOfWorkers() == 0);
777 q->addWorker();
778 BOOST_CHECK(q->getNumberOfWorkers() == 1);
779 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
780 BOOST_CHECK(q->getOperationCount() == 0);
781
e346a0b3
S
782 // Check if too many args
783 BOOST_CHECK_THROW(CallRPC("z_listoperationids toomany args"), runtime_error);
784
fc4b127e
S
785 Value retValue;
786 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listoperationids"));
787 BOOST_CHECK(retValue.get_array().size() == 2);
788
789 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
790 Array array = retValue.get_array();
791 BOOST_CHECK(array.size() == 2);
792
793 // idempotent
794 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
795 array = retValue.get_array();
796 BOOST_CHECK(array.size() == 2);
797
798 for (Value v : array) {
799 Object obj = v.get_obj();
800 Value id = find_value(obj, "id");
801
802 Value result;
803 // removes result from internal storage
804 BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
805 Array resultArray = result.get_array();
806 BOOST_CHECK(resultArray.size() == 1);
807
808 Object resultObj = resultArray.front().get_obj();
809 Value resultId = find_value(resultObj, "id");
810 BOOST_CHECK_EQUAL(id.get_str(), resultId.get_str());
da5e7e51
S
811
812 // verify the operation has been removed
813 BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
814 resultArray = result.get_array();
815 BOOST_CHECK(resultArray.size() == 0);
fc4b127e
S
816 }
817
818 // operations removed
819 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
820 array = retValue.get_array();
821 BOOST_CHECK(array.size() == 0);
822
823 q->close();
824}
825
826BOOST_AUTO_TEST_CASE(rpc_z_sendmany_parameters)
827{
828 SelectParams(CBaseChainParams::TESTNET);
829
830 LOCK(pwalletMain->cs_wallet);
831
832 BOOST_CHECK_THROW(CallRPC("z_sendmany"), runtime_error);
833 BOOST_CHECK_THROW(CallRPC("z_sendmany toofewargs"), runtime_error);
af53da02 834 BOOST_CHECK_THROW(CallRPC("z_sendmany just too many args here"), runtime_error);
fc4b127e
S
835
836 // bad from address
da5e7e51 837 BOOST_CHECK_THROW(CallRPC("z_sendmany "
ee64b5e7 838 "INVALIDtmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ []"), runtime_error);
fc4b127e 839 // empty amounts
da5e7e51 840 BOOST_CHECK_THROW(CallRPC("z_sendmany "
ee64b5e7 841 "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ []"), runtime_error);
fc4b127e
S
842
843 // don't have the spending key for this address
da5e7e51
S
844 BOOST_CHECK_THROW(CallRPC("z_sendmany "
845 "tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"
846 "UkJ1oSfbhTJhm72WiZizvkZz5aH1 []"), runtime_error);
fc4b127e
S
847
848 // duplicate address
da5e7e51 849 BOOST_CHECK_THROW(CallRPC("z_sendmany "
ee64b5e7
JG
850 "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
851 "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0},"
852 " {\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":12.0} ]"
da5e7e51 853 ), runtime_error);
fc4b127e 854
af53da02
S
855 // invalid fee amount, cannot be negative
856 BOOST_CHECK_THROW(CallRPC("z_sendmany "
857 "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
858 "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0}] "
859 "1 -0.0001"
860 ), runtime_error);
861
862 // invalid fee amount, bigger than MAX_MONEY
863 BOOST_CHECK_THROW(CallRPC("z_sendmany "
864 "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
865 "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0}] "
866 "1 21000001"
867 ), runtime_error);
868
869 // fee amount is bigger than sum of outputs
870 BOOST_CHECK_THROW(CallRPC("z_sendmany "
871 "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
872 "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0}] "
873 "1 50.00000001"
874 ), runtime_error);
875
fc4b127e 876 // memo bigger than allowed length of ZC_MEMO_SIZE
cff6f0ac
S
877 std::vector<char> v (2 * (ZC_MEMO_SIZE+1)); // x2 for hexadecimal string format
878 std::fill(v.begin(),v.end(), 'A');
fc4b127e
S
879 std::string badmemo(v.begin(), v.end());
880 CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
881 std::string zaddr1 = pa.ToString();
ee64b5e7 882 BOOST_CHECK_THROW(CallRPC(string("z_sendmany tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ ")
da5e7e51 883 + "[{\"address\":\"" + zaddr1 + "\", \"amount\":123.456}]"), runtime_error);
fc4b127e
S
884
885 // Test constructor of AsyncRPCOperation_sendmany
886 try {
887 std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany("",{}, {}, -1));
888 } catch (const Object& objError) {
da5e7e51 889 BOOST_CHECK( find_error(objError, "Minconf cannot be negative"));
fc4b127e
S
890 }
891
892 try {
893 std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany("",{}, {}, 1));
894 } catch (const Object& objError) {
da5e7e51 895 BOOST_CHECK( find_error(objError, "From address parameter missing"));
fc4b127e
S
896 }
897
898 try {
ee64b5e7 899 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ", {}, {}, 1) );
fc4b127e 900 } catch (const Object& objError) {
da5e7e51 901 BOOST_CHECK( find_error(objError, "No recipients"));
fc4b127e
S
902 }
903
904 try {
905 std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
906 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("INVALID", recipients, {}, 1) );
907 } catch (const Object& objError) {
da5e7e51 908 BOOST_CHECK( find_error(objError, "payment address is invalid"));
fc4b127e
S
909 }
910
f92f0047 911 // Testnet payment addresses begin with 'zt'. This test detects an incorrect prefix.
fc4b127e
S
912 try {
913 std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
914 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("zcMuhvq8sEkHALuSU2i4NbNQxshSAYrpCExec45ZjtivYPbuiFPwk6WHy4SvsbeZ4siy1WheuRGjtaJmoD1J8bFqNXhsG6U", recipients, {}, 1) );
915 } catch (const Object& objError) {
da5e7e51 916 BOOST_CHECK( find_error(objError, "payment address is for wrong network type"));
fc4b127e
S
917 }
918
919 // Note: The following will crash as a google test because AsyncRPCOperation_sendmany
920 // invokes a method on pwalletMain, which is undefined in the google test environment.
921 try {
922 std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
f92f0047 923 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("ztjiDe569DPNbyTE6TSdJTaSDhoXEHLGvYoUnBU1wfVNU52TEyT6berYtySkd21njAeEoh8fFJUT42kua9r8EnhBaEKqCpP", recipients, {}, 1) );
fc4b127e 924 } catch (const Object& objError) {
da5e7e51 925 BOOST_CHECK( find_error(objError, "no spending key found for zaddr"));
fc4b127e
S
926 }
927}
928
cff6f0ac 929
fc4b127e
S
930// TODO: test private methods
931BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
932{
933 SelectParams(CBaseChainParams::TESTNET);
934
935 LOCK(pwalletMain->cs_wallet);
936
937 Value retValue;
938
939 // add keys manually
940 BOOST_CHECK_NO_THROW(retValue = CallRPC("getnewaddress"));
941 std::string taddr1 = retValue.get_str();
942 CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
943 std::string zaddr1 = pa.ToString();
944
945 // there are no utxos to spend
946 {
947 std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
948 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(taddr1, {}, recipients, 1) );
949 operation->main();
950 BOOST_CHECK(operation->isFailed());
951 std::string msg = operation->getErrorMessage();
952 BOOST_CHECK( msg.find("Insufficient funds, no UTXOs found") != string::npos);
953 }
954
955 // there are no unspent notes to spend
956 {
957 std::vector<SendManyRecipient> recipients = { SendManyRecipient(taddr1,100.0, "DEADBEEF") };
958 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
959 operation->main();
960 BOOST_CHECK(operation->isFailed());
961 std::string msg = operation->getErrorMessage();
962 BOOST_CHECK( msg.find("Insufficient funds, no unspent notes") != string::npos);
963 }
964
cff6f0ac
S
965 // get_memo_from_hex_string())
966 {
967 std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
968 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
969 std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
970 TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
971
972 std::string memo = "DEADBEEF";
973 boost::array<unsigned char, ZC_MEMO_SIZE> array = proxy.get_memo_from_hex_string(memo);
974 BOOST_CHECK_EQUAL(array[0], 0xDE);
975 BOOST_CHECK_EQUAL(array[1], 0xAD);
976 BOOST_CHECK_EQUAL(array[2], 0xBE);
977 BOOST_CHECK_EQUAL(array[3], 0xEF);
978 for (int i=4; i<ZC_MEMO_SIZE; i++) {
979 BOOST_CHECK_EQUAL(array[i], 0x00); // zero padding
980 }
981
982 // memo is longer than allowed
983 std::vector<char> v (2 * (ZC_MEMO_SIZE+1));
984 std::fill(v.begin(),v.end(), 'A');
985 std::string bigmemo(v.begin(), v.end());
986
987 try {
988 proxy.get_memo_from_hex_string(bigmemo);
989 } catch (const Object& objError) {
da5e7e51 990 BOOST_CHECK( find_error(objError, "too big"));
cff6f0ac
S
991 }
992
993 // invalid hexadecimal string
994 std::fill(v.begin(),v.end(), '@'); // not a hex character
995 std::string badmemo(v.begin(), v.end());
996
997 try {
998 proxy.get_memo_from_hex_string(badmemo);
999 } catch (const Object& objError) {
da5e7e51 1000 BOOST_CHECK( find_error(objError, "hexadecimal format"));
cff6f0ac
S
1001 }
1002
1003 // odd length hexadecimal string
1004 std::fill(v.begin(),v.end(), 'A');
1005 v.resize(v.size() - 1);
da5e7e51 1006 assert(v.size() %2 == 1); // odd length
cff6f0ac
S
1007 std::string oddmemo(v.begin(), v.end());
1008 try {
1009 proxy.get_memo_from_hex_string(oddmemo);
1010 } catch (const Object& objError) {
da5e7e51 1011 BOOST_CHECK( find_error(objError, "hexadecimal format"));
cff6f0ac
S
1012 }
1013 }
1014
1015
1016 // add_taddr_change_output_to_tx() will append a vout to a raw transaction
1017 {
1018 std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
1019 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
1020 std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1021 TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1022
1023 CTransaction tx = proxy.getTx();
1024 BOOST_CHECK(tx.vout.size() == 0);
1025
1026 CAmount amount = 123.456;
1027 proxy.add_taddr_change_output_to_tx(amount);
1028 tx = proxy.getTx();
1029 BOOST_CHECK(tx.vout.size() == 1);
1030 CTxOut out = tx.vout[0];
1031 BOOST_CHECK_EQUAL(out.nValue, amount);
1032
1033 amount = 1.111;
1034 proxy.add_taddr_change_output_to_tx(amount);
1035 tx = proxy.getTx();
1036 BOOST_CHECK(tx.vout.size() == 2);
1037 out = tx.vout[1];
1038 BOOST_CHECK_EQUAL(out.nValue, amount);
1039 }
1040
1041 // add_taddr_outputs_to_tx() will append many vouts to a raw transaction
1042 {
1043 std::vector<SendManyRecipient> recipients = {
ee64b5e7
JG
1044 SendManyRecipient("tmTGScYwiLMzHe4uGZtBYmuqoW4iEoYNMXt",CAmount(1.23), ""),
1045 SendManyRecipient("tmUSbHz3vxnwLvRyNDXbwkZxjVyDodMJEhh",CAmount(4.56), ""),
1046 SendManyRecipient("tmYZAXYPCP56Xa5JQWWPZuK7o7bfUQW6kkd",CAmount(7.89), ""),
cff6f0ac
S
1047 };
1048 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
1049 std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1050 TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1051
1052 proxy.add_taddr_outputs_to_tx();
1053
1054 CTransaction tx = proxy.getTx();
1055 BOOST_CHECK(tx.vout.size() == 3);
1056 BOOST_CHECK_EQUAL(tx.vout[0].nValue, CAmount(1.23));
1057 BOOST_CHECK_EQUAL(tx.vout[1].nValue, CAmount(4.56));
1058 BOOST_CHECK_EQUAL(tx.vout[2].nValue, CAmount(7.89));
1059 }
1060
1061
1062 // Raw joinsplit is a zaddr->zaddr
1063 {
1064 std::string raw = "020000000000000000000100000000000000001027000000000000183a0d4c46c369078705e39bcfebee59a978dbd210ce8de3efc9555a03fbabfd3cea16693d730c63850d7e48ccde79854c19adcb7e9dcd7b7d18805ee09083f6b16e1860729d2d4a90e2f2acd009cf78b5eb0f4a6ee4bdb64b1262d7ce9eb910c460b02022991e968d0c50ee44908e4ccccbc591d0053bcca154dd6d6fc400a29fa686af4682339832ccea362a62aeb9df0d5aa74f86a1e75ac0f48a8ccc41e0a940643c6c33e1d09223b0a46eaf47a1bb4407cfc12b1dcf83a29c0cef51e45c7876ca5b9e5bae86d92976eb3ef68f29cd29386a8be8451b50f82bf9da10c04651868655194da8f6ed3d241bb5b5ff93a3e2bbe44644544d88bcde5cc35978032ee92699c7a61fcbb395e7583f47e698c4d53ede54f956629400bf510fb5e22d03158cc10bdcaaf29e418ef18eb6480dd9c8b9e2a377809f9f32a556ef872febd0021d4ad013aa9f0b7255e98e408d302abefd33a71180b720271835b487ab309e160b06dfe51932120fb84a7ede16b20c53599a11071592109e10260f265ee60d48c62bfe24074020e9b586ce9e9356e68f2ad1a9538258234afe4b83a209f178f45202270eaeaeecaf2ce3100b2c5a714f75f35777a9ebff5ebf47059d2bbf6f3726190216468f2b152673b766225b093f3a2f827c86d6b48b42117fec1d0ac38dd7af700308dcfb02eba821612b16a2c164c47715b9b0c93900893b1aba2ea03765c94d87022db5be06ab338d1912e0936dfe87586d0a8ee49144a6cd2e306abdcb652faa3e0222739deb23154d778b50de75069a4a2cce1208cd1ced3cb4744c9888ce1c2fcd2e66dc31e62d3aa9e423d7275882525e9981f92e84ac85975b8660739407efbe1e34c2249420fde7e17db3096d5b22e83d051d01f0e6e7690dca7d168db338aadf0897fedac10de310db2b1bff762d322935dddbb60c2efb8b15d231fa17b84630371cb275c209f0c4c7d0c68b150ea5cd514122215e3f7fcfb351d69514788d67c2f3c8922581946e3a04bdf1f07f15696ca76eb95b10698bf1188fd882945c57657515889d042a6fc45d38cbc943540c4f0f6d1c45a1574c81f3e42d1eb8702328b729909adee8a5cfed7c79d54627d1fd389af941d878376f7927b9830ca659bf9ab18c5ca5192d52d02723008728d03701b8ab3e1c4a3109409ec0b13df334c7deec3523eeef4c97b5603e643de3a647b873f4c1b47fbfc6586ba66724f112e51fc93839648005043620aa3ce458e246d77977b19c53d98e3e812de006afc1a79744df236582943631d04cc02941ac4be500e4ed9fb9e3e7cc187b1c4050fad1d9d09d5fd70d5d01d615b439d8c0015d2eb10398bcdbf8c4b2bd559dbe4c288a186aed3f86f608da4d582e120c4a896e015e2241900d1daeccd05db968852677c71d752bec46de9962174b46f980e8cc603654daf8b98a3ee92dac066033954164a89568b70b1780c2ce2410b2f816dbeddb2cd463e0c8f21a52cf6427d9647a6fd4bafa8fb4cd4d47ac057b0160bee86c6b2fb8adce214c2bcdda277512200adf0eaa5d2114a2c077b009836a68ec254bfe56f51d147b9afe2ddd9cb917c0c2de19d81b7b8fd9f4574f51fa1207630dc13976f4d7587c962f761af267de71f3909a576e6bedaf6311633910d291ac292c467cc8331ef577aef7646a5d949322fa0367a49f20597a13def53136ee31610395e3e48d291fd8f58504374031fe9dcfba5e06086ebcf01a9106f6a4d6e16e19e4c5bb893f7da79419c94eca31a384be6fa1747284dee0fc3bbc8b1b860172c10b29c1594bb8c747d7fe05827358ff2160f49050001625ffe2e880bd7fc26cd0ffd89750745379a8e862816e08a5a2008043921ab6a4976064ac18f7ee37b6628bc0127d8d5ebd3548e41d8881a082d86f20b32e33094f15a0e6ea6074b08c6cd28142de94713451640a55985051f5577eb54572699d838cb34a79c8939e981c0c277d06a6e2ce69ccb74f8a691ff08f81d8b99e6a86223d29a2b7c8e7b041aba44ea678ae654277f7e91cbfa79158b989164a3d549d9f4feb0cc43169699c13e321fe3f4b94258c75d198ff9184269cd6986c55409e07528c93f64942c6c283ce3917b4bf4c3be2fe3173c8c38cccb35f1fbda0ca88b35a599c0678cb22aa8eabea8249dbd2e4f849fffe69803d299e435ebcd7df95854003d8eda17a74d98b4be0e62d45d7fe48c06a6f464a14f8e0570077cc631279092802a89823f031eef5e1028a6d6fdbd502869a731ee7d28b4d6c71b419462a30d31442d3ee444ffbcbd16d558c9000c97e949c2b1f9d6f6d8db7b9131ebd963620d3fc8595278d6f8fdf49084325373196d53e64142fa5a23eccd6ef908c4d80b8b3e6cc334b7f7012103a3682e4678e9b518163d262a39a2c1a69bf88514c52b7ccd7cc8dc80e71f7c2ec0701cff982573eb0c2c4daeb47fa0b586f4451c10d1da2e5d182b03dd067a5e971b3a6138ca6667aaf853d2ac03b80a1d5870905f2cfb6c78ec3c3719c02f973d638a0f973424a2b0f2b0023f136d60092fe15fba4bc180b9176bd0ff576e053f1af6939fe9ca256203ffaeb3e569f09774d2a6cbf91873e56651f4d6ff77e0b5374b0a1a201d7e523604e0247644544cc571d48c458a4f96f45580b";
1065 Object obj;
1066 obj.push_back(Pair("rawtxn", raw));
1067
1068 // we have the spending key for the dummy recipient zaddr1
1069 std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
1070
1071 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, {}, recipients, 1) );
1072 std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1073 TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1074
1075 // Enable test mode so tx is not sent
1076 static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
da5e7e51
S
1077
1078 // Pretend that the operation completed successfully
1079 proxy.set_state(OperationStatus::SUCCESS);
cff6f0ac 1080
da5e7e51 1081 // Verify test mode is returning output (since no input taddrs, signed and unsigned are the same).
cff6f0ac 1082 BOOST_CHECK_NO_THROW( proxy.sign_send_raw_transaction(obj) );
da5e7e51
S
1083 Value result = operation->getResult();
1084 BOOST_CHECK(!result.is_null());
1085 Object resultObj = result.get_obj();
1086 std::string hex = find_value(resultObj, "hex").get_str();
1087 BOOST_CHECK_EQUAL(hex, raw);
cff6f0ac 1088 }
7b79275e
S
1089
1090
1091 // Test the perform_joinsplit methods.
1092 {
1093 // Dummy input so the operation object can be instantiated.
1094 std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
1095
1096 std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, {}, recipients, 1) );
1097 std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1098 TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1099
1100 // Enable test mode so tx is not sent and proofs are not generated
1101 static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
1102
1103 AsyncJoinSplitInfo info;
1104 std::vector<boost::optional < ZCIncrementalWitness>> witnesses;
1105 uint256 anchor;
1106 try {
1107 proxy.perform_joinsplit(info, witnesses, anchor);
1108 } catch (const std::runtime_error & e) {
1109 BOOST_CHECK( string(e.what()).find("anchor is null")!= string::npos);
1110 }
1111
1112 try {
1113 std::vector<JSOutPoint> v;
1114 proxy.perform_joinsplit(info, v);
1115 } catch (const std::runtime_error & e) {
1116 BOOST_CHECK( string(e.what()).find("anchor is null")!= string::npos);
1117 }
1118
1119 info.notes.push_back(Note());
1120 try {
1121 proxy.perform_joinsplit(info);
1122 } catch (const std::runtime_error & e) {
1123 BOOST_CHECK( string(e.what()).find("number of notes")!= string::npos);
1124 }
1125
1126 info.notes.clear();
1127 info.vjsin.push_back(JSInput());
1128 info.vjsin.push_back(JSInput());
1129 info.vjsin.push_back(JSInput());
1130 try {
1131 proxy.perform_joinsplit(info);
1132 } catch (const std::runtime_error & e) {
1133 BOOST_CHECK( string(e.what()).find("unsupported joinsplit input")!= string::npos);
1134 }
1135
1136 info.vjsin.clear();
1137 try {
1138 proxy.perform_joinsplit(info);
1139 } catch (const std::runtime_error & e) {
1140 BOOST_CHECK( string(e.what()).find("JoinSplit verifying key not loaded")!= string::npos);
1141 }
1142 }
1143
fc4b127e
S
1144}
1145
60f762a5 1146
73699cea
S
1147/*
1148 * This test covers storing encrypted zkeys in the wallet.
1149 */
1150BOOST_AUTO_TEST_CASE(rpc_wallet_encrypted_wallet_zkeys)
1151{
1152 LOCK2(cs_main, pwalletMain->cs_wallet);
1153 Value retValue;
1154 int n = 100;
1155
1156 // wallet should currently be empty
1157 std::set<libzcash::PaymentAddress> addrs;
1158 pwalletMain->GetPaymentAddresses(addrs);
1159 BOOST_CHECK(addrs.size()==0);
1160
1161 // create keys
1162 for (int i = 0; i < n; i++) {
1163 CallRPC("z_getnewaddress");
1164 }
1165
1166 // Verify we can list the keys imported
1167 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1168 Array arr = retValue.get_array();
1169 BOOST_CHECK(arr.size() == n);
1170
d8e06e3f
JG
1171 // Verify that the wallet encryption RPC is disabled
1172 BOOST_CHECK_THROW(CallRPC("encryptwallet passphrase"), runtime_error);
1173
82bd9ee8 1174 // Encrypt the wallet (we can't call RPC encryptwallet as that shuts down node)
73699cea
S
1175 SecureString strWalletPass;
1176 strWalletPass.reserve(100);
1177 strWalletPass = "hello";
6be367ea
S
1178
1179 boost::filesystem::current_path(GetArg("-datadir","/tmp/thisshouldnothappen"));
73699cea
S
1180 BOOST_CHECK(pwalletMain->EncryptWallet(strWalletPass));
1181
1182 // Verify we can still list the keys imported
1183 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1184 arr = retValue.get_array();
1185 BOOST_CHECK(arr.size() == n);
1186
1187 // Try to add a new key, but we can't as the wallet is locked
1188 BOOST_CHECK_THROW(CallRPC("z_getnewaddress"), runtime_error);
1189
1190 // We can't call RPC walletpassphrase as that invokes RPCRunLater which breaks tests.
1191 // So we manually unlock.
1192 BOOST_CHECK(pwalletMain->Unlock(strWalletPass));
1193
1194 // Now add a key
1195 BOOST_CHECK_NO_THROW(CallRPC("z_getnewaddress"));
1196
1197 // Verify the key has been added
1198 BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1199 arr = retValue.get_array();
1200 BOOST_CHECK(arr.size() == n+1);
1201
1202 // We can't simulate over RPC the wallet closing and being reloaded
1203 // but there are tests for this in gtest.
1204}
1205
b922924d 1206BOOST_AUTO_TEST_SUITE_END()
This page took 0.317339 seconds and 4 git commands to generate.