townforge/tests/db_tests/main.cpp
2021-04-27 18:15:29 +00:00

156 lines
4.9 KiB
C++

// Copyright (c) 2020, Crypto City
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <unistd.h>
#include <atomic>
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <boost/filesystem.hpp>
#include "include_base_utils.h"
#include "misc_log_ex.h"
#include "string_tools.h"
#include "version.h"
#include "common/util.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "blockchain_db/blockchain_db.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "db_tests"
#define RUN(test) \
extern bool db_tests_##test(cryptonote::BlockchainDB&); \
run_test(#test, db_tests_##test, *db.get())
static void run_test(const char *name, bool (*f)(cryptonote::BlockchainDB&), cryptonote::BlockchainDB &db)
{
try
{
std::cout << "Testing: " << name << std::endl;
if (!f(db))
{
std::cerr << "Error in " << name << std::endl;
exit(1);
}
}
catch (const std::exception &e)
{
std::cerr << "Error in " << name << ": " << e.what() << std::endl;
exit(1);
}
}
namespace po = boost::program_options;
int main(int argc, char* argv[])
{
TRY_ENTRY();
epee::string_tools::set_module_name_and_folder(argv[0]);
tools::on_startup();
po::options_description desc_cmd_only("Command line options");
po::options_description desc_cmd_sett("Command line options and settings options");
const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
command_line::add_arg(desc_cmd_sett, arg_log_level);
command_line::add_arg(desc_cmd_only, command_line::arg_help);
po::options_description desc_options("Allowed options");
desc_options.add(desc_cmd_only).add(desc_cmd_sett);
po::variables_map vm;
bool r = command_line::handle_error_helper(desc_options, [&]()
{
po::store(po::parse_command_line(argc, argv, desc_options), vm);
po::notify(vm);
return true;
});
if (! r)
return 1;
if (command_line::get_arg(vm, command_line::arg_help))
{
std::cout << "Townforge '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
std::cout << desc_options << std::endl;
return 1;
}
mlog_configure(mlog_get_default_log_path("db_tests.log"), true);
mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str());
boost::filesystem::path folder(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path());
CHECK_AND_ASSERT_MES (boost::filesystem::exists(folder) || boost::filesystem::create_directories(folder), false,
std::string("Failed to create directory ").append(folder.string()).c_str());
std::unique_ptr<cryptonote::BlockchainDB> db(cryptonote::new_db());
if (db == NULL)
{
LOG_ERROR("Failed to initialize a database");
return false;
}
folder /= db->get_db_name();
if (!db->remove_data_file(folder.string()))
{
LOG_ERROR("Failed to reset database");
return false;
}
// default to fast:async:1
const uint64_t DEFAULT_FLAGS = DBF_FAST;
db->open(folder.string(), DEFAULT_FLAGS);
if(!db->m_open)
{
LOG_ERROR("Failed to open database");
return false;
}
db->batch_start();
RUN(basic);
RUN(cc_blobs);
RUN(cc_foreclosures);
RUN(cc_random_seed);
RUN(cc_runestones);
RUN(cc_script_variables);
RUN(cc_scripts);
RUN(cc_script_effects);
RUN(cc_item_counts);
RUN(cc_invitations);
RUN(cc_attributes);
RUN(cc_auctions);
db->batch_stop();
return 0;
CATCH_ENTRY("main", 1);
}