diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -1847,6 +1847,23 @@ peerLogic.reset(new PeerLogicValidation(&connman, scheduler)); RegisterValidationInterface(peerLogic.get()); + // sanitize comments per BIP-0014, format user agent and check total size + std::vector uacomments; + for (const std::string &cmt : gArgs.GetArgs("-uacomment")) { + if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT)) + return InitError(strprintf( + _("User Agent comment (%s) contains unsafe characters."), cmt)); + uacomments.push_back(cmt); + } + const std::string strSubVersion = + FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments); + if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) { + return InitError(strprintf( + _("Total length of network version string (%i) exceeds maximum " + "length (%i). Reduce the number or size of uacomments."), + strSubVersion.size(), MAX_SUBVERSION_LENGTH)); + } + if (gArgs.IsArgSet("-onlynet")) { std::set nets; for (const std::string &snet : gArgs.GetArgs("-onlynet")) { diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -3074,28 +3074,15 @@ std::vector uacomments; uacomments.push_back("EB" + eb); - // sanitize comments per BIP-0014, format user agent and check total size + // Comments are checked for char compliance at startup, it is safe to add + // them to the user agent string for (const std::string &cmt : gArgs.GetArgs("-uacomment")) { - if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT)) { - LogPrintf( - "User Agent comment (%s) contains unsafe characters. " - "We are going to use a sanitize version of the comment.\n", - cmt); - } uacomments.push_back(cmt); } + // Size compliance is checked at startup, it is safe to not check it again std::string subversion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments); - if (subversion.size() > MAX_SUBVERSION_LENGTH) { - LogPrintf("Total length of network version string (%i) exceeds maximum " - "length (%i). Reduce the number or size of uacomments. " - "String has been resized to the max length allowed.\n", - subversion.size(), MAX_SUBVERSION_LENGTH); - subversion.resize(MAX_SUBVERSION_LENGTH - 2); - subversion.append(")/"); - LogPrintf("Current network string has been set to: %s\n", subversion); - } return subversion; } diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -13,7 +13,6 @@ #include "streams.h" #include "test/test_bitcoin.h" -#include #include #include @@ -59,6 +58,18 @@ } }; +class NetTestConfig : public DummyConfig { +public: + bool SetMaxBlockSize(uint64_t maxBlockSize) override { + nMaxBlockSize = maxBlockSize; + return true; + } + uint64_t GetMaxBlockSize() const override { return nMaxBlockSize; } + +private: + uint64_t nMaxBlockSize; +}; + CDataStream AddrmanToStream(CAddrManSerializationMock &_addrman) { CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION); ssPeersIn << FLATDATA(Params().DiskMagic()); @@ -184,31 +195,19 @@ BOOST_CHECK_EQUAL(getSubVersionEB(0), "0.0"); } -BOOST_AUTO_TEST_CASE(test_userAgentLength) { - GlobalConfig config; +BOOST_AUTO_TEST_CASE(test_userAgent) { + NetTestConfig config; config.SetMaxBlockSize(8000000); - std::string long_uacomment = "very very very very very very very very very " - "very very very very very very very very very " - "very very very very very very very very very " - "very very very very very very very very very " - "very very very very very very very very very " - "very very very very very very very very very " - "very very very very very very very very very " - "very very very very very very long comment"; - gArgs.ForceSetMultiArg("-uacomment", long_uacomment); - - std::ostringstream versionMessage; - versionMessage << "/Bitcoin ABC:" << CLIENT_VERSION_MAJOR << "." - << CLIENT_VERSION_MINOR << "." << CLIENT_VERSION_REVISION - << "(EB8.0; very very very very very " - "very very very very very very very very very very very " - "very very very very very very very very very very very " - "very very very very very very very very very very very " - "very very very very very very very ve)/"; - - BOOST_CHECK_EQUAL(userAgent(config).size(), MAX_SUBVERSION_LENGTH); - BOOST_CHECK_EQUAL(userAgent(config), versionMessage.str()); + const std::string uacomment = "A very nice comment"; + gArgs.ForceSetMultiArg("-uacomment", uacomment); + + const std::string versionMessage = + "/Bitcoin ABC:" + std::to_string(CLIENT_VERSION_MAJOR) + "." + + std::to_string(CLIENT_VERSION_MINOR) + "." + + std::to_string(CLIENT_VERSION_REVISION) + "(EB8.0; " + uacomment + ")/"; + + BOOST_CHECK_EQUAL(userAgent(config), versionMessage); } BOOST_AUTO_TEST_SUITE_END()