You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpc_wallet_tests.cpp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2013-2014 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "rpcserver.h"
  5. #include "rpcclient.h"
  6. #include "base58.h"
  7. #include "main.h"
  8. #include "wallet/wallet.h"
  9. #include "test/test_bitcoin.h"
  10. #include <boost/algorithm/string.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. using namespace std;
  13. using namespace json_spirit;
  14. extern Array createArgs(int nRequired, const char* address1 = NULL, const char* address2 = NULL);
  15. extern Value CallRPC(string args);
  16. extern CWallet* pwalletMain;
  17. BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
  18. BOOST_AUTO_TEST_CASE(rpc_addmultisig)
  19. {
  20. LOCK(pwalletMain->cs_wallet);
  21. rpcfn_type addmultisig = tableRPC["addmultisigaddress"]->actor;
  22. // old, 65-byte-long:
  23. const char address1Hex[] = "0434e3e09f49ea168c5bbf53f877ff4206923858aab7c7e1df25bc263978107c95e35065a27ef6f1b27222db0ec97e0e895eaca603d3ee0d4c060ce3d8a00286c8";
  24. // new, compressed:
  25. const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4";
  26. Value v;
  27. CBitcoinAddress address;
  28. BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false));
  29. address.SetString(v.get_str());
  30. BOOST_CHECK(address.IsValid() && address.IsScript());
  31. BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false));
  32. address.SetString(v.get_str());
  33. BOOST_CHECK(address.IsValid() && address.IsScript());
  34. BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false));
  35. address.SetString(v.get_str());
  36. BOOST_CHECK(address.IsValid() && address.IsScript());
  37. BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error);
  38. BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error);
  39. BOOST_CHECK_THROW(addmultisig(createArgs(2, address1Hex), false), runtime_error);
  40. BOOST_CHECK_THROW(addmultisig(createArgs(1, ""), false), runtime_error);
  41. BOOST_CHECK_THROW(addmultisig(createArgs(1, "NotAValidPubkey"), false), runtime_error);
  42. string short1(address1Hex, address1Hex + sizeof(address1Hex) - 2); // last byte missing
  43. BOOST_CHECK_THROW(addmultisig(createArgs(2, short1.c_str()), false), runtime_error);
  44. string short2(address1Hex + 1, address1Hex + sizeof(address1Hex)); // first byte missing
  45. BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error);
  46. }
  47. BOOST_AUTO_TEST_CASE(rpc_wallet)
  48. {
  49. // Test RPC calls for various wallet statistics
  50. Value r;
  51. LOCK2(cs_main, pwalletMain->cs_wallet);
  52. CPubKey demoPubkey = pwalletMain->GenerateNewKey();
  53. CBitcoinAddress demoAddress = CBitcoinAddress(CTxDestination(demoPubkey.GetID()));
  54. Value retValue;
  55. string strAccount = "walletDemoAccount";
  56. string strPurpose = "receive";
  57. BOOST_CHECK_NO_THROW({ /*Initialize Wallet with an account */
  58. CWalletDB walletdb(pwalletMain->strWalletFile);
  59. CAccount account;
  60. account.vchPubKey = demoPubkey;
  61. pwalletMain->SetAddressBook(account.vchPubKey.GetID(), strAccount, strPurpose);
  62. walletdb.WriteAccount(strAccount, account);
  63. });
  64. CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey();
  65. CBitcoinAddress setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID()));
  66. /*********************************
  67. * setaccount
  68. *********************************/
  69. BOOST_CHECK_NO_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " nullaccount"));
  70. /* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ is not owned by the test wallet. */
  71. BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ nullaccount"), runtime_error);
  72. BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error);
  73. /* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X (33 chars) is an illegal address (should be 34 chars) */
  74. BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X nullaccount"), runtime_error);
  75. /*********************************
  76. * getbalance
  77. *********************************/
  78. BOOST_CHECK_NO_THROW(CallRPC("getbalance"));
  79. BOOST_CHECK_NO_THROW(CallRPC("getbalance " + demoAddress.ToString()));
  80. /*********************************
  81. * listunspent
  82. *********************************/
  83. BOOST_CHECK_NO_THROW(CallRPC("listunspent"));
  84. BOOST_CHECK_THROW(CallRPC("listunspent string"), runtime_error);
  85. BOOST_CHECK_THROW(CallRPC("listunspent 0 string"), runtime_error);
  86. BOOST_CHECK_THROW(CallRPC("listunspent 0 1 not_array"), runtime_error);
  87. BOOST_CHECK_THROW(CallRPC("listunspent 0 1 [] extra"), runtime_error);
  88. BOOST_CHECK_NO_THROW(r = CallRPC("listunspent 0 1 []"));
  89. BOOST_CHECK(r.get_array().empty());
  90. /*********************************
  91. * listreceivedbyaddress
  92. *********************************/
  93. BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress"));
  94. BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0"));
  95. BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress not_int"), runtime_error);
  96. BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 not_bool"), runtime_error);
  97. BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0 true"));
  98. BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 true extra"), runtime_error);
  99. /*********************************
  100. * listreceivedbyaccount
  101. *********************************/
  102. BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount"));
  103. BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0"));
  104. BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount not_int"), runtime_error);
  105. BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 not_bool"), runtime_error);
  106. BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0 true"));
  107. BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 true extra"), runtime_error);
  108. /*********************************
  109. * listsinceblock
  110. *********************************/
  111. BOOST_CHECK_NO_THROW(CallRPC("listsinceblock"));
  112. /*********************************
  113. * listtransactions
  114. *********************************/
  115. BOOST_CHECK_NO_THROW(CallRPC("listtransactions"));
  116. BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString()));
  117. BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20"));
  118. BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20 0"));
  119. BOOST_CHECK_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " not_int"), runtime_error);
  120. /*********************************
  121. * listlockunspent
  122. *********************************/
  123. BOOST_CHECK_NO_THROW(CallRPC("listlockunspent"));
  124. /*********************************
  125. * listaccounts
  126. *********************************/
  127. BOOST_CHECK_NO_THROW(CallRPC("listaccounts"));
  128. /*********************************
  129. * listaddressgroupings
  130. *********************************/
  131. BOOST_CHECK_NO_THROW(CallRPC("listaddressgroupings"));
  132. /*********************************
  133. * getrawchangeaddress
  134. *********************************/
  135. BOOST_CHECK_NO_THROW(CallRPC("getrawchangeaddress"));
  136. /*********************************
  137. * getnewaddress
  138. *********************************/
  139. BOOST_CHECK_NO_THROW(CallRPC("getnewaddress"));
  140. BOOST_CHECK_NO_THROW(CallRPC("getnewaddress getnewaddress_demoaccount"));
  141. /*********************************
  142. * getaccountaddress
  143. *********************************/
  144. BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress \"\""));
  145. BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress accountThatDoesntExists")); // Should generate a new account
  146. BOOST_CHECK_NO_THROW(retValue = CallRPC("getaccountaddress " + strAccount));
  147. BOOST_CHECK(CBitcoinAddress(retValue.get_str()).Get() == demoAddress.Get());
  148. /*********************************
  149. * getaccount
  150. *********************************/
  151. BOOST_CHECK_THROW(CallRPC("getaccount"), runtime_error);
  152. BOOST_CHECK_NO_THROW(CallRPC("getaccount " + demoAddress.ToString()));
  153. /*********************************
  154. * signmessage + verifymessage
  155. *********************************/
  156. BOOST_CHECK_NO_THROW(retValue = CallRPC("signmessage " + demoAddress.ToString() + " mymessage"));
  157. BOOST_CHECK_THROW(CallRPC("signmessage"), runtime_error);
  158. /* Should throw error because this address is not loaded in the wallet */
  159. BOOST_CHECK_THROW(CallRPC("signmessage 1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ mymessage"), runtime_error);
  160. /* missing arguments */
  161. BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString()), runtime_error);
  162. BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str()), runtime_error);
  163. /* Illegal address */
  164. BOOST_CHECK_THROW(CallRPC("verifymessage 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X " + retValue.get_str() + " mymessage"), runtime_error);
  165. /* wrong address */
  166. BOOST_CHECK(CallRPC("verifymessage 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ " + retValue.get_str() + " mymessage").get_bool() == false);
  167. /* Correct address and signature but wrong message */
  168. BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " wrongmessage").get_bool() == false);
  169. /* Correct address, message and signature*/
  170. BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " mymessage").get_bool() == true);
  171. /*********************************
  172. * getaddressesbyaccount
  173. *********************************/
  174. BOOST_CHECK_THROW(CallRPC("getaddressesbyaccount"), runtime_error);
  175. BOOST_CHECK_NO_THROW(retValue = CallRPC("getaddressesbyaccount " + strAccount));
  176. Array arr = retValue.get_array();
  177. BOOST_CHECK(arr.size() > 0);
  178. BOOST_CHECK(CBitcoinAddress(arr[0].get_str()).Get() == demoAddress.Get());
  179. }
  180. BOOST_AUTO_TEST_SUITE_END()