You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmonero/tests/bcstatus_tests.cpp

152 lines
3.4 KiB

6 years ago
//
// Created by mwo on 15/06/18.
//
#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;
6 years ago
using ::testing::Return;
using ::testing::Throw;
using ::testing::_;
6 years ago
using ::testing::internal::FilePath;
6 years ago
class MockMicroCore : public xmreg::MicroCore
{
public:
MOCK_METHOD2(init, bool(const string& _blockchain_path, network_type nt));
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));
};
6 years ago
class BCSTATUS_TEST : public ::testing::Test
{
public:
static void
SetUpTestCase()
{
string config_path {"../config/config.json"};
6 years ago
config_json = xmreg::BlockchainSetup::read_config(config_path);
6 years ago
}
protected:
virtual void
SetUp()
6 years ago
{
6 years ago
bc_setup = xmreg::BlockchainSetup{net_type, do_not_relay, config_json};
6 years ago
mcore = std::make_unique<MockMicroCore>();
mcore_ptr = mcore.get();
bcs = std::make_unique<xmreg::CurrentBlockchainStatus>(bc_setup, std::move(mcore));
6 years ago
}
network_type net_type {network_type::STAGENET};
bool do_not_relay {false};
xmreg::BlockchainSetup bc_setup;
6 years ago
std::unique_ptr<MockMicroCore> mcore;
std::unique_ptr<xmreg::CurrentBlockchainStatus> bcs;
MockMicroCore* mcore_ptr;
6 years ago
static json config_json;
};
json BCSTATUS_TEST::config_json;
TEST_F(BCSTATUS_TEST, DefaultConstruction)
{
xmreg::CurrentBlockchainStatus bcs {bc_setup, nullptr};
EXPECT_TRUE(true);
}
6 years ago
6 years ago
TEST_F(BCSTATUS_TEST, InitMoneroBlockchain)
{
6 years ago
EXPECT_CALL(*mcore_ptr, init(_, _))
.WillOnce(Return(true));
EXPECT_TRUE(bcs->init_monero_blockchain());
}
TEST_F(BCSTATUS_TEST, GetBlock)
{
EXPECT_CALL(*mcore_ptr, get_block_from_height(_, _))
.WillOnce(Return(true));
uint64_t height = 1000;
block blk;
EXPECT_TRUE(bcs->get_block(height, blk));
}
ACTION(ThrowBlockDNE)
{
throw BLOCK_DNE("ddddd");
}
TEST_F(BCSTATUS_TEST, GetBlockRange)
{
vector<block> blocks_to_return {block(), block(), block()};
EXPECT_CALL(*mcore_ptr, get_blocks_range(_, _))
.WillOnce(Return(blocks_to_return));
uint64_t h1 = 1000;
uint64_t h2 = h1+2;
vector<block> blocks = bcs->get_blocks_range(h1, h2);
EXPECT_EQ(blocks, blocks_to_return);
EXPECT_CALL(*mcore_ptr, get_blocks_range(_, _))
.WillOnce(ThrowBlockDNE());
blocks = bcs->get_blocks_range(h1, h2);
EXPECT_TRUE(blocks.empty());
6 years ago
}
6 years ago
TEST_F(BCSTATUS_TEST, GetBlockTxs)
{
EXPECT_CALL(*mcore_ptr, get_transactions(_, _, _))
.WillOnce(Return(true));
const block dummy_blk;
vector<transaction> blk_txs;
vector<crypto::hash> missed_txs;
EXPECT_TRUE(bcs->get_block_txs(dummy_blk, blk_txs, missed_txs));
}
6 years ago
}