evmc/test/vmtester/vmtester.cpp

93 lines
2.5 KiB
C++
Raw Normal View History

2018-04-18 14:22:24 +00:00
// EVMC: Ethereum Client-VM Connector API.
2018-04-12 09:01:24 +00:00
// Copyright 2018 Pawel Bylica.
// Licensed under the MIT License. See the LICENSE file.
2018-04-10 22:37:03 +00:00
#include "vmtester.hpp"
2018-04-10 15:11:43 +00:00
#include <boost/dll.hpp>
2018-04-11 15:37:03 +00:00
#include <boost/dll/alias.hpp>
2018-04-10 22:37:03 +00:00
#include <boost/function.hpp>
2018-04-11 15:37:03 +00:00
#include <boost/program_options.hpp>
2018-04-10 15:11:43 +00:00
#include <iostream>
2018-04-10 22:37:03 +00:00
#include <memory>
2018-04-10 15:11:43 +00:00
2018-04-10 22:37:03 +00:00
namespace fs = boost::filesystem;
2018-04-10 15:11:43 +00:00
namespace dll = boost::dll;
2018-04-11 15:37:03 +00:00
namespace opts = boost::program_options;
2018-04-10 15:11:43 +00:00
2018-04-11 15:37:03 +00:00
extern "C" using evmc_create_fn = evmc_instance * ();
2018-04-10 22:37:03 +00:00
2018-04-10 15:11:43 +00:00
namespace
{
2018-04-10 22:37:03 +00:00
boost::function<evmc_create_fn> create_fn;
std::unique_ptr<evmc_instance, evmc_destroy_fn> create_vm()
{
auto vm = create_fn();
return {vm, vm->destroy};
}
2018-04-11 15:37:03 +00:00
} // namespace
2018-04-10 15:11:43 +00:00
2018-04-10 22:37:03 +00:00
evmc_instance* get_vm_instance()
2018-04-10 15:11:43 +00:00
{
2018-04-10 22:37:03 +00:00
static auto vm = create_vm();
return vm.get();
2018-04-10 15:11:43 +00:00
}
2018-04-10 22:37:03 +00:00
int main(int argc, char* argv[])
2018-04-12 09:01:24 +00:00
{
2018-04-11 15:37:03 +00:00
try
{
fs::path vm_path;
opts::options_description desc("EVMC VM Tester Options");
auto add_option = desc.add_options();
add_option("help", "Show help message");
add_option("vm", opts::value(&vm_path)->value_name("path")->required(),
2018-05-11 11:24:33 +00:00
"Path to the VM shared library to be tested");
2018-04-11 15:37:03 +00:00
opts::positional_options_description positional;
positional.add("vm", 1);
testing::InitGoogleTest(&argc, argv);
opts::variables_map variables_map;
opts::store(
opts::command_line_parser(argc, argv).options(desc).positional(positional).run(),
variables_map);
if (variables_map.count("help"))
{
std::cout << "\n" << desc << "\n";
return 0;
}
opts::notify(variables_map);
auto symbols = dll::library_info{vm_path}.symbols();
2018-05-11 11:24:33 +00:00
auto it = std::find_if(symbols.begin(), symbols.end(), [](const std::string& symbol) {
return symbol.find("evmc_create_") == 0;
});
2018-04-11 15:37:03 +00:00
if (it == symbols.end())
{
std::cerr << "EVMC create function not found in " << vm_path.string() << "\n";
2018-04-11 15:37:03 +00:00
return 2;
}
const std::string& create_fn_name = *it;
std::cout << "Testing " << vm_path.filename().string() << "\n "
<< "Path: " << vm_path.string() << "\n "
<< "Create function: " << create_fn_name << "()\n\n";
create_fn = dll::import<evmc_create_fn>(vm_path, create_fn_name);
return RUN_ALL_TESTS();
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << "\n";
2018-04-10 22:37:03 +00:00
return 1;
2018-04-11 15:37:03 +00:00
}
2018-04-12 09:01:24 +00:00
}