From 430f1ea802dac3c9fb19e1c508d36eab6dddc5c4 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Mon, 20 Aug 2018 08:31:40 +0800 Subject: [PATCH] txsearch_tests.cpp initialized --- tests/CMakeLists.txt | 5 ++ tests/bcstatus_tests.cpp | 156 +----------------------------------- tests/helpers.h | 5 +- tests/mocks.h | 169 +++++++++++++++++++++++++++++++++++++++ tests/txsearch_tests.cpp | 95 ++++++++++++++++++++++ 5 files changed, 273 insertions(+), 157 deletions(-) create mode 100644 tests/mocks.h create mode 100644 tests/txsearch_tests.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 54469fa..423f895 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/bcstatus_tests.cpp b/tests/bcstatus_tests.cpp index 1140b31..76ed711 100644 --- a/tests/bcstatus_tests.cpp +++ b/tests/bcstatus_tests.cpp @@ -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(const uint64_t& h1, - const uint64_t& h2)); - - MOCK_CONST_METHOD3(get_transactions, - bool(const std::vector& txs_ids, - std::vector& txs, - std::vector& 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& absolute_offsets, - vector& 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 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_infos, - vector& 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& 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 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 @@ -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}; diff --git a/tests/helpers.h b/tests/helpers.h index 18ee3aa..9a20722 100644 --- a/tests/helpers.h +++ b/tests/helpers.h @@ -1,5 +1,4 @@ -#ifndef HELPERS_H -#define HELPERS_H +#pragma once #include @@ -25,5 +24,3 @@ ACC_FROM_HEX(hex_address); - -#endif // HELPERS_H diff --git a/tests/mocks.h b/tests/mocks.h new file mode 100644 index 0000000..041ef06 --- /dev/null +++ b/tests/mocks.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(const uint64_t& h1, + const uint64_t& h2)); + + MOCK_CONST_METHOD3(get_transactions, + bool(const std::vector& txs_ids, + std::vector& txs, + std::vector& 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& absolute_offsets, + vector& 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 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_infos, + vector& 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& 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 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()); + +}; + + +} diff --git a/tests/txsearch_tests.cpp b/tests/txsearch_tests.cpp new file mode 100644 index 0000000..f923b94 --- /dev/null +++ b/tests/txsearch_tests.cpp @@ -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 +{ +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(); + mcore_ptr = mcore.get(); + + rpc = std::make_unique("dummy deamon url"); + rpc_ptr = rpc.get(); + + bcs = std::make_unique( + 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 mcore; + std::unique_ptr rpc; + std::unique_ptr bcs; + + MockMicroCore* mcore_ptr; + MockRPCCalls* rpc_ptr; + + static json config_json; +}; + +json TXSEARCH_TEST::config_json; + + + +}