Skip to content

Commit

Permalink
Improve message_header to string output and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dsiganos committed Jan 25, 2022
1 parent f67d48c commit 73eae62
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
27 changes: 12 additions & 15 deletions nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,18 @@ TEST (message, confirm_req_hash_batch_serialization)
ASSERT_EQ (header.count_get (), req.roots_hashes.size ());
}

TEST (message, keepalive_to_string)
// this unit test checks that conversion of message_header to string works as expected
TEST (message, message_header_to_string)
{
// header identical to keepalive_serialization at the top of this file
nano::keepalive request1{ nano::dev::network_params.network };
std::vector<uint8_t> bytes;
{
nano::vectorstream stream (bytes);
request1.serialize (stream);
}
auto error (false);
nano::bufferstream stream (bytes.data (), bytes.size ());
nano::message_header header (error, stream);

std::string header_string = header.to_string ();
// calculate expected string
int maxver = nano::dev::network_params.network.protocol_version;
int minver = nano::dev::network_params.network.protocol_version_min;
std::stringstream ss;
ss << "NetID: 5241(dev), VerMaxUsingMin: " << maxver << "/" << maxver << "/" << minver << ", MsgType: 2(keepalive), Extensions: 0000";
auto expected_str = ss.str ();

ASSERT_TRUE (boost::algorithm::contains (header_string, "MsgType: 2(keepalive)"));
ASSERT_FALSE (boost::algorithm::contains (header_string, ": ,"));
// check expected vs real
nano::keepalive keepalive_msg{ nano::dev::network_params.network };
std::string header_string = keepalive_msg.header.to_string ();
ASSERT_EQ (expected_str, header_string);
}
8 changes: 8 additions & 0 deletions nano/lib/numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,14 @@ std::string nano::to_string_hex (uint64_t const value_a)
return stream.str ();
}

std::string nano::to_string_hex (uint16_t const value_a)
{
std::stringstream stream;
stream << std::hex << std::noshowbase << std::setw (4) << std::setfill ('0');
stream << value_a;
return stream.str ();
}

bool nano::from_string_hex (std::string const & value_a, uint64_t & target_a)
{
auto error (value_a.empty ());
Expand Down
1 change: 1 addition & 0 deletions nano/lib/numbers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ nano::public_key pub_key (nano::raw_key const &);

/* Conversion methods */
std::string to_string_hex (uint64_t const);
std::string to_string_hex (uint16_t const);
bool from_string_hex (std::string const &, uint64_t &);

/**
Expand Down
4 changes: 2 additions & 2 deletions nano/node/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ std::string nano::message_header::to_string ()

std::stringstream stream;

stream << boost::format ("NetID: %1%(%2%), ") % static_cast<int> (network) % nano::network::to_string (network);
stream << boost::format ("NetID: %1%(%2%), ") % nano::to_string_hex (static_cast<uint16_t> (network)) % nano::network::to_string (network);
stream << boost::format ("VerMaxUsingMin: %1%/%2%/%3%, ") % version_max_l % version_using_l % version_min_l;
stream << boost::format ("MsgType: %1%(%2%), ") % type_l % type_text;
stream << boost::format ("Extensions: %1%") % nano::to_string_hex (extensions.to_ulong ());
stream << boost::format ("Extensions: %1%") % nano::to_string_hex (static_cast<uint16_t> (extensions.to_ulong ()));

return stream.str ();
}
Expand Down

0 comments on commit 73eae62

Please sign in to comment.