diff --git a/src/bitcoinapi/bitcoinapi.cpp b/src/bitcoinapi/bitcoinapi.cpp index c4c924b..4aa5edc 100644 --- a/src/bitcoinapi/bitcoinapi.cpp +++ b/src/bitcoinapi/bitcoinapi.cpp @@ -34,7 +34,7 @@ BitcoinAPI::BitcoinAPI(const string& user, const string& password, const string& : httpClient(new HttpClient("http://" + user + ":" + password + "@" + host + ":" + NumberToString(port))), client(new Client(*httpClient, JSONRPC_CLIENT_V1)) { - httpClient->SetTimeout(20000); + httpClient->SetTimeout(50000); } BitcoinAPI::~BitcoinAPI() @@ -174,7 +174,7 @@ vector BitcoinAPI::getaddednodeinfo(bool dns, const std::string& nod netaddress_t net; net.address = val2["address"].asString(); - net.connected = val2["connected"].asBool(); + net.connected = val2["connected"].asString(); node.addresses.push_back(net); } diff --git a/src/bitcoinapi/exception.h b/src/bitcoinapi/exception.h index a0d4a86..d5dcb5a 100644 --- a/src/bitcoinapi/exception.h +++ b/src/bitcoinapi/exception.h @@ -13,6 +13,14 @@ #include #include +#include +#include +#include + +using Json::Value; +using Json::Reader; + + class BitcoinException: public std::exception { private: @@ -21,9 +29,8 @@ class BitcoinException: public std::exception public: explicit BitcoinException(int errcode, const std::string& message) { - this->code = errcode; - this->msg = parse(message); - + this->code = parseCode(message); + this->msg = parseMessage(message); } ~BitcoinException() throw() { }; @@ -35,22 +42,53 @@ class BitcoinException: public std::exception return msg; } - std::string parse(const std::string& in){ - std::string out = in; - std::string pattern = ": "; - unsigned int pos = out.find(pattern); - if(pos <= out.size()){ - out.erase(0, pos+pattern.size()); - out[0] = toupper(out[0]); + + std::string removePrefix(const std::string& in, const std::string& pattern){ + std::string ret = in; + + unsigned int pos = ret.find(pattern); + + if(pos <= ret.size()){ + ret.erase(0, pos+pattern.size()); + } + + return ret; + } + + + int parseCode(const std::string& in){ + Value root; + Reader reader; + + /* Remove JSON prefix */ + std::string strJson = removePrefix(in, "INTERNAL_ERROR: : "); + int ret = -1; + + /* Parse error message */ + bool parsingSuccessful = reader.parse(strJson.c_str(), root); + if(parsingSuccessful) { + ret = root["error"]["code"].asInt(); } - return out; + return ret; } - virtual const char* what() const throw (){ - std::stringstream out; - out << "Error " << code << ": " << msg; - return out.str().c_str(); + std::string parseMessage(const std::string& in){ + Value root; + Reader reader; + + /* Remove JSON prefix */ + std::string strJson = removePrefix(in, "INTERNAL_ERROR: : "); + std::string ret = "Error during parsing of >>" + strJson + "<<"; + + /* Parse error message */ + bool parsingSuccessful = reader.parse(strJson.c_str(), root); + if(parsingSuccessful) { + ret = removePrefix(root["error"]["message"].asString(), "Error: "); + ret[0] = toupper(ret[0]); + } + + return ret; } }; diff --git a/src/test/accounting.cpp b/src/test/accounting.cpp index 5160a75..b158705 100644 --- a/src/test/accounting.cpp +++ b/src/test/accounting.cpp @@ -17,8 +17,8 @@ BOOST_AUTO_TEST_CASE(GetBalance) { MyFixture fx; - BOOST_REQUIRE_NO_THROW(fx.btc.getbalance()); - BOOST_REQUIRE_NO_THROW(fx.btc.getbalance("")); + NO_THROW(fx.btc.getbalance()); + NO_THROW(fx.btc.getbalance("")); #ifdef VERBOSE double response = fx.btc.getbalance(); @@ -32,7 +32,7 @@ BOOST_AUTO_TEST_CASE(GetReceivedByAccount) { MyFixture fx; - BOOST_REQUIRE_NO_THROW(fx.btc.getreceivedbyaccount("")); + NO_THROW(fx.btc.getreceivedbyaccount("")); #ifdef VERBOSE double response = fx.btc.getreceivedbyaccount(""); @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(GetReceivedByAddress) { std::string param = "36m2bHwsUnkpGsZgVAEmeJRf2CeViDm6RV"; - BOOST_REQUIRE_NO_THROW(fx.btc.getreceivedbyaddress(param)); + NO_THROW(fx.btc.getreceivedbyaddress(param)); #ifdef VERBOSE double response = fx.btc.getreceivedbyaddress(param); @@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(ListReceivedByAccount) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listreceivedbyaccount(1,true)); + NO_THROW(response = fx.btc.listreceivedbyaccount(1,true)); #ifdef VERBOSE std::cout << "=== listreceivedbyaccount ===" << std::endl; @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(ListReceivedByAddress) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listreceivedbyaddress(1,true)); + NO_THROW(response = fx.btc.listreceivedbyaddress(1,true)); #ifdef VERBOSE std::cout << "=== listreceivedbyaccount ===" << std::endl; @@ -106,12 +106,8 @@ BOOST_AUTO_TEST_CASE(GetTransaction) { std::string txid = "5a8f750129702d4e0ccd3e6fa91193d8191ea9742a36835b43d3b3c56ad816d1"; gettransaction_t response; - try { - response = fx.btc.gettransaction(txid, false); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -5); - return; - } + + NO_THROW_EXCEPT(response = fx.btc.gettransaction(txid, false), -5); #ifdef VERBOSE std::cout << "=== gettransaction ===" << std::endl; @@ -151,7 +147,7 @@ BOOST_AUTO_TEST_CASE(ListTransactions) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listtransactions()); + NO_THROW(response = fx.btc.listtransactions()); #ifdef VERBOSE std::cout << "=== listtransactions ===" << std::endl; @@ -185,14 +181,9 @@ BOOST_AUTO_TEST_CASE(GetAccount) { std::string address; std::string response; - try { - address = fx.btc.getnewaddress("TestUser"); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -12); - BOOST_WARN_MESSAGE(false, e.getMessage()); - } + NO_THROW_EXCEPT(address = fx.btc.getnewaddress("TestUser"), -12); - BOOST_REQUIRE_NO_THROW(response = fx.btc.getaccount(address)); + NO_THROW(response = fx.btc.getaccount(address)); BOOST_REQUIRE(response == "TestUser"); #ifdef VERBOSE @@ -209,16 +200,10 @@ BOOST_AUTO_TEST_CASE(GetAccountAddress) { std::string response; /* Unlock wallet and refill the keypool */ - try { - fx.btc.walletpassphrase("123456", 10); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -14); - BOOST_WARN_MESSAGE(false, e.getMessage()); - return; - } + NO_THROW_EXCEPT(fx.btc.walletpassphrase("123456", 10), -14); - BOOST_REQUIRE_NO_THROW(fx.btc.keypoolrefill()); - BOOST_REQUIRE_NO_THROW(address = fx.btc.getnewaddress("TestUser")); + NO_THROW(fx.btc.keypoolrefill()); + NO_THROW(address = fx.btc.getnewaddress("TestUser")); BOOST_REQUIRE(address.length() >= 27 && address.length() <= 34); #ifdef VERBOSE @@ -232,7 +217,7 @@ BOOST_AUTO_TEST_CASE(GetAddressesByAccount) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getaddressesbyaccount("TestUser")); + NO_THROW(response = fx.btc.getaddressesbyaccount("TestUser")); BOOST_REQUIRE(response.size() >= 1); #ifdef VERBOSE @@ -249,7 +234,7 @@ BOOST_AUTO_TEST_CASE(ListAccounts) { MyFixture fx; std::map response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listaccounts()); + NO_THROW(response = fx.btc.listaccounts()); #ifdef VERBOSE std::cout << "=== listaccounts ===" << std::endl; @@ -287,8 +272,8 @@ BOOST_AUTO_TEST_CASE(SetAccount) { MyFixture fx; std::string address; - BOOST_REQUIRE_NO_THROW(address = fx.btc.getnewaddress("TestUser")) - BOOST_REQUIRE_NO_THROW(fx.btc.setaccount(address,"TestUser2")); + NO_THROW_EXCEPT(address = fx.btc.getnewaddress("TestUser"), -12); + NO_THROW(fx.btc.setaccount(address,"TestUser2")); } BOOST_AUTO_TEST_CASE(SendToAddress) { @@ -348,7 +333,7 @@ BOOST_AUTO_TEST_CASE(GetTxOut) { std::string txid = "105d8fd318533d4559492b85a27039f2bf8bdfed39b8e671753289eaeb3829ae"; utxoinfo_t response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.gettxout(txid, 0)); + NO_THROW(response = fx.btc.gettxout(txid, 0)); #ifdef VERBOSE std::cout << "=== gettxout ===" << std::endl; @@ -374,7 +359,7 @@ BOOST_AUTO_TEST_CASE(GetTxOutSetInfo) { utxosetinfo_t response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.gettxoutsetinfo()); + NO_THROW(response = fx.btc.gettxoutsetinfo()); #ifdef VERBOSE std::cout << "=== gettxoutsetinfo ===" << std::endl; @@ -393,7 +378,7 @@ BOOST_AUTO_TEST_CASE(ListUnspent) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listunspent()); + NO_THROW(response = fx.btc.listunspent()); #ifdef VERBOSE std::cout << "=== listunspent ===" << std::endl; @@ -415,7 +400,7 @@ BOOST_AUTO_TEST_CASE(ListLockUnspent) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listlockunspent()); + NO_THROW(response = fx.btc.listlockunspent()); #ifdef VERBOSE std::cout << "=== listlockunspent ===" << std::endl; @@ -436,7 +421,7 @@ BOOST_AUTO_TEST_CASE(LockUnspent) { param.push_back(tmp); bool response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.lockunspent(false, param)); + NO_THROW(response = fx.btc.lockunspent(false, param)); BOOST_REQUIRE(response == true); } @@ -445,7 +430,7 @@ BOOST_AUTO_TEST_CASE(ListAddressGroupings) { MyFixture fx; std::vector< std::vector > response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listaddressgroupings()); + NO_THROW(response = fx.btc.listaddressgroupings()); #ifdef VERBOSE std::cout << "=== listaddressgroupings ===" << std::endl; diff --git a/src/test/general.cpp b/src/test/general.cpp index 083da9f..84f300f 100644 --- a/src/test/general.cpp +++ b/src/test/general.cpp @@ -10,8 +10,9 @@ BOOST_AUTO_TEST_SUITE(GeneralTests) BOOST_AUTO_TEST_CASE(GetInfo) { MyFixture fx; - - getinfo_t info = fx.btc.getinfo(); + getinfo_t info; + + NO_THROW(info = fx.btc.getinfo()); BOOST_REQUIRE(info.protocolversion >= 70002); #ifdef VERBOSE diff --git a/src/test/main.cpp b/src/test/main.cpp index e6a97a3..8107b15 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -6,6 +6,23 @@ #include #include +#define NO_THROW(METHOD) \ + try { \ + (METHOD); \ + } catch (BitcoinException& e) { \ + BOOST_REQUIRE_MESSAGE(false, e.what()); \ + } + +#define NO_THROW_EXCEPT(METHOD, EXCEPTION) \ + try { \ + (METHOD); \ + } catch (BitcoinException& e) { \ + std::stringstream err; \ + err << "Error (" << e.getCode() << "): " << e.getMessage(); \ + BOOST_REQUIRE_MESSAGE(e.getCode() == (EXCEPTION), err.str()); \ + BOOST_WARN_MESSAGE(false, err.str()); \ + return; \ + } struct MyFixture { diff --git a/src/test/mining.cpp b/src/test/mining.cpp index 17b8b8d..8d8eaf1 100644 --- a/src/test/mining.cpp +++ b/src/test/mining.cpp @@ -11,7 +11,7 @@ BOOST_AUTO_TEST_CASE(GetBestBlockHash) { MyFixture fx; std::string response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getbestblockhash()); + NO_THROW(response = fx.btc.getbestblockhash()); BOOST_REQUIRE(response.size() == 64); #ifdef VERBOSE @@ -25,7 +25,7 @@ BOOST_AUTO_TEST_CASE(GetBlockHash) { MyFixture fx; std::string response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getblockhash(1)); + NO_THROW(response = fx.btc.getblockhash(1)); BOOST_REQUIRE(response.size() == 64); #ifdef VERBOSE @@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(GetBlockCount) { MyFixture fx; int response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getblockcount()); + NO_THROW(response = fx.btc.getblockcount()); BOOST_REQUIRE(response >= 10); #ifdef VERBOSE @@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(GetBlock) { MyFixture fx; blockinfo_t response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getblock("00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048")); + NO_THROW(response = fx.btc.getblock("00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048")); BOOST_REQUIRE(response.height == 1); #ifdef VERBOSE @@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(GetGenerate) { MyFixture fx; bool response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getgenerate()); + NO_THROW(response = fx.btc.getgenerate()); BOOST_REQUIRE(response == true || response == false); #ifdef VERBOSE @@ -98,10 +98,10 @@ BOOST_AUTO_TEST_CASE(SetGenerate) { MyFixture fx; - BOOST_REQUIRE_NO_THROW(fx.btc.setgenerate(true)); + NO_THROW(fx.btc.setgenerate(true)); BOOST_REQUIRE(fx.btc.getgenerate() == true); - BOOST_REQUIRE_NO_THROW(fx.btc.setgenerate(false)); + NO_THROW(fx.btc.setgenerate(false)); BOOST_REQUIRE(fx.btc.getgenerate() == false); } @@ -112,7 +112,7 @@ BOOST_AUTO_TEST_CASE(GetDifficulty) { MyFixture fx; double response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getdifficulty()); + NO_THROW(response = fx.btc.getdifficulty()); BOOST_REQUIRE(response >= 100000); #ifdef VERBOSE @@ -127,7 +127,7 @@ BOOST_AUTO_TEST_CASE(GetMiningInfo) { MyFixture fx; mininginfo_t response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getmininginfo()); + NO_THROW(response = fx.btc.getmininginfo()); BOOST_REQUIRE(response.blocks > 1); #ifdef VERBOSE @@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(ListSinceBlock) { MyFixture fx; txsinceblock_t response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.listsinceblock("", 1)); + NO_THROW(response = fx.btc.listsinceblock("", 1)); #ifdef VERBOSE std::cout << "=== listsinceblock ===" << std::endl; diff --git a/src/test/node.cpp b/src/test/node.cpp index ddb56fd..e765e17 100644 --- a/src/test/node.cpp +++ b/src/test/node.cpp @@ -11,21 +11,7 @@ BOOST_AUTO_TEST_CASE(AddNode) { MyFixture fx; /* Add node */ - BOOST_REQUIRE_NO_THROW(fx.btc.addnode("127.0.0.255", "add")); - - std::vector nodeVec; - BOOST_REQUIRE_NO_THROW(nodeVec = fx.btc.getaddednodeinfo(false)); - - bool found = false; - for(std::vector::iterator it = nodeVec.begin(); it != nodeVec.end(); it++){ - nodeinfo_t node = (*it); - if(node.addednode == "127.0.0.255"){ - found = true; - } - } - BOOST_REQUIRE(found); - - + NO_THROW(fx.btc.addnode("127.0.0.255", "add")); } BOOST_AUTO_TEST_CASE(GetAddedNodeInfo) { @@ -33,7 +19,7 @@ BOOST_AUTO_TEST_CASE(GetAddedNodeInfo) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getaddednodeinfo(true, "127.0.0.255")); + NO_THROW(response = fx.btc.getaddednodeinfo(true, "127.0.0.255")); #ifdef VERBOSE std::cout << "=== getaddednodeinfo ===" << std::endl; @@ -56,10 +42,10 @@ BOOST_AUTO_TEST_CASE(RemoveNode){ MyFixture fx; - BOOST_REQUIRE_NO_THROW(fx.btc.addnode("127.0.0.255", "remove")); + NO_THROW(fx.btc.addnode("127.0.0.255", "remove")); std::vector nodeVec; - BOOST_REQUIRE_NO_THROW(nodeVec = fx.btc.getaddednodeinfo(false)); + NO_THROW(nodeVec = fx.btc.getaddednodeinfo(false)); bool found = false; for(std::vector::iterator it = nodeVec.begin(); it != nodeVec.end(); it++){ @@ -76,7 +62,7 @@ BOOST_AUTO_TEST_CASE(ConnectionCount){ MyFixture fx; int count; - BOOST_REQUIRE_NO_THROW(count = fx.btc.getconnectioncount()); + NO_THROW(count = fx.btc.getconnectioncount()); BOOST_REQUIRE(count >= 1); } @@ -85,7 +71,7 @@ BOOST_AUTO_TEST_CASE(GetPeerInfo){ MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getpeerinfo()); + NO_THROW(response = fx.btc.getpeerinfo()); BOOST_REQUIRE(response.size() >= 1); #ifdef VERBOSE diff --git a/src/test/rawtransaction.cpp b/src/test/rawtransaction.cpp index 22e53b2..354c57c 100644 --- a/src/test/rawtransaction.cpp +++ b/src/test/rawtransaction.cpp @@ -20,7 +20,7 @@ BOOST_AUTO_TEST_CASE(GetRawTransactionVerbose) { getrawtransaction_t response; std::string txid = "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getrawtransaction(txid,1)); + NO_THROW(response = fx.btc.getrawtransaction(txid,1)); #ifdef VERBOSE std::cout << "=== getrawtransaction (verbose) ===" << std::endl; @@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE(DecodeRawTransaction) { "828fd388acec00a0a9010000001976a9146bbc6f8dcd25dfe35222e991b4a1" "c3105b302aa588ac00000000"; - BOOST_REQUIRE_NO_THROW(response = fx.btc.decoderawtransaction(rawHexTx)); + NO_THROW(response = fx.btc.decoderawtransaction(rawHexTx)); #ifdef VERBOSE std::cout << "=== decoderawtransaction ===" << std::endl; @@ -132,11 +132,7 @@ BOOST_AUTO_TEST_CASE(SendRawTransaction) { "828fd388acec00a0a9010000001976a9146bbc6f8dcd25dfe35222e991b4a1" "c3105b302aa588ac00000000"; - try{ - fx.btc.sendrawtransaction(txid, false); - }catch(BitcoinException& e){ - BOOST_REQUIRE(e.getCode() == -25 || e.getCode() == -27); - } + NO_THROW_EXCEPT(fx.btc.sendrawtransaction(txid, false), -27); } BOOST_AUTO_TEST_CASE(CreateRawTransaction) { @@ -155,7 +151,7 @@ BOOST_AUTO_TEST_CASE(CreateRawTransaction) { "94a339cdf320100000000ffffffff0110270000000000001976a9148dd50234" "78a002a1d3551445c3479f6f6ae611ad88ac00000000"; - BOOST_REQUIRE_NO_THROW(response = fx.btc.createrawtransaction(param1, param2)); + NO_THROW(response = fx.btc.createrawtransaction(param1, param2)); BOOST_REQUIRE(response == expected); } @@ -181,7 +177,7 @@ BOOST_AUTO_TEST_CASE(SignRawTransaction) { signrawtransaction_t response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.signrawtransaction(param1,param2,param3,"ALL")); + NO_THROW(response = fx.btc.signrawtransaction(param1,param2,param3,"ALL")); BOOST_REQUIRE(response.hex.size() >= param1.size() + 100); BOOST_REQUIRE(response.complete == true); @@ -192,7 +188,7 @@ BOOST_AUTO_TEST_CASE(GetRawMempool) { MyFixture fx; std::vector response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getrawmempool()); + NO_THROW(response = fx.btc.getrawmempool()); BOOST_REQUIRE(response.size() >= 0); #ifdef VERBOSE @@ -209,7 +205,7 @@ BOOST_AUTO_TEST_CASE(GetRawChangeAddress) { MyFixture fx; std::string response; - BOOST_REQUIRE_NO_THROW(response = fx.btc.getrawchangeaddress()); + NO_THROW_EXCEPT(response = fx.btc.getrawchangeaddress(), -12); BOOST_REQUIRE(response.size() >= 27 && response.size() <= 34); #ifdef VERBOSE diff --git a/src/test/wallet.cpp b/src/test/wallet.cpp index b37221e..3f06c3e 100644 --- a/src/test/wallet.cpp +++ b/src/test/wallet.cpp @@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(WalletBackup) { BOOST_REQUIRE(!exists(bPath)); /* Create backup and check if file exists */ - BOOST_REQUIRE_NO_THROW(fx.btc.backupwallet(bPath.generic_string())); + NO_THROW(fx.btc.backupwallet(bPath.generic_string())); BOOST_REQUIRE(exists(bPath)); @@ -41,44 +41,28 @@ BOOST_AUTO_TEST_CASE(EncryptWallet) { /* IF wallet is not encrypted THEN wallet will be encrypted IF wallet is encrypted THEN error -15 is thrown */ - try { - fx.btc.encryptwallet("123456"); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -15); - BOOST_WARN_MESSAGE(false, e.getMessage()); - } - + NO_THROW_EXCEPT(fx.btc.encryptwallet("123456"), -15); } BOOST_AUTO_TEST_CASE(WalletPassphrase) { MyFixture fx; - try { - fx.btc.walletpassphrase("123456", 10); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -14); - BOOST_WARN_MESSAGE(false, e.getMessage()); - } + NO_THROW_EXCEPT(fx.btc.walletpassphrase("123456", 10), -14); } BOOST_AUTO_TEST_CASE(WalletLock) { MyFixture fx; - BOOST_REQUIRE_NO_THROW(fx.btc.walletlock()); + NO_THROW(fx.btc.walletlock()); } BOOST_AUTO_TEST_CASE(WalletPassphraseChange) { MyFixture fx; - try { - fx.btc.walletpassphrasechange("123456", "123456"); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -14); - BOOST_WARN_MESSAGE(false, e.getMessage()); - } + NO_THROW_EXCEPT(fx.btc.walletpassphrasechange("123456", "123456"), -14); } BOOST_AUTO_TEST_CASE(ImportDumpPrivkey) { @@ -90,19 +74,13 @@ BOOST_AUTO_TEST_CASE(ImportDumpPrivkey) { std::string response; /* Check if passphrase is correct and unlock wallet */ - try { - fx.btc.walletpassphrase("123456", 10); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -14); - BOOST_WARN_MESSAGE(false, e.getMessage()); - return; - } + NO_THROW_EXCEPT(fx.btc.walletpassphrase("123456", 10), -14); /* Attempt to import the privkey */ - BOOST_REQUIRE_NO_THROW(fx.btc.importprivkey(param1, "TestKey", false)); + NO_THROW(fx.btc.importprivkey(param1, "TestKey", false)); /* Dump private key */ - BOOST_REQUIRE_NO_THROW(response = fx.btc.dumpprivkey(param2)); + NO_THROW(response = fx.btc.dumpprivkey(param2)); BOOST_REQUIRE(param1 == response); #ifdef VERBOSE @@ -111,7 +89,7 @@ BOOST_AUTO_TEST_CASE(ImportDumpPrivkey) { #endif /* Clean up */ - BOOST_REQUIRE_NO_THROW(fx.btc.walletlock()); + NO_THROW(fx.btc.walletlock()); } BOOST_AUTO_TEST_CASE(AddMultisigAddress) { @@ -127,7 +105,7 @@ BOOST_AUTO_TEST_CASE(AddMultisigAddress) { std::string response; std::string address = "36m2bHwsUnkpGsZgVAEmeJRf2CeViDm6RV"; - BOOST_REQUIRE_NO_THROW(response = fx.btc.addmultisigaddress(2, param)); + NO_THROW(response = fx.btc.addmultisigaddress(2, param)); BOOST_REQUIRE(response == address); } @@ -148,7 +126,7 @@ BOOST_AUTO_TEST_CASE(CreateMultisig) { "3ae4210343947d178f20b8267488e488442650c27e1e9956c824077f646d6ce13a" "285d8452ae"; - BOOST_REQUIRE_NO_THROW(response = fx.btc.createmultisig(2, param)); + NO_THROW(response = fx.btc.createmultisig(2, param)); BOOST_REQUIRE(response.address == address); BOOST_REQUIRE(response.redeemScript == redeemScript); } @@ -159,14 +137,7 @@ BOOST_AUTO_TEST_CASE(GetNewAddress) { std::string response; - try { - response = fx.btc.getnewaddress(); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -12); - BOOST_TEST_MESSAGE("[WARNING]: Insufficient keypool size"); - return; - } - + NO_THROW_EXCEPT(response = fx.btc.getnewaddress(), -12); BOOST_REQUIRE(response.length() >= 27 && response.length() <= 34); #ifdef VERBOSE @@ -182,8 +153,8 @@ BOOST_AUTO_TEST_CASE(ValidateAddress) { std::string param; validateaddress_t response; - BOOST_REQUIRE_NO_THROW(param = fx.btc.getaccountaddress("")); - BOOST_REQUIRE_NO_THROW(response = fx.btc.validateaddress(param)); + NO_THROW(param = fx.btc.getaccountaddress("")); + NO_THROW(response = fx.btc.validateaddress(param)); #ifdef VERBOSE std::cout << "=== validateaddress ===" << std::endl; @@ -204,27 +175,21 @@ BOOST_AUTO_TEST_CASE(KeypoolRefill) { int oldkeypoolsize; int newkeypoolsize; int refreshedkeypoolsize; - + std::string addr; /* Get current keypool size and use one key */ - BOOST_REQUIRE_NO_THROW(oldkeypoolsize = fx.btc.getinfo().keypoolsize); - BOOST_REQUIRE_NO_THROW(fx.btc.getnewaddress()); - BOOST_REQUIRE_NO_THROW(newkeypoolsize = fx.btc.getinfo().keypoolsize); + NO_THROW(oldkeypoolsize = fx.btc.getinfo().keypoolsize); + NO_THROW(addr = fx.btc.getnewaddress()); + NO_THROW(newkeypoolsize = fx.btc.getinfo().keypoolsize); - BOOST_REQUIRE((oldkeypoolsize - 1) == newkeypoolsize); + BOOST_REQUIRE((oldkeypoolsize - 1) == newkeypoolsize || newkeypoolsize == 101); /* Check if passphrase is correct and unlock wallet */ - try { - fx.btc.walletpassphrase("123456", 10); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -14); - BOOST_WARN_MESSAGE(false, e.getMessage()); - return; - } + NO_THROW_EXCEPT(fx.btc.walletpassphrase("123456", 10), -14); /* Then refill the keypool */ - BOOST_REQUIRE_NO_THROW(fx.btc.keypoolrefill()); - BOOST_REQUIRE_NO_THROW(refreshedkeypoolsize = fx.btc.getinfo().keypoolsize); + NO_THROW(fx.btc.keypoolrefill()); + NO_THROW(refreshedkeypoolsize = fx.btc.getinfo().keypoolsize); BOOST_REQUIRE(refreshedkeypoolsize == 101); } @@ -235,10 +200,10 @@ BOOST_AUTO_TEST_CASE(SetTxFee) { bool response; double getinfofee; - BOOST_REQUIRE_NO_THROW(response = fx.btc.settxfee(0.0002)); + NO_THROW(response = fx.btc.settxfee(0.0002)); BOOST_REQUIRE(response == true); - BOOST_REQUIRE_NO_THROW(getinfofee = fx.btc.getinfo().paytxfee); + NO_THROW(getinfofee = fx.btc.getinfo().paytxfee); BOOST_REQUIRE(getinfofee == 0.0002); } @@ -253,18 +218,12 @@ BOOST_AUTO_TEST_CASE(SignVerifyMessage) { bool response = false; /* Check if passphrase is correct and unlock wallet */ - try { - fx.btc.walletpassphrase("123456", 10); - } catch (BitcoinException& e) { - BOOST_REQUIRE(e.getCode() == -14); - BOOST_TEST_MESSAGE("[WARNING]: Incorrect wallet passphrase"); - return; - } - - BOOST_REQUIRE_NO_THROW(addr = fx.btc.getaccountaddress("")); - BOOST_REQUIRE_NO_THROW(sig = fx.btc.signmessage(addr, msg1)); - - BOOST_REQUIRE_NO_THROW(response = fx.btc.verifymessage(addr, sig, msg1)); + NO_THROW_EXCEPT(fx.btc.walletpassphrase("123456", 10), -14); + + NO_THROW(addr = fx.btc.getaccountaddress("")); + NO_THROW(sig = fx.btc.signmessage(addr, msg1)); + + NO_THROW(response = fx.btc.verifymessage(addr, sig, msg1)); BOOST_REQUIRE(response == true); BOOST_REQUIRE_NO_THROW(response = fx.btc.verifymessage(addr, sig, msg2));