Skip to content

Commit

Permalink
Merge branch 'debug'
Browse files Browse the repository at this point in the history
  • Loading branch information
minium committed Feb 21, 2016
2 parents c9b8211 + 97e7397 commit f592584
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 170 deletions.
4 changes: 2 additions & 2 deletions src/bitcoinapi/bitcoinapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -174,7 +174,7 @@ vector<nodeinfo_t> 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);
}
Expand Down
68 changes: 53 additions & 15 deletions src/bitcoinapi/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
#include <string>
#include <sstream>

#include <jsoncpp/json/json.h>
#include <jsoncpp/json/reader.h>
#include <jsoncpp/json/value.h>

using Json::Value;
using Json::Reader;


class BitcoinException: public std::exception
{
private:
Expand All @@ -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() { };

Expand All @@ -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;
}
};

Expand Down
63 changes: 24 additions & 39 deletions src/test/accounting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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("");
Expand All @@ -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);
Expand All @@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(ListReceivedByAccount) {
MyFixture fx;

std::vector<accountinfo_t> 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;
Expand All @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(ListReceivedByAddress) {
MyFixture fx;

std::vector<addressinfo_t> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -151,7 +147,7 @@ BOOST_AUTO_TEST_CASE(ListTransactions) {
MyFixture fx;

std::vector<transactioninfo_t> response;
BOOST_REQUIRE_NO_THROW(response = fx.btc.listtransactions());
NO_THROW(response = fx.btc.listtransactions());

#ifdef VERBOSE
std::cout << "=== listtransactions ===" << std::endl;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -232,7 +217,7 @@ BOOST_AUTO_TEST_CASE(GetAddressesByAccount) {
MyFixture fx;

std::vector<std::string> response;
BOOST_REQUIRE_NO_THROW(response = fx.btc.getaddressesbyaccount("TestUser"));
NO_THROW(response = fx.btc.getaddressesbyaccount("TestUser"));
BOOST_REQUIRE(response.size() >= 1);

#ifdef VERBOSE
Expand All @@ -249,7 +234,7 @@ BOOST_AUTO_TEST_CASE(ListAccounts) {
MyFixture fx;

std::map<std::string, double> response;
BOOST_REQUIRE_NO_THROW(response = fx.btc.listaccounts());
NO_THROW(response = fx.btc.listaccounts());

#ifdef VERBOSE
std::cout << "=== listaccounts ===" << std::endl;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -393,7 +378,7 @@ BOOST_AUTO_TEST_CASE(ListUnspent) {
MyFixture fx;

std::vector<unspenttxout_t> response;
BOOST_REQUIRE_NO_THROW(response = fx.btc.listunspent());
NO_THROW(response = fx.btc.listunspent());

#ifdef VERBOSE
std::cout << "=== listunspent ===" << std::endl;
Expand All @@ -415,7 +400,7 @@ BOOST_AUTO_TEST_CASE(ListLockUnspent) {
MyFixture fx;

std::vector<txout_t> response;
BOOST_REQUIRE_NO_THROW(response = fx.btc.listlockunspent());
NO_THROW(response = fx.btc.listlockunspent());

#ifdef VERBOSE
std::cout << "=== listlockunspent ===" << std::endl;
Expand All @@ -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);
}

Expand All @@ -445,7 +430,7 @@ BOOST_AUTO_TEST_CASE(ListAddressGroupings) {
MyFixture fx;
std::vector< std::vector<addressgrouping_t> > response;

BOOST_REQUIRE_NO_THROW(response = fx.btc.listaddressgroupings());
NO_THROW(response = fx.btc.listaddressgroupings());

#ifdef VERBOSE
std::cout << "=== listaddressgroupings ===" << std::endl;
Expand Down
5 changes: 3 additions & 2 deletions src/test/general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
#include <bitcoinapi/bitcoinapi.h>
#include <bitcoinapi/exception.h>

#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 {

Expand Down
Loading

0 comments on commit f592584

Please sign in to comment.