Validate port value in SplitHostPort

Forward the validation of the port from `ParseUInt16(...)`.
Consider port 0 as invalid.
Add suitable test for the `SplitHostPort` function.
Add doxygen description to the `SplitHostPort` function.
This commit is contained in:
amadeuszpawlik
2021-05-27 13:53:42 +02:00
committed by Amadeusz Pawlik
parent 5e82b9ba96
commit f8387c4234
3 changed files with 37 additions and 5 deletions

View File

@@ -84,12 +84,12 @@ BOOST_AUTO_TEST_CASE(netbase_properties)
}
bool static TestSplitHost(const std::string& test, const std::string& host, uint16_t port)
bool static TestSplitHost(const std::string& test, const std::string& host, uint16_t port, bool validPort=true)
{
std::string hostOut;
uint16_t portOut{0};
SplitHostPort(test, portOut, hostOut);
return hostOut == host && port == portOut;
bool validPortOut = SplitHostPort(test, portOut, hostOut);
return hostOut == host && portOut == port && validPortOut == validPort;
}
BOOST_AUTO_TEST_CASE(netbase_splithost)
@@ -109,6 +109,23 @@ BOOST_AUTO_TEST_CASE(netbase_splithost)
BOOST_CHECK(TestSplitHost(":8333", "", 8333));
BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
BOOST_CHECK(TestSplitHost("", "", 0));
BOOST_CHECK(TestSplitHost(":65535", "", 65535));
BOOST_CHECK(TestSplitHost(":65536", ":65536", 0, false));
BOOST_CHECK(TestSplitHost(":-1", ":-1", 0, false));
BOOST_CHECK(TestSplitHost("[]:70001", "[]:70001", 0, false));
BOOST_CHECK(TestSplitHost("[]:-1", "[]:-1", 0, false));
BOOST_CHECK(TestSplitHost("[]:-0", "[]:-0", 0, false));
BOOST_CHECK(TestSplitHost("[]:0", "", 0, false));
BOOST_CHECK(TestSplitHost("[]:1/2", "[]:1/2", 0, false));
BOOST_CHECK(TestSplitHost("[]:1E2", "[]:1E2", 0, false));
BOOST_CHECK(TestSplitHost("127.0.0.1:65536", "127.0.0.1:65536", 0, false));
BOOST_CHECK(TestSplitHost("127.0.0.1:0", "127.0.0.1", 0, false));
BOOST_CHECK(TestSplitHost("127.0.0.1:", "127.0.0.1:", 0, false));
BOOST_CHECK(TestSplitHost("127.0.0.1:1/2", "127.0.0.1:1/2", 0, false));
BOOST_CHECK(TestSplitHost("127.0.0.1:1E2", "127.0.0.1:1E2", 0, false));
BOOST_CHECK(TestSplitHost("www.bitcoincore.org:65536", "www.bitcoincore.org:65536", 0, false));
BOOST_CHECK(TestSplitHost("www.bitcoincore.org:0", "www.bitcoincore.org", 0, false));
BOOST_CHECK(TestSplitHost("www.bitcoincore.org:", "www.bitcoincore.org:", 0, false));
}
bool static TestParse(std::string src, std::string canon)