Skip to content

Commit

Permalink
GH-1101 Additional test cases and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Jan 24, 2025
1 parent 6c2442b commit 641eba1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 8 additions & 3 deletions plugins/net_plugin/include/eosio/net_plugin/net_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ namespace detail {
inline std::tuple<std::string, std::string, std::string> split_host_port_remainder(const std::string& peer_add, bool should_throw) {
using std::string;
// host:port[:trx|:blk][:<rate>]
if (peer_add.empty()) return {};
if (peer_add.empty()) {
EOS_ASSERT(!should_throw, chain::plugin_config_exception, "Address specification is empty" );
return {};
}

auto colon_count = std::count(peer_add.begin(), peer_add.end(), ':');
string::size_type end_bracket = 0;
Expand All @@ -66,7 +69,9 @@ namespace detail {
return {};
}
string::size_type colon = peer_add.find(':', end_bracket+1);
if (colon == string::npos || colon == 0) {
if (colon == string::npos) {
EOS_ASSERT(!should_throw, chain::plugin_config_exception,
"Invalid address specification ${a}; missing port specification.", ("a", peer_add));
return {};
}
if (end_bracket != 0 && end_bracket+1 != colon) {
Expand All @@ -92,7 +97,7 @@ namespace detail {

constexpr bool should_throw = false;
auto [host, port, remainder] = detail::split_host_port_remainder(peer_add, should_throw);
if (host.empty()) return {};
if (host.empty() || port.empty()) return {};

std::string type;
if (remainder.starts_with("blk") || remainder.starts_with("trx")) {
Expand Down
18 changes: 18 additions & 0 deletions plugins/net_plugin/tests/rate_limit_parse_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ BOOST_AUTO_TEST_CASE(test_parse_rate_limit) {
, "0.0.0.0:9877:trx:640KB/s - additional info"
, "[2001:db8:85a3:8d3:1319:8a2e:370:7348]additional info:trx:640KB/s"
, "0.0.0.0"
, "0.0.0.0:"
, "0.0.0.0::"
};
size_t which = 0;
auto [listen_addr, block_sync_rate_limit] = eosio::net_utils::parse_listen_address(p2p_addresses.at(which++));
Expand Down Expand Up @@ -109,6 +111,12 @@ BOOST_AUTO_TEST_CASE(test_parse_rate_limit) {
BOOST_CHECK_EXCEPTION(eosio::net_utils::parse_listen_address(p2p_addresses.at(which++)), eosio::chain::plugin_config_exception,
[](const eosio::chain::plugin_config_exception& e)
{return std::strstr(e.top_message().c_str(), "unexpected number of colons");});
BOOST_CHECK_EXCEPTION(eosio::net_utils::parse_listen_address(p2p_addresses.at(which++)), eosio::chain::plugin_config_exception,
[](const eosio::chain::plugin_config_exception& e)
{return std::strstr(e.top_message().c_str(), "host or port missing");});
BOOST_CHECK_EXCEPTION(eosio::net_utils::parse_listen_address(p2p_addresses.at(which++)), eosio::chain::plugin_config_exception,
[](const eosio::chain::plugin_config_exception& e)
{return std::strstr(e.top_message().c_str(), "host or port missing");});
}

BOOST_AUTO_TEST_CASE(test_split_host_port_type) {
Expand Down Expand Up @@ -139,6 +147,8 @@ BOOST_AUTO_TEST_CASE(test_split_host_port_type) {
, "0.0.0.0:9877:trx:640KB/s - additional info"
, "[2001:db8:85a3:8d3:1319:8a2e:370:7348]additional info:trx:640KB/s"
, "0.0.0.0"
, "0.0.0.0:"
, "0.0.0.0::"
};
size_t which = 0;
auto [host, port, type] = eosio::net_utils::split_host_port_type(p2p_addresses.at(which++));
Expand Down Expand Up @@ -245,4 +255,12 @@ BOOST_AUTO_TEST_CASE(test_split_host_port_type) {
BOOST_CHECK_EQUAL(host, "");
BOOST_CHECK_EQUAL(port, "");
BOOST_CHECK_EQUAL(type, "");
std::tie(host, port, type) = eosio::net_utils::split_host_port_type(p2p_addresses.at(which++));
BOOST_CHECK_EQUAL(host, "");
BOOST_CHECK_EQUAL(port, "");
BOOST_CHECK_EQUAL(type, "");
std::tie(host, port, type) = eosio::net_utils::split_host_port_type(p2p_addresses.at(which++));
BOOST_CHECK_EQUAL(host, "");
BOOST_CHECK_EQUAL(port, "");
BOOST_CHECK_EQUAL(type, "");
}

0 comments on commit 641eba1

Please sign in to comment.