BlockchainSetup tested

pull/93/merge
moneroexamples 6 years ago
parent b9c4ed69b7
commit 808e9e8494

@ -16,19 +16,7 @@ BlockchainSetup::BlockchainSetup(
config_path {_config_path}
{
// check if config-file provided exist
if (!boost::filesystem::exists(config_path))
throw std::runtime_error("Config file " + config_path + " does not exist");
try
{
// try reading and parsing json config file provided
std::ifstream i(config_path);
i >> config_json;
}
catch (const std::exception& e)
{
throw std::runtime_error(string{"Error reading confing file: "} + e.what());
}
config_json = read_config(config_path);
_init();
}
@ -51,22 +39,29 @@ BlockchainSetup::parse_addr_and_viewkey()
if (import_payment_address_str.empty() || import_payment_viewkey_str.empty())
std::runtime_error("config address or viewkey are empty.");
if (!parse_str_address(
import_payment_address_str,
import_payment_address,
net_type))
try
{
cerr << "Cant parse address_str: "
<< import_payment_address_str
<< endl;
throw std::runtime_error("Cant parse config address: " + import_payment_address_str);
}
if (!parse_str_address(
import_payment_address_str,
import_payment_address,
net_type))
{
throw std::runtime_error("Cant parse config address: " + import_payment_address_str);
}
if (!xmreg::parse_str_secret_key(
import_payment_viewkey_str,
import_payment_viewkey))
{
throw std::runtime_error("Cant parse config viewkey: " + import_payment_viewkey_str);
}
if (!xmreg::parse_str_secret_key(
import_payment_viewkey_str,
import_payment_viewkey))
}
catch (std::exception const& e)
{
throw std::runtime_error("Cant parse config viewkey: " + import_payment_viewkey_str);
throw;
}
}
@ -110,7 +105,50 @@ BlockchainSetup::get_blockchain_path()
}
void
string
BlockchainSetup::get_network_name(network_type n_type)
{
switch (n_type)
{
case network_type::MAINNET:
return "mainnet";
case network_type::TESTNET:
return "testnet";
case network_type::STAGENET:
return "stagenet";
default:
throw std::runtime_error("wrong network");
}
}
json
BlockchainSetup::read_config(string config_json_path)
{
// check if config-file provided exist
if (!boost::filesystem::exists(config_json_path))
throw std::runtime_error("Config file " + config_json_path + " does not exist");
json _config;
try
{
// try reading and parsing json config file provided
std::ifstream i(config_json_path);
i >> _config;
}
catch (const std::exception& e)
{
throw std::runtime_error(string{"Cant parse json string as json: "} + e.what());
}
return _config;
}
void
BlockchainSetup::_init()
{
refresh_block_status_every_seconds = config_json["refresh_block_status_every_seconds"];

@ -67,6 +67,12 @@ public:
json
get_config() const {return config_json;}
static string
get_network_name(network_type n_type);
static json
read_config(string config_json_path);
private:
void _init();

@ -19,7 +19,13 @@ endmacro()
resource_dir("./res")
add_om_test(mysql)
add_om_test(microcore)
SETUP_TARGET_FOR_COVERAGE(
NAME test_mysql_coverage # New target name
EXECUTABLE mysql_tests)
NAME mysql_coverage # New target name
EXECUTABLE mysql_tests)
SETUP_TARGET_FOR_COVERAGE(
NAME microcore_coverage # New target name
EXECUTABLE microcore_tests)

@ -3,8 +3,7 @@
//
#include "../src/MicroCore.h"
#include "../src/YourMoneroRequests.h"
#include "../src/MysqlPing.h"
#include "../src/BlockchainSetup.h"
#include "gmock/gmock.h"
@ -25,45 +24,206 @@ using namespace std::chrono_literals;
using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::internal::FilePath;
class DifferentNetworks :
public ::testing::TestWithParam<network_type>
{
};
class BlockchainSetupTest : public DifferentNetworks
{};
TEST_P(BlockchainSetupTest, ReadInConfigFile)
{
network_type net_type = GetParam();
string net_name = xmreg::BlockchainSetup::get_network_name(net_type);
bool do_not_relay {false};
string confing_path {"../config/config.json"};
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, confing_path};
json config_json = xmreg::BlockchainSetup::read_config(confing_path);
EXPECT_EQ(bc_setup.import_payment_address_str,
config_json["wallet_import"][net_name]["address"]);
// we expact that bc_setup will have json config equal to what we read manually
EXPECT_EQ(bc_setup.get_config(), config_json);
xmreg::BlockchainSetup bc_setup2 {net_type, do_not_relay, config_json};
EXPECT_EQ(bc_setup2.import_payment_address_str,
config_json["wallet_import"][net_name]["address"]);
}
TEST_P(BlockchainSetupTest, ReadInConfigFileFailure)
{
network_type net_type = GetParam();
string net_name = xmreg::BlockchainSetup::get_network_name(net_type);
bool do_not_relay {false};
string confing_path {"../non_exisiting/config.json"};
json
readin_config()
try
{
// expect throw if confing file does not exist
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, confing_path};
}
catch (std::exception const& e)
{
EXPECT_THAT(e.what(), HasSubstr("does not exist"));
}
confing_path = "./tests/res/config_ill_formed.json";
if (!boost::filesystem::exists(confing_path))
confing_path = "./res/config_ill_formed.json";
FilePath fp {confing_path};
ASSERT_TRUE(fp.FileOrDirectoryExists());
try
{
// expect throw if confing file is illformed and cant be parsed into json
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, confing_path};
}
catch (std::exception const& e)
{
EXPECT_THAT(e.what(), HasSubstr("Cant parse json "));
}
}
TEST_P(BlockchainSetupTest, ParsingConfigFileFailure)
{
// read in confing json file and get test db info
network_type net_type = GetParam();
string net_name = xmreg::BlockchainSetup::get_network_name(net_type);
bool do_not_relay {false};
string config_path {"../config/config.json"};
json config_json = xmreg::BlockchainSetup::read_config(config_path);
std::string config_json_path{"../config/config.json"};
json wrong_data = config_json;
// check if config-file provided exist
if (!boost::filesystem::exists(config_json_path))
// make mistake in address
wrong_data["wallet_import"][net_name]["address"] = string {"00011231"};
try
{
// expect throw if cant parase address or viewkey
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, wrong_data};
}
catch (std::exception const& e)
{
std::cerr << "Config file " << config_json_path
<< " does not exist\n";
EXPECT_TRUE(true);
}
// provide empty address
wrong_data = config_json;
wrong_data["wallet_import"][net_name]["address"] = string {};
return {};
try
{
// expect throw if cant parase address or viewkey
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, wrong_data};
}
catch (std::exception const& e)
{
EXPECT_TRUE(true);
}
json config_json;
// provide wrong viewkey
wrong_data = config_json;
wrong_data["wallet_import"][net_name]["viewkey"] = string {"00011231"};
try
{
// try reading and parsing json config file provided
std::ifstream i(config_json_path);
i >> config_json;
// expect throw if cant parase address or viewkey
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, wrong_data};
}
catch (std::exception const& e)
{
EXPECT_TRUE(true);
}
}
TEST(BlockchainSetupTest, WrongNetworkType)
{
network_type net_type = network_type::UNDEFINED;
try
{
// expect throw if cant parase address or viewkey
xmreg::BlockchainSetup::get_network_name(net_type);
}
catch (std::exception const& e)
{
EXPECT_TRUE(true);
}
bool do_not_relay {false};
string config_path {"../config/config.json"};
return config_json;
json config_json = xmreg::BlockchainSetup::read_config(config_path);
try
{
// expect throw if cant parase address or viewkey
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, config_json};
}
catch (const std::exception &e)
catch (std::exception const& e)
{
std::cerr << "Error reading confing file "
<< config_json_path << ": "
<< e.what() << '\n';
return {};
EXPECT_TRUE(true);
}
}
TEST_P(BlockchainSetupTest, WrongBlockchainPath)
{
network_type net_type = GetParam();
string net_name = xmreg::BlockchainSetup::get_network_name(net_type);
return {};
bool do_not_relay {false};
string config_path {"../config/config.json"};
json config_json = xmreg::BlockchainSetup::read_config(config_path);
// make mistake in address
config_json["blockchain-path"][net_name]= string {"/wrong/path/to/bloclckahin"};
try
{
// expect throw if cant parase address or viewkey
xmreg::BlockchainSetup bc_setup {net_type, do_not_relay, config_json};
}
catch (std::exception const& e)
{
EXPECT_TRUE(true);
}
}
INSTANTIATE_TEST_CASE_P(
DifferentMoneroNetworks, BlockchainSetupTest,
::testing::Values(
network_type::MAINNET,
network_type::TESTNET,
network_type::STAGENET));
class MICROCORE_TEST : public ::testing::Test
{
@ -73,10 +233,7 @@ public:
SetUpTestCase()
{
db_config = readin_config();
if (db_config.empty())
FAIL() << "Cant readin_config()";
}
protected:
@ -85,15 +242,14 @@ protected:
virtual void
SetUp()
{
//bc_setup = xmreg::BlockchainSetup("../config/config.json");
//if (config_json.empty())
// FAIL() << "Cant readin_config()";
}
static std::string db_data;
static json db_config;
std::shared_ptr<xmreg::MySqlAccounts> xmr_accounts;
std::shared_ptr<xmreg::CurrentBlockchainStatus> current_bc_status;
xmreg::BlockchainSetup bc_setup;
};
std::string MYSQL_TEST::db_data;
json MYSQL_TEST::db_config;
}

@ -0,0 +1,67 @@
{
"daemon-url" :
{
"_comment" : "MISSING COMMA is INVALID JSON format -->"
"mainnet" : "http://127.0.0.1:18081",
"testnet" : "http://127.0.0.1:28081",
"stagenet" : "http://127.0.0.1:38081"
},
"blockchain-path" :
{
"_comment" : "if paths are empty, default Monero paths will be used",
"mainnet" : "",
"testnet" : "",
"stagenet" : ""
},
"database" :
{
"_comment" : "how should the backend connect to the mysql database",
"url" : "127.0.0.1",
"port" : 3306,
"dbname" : "openmonero",
"user" : "root",
"password" : "root"
},
"database_test":
{
"_comment" : "how should the backend connect to the test mysql database",
"url" : "127.0.0.1",
"port" : 3306,
"dbname" : "openmonero_test",
"user" : "root",
"password" : "root"
},
"wallet_import" :
{
"_comment": "if fee is 0, then importing is free. fee is in base 1e12, e.g., 0.1 xmr is 0.1 x 1e12 = 100000000000",
"fee" : 0,
"testnet" :
{
"address" : "9tzmPMTViHYM3z6NAgQni1Qm1Emzxy5hQFibPgWD3LVTAz91yok5Eni1pH6zKhBHzpTU15GZooPHSGHXFvFuXEdmEG2sWAZ",
"viewkey" : "ae6184287ca791609844f140b8502ccfa2223c04c8699cf31fcd0af1f1d0be08"
},
"mainnet" :
{
"_comment": "these are official monero project donation address and viewkey. change it to yours",
"address" : "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A",
"viewkey" : "f359631075708155cc3d92a32b75a7d02a5dcf27756707b47a2b31b21c389501"
},
"stagenet" :
{
"address" : "53mqDDKtVkib8inMa41HuNJG4tj9CcaNKGr6EVSbvhWGJdpDQCiNNYBUNF1oDb8BczU5aD68d3HNKXaEsPq8cvbQE2FBkTS",
"viewkey" : "53c5850e895122574c53a4f952c726be3fe22bcd2b08f4bfed8946d887cc950b"
}
},
"refresh_block_status_every_seconds" : 10,
"blocks_search_lookahead" : 200,
"search_thread_life_in_seconds" : 120,
"max_number_of_blocks_to_import" : 132000,
"ssl" :
{
"enable" : false,
"_comment": "below are example paths to crt and key files need to be given",
"ssl-key": "file:///tmp/server.key",
"ssl-crt": "file:///tmp/server.crt",
"dh-pem" : "file:///tmp/dh2048.pem"
}
}

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save