JsonTx helper class for unit tests added

new_rpc
moneroexamples 5 years ago
parent eb34693b37
commit 6e6d980f80

@ -64,14 +64,17 @@ endmacro(create_git_version)
macro(resource_dir srcDir)
# Scan through resource folder for updated files and copy if none existing or changed
file (GLOB_RECURSE resources "${srcDir}/*.*")
foreach(resource ${resources})
get_filename_component(filename ${resource} NAME)
get_filename_component(dir ${resource} DIRECTORY)
get_filename_component(dirname ${dir} NAME)
# message("${dirname} ${srcDir}")
set(topdir ${dirname})
set (output "")
set(output "")
while(NOT ${dirname} STREQUAL ${srcDir})
get_filename_component(path_component ${dir} NAME)
@ -90,7 +93,7 @@ macro(resource_dir srcDir)
${resource}
${output}
)
add_custom_target(${filename} ALL DEPENDS ${resource} ${output})
add_custom_target("${dirname}${filename}" ALL DEPENDS ${resource} ${output})
endforeach()
endmacro(resource_dir)
endmacro(resource_dir)

@ -194,7 +194,7 @@ void Input::identify(transaction const& tx,
{
// get basic information about mixn's output
output_data_t const& output_data
= mixin_outputs[count];
= mixin_outputs.at(count);
// before going to the mysql, check our known outputs cash
// if the key exists. Its much faster than going to mysql

@ -95,12 +95,12 @@ protected:
class Input : public BaseIdentifier
{
public:
using key_imgs_map_t = unordered_map<public_key, uint64_t>;
//output_pubk , amount
using known_outputs_t = unordered_map<public_key, uint64_t>;
Input(address_parse_info const* _a,
secret_key const* _viewkey,
key_imgs_map_t const* _known_outputs,
known_outputs_t const* _known_outputs,
MicroCore* _mcore)
: BaseIdentifier(_a, _viewkey),
known_outputs {_known_outputs},
@ -127,7 +127,7 @@ protected:
};
secret_key const* viewkey {nullptr};
key_imgs_map_t const* known_outputs {nullptr};
known_outputs_t const* known_outputs {nullptr};
MicroCore* mcore {nullptr};
vector<info> identified_inputs;
};

@ -3,7 +3,8 @@
macro(add_om_test _TEST_NAME)
add_executable(${_TEST_NAME}_tests
${_TEST_NAME}_tests.cpp
${_TEST_NAME}_tests.cpp
JsonTx.cpp
#${MONERO_DIR}/tests/core_tests/chaingen.cpp
#${MONERO_DIR}/tests/core_tests/chaingen001.cpp
)
@ -17,7 +18,7 @@ macro(add_om_test _TEST_NAME)
endmacro()
resource_dir("./res")
resource_dir("res")
add_om_test(mysql)
add_om_test(microcore)

@ -0,0 +1,105 @@
#include "JsonTx.h"
namespace xmreg
{
JsonTx::JsonTx(json _jtx): jtx {std::move(_jtx)}
{
}
JsonTx::JsonTx(string _path): jpath {std::move(_path)}
{
if (!read_config())
{
throw std::runtime_error("Cant read " + jpath);
}
init();
}
void
JsonTx::init()
{
ntype = cryptonote::network_type {jtx["nettype"]};
addr_and_viewkey_from_string(
jtx["sender"]["address"], jtx["sender"]["viewkey"], \
ntype, sender.address, sender.viewkey);
parse_str_secret_key(jtx["sender"]["spendkey"], sender.spendkey);
sender.amount = jtx["sender"]["amount"];
sender.change = jtx["sender"]["change"];
for (auto const& recpient: jtx["recipient"])
{
recipients.push_back(account{});
addr_and_viewkey_from_string(
recpient["address"], recpient["viewkey"], \
ntype, recipients.back().address,
recipients.back().viewkey);
parse_str_secret_key(recpient["spendkey"],
recipients.back().spendkey);
recipients.back().amount = recpient["amount"];
}
if (!hex_to_tx(jtx["hex"], tx, tx_hash, tx_prefix_hash))
{
throw std::runtime_error("hex_to_tx(jtx[\"hex\"], tx, tx_hash, tx_prefix_hash)");
}
}
bool
JsonTx::read_config()
{
if (!boost::filesystem::exists(jpath))
{
cerr << "Config file " << jpath << " does not exist\n";
return false;
}
try
{
// try reading and parsing json config file provided
std::ifstream i(jpath);
i >> jtx;
}
catch (std::exception const& e)
{
cerr << "Cant parse json string as json: " << e.what() << endl;
return false;
}
return true;
}
bool
check_and_adjust_path(string& in_path)
{
if (!boost::filesystem::exists(in_path))
in_path = "./tests/" + in_path;
return boost::filesystem::exists(in_path);
}
boost::optional<JsonTx>
construct_jsontx(string tx_hash)
{
string tx_path = "./tx/" + tx_hash + ".json";
if (!check_and_adjust_path(tx_path))
{
return {};
}
return JsonTx {tx_path};
}
}

@ -0,0 +1,62 @@
#ifndef JSONTX_H
#define JSONTX_H
#include "../src/tools.h"
namespace xmreg
{
/**
* @brief reprsents txs found in tests/res/tx folder
*
* Usef for unit testing and mocking tx data, which
* otherwise would need to be fetched from blockchain
*
*/
class JsonTx
{
public:
struct account
{
address_parse_info address {};
secret_key viewkey {};
secret_key spendkey {};
uint64_t amount {0};
uint64_t change {0};
};
json jtx;
transaction tx;
crypto::hash tx_hash; \
crypto::hash tx_prefix_hash;
crypto::public_key tx_pub_key;
string jpath;
network_type ntype;
account sender;
vector<account> recipients;
explicit JsonTx(json _jtx);
explicit JsonTx(string _path);
private:
void init();
bool read_config();
};
bool
check_and_adjust_path(string& in_path);
boost::optional<JsonTx>
construct_jsontx(string tx_hash);
}
#endif // JSONTX_H

@ -33,6 +33,35 @@
address_str, viewkey_str, \
net_type, address, viewkey));
#define SPENDKEY_FROM_STRING(spendkey_str) \
crypto::secret_key spendkey; \
ASSERT_TRUE(xmreg::parse_str_secret_key(spendkey_str, spendkey));
#define TX_AND_ADDR_VIEWKEY_FROM_STRING(hex_tx, address_str, viewkey_str, net_type) \
TX_FROM_HEX(hex_tx); \
ADDR_VIEWKEY_FROM_STRING(address_str, viewkey_str, net_type);
#define GET_RING_MEMBER_OUTPUTS(ring_member_data_hex) \
MockGettingOutputs::ring_members_mock_map_t mock_ring_members_data; \
ASSERT_TRUE(xmreg::output_data_from_hex(ring_member_data_hex, \
mock_ring_members_data)); \
MockGettingOutputs get_outputs(mock_ring_members_data);
#define GET_KNOWN_OUTPUTS(known_outputs_csv) \
Input::known_outputs_t known_outputs; \
ASSERT_TRUE(check_and_adjust_path(known_outputs_csv)); \
ASSERT_TRUE(xmreg::populate_known_outputs_from_csv( \
known_outputs_csv, known_outputs));
#define MOCK_MCORE_GET_OUTPUT_KEY() \
auto mcore = std::make_unique<MockMicroCore>(); \
EXPECT_CALL(*mcore, get_output_key(_,_,_)) \
.WillRepeatedly(Invoke(&get_outputs, \
&MockGettingOutputs \
::get_output_key));

@ -8,7 +8,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "JsonTx.h"
namespace
{
@ -209,7 +209,7 @@ struct MockGettingOutputs
// based on absolute_offsets
virtual bool
get_output_keys(
const uint64_t& amount,
uint64_t amount,
vector<uint64_t> absolute_offsets,
vector<cryptonote::output_data_t>& outputs)
{
@ -231,7 +231,7 @@ struct MockGettingOutputs
*/
virtual void
get_output_key(
const uint64_t& amount,
uint64_t amount,
vector<uint64_t> absolute_offsets,
vector<cryptonote::output_data_t>& outputs)
{
@ -240,13 +240,4 @@ struct MockGettingOutputs
};
bool
check_and_adjust_path(string& in_path)
{
if (!boost::filesystem::exists(in_path))
in_path = "./tests/" + in_path;
return boost::filesystem::exists(in_path);
}
}

@ -159,7 +159,7 @@ TEST_F(OUTPUTIDENT_TEST, OutgingPreRingctTransaction)
xmreg::TxSearch::known_outputs_t known_outputs;
ASSERT_TRUE(check_and_adjust_path(known_outputs_csv_9wq792k));
ASSERT_TRUE(xmreg::check_and_adjust_path(known_outputs_csv_9wq792k));
ASSERT_TRUE(xmreg::populate_known_outputs_from_csv(
known_outputs_csv_9wq792k, known_outputs));
@ -230,7 +230,7 @@ TEST_F(OUTPUTIDENT_TEST, SweepUnmixableTransaction)
xmreg::TxSearch::known_outputs_t known_outputs;
ASSERT_TRUE(check_and_adjust_path(known_outputs_csv_56bCoE));
ASSERT_TRUE(xmreg::check_and_adjust_path(known_outputs_csv_56bCoE));
ASSERT_TRUE(xmreg::populate_known_outputs_from_csv(
known_outputs_csv_56bCoE, known_outputs));
@ -304,7 +304,7 @@ TEST_F(OUTPUTIDENT_TEST, OutgingMixRingctTransaction)
xmreg::TxSearch::known_outputs_t known_outputs;
ASSERT_TRUE(check_and_adjust_path(known_outputs_csv_2_56bCoE));
ASSERT_TRUE(xmreg::check_and_adjust_path(known_outputs_csv_2_56bCoE));
ASSERT_TRUE(xmreg::populate_known_outputs_from_csv(
known_outputs_csv_2_56bCoE, known_outputs));
@ -388,7 +388,7 @@ TEST_F(OUTPUTIDENT_TEST, OutgingRingctTransaction)
xmreg::TxSearch::known_outputs_t known_outputs;
ASSERT_TRUE(check_and_adjust_path(known_outputs_csv_2_56bCoE));
ASSERT_TRUE(xmreg::check_and_adjust_path(known_outputs_csv_2_56bCoE));
ASSERT_TRUE(xmreg::populate_known_outputs_from_csv(
known_outputs_csv_2_56bCoE, known_outputs));

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