From 712d6981f6a5de8033a26ca3a4f6344b2cbad190 Mon Sep 17 00:00:00 2001 From: Wesley Shillingford Date: Wed, 15 Jan 2020 21:36:12 +0000 Subject: [PATCH] The start of CLI tests (#2403) * The start of CLI tests * Add needed header file after merge * Serg review comments --- nano/core_test/CMakeLists.txt | 1 + nano/core_test/cli.cpp | 58 +++++++++++++++++++++++++++++++++++ nano/node/cli.cpp | 4 +-- nano/node/cli.hpp | 2 +- 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 nano/core_test/cli.cpp diff --git a/nano/core_test/CMakeLists.txt b/nano/core_test/CMakeLists.txt index 77ea58ee..69e8b945 100644 --- a/nano/core_test/CMakeLists.txt +++ b/nano/core_test/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable (core_test block.cpp block_store.cpp bootstrap.cpp + cli.cpp confirmation_height.cpp confirmation_solicitor.cpp conflicts.cpp diff --git a/nano/core_test/cli.cpp b/nano/core_test/cli.cpp new file mode 100644 index 00000000..eda13af6 --- /dev/null +++ b/nano/core_test/cli.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +#include + +#include + +#include + +using namespace std::chrono_literals; + +namespace +{ +std::string call_cli_command (boost::program_options::variables_map const & vm); +} + +TEST (cli, key_create) +{ + boost::program_options::variables_map vm; + vm.emplace ("key_create", boost::program_options::variable_value ()); + auto output = call_cli_command (vm); + + // Extract the private, public and account values. The regular expression extracts anything between the semi-colon and new line. + std::regex regexpr (": (\\w+)"); + std::smatch matches; + std::vector vals; + std::string::const_iterator search_start (output.cbegin ()); + while (std::regex_search (search_start, output.cend (), matches, regexpr)) + { + ASSERT_NE (matches[1].str (), ""); + vals.push_back (matches[1].str ()); + search_start = matches.suffix ().first; + } + + // Get the contents of the private key and check that the public key and account are successfully derived from the private key + auto private_key_str = vals.front (); + nano::private_key private_key; + private_key.decode_hex (private_key_str); + + auto public_key = nano::pub_key (private_key); + ASSERT_EQ (vals[1], public_key.to_string ()); + ASSERT_EQ (vals[2], public_key.to_account ()); +} + +namespace +{ +std::string call_cli_command (boost::program_options::variables_map const & vm) +{ + std::stringstream ss; + nano::cout_redirect redirect (ss.rdbuf ()); + + // Execute CLI command. This populates the stringstream with a string like: "Private: 123\n Public: 456\n Account: nano_123" + auto ec = nano::handle_node_options (vm); + release_assert (!static_cast (ec)); + return ss.str (); +} +} \ No newline at end of file diff --git a/nano/node/cli.cpp b/nano/node/cli.cpp index 1035c32d..a3c0c372 100644 --- a/nano/node/cli.cpp +++ b/nano/node/cli.cpp @@ -153,7 +153,7 @@ void database_write_lock_error (std::error_code & ec) ec = nano::error_cli::database_write_error; } -bool copy_database (boost::filesystem::path const & data_path, boost::program_options::variables_map & vm, boost::filesystem::path const & output_path, std::error_code & ec) +bool copy_database (boost::filesystem::path const & data_path, boost::program_options::variables_map const & vm, boost::filesystem::path const & output_path, std::error_code & ec) { bool success = false; bool needs_to_write = vm.count ("unchecked_clear") || vm.count ("clear_send_ids") || vm.count ("online_weight_clear") || vm.count ("peer_clear") || vm.count ("confirmation_height_clear") || vm.count ("rebuild_database"); @@ -203,7 +203,7 @@ bool copy_database (boost::filesystem::path const & data_path, boost::program_op } } -std::error_code nano::handle_node_options (boost::program_options::variables_map & vm) +std::error_code nano::handle_node_options (boost::program_options::variables_map const & vm) { std::error_code ec; boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as ()) : nano::working_path (); diff --git a/nano/node/cli.hpp b/nano/node/cli.hpp index 484ab712..7ba4be5d 100644 --- a/nano/node/cli.hpp +++ b/nano/node/cli.hpp @@ -22,7 +22,7 @@ enum class error_cli void add_node_options (boost::program_options::options_description &); void add_node_flag_options (boost::program_options::options_description &); std::error_code update_flags (nano::node_flags &, boost::program_options::variables_map const &); -std::error_code handle_node_options (boost::program_options::variables_map &); +std::error_code handle_node_options (boost::program_options::variables_map const &); } REGISTER_ERROR_CODES (nano, error_cli)