2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2013-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2016-01-15 01:55:17 +01:00
|
|
|
#include "rpc/server.h"
|
|
|
|
#include "rpc/client.h"
|
2013-11-30 11:10:35 +01:00
|
|
|
|
|
|
|
#include "base58.h"
|
2015-02-05 01:21:11 +01:00
|
|
|
#include "main.h"
|
2015-02-03 21:09:47 +01:00
|
|
|
#include "wallet/wallet.h"
|
2013-11-30 11:10:35 +01:00
|
|
|
|
2015-03-03 16:49:12 +01:00
|
|
|
#include "test/test_bitcoin.h"
|
|
|
|
|
2013-11-30 11:10:35 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2015-09-04 16:11:34 +02:00
|
|
|
#include <univalue.h>
|
2015-05-18 14:02:18 +02:00
|
|
|
|
2013-11-30 11:10:35 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2015-05-13 21:29:19 +02:00
|
|
|
extern UniValue createArgs(int nRequired, const char* address1 = NULL, const char* address2 = NULL);
|
2015-05-18 14:02:18 +02:00
|
|
|
extern UniValue CallRPC(string args);
|
2013-11-30 11:10:35 +01:00
|
|
|
|
2014-01-23 20:18:26 +01:00
|
|
|
extern CWallet* pwalletMain;
|
|
|
|
|
2015-03-03 16:49:12 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
|
2013-11-30 11:10:35 +01:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(rpc_addmultisig)
|
|
|
|
{
|
|
|
|
rpcfn_type addmultisig = tableRPC["addmultisigaddress"]->actor;
|
|
|
|
|
|
|
|
// old, 65-byte-long:
|
|
|
|
const char address1Hex[] = "0434e3e09f49ea168c5bbf53f877ff4206923858aab7c7e1df25bc263978107c95e35065a27ef6f1b27222db0ec97e0e895eaca603d3ee0d4c060ce3d8a00286c8";
|
|
|
|
// new, compressed:
|
|
|
|
const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4";
|
|
|
|
|
2015-05-18 14:02:18 +02:00
|
|
|
UniValue v;
|
2013-11-30 11:10:35 +01:00
|
|
|
CBitcoinAddress address;
|
|
|
|
BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false));
|
|
|
|
address.SetString(v.get_str());
|
|
|
|
BOOST_CHECK(address.IsValid() && address.IsScript());
|
|
|
|
|
|
|
|
BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false));
|
|
|
|
address.SetString(v.get_str());
|
|
|
|
BOOST_CHECK(address.IsValid() && address.IsScript());
|
|
|
|
|
|
|
|
BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false));
|
|
|
|
address.SetString(v.get_str());
|
|
|
|
BOOST_CHECK(address.IsValid() && address.IsScript());
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(addmultisig(createArgs(2, address1Hex), false), runtime_error);
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(addmultisig(createArgs(1, ""), false), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(addmultisig(createArgs(1, "NotAValidPubkey"), false), runtime_error);
|
|
|
|
|
2014-10-02 18:19:18 +02:00
|
|
|
string short1(address1Hex, address1Hex + sizeof(address1Hex) - 2); // last byte missing
|
2013-11-30 11:10:35 +01:00
|
|
|
BOOST_CHECK_THROW(addmultisig(createArgs(2, short1.c_str()), false), runtime_error);
|
|
|
|
|
2014-10-02 18:19:18 +02:00
|
|
|
string short2(address1Hex + 1, address1Hex + sizeof(address1Hex)); // first byte missing
|
2013-11-30 11:10:35 +01:00
|
|
|
BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(rpc_wallet)
|
|
|
|
{
|
|
|
|
// Test RPC calls for various wallet statistics
|
2015-05-18 14:02:18 +02:00
|
|
|
UniValue r;
|
2015-08-07 22:15:38 +02:00
|
|
|
CPubKey demoPubkey;
|
|
|
|
CBitcoinAddress demoAddress;
|
2015-05-18 14:02:18 +02:00
|
|
|
UniValue retValue;
|
2014-10-02 18:19:18 +02:00
|
|
|
string strAccount = "walletDemoAccount";
|
2015-08-07 22:15:38 +02:00
|
|
|
CBitcoinAddress setaccountDemoAddress;
|
|
|
|
{
|
|
|
|
LOCK(pwalletMain->cs_wallet);
|
|
|
|
|
|
|
|
demoPubkey = pwalletMain->GenerateNewKey();
|
|
|
|
demoAddress = CBitcoinAddress(CTxDestination(demoPubkey.GetID()));
|
|
|
|
string strPurpose = "receive";
|
|
|
|
BOOST_CHECK_NO_THROW({ /*Initialize Wallet with an account */
|
|
|
|
CWalletDB walletdb(pwalletMain->strWalletFile);
|
|
|
|
CAccount account;
|
|
|
|
account.vchPubKey = demoPubkey;
|
|
|
|
pwalletMain->SetAddressBook(account.vchPubKey.GetID(), strAccount, strPurpose);
|
|
|
|
walletdb.WriteAccount(strAccount, account);
|
|
|
|
});
|
|
|
|
|
|
|
|
CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey();
|
|
|
|
setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID()));
|
|
|
|
}
|
2014-10-02 18:19:18 +02:00
|
|
|
/*********************************
|
|
|
|
* setaccount
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " nullaccount"));
|
|
|
|
/* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ is not owned by the test wallet. */
|
|
|
|
BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ nullaccount"), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error);
|
|
|
|
/* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X (33 chars) is an illegal address (should be 34 chars) */
|
|
|
|
BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X nullaccount"), runtime_error);
|
2014-03-10 19:23:19 +01:00
|
|
|
|
2014-12-23 08:41:14 +01:00
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* getbalance
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getbalance"));
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getbalance " + demoAddress.ToString()));
|
|
|
|
|
2014-03-10 19:23:19 +01:00
|
|
|
/*********************************
|
|
|
|
* listunspent
|
|
|
|
*********************************/
|
2013-11-30 11:10:35 +01:00
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listunspent"));
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listunspent string"), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listunspent 0 string"), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listunspent 0 1 not_array"), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listunspent 0 1 [] extra"), runtime_error);
|
2014-10-02 18:19:18 +02:00
|
|
|
BOOST_CHECK_NO_THROW(r = CallRPC("listunspent 0 1 []"));
|
2013-11-30 11:10:35 +01:00
|
|
|
BOOST_CHECK(r.get_array().empty());
|
|
|
|
|
2014-03-10 19:23:19 +01:00
|
|
|
/*********************************
|
2014-10-02 18:19:18 +02:00
|
|
|
* listreceivedbyaddress
|
|
|
|
*********************************/
|
2013-11-30 11:10:35 +01:00
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress"));
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0"));
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress not_int"), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 not_bool"), runtime_error);
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0 true"));
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 true extra"), runtime_error);
|
|
|
|
|
2014-03-10 19:23:19 +01:00
|
|
|
/*********************************
|
2014-10-02 18:19:18 +02:00
|
|
|
* listreceivedbyaccount
|
|
|
|
*********************************/
|
2013-11-30 11:10:35 +01:00
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount"));
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0"));
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount not_int"), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 not_bool"), runtime_error);
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0 true"));
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 true extra"), runtime_error);
|
2014-03-10 19:23:19 +01:00
|
|
|
|
2014-12-23 08:41:14 +01:00
|
|
|
/*********************************
|
|
|
|
* listsinceblock
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listsinceblock"));
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* listtransactions
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listtransactions"));
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString()));
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20"));
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20 0"));
|
|
|
|
BOOST_CHECK_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " not_int"), runtime_error);
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* listlockunspent
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listlockunspent"));
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* listaccounts
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listaccounts"));
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* listaddressgroupings
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("listaddressgroupings"));
|
|
|
|
|
2014-03-10 19:23:19 +01:00
|
|
|
/*********************************
|
2014-10-02 18:19:18 +02:00
|
|
|
* getrawchangeaddress
|
|
|
|
*********************************/
|
2014-03-10 19:23:19 +01:00
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getrawchangeaddress"));
|
|
|
|
|
|
|
|
/*********************************
|
2014-10-02 18:19:18 +02:00
|
|
|
* getnewaddress
|
|
|
|
*********************************/
|
2014-03-10 19:23:19 +01:00
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getnewaddress"));
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getnewaddress getnewaddress_demoaccount"));
|
|
|
|
|
|
|
|
/*********************************
|
2014-10-02 18:19:18 +02:00
|
|
|
* getaccountaddress
|
|
|
|
*********************************/
|
2014-03-10 19:23:19 +01:00
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress \"\""));
|
2014-10-02 18:19:18 +02:00
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress accountThatDoesntExists")); // Should generate a new account
|
|
|
|
BOOST_CHECK_NO_THROW(retValue = CallRPC("getaccountaddress " + strAccount));
|
|
|
|
BOOST_CHECK(CBitcoinAddress(retValue.get_str()).Get() == demoAddress.Get());
|
2014-03-10 19:23:19 +01:00
|
|
|
|
2014-10-02 18:19:18 +02:00
|
|
|
/*********************************
|
|
|
|
* getaccount
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_THROW(CallRPC("getaccount"), runtime_error);
|
|
|
|
BOOST_CHECK_NO_THROW(CallRPC("getaccount " + demoAddress.ToString()));
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* signmessage + verifymessage
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_NO_THROW(retValue = CallRPC("signmessage " + demoAddress.ToString() + " mymessage"));
|
|
|
|
BOOST_CHECK_THROW(CallRPC("signmessage"), runtime_error);
|
|
|
|
/* Should throw error because this address is not loaded in the wallet */
|
|
|
|
BOOST_CHECK_THROW(CallRPC("signmessage 1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ mymessage"), runtime_error);
|
|
|
|
|
|
|
|
/* missing arguments */
|
|
|
|
BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString()), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str()), runtime_error);
|
|
|
|
/* Illegal address */
|
|
|
|
BOOST_CHECK_THROW(CallRPC("verifymessage 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X " + retValue.get_str() + " mymessage"), runtime_error);
|
|
|
|
/* wrong address */
|
|
|
|
BOOST_CHECK(CallRPC("verifymessage 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ " + retValue.get_str() + " mymessage").get_bool() == false);
|
|
|
|
/* Correct address and signature but wrong message */
|
|
|
|
BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " wrongmessage").get_bool() == false);
|
|
|
|
/* Correct address, message and signature*/
|
|
|
|
BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " mymessage").get_bool() == true);
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* getaddressesbyaccount
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_THROW(CallRPC("getaddressesbyaccount"), runtime_error);
|
|
|
|
BOOST_CHECK_NO_THROW(retValue = CallRPC("getaddressesbyaccount " + strAccount));
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue arr = retValue.get_array();
|
2014-10-02 18:19:18 +02:00
|
|
|
BOOST_CHECK(arr.size() > 0);
|
|
|
|
BOOST_CHECK(CBitcoinAddress(arr[0].get_str()).Get() == demoAddress.Get());
|
2015-04-25 03:27:30 +02:00
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* fundrawtransaction
|
|
|
|
*********************************/
|
|
|
|
BOOST_CHECK_THROW(CallRPC("fundrawtransaction 28z"), runtime_error);
|
|
|
|
BOOST_CHECK_THROW(CallRPC("fundrawtransaction 01000000000180969800000000001976a91450ce0a4b0ee0ddeb633da85199728b940ac3fe9488ac00000000"), runtime_error);
|
2013-11-30 11:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|