Branch data Line data Source code
# 1 : : // Copyright (c) 2012-2020 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #include <test/util/setup_common.h>
# 6 : : #include <util/strencodings.h>
# 7 : : #include <util/system.h>
# 8 : :
# 9 : : #include <string>
# 10 : : #include <utility>
# 11 : : #include <vector>
# 12 : :
# 13 : : #include <boost/algorithm/string.hpp>
# 14 : : #include <boost/test/unit_test.hpp>
# 15 : :
# 16 : : namespace getarg_tests{
# 17 : : class LocalTestingSetup : BasicTestingSetup {
# 18 : : protected:
# 19 : : void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args);
# 20 : : void ResetArgs(const std::string& strArg);
# 21 : : ArgsManager m_local_args;
# 22 : : };
# 23 : : }
# 24 : :
# 25 : : BOOST_FIXTURE_TEST_SUITE(getarg_tests, LocalTestingSetup)
# 26 : :
# 27 : : void LocalTestingSetup :: ResetArgs(const std::string& strArg)
# 28 : 54 : {
# 29 : 54 : std::vector<std::string> vecArg;
# 30 [ + + ]: 54 : if (strArg.size())
# 31 : 50 : boost::split(vecArg, strArg, IsSpace, boost::token_compress_on);
# 32 : :
# 33 : : // Insert dummy executable name:
# 34 : 54 : vecArg.insert(vecArg.begin(), "testbitcoin");
# 35 : :
# 36 : : // Convert to char*:
# 37 : 54 : std::vector<const char*> vecChar;
# 38 [ + + ]: 54 : for (const std::string& s : vecArg)
# 39 : 130 : vecChar.push_back(s.c_str());
# 40 : :
# 41 : 54 : std::string error;
# 42 : 54 : BOOST_CHECK(m_local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
# 43 : 54 : }
# 44 : :
# 45 : : void LocalTestingSetup :: SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
# 46 : 12 : {
# 47 : 12 : m_local_args.ClearArgs();
# 48 [ + + ]: 26 : for (const auto& arg : args) {
# 49 : 26 : m_local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
# 50 : 26 : }
# 51 : 12 : }
# 52 : :
# 53 : : BOOST_AUTO_TEST_CASE(boolarg)
# 54 : 2 : {
# 55 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 56 : 2 : SetupArgs({foo});
# 57 : 2 : ResetArgs("-foo");
# 58 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
# 59 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
# 60 : :
# 61 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-fo", false));
# 62 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-fo", true));
# 63 : :
# 64 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-fooo", false));
# 65 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-fooo", true));
# 66 : :
# 67 : 2 : ResetArgs("-foo=0");
# 68 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 69 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 70 : :
# 71 : 2 : ResetArgs("-foo=1");
# 72 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
# 73 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
# 74 : :
# 75 : : // New 0.6 feature: auto-map -nosomething to !-something:
# 76 : 2 : ResetArgs("-nofoo");
# 77 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 78 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 79 : :
# 80 : 2 : ResetArgs("-nofoo=1");
# 81 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 82 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 83 : :
# 84 : 2 : ResetArgs("-foo -nofoo"); // -nofoo should win
# 85 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 86 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 87 : :
# 88 : 2 : ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
# 89 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 90 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 91 : :
# 92 : 2 : ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
# 93 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
# 94 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
# 95 : :
# 96 : : // New 0.6 feature: treat -- same as -:
# 97 : 2 : ResetArgs("--foo=1");
# 98 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
# 99 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
# 100 : :
# 101 : 2 : ResetArgs("--nofoo=1");
# 102 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 103 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 104 : :
# 105 : 2 : }
# 106 : :
# 107 : : BOOST_AUTO_TEST_CASE(stringarg)
# 108 : 2 : {
# 109 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 110 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 111 : 2 : SetupArgs({foo, bar});
# 112 : 2 : ResetArgs("");
# 113 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
# 114 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
# 115 : :
# 116 : 2 : ResetArgs("-foo -bar");
# 117 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
# 118 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
# 119 : :
# 120 : 2 : ResetArgs("-foo=");
# 121 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
# 122 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
# 123 : :
# 124 : 2 : ResetArgs("-foo=11");
# 125 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "11");
# 126 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "11");
# 127 : :
# 128 : 2 : ResetArgs("-foo=eleven");
# 129 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "eleven");
# 130 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
# 131 : :
# 132 : 2 : }
# 133 : :
# 134 : : BOOST_AUTO_TEST_CASE(intarg)
# 135 : 2 : {
# 136 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 137 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 138 : 2 : SetupArgs({foo, bar});
# 139 : 2 : ResetArgs("");
# 140 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", 11), 11);
# 141 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", 0), 0);
# 142 : :
# 143 : 2 : ResetArgs("-foo -bar");
# 144 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", 11), 0);
# 145 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-bar", 11), 0);
# 146 : :
# 147 : 2 : ResetArgs("-foo=11 -bar=12");
# 148 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", 0), 11);
# 149 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-bar", 11), 12);
# 150 : :
# 151 : 2 : ResetArgs("-foo=NaN -bar=NotANumber");
# 152 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", 1), 0);
# 153 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-bar", 11), 0);
# 154 : 2 : }
# 155 : :
# 156 : : BOOST_AUTO_TEST_CASE(doubledash)
# 157 : 2 : {
# 158 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 159 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 160 : 2 : SetupArgs({foo, bar});
# 161 : 2 : ResetArgs("--foo");
# 162 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetBoolArg("-foo", false), true);
# 163 : :
# 164 : 2 : ResetArgs("--foo=verbose --bar=1");
# 165 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "verbose");
# 166 : 2 : BOOST_CHECK_EQUAL(m_local_args.GetArg("-bar", 0), 1);
# 167 : 2 : }
# 168 : :
# 169 : : BOOST_AUTO_TEST_CASE(boolargno)
# 170 : 2 : {
# 171 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 172 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 173 : 2 : SetupArgs({foo, bar});
# 174 : 2 : ResetArgs("-nofoo");
# 175 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 176 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 177 : :
# 178 : 2 : ResetArgs("-nofoo=1");
# 179 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 180 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 181 : :
# 182 : 2 : ResetArgs("-nofoo=0");
# 183 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
# 184 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
# 185 : :
# 186 : 2 : ResetArgs("-foo --nofoo"); // --nofoo should win
# 187 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
# 188 : 2 : BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
# 189 : :
# 190 : 2 : ResetArgs("-nofoo -foo"); // foo always wins:
# 191 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
# 192 : 2 : BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
# 193 : 2 : }
# 194 : :
# 195 : : BOOST_AUTO_TEST_CASE(logargs)
# 196 : 2 : {
# 197 : 2 : const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_BOOL);
# 198 : 2 : const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_BOOL);
# 199 : 2 : const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
# 200 : 2 : const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
# 201 : 2 : SetupArgs({okaylog_bool, okaylog_negbool, okaylog, dontlog});
# 202 : 2 : ResetArgs("-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
# 203 : :
# 204 : : // Everything logged to debug.log will also append to str
# 205 : 2 : std::string str;
# 206 : 2 : auto print_connection = LogInstance().PushBackCallback(
# 207 : 8 : [&str](const std::string& s) {
# 208 : 8 : str += s;
# 209 : 8 : });
# 210 : :
# 211 : : // Log the arguments
# 212 : 2 : m_local_args.LogArgs();
# 213 : :
# 214 : 2 : LogInstance().DeleteCallback(print_connection);
# 215 : : // Check that what should appear does, and what shouldn't doesn't.
# 216 : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
# 217 : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
# 218 : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
# 219 : 2 : BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
# 220 : 2 : BOOST_CHECK(str.find("private") == std::string::npos);
# 221 : 2 : }
# 222 : :
# 223 : : BOOST_AUTO_TEST_SUITE_END()
|