Adding to_string method on keepalive (#3976)

* adding to_string method on keepalive

* Improve the test case

Co-authored-by: Dimitrios Siganos <dimitris@siganos.org>
This commit is contained in:
Ian Cunningham 2022-10-21 12:00:21 -05:00 committed by GitHub
commit 1f5d2e5df3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 2 deletions

View file

@ -276,3 +276,31 @@ TEST (message, bulk_pull_serialization)
ASSERT_FALSE (error);
ASSERT_TRUE (header.bulk_pull_ascending ());
}
TEST (message, keepalive_to_string)
{
nano::message_header hdr{ nano::dev::network_params.network, nano::message_type::keepalive };
std::string expected = hdr.to_string ();
nano::keepalive keepalive = nano::keepalive (nano::dev::network_params.network);
ASSERT_EQ (keepalive.to_string (), expected + "\n:::0\n:::0\n:::0\n:::0\n:::0\n:::0\n:::0\n:::0");
expected.append ("\n:::0");
keepalive.peers[1] = nano::endpoint{ boost::asio::ip::make_address_v6 ("::1"), 45 };
expected.append ("\n::1:45");
keepalive.peers[2] = nano::endpoint{ boost::asio::ip::make_address_v6 ("2001:db8:85a3:8d3:1319:8a2e:370:7348"), 0 };
expected.append ("\n2001:db8:85a3:8d3:1319:8a2e:370:7348:0");
keepalive.peers[3] = nano::endpoint{ boost::asio::ip::make_address_v6 ("::"), 65535 };
expected.append ("\n:::65535");
for (int i = 4; i < keepalive.peers.size (); i++)
{
keepalive.peers[i] = nano::endpoint{ boost::asio::ip::make_address_v6 ("::ffff:1.2.3.4"), 1234 };
expected.append ("\n::ffff:1.2.3.4:1234");
}
ASSERT_EQ (keepalive.to_string (), expected);
}

View file

@ -144,7 +144,7 @@ nano::stat::detail nano::to_stat_detail (nano::message_type message_type)
return {};
}
std::string nano::message_header::to_string ()
std::string nano::message_header::to_string () const
{
// Cast to uint16_t to get integer value since uint8_t is treated as an unsigned char in string formatting.
uint16_t type_l = static_cast<uint16_t> (type);
@ -712,6 +712,21 @@ bool nano::keepalive::operator== (nano::keepalive const & other_a) const
return peers == other_a.peers;
}
std::string nano::keepalive::to_string () const
{
std::stringstream stream;
stream << header.to_string ();
for (auto peer = peers.begin (); peer != peers.end (); ++peer)
{
stream << "\n"
<< peer->address ().to_string () + ":" + std::to_string (peer->port ());
}
return stream.str ();
}
/*
* publish
*/

View file

@ -64,7 +64,7 @@ public:
uint8_t version_max;
uint8_t version_using;
uint8_t version_min;
std::string to_string ();
std::string to_string () const;
public:
nano::message_type type;
@ -160,6 +160,7 @@ public:
bool operator== (nano::keepalive const &) const;
std::array<nano::endpoint, 8> peers;
static std::size_t constexpr size = 8 * (16 + 2);
std::string to_string () const;
};
class publish final : public message