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