txsearch_tests.cpp initialized

pull/85/head
moneroexamples 6 years ago
parent 774cc71ab0
commit 430f1ea802

@ -22,6 +22,7 @@ resource_dir("./res")
add_om_test(mysql)
add_om_test(microcore)
add_om_test(bcstatus)
add_om_test(txsearch)
SETUP_TARGET_FOR_COVERAGE(
NAME mysql_cov
@ -35,3 +36,7 @@ SETUP_TARGET_FOR_COVERAGE(
SETUP_TARGET_FOR_COVERAGE(
NAME bcstatus_cov
EXECUTABLE bcstatus_tests)
SETUP_TARGET_FOR_COVERAGE(
NAME txsearch_cov
EXECUTABLE txsearch_tests)

@ -1,6 +1,3 @@
//
// Created by mwo on 15/06/18.
//
#include "../src/MicroCore.h"
#include "../src/CurrentBlockchainStatus.h"
@ -10,161 +7,13 @@
#include "gtest/gtest.h"
#include "helpers.h"
#include "mocks.h"
namespace
{
//using json = nlohmann::json;
using namespace std;
using namespace cryptonote;
using namespace epee::string_tools;
using namespace std::chrono_literals;
using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::Return;
using ::testing::Throw;
using ::testing::DoAll;
using ::testing::SetArgReferee;
using ::testing::SetArgPointee;
using ::testing::_;
using ::testing::internal::FilePath;
class MockMicroCore : public xmreg::MicroCore
{
public:
MOCK_METHOD2(init, bool(const string& _blockchain_path,
network_type nt));
MOCK_CONST_METHOD0(get_current_blockchain_height, uint64_t());
MOCK_CONST_METHOD2(get_block_from_height,
bool(uint64_t height, block& blk));
MOCK_CONST_METHOD2(get_blocks_range,
std::vector<block>(const uint64_t& h1,
const uint64_t& h2));
MOCK_CONST_METHOD3(get_transactions,
bool(const std::vector<crypto::hash>& txs_ids,
std::vector<transaction>& txs,
std::vector<crypto::hash>& missed_txs));
MOCK_CONST_METHOD1(have_tx, bool(crypto::hash const& tx_hash));
MOCK_CONST_METHOD1(get_tx_block_height,
uint64_t(crypto::hash const& tx_hash));
MOCK_CONST_METHOD2(tx_exists,
bool(crypto::hash const& tx_hash,
uint64_t& tx_id));
MOCK_CONST_METHOD2(get_output_tx_and_index,
tx_out_index(uint64_t const& amount,
uint64_t const& index));
MOCK_CONST_METHOD2(get_tx,
bool(crypto::hash const& tx_hash,
transaction& tx));
MOCK_METHOD3(get_output_key,
void(const uint64_t& amount,
const vector<uint64_t>& absolute_offsets,
vector<cryptonote::output_data_t>& outputs));
MOCK_METHOD2(get_output_key,
output_data_t(uint64_t amount,
uint64_t global_amount_index));
MOCK_CONST_METHOD1(get_tx_amount_output_indices,
std::vector<uint64_t>(uint64_t const& tx_id));
MOCK_CONST_METHOD2(get_random_outs_for_amounts,
bool(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request const& req,
COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res));
MOCK_CONST_METHOD2(get_outs,
bool(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req,
COMMAND_RPC_GET_OUTPUTS_BIN::response& res));
MOCK_CONST_METHOD1(get_dynamic_per_kb_fee_estimate,
uint64_t(uint64_t const& grace_blocks));
MOCK_CONST_METHOD2(get_mempool_txs,
bool(vector<tx_info>& tx_infos,
vector<spent_key_image_info>& key_image_infos));
// dont need to mock this function currently
// MOCK_METHOD3(decrypt_payment_id,
// bool(crypto::hash8& payment_id,
// crypto::public_key const& public_key,
// crypto::secret_key const& secret_key));
};
class MockRPCCalls : public xmreg::RPCCalls
{
public:
MockRPCCalls(string _deamon_url)
: xmreg::RPCCalls(_deamon_url)
{}
MOCK_METHOD3(commit_tx, bool(const string& tx_blob,
string& error_msg,
bool do_not_relay));
};
class MockTxSearch : public xmreg::TxSearch
{
public:
MOCK_METHOD0(operator_fcall, void());
virtual void operator()() {operator_fcall();}
MOCK_METHOD0(ping, void());
MOCK_CONST_METHOD0(still_searching, bool());
MOCK_CONST_METHOD0(get_searched_blk_no, uint64_t());
MOCK_METHOD0(get_known_outputs_keys,
xmreg::TxSearch::known_outputs_t());
MOCK_CONST_METHOD0(get_xmr_address_viewkey,
xmreg::TxSearch::addr_view_t());
// google mock has some issues with nlohmann::json
// as input or return parameters in functions it mocks.
// it resutls in wornings, so to avoid it we are going
// to proxy json as strings.
MOCK_METHOD2(mock_find_txs_in_mempool,
void(xmreg::TxSearch::pool_txs_t mempool_txs,
vector<string>& transactions_json_str));
// now we need to manualy overwrite TxSearch::find_txs_in_mempool
// to return j_transactions based on transactions_json_str
// from the mock_find_txs_in_mempool
virtual void
find_txs_in_mempool(pool_txs_t mempool_txs,
nlohmann::json* j_transactions)
{
vector<string> transactions_json_str;
mock_find_txs_in_mempool(mempool_txs, transactions_json_str);
*j_transactions = nlohmann::json::array();
for (auto& json_str: transactions_json_str)
j_transactions->push_back(nlohmann::json::parse(json_str));
}
MOCK_METHOD0(get_exception_ptr, std::exception_ptr());
};
class BCSTATUS_TEST : public ::testing::TestWithParam<network_type>
@ -211,9 +60,10 @@ protected:
static nlohmann::json config_json;
};
nlohmann::json BCSTATUS_TEST::config_json;
TEST_P(BCSTATUS_TEST, DefaultConstruction)
{
xmreg::CurrentBlockchainStatus bcs {bc_setup, nullptr, nullptr};

@ -1,5 +1,4 @@
#ifndef HELPERS_H
#define HELPERS_H
#pragma once
#include <string>
@ -25,5 +24,3 @@
ACC_FROM_HEX(hex_address);
#endif // HELPERS_H

@ -0,0 +1,169 @@
#pragma once
#include "../src/MicroCore.h"
#include "../src/CurrentBlockchainStatus.h"
#include "../src/ThreadRAII.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace
{
//using json = nlohmann::json;
using namespace std;
using namespace cryptonote;
using namespace epee::string_tools;
using namespace std::chrono_literals;
using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::Return;
using ::testing::Throw;
using ::testing::DoAll;
using ::testing::SetArgReferee;
using ::testing::SetArgPointee;
using ::testing::_;
using ::testing::internal::FilePath;
class MockMicroCore : public xmreg::MicroCore
{
public:
MOCK_METHOD2(init, bool(const string& _blockchain_path,
network_type nt));
MOCK_CONST_METHOD0(get_current_blockchain_height, uint64_t());
MOCK_CONST_METHOD2(get_block_from_height,
bool(uint64_t height, block& blk));
MOCK_CONST_METHOD2(get_blocks_range,
std::vector<block>(const uint64_t& h1,
const uint64_t& h2));
MOCK_CONST_METHOD3(get_transactions,
bool(const std::vector<crypto::hash>& txs_ids,
std::vector<transaction>& txs,
std::vector<crypto::hash>& missed_txs));
MOCK_CONST_METHOD1(have_tx, bool(crypto::hash const& tx_hash));
MOCK_CONST_METHOD1(get_tx_block_height,
uint64_t(crypto::hash const& tx_hash));
MOCK_CONST_METHOD2(tx_exists,
bool(crypto::hash const& tx_hash,
uint64_t& tx_id));
MOCK_CONST_METHOD2(get_output_tx_and_index,
tx_out_index(uint64_t const& amount,
uint64_t const& index));
MOCK_CONST_METHOD2(get_tx,
bool(crypto::hash const& tx_hash,
transaction& tx));
MOCK_METHOD3(get_output_key,
void(const uint64_t& amount,
const vector<uint64_t>& absolute_offsets,
vector<cryptonote::output_data_t>& outputs));
MOCK_METHOD2(get_output_key,
output_data_t(uint64_t amount,
uint64_t global_amount_index));
MOCK_CONST_METHOD1(get_tx_amount_output_indices,
std::vector<uint64_t>(uint64_t const& tx_id));
MOCK_CONST_METHOD2(get_random_outs_for_amounts,
bool(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request const& req,
COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res));
MOCK_CONST_METHOD2(get_outs,
bool(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req,
COMMAND_RPC_GET_OUTPUTS_BIN::response& res));
MOCK_CONST_METHOD1(get_dynamic_per_kb_fee_estimate,
uint64_t(uint64_t const& grace_blocks));
MOCK_CONST_METHOD2(get_mempool_txs,
bool(vector<tx_info>& tx_infos,
vector<spent_key_image_info>& key_image_infos));
// dont need to mock this function currently
// MOCK_METHOD3(decrypt_payment_id,
// bool(crypto::hash8& payment_id,
// crypto::public_key const& public_key,
// crypto::secret_key const& secret_key));
};
class MockRPCCalls : public xmreg::RPCCalls
{
public:
MockRPCCalls(string _deamon_url)
: xmreg::RPCCalls(_deamon_url)
{}
MOCK_METHOD3(commit_tx, bool(const string& tx_blob,
string& error_msg,
bool do_not_relay));
};
class MockTxSearch : public xmreg::TxSearch
{
public:
MOCK_METHOD0(operator_fcall, void());
virtual void operator()() {operator_fcall();}
MOCK_METHOD0(ping, void());
MOCK_CONST_METHOD0(still_searching, bool());
MOCK_CONST_METHOD0(get_searched_blk_no, uint64_t());
MOCK_METHOD0(get_known_outputs_keys,
xmreg::TxSearch::known_outputs_t());
MOCK_CONST_METHOD0(get_xmr_address_viewkey,
xmreg::TxSearch::addr_view_t());
// google mock has some issues with nlohmann::json
// as input or return parameters in functions it mocks.
// it resutls in wornings, so to avoid it we are going
// to proxy json as strings.
MOCK_METHOD2(mock_find_txs_in_mempool,
void(xmreg::TxSearch::pool_txs_t mempool_txs,
vector<string>& transactions_json_str));
// now we need to manualy overwrite TxSearch::find_txs_in_mempool
// to return j_transactions based on transactions_json_str
// from the mock_find_txs_in_mempool
virtual void
find_txs_in_mempool(pool_txs_t mempool_txs,
nlohmann::json* j_transactions)
{
vector<string> transactions_json_str;
mock_find_txs_in_mempool(mempool_txs, transactions_json_str);
*j_transactions = nlohmann::json::array();
for (auto& json_str: transactions_json_str)
j_transactions->push_back(nlohmann::json::parse(json_str));
}
MOCK_METHOD0(get_exception_ptr, std::exception_ptr());
};
}

@ -0,0 +1,95 @@
#include "../src/MicroCore.h"
#include "../src/CurrentBlockchainStatus.h"
#include "../src/ThreadRAII.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "helpers.h"
#include "mocks.h"
namespace
{
using json = nlohmann::json;
class OUTPUTIDENT_TEST : public ::testing::Test
{
public:
static void
SetUpTestCase()
{
string config_path {"../config/config.json"};
config_json = xmreg::BlockchainSetup::read_config(config_path);
}
protected:
virtual void
SetUp()
{
bc_setup = xmreg::BlockchainSetup{net_type, do_not_relay, config_json};
}
network_type net_type {network_type::STAGENET};
bool do_not_relay {false};
xmreg::BlockchainSetup bc_setup;
static json config_json;
};
json OUTPUTIDENT_TEST::config_json;
class TXSEARCH_TEST : public ::testing::TestWithParam<network_type>
{
public:
static void
SetUpTestCase()
{
string config_path {"../config/config.json"};
config_json = xmreg::BlockchainSetup::read_config(config_path);
}
protected:
virtual void
SetUp()
{
net_type = GetParam();
bc_setup = xmreg::BlockchainSetup {
net_type, do_not_relay, config_json};
mcore = std::make_unique<MockMicroCore>();
mcore_ptr = mcore.get();
rpc = std::make_unique<MockRPCCalls>("dummy deamon url");
rpc_ptr = rpc.get();
bcs = std::make_unique<xmreg::CurrentBlockchainStatus>(
bc_setup, std::move(mcore), std::move(rpc));
}
network_type net_type {network_type::STAGENET};
bool do_not_relay {false};
xmreg::BlockchainSetup bc_setup;
std::unique_ptr<MockMicroCore> mcore;
std::unique_ptr<MockRPCCalls> rpc;
std::unique_ptr<xmreg::CurrentBlockchainStatus> bcs;
MockMicroCore* mcore_ptr;
MockRPCCalls* rpc_ptr;
static json config_json;
};
json TXSEARCH_TEST::config_json;
}
Loading…
Cancel
Save