Branch data Line data Source code
# 1 : : // Copyright (c) 2012-2021 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 <univalue.h>
# 7 : : #include <util/settings.h>
# 8 : : #include <util/strencodings.h>
# 9 : : #include <util/system.h>
# 10 : :
# 11 : : #include <limits>
# 12 : : #include <string>
# 13 : : #include <utility>
# 14 : : #include <vector>
# 15 : :
# 16 : : #include <boost/algorithm/string.hpp>
# 17 : : #include <boost/test/unit_test.hpp>
# 18 : :
# 19 : : BOOST_FIXTURE_TEST_SUITE(getarg_tests, BasicTestingSetup)
# 20 : :
# 21 : : void ResetArgs(ArgsManager& local_args, const std::string& strArg)
# 22 : 108 : {
# 23 : 108 : std::vector<std::string> vecArg;
# 24 [ + + ]: 108 : if (strArg.size())
# 25 : 100 : boost::split(vecArg, strArg, IsSpace, boost::token_compress_on);
# 26 : :
# 27 : : // Insert dummy executable name:
# 28 : 108 : vecArg.insert(vecArg.begin(), "testbitcoin");
# 29 : :
# 30 : : // Convert to char*:
# 31 : 108 : std::vector<const char*> vecChar;
# 32 [ + + ]: 108 : for (const std::string& s : vecArg)
# 33 : 236 : vecChar.push_back(s.c_str());
# 34 : :
# 35 : 108 : std::string error;
# 36 : 108 : BOOST_CHECK(local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
# 37 : 108 : }
# 38 : :
# 39 : : void SetupArgs(ArgsManager& local_args, const std::vector<std::pair<std::string, unsigned int>>& args)
# 40 : 16 : {
# 41 [ + + ]: 30 : for (const auto& arg : args) {
# 42 : 30 : local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
# 43 : 30 : }
# 44 : 16 : }
# 45 : :
# 46 : : // Test behavior of GetArg functions when string, integer, and boolean types
# 47 : : // are specified in the settings.json file. GetArg functions are convenience
# 48 : : // functions. The GetSetting method can always be used instead of GetArg
# 49 : : // methods to retrieve original values, and there's not always an objective
# 50 : : // answer to what GetArg behavior is best in every case. This test makes sure
# 51 : : // there's test coverage for whatever the current behavior is, so it's not
# 52 : : // broken or changed unintentionally.
# 53 : : BOOST_AUTO_TEST_CASE(setting_args)
# 54 : 2 : {
# 55 : 2 : ArgsManager args;
# 56 : 2 : SetupArgs(args, {{"-foo", ArgsManager::ALLOW_ANY}});
# 57 : :
# 58 : 26 : auto set_foo = [&](const util::SettingsValue& value) {
# 59 : 26 : args.LockSettings([&](util::Settings& settings) {
# 60 : 26 : settings.rw_settings["foo"] = value;
# 61 : 26 : });
# 62 : 26 : };
# 63 : :
# 64 : 2 : set_foo("str");
# 65 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"str\"");
# 66 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "str");
# 67 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
# 68 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
# 69 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
# 70 : :
# 71 : 2 : set_foo("99");
# 72 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"99\"");
# 73 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
# 74 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
# 75 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
# 76 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
# 77 : :
# 78 : 2 : set_foo("3.25");
# 79 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"3.25\"");
# 80 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
# 81 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 3);
# 82 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
# 83 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
# 84 : :
# 85 : 2 : set_foo("0");
# 86 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"0\"");
# 87 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
# 88 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
# 89 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
# 90 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
# 91 : :
# 92 : 2 : set_foo("");
# 93 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"\"");
# 94 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "");
# 95 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
# 96 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
# 97 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
# 98 : :
# 99 : 2 : set_foo(99);
# 100 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "99");
# 101 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
# 102 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
# 103 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
# 104 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
# 105 : :
# 106 : 2 : set_foo(3.25);
# 107 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "3.25");
# 108 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
# 109 : 2 : BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
# 110 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
# 111 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
# 112 : :
# 113 : 2 : set_foo(0);
# 114 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "0");
# 115 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
# 116 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
# 117 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
# 118 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
# 119 : :
# 120 : 2 : set_foo(true);
# 121 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "true");
# 122 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "1");
# 123 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 1);
# 124 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
# 125 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
# 126 : :
# 127 : 2 : set_foo(false);
# 128 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "false");
# 129 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
# 130 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
# 131 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
# 132 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
# 133 : :
# 134 : 2 : set_foo(UniValue::VOBJ);
# 135 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "{}");
# 136 : 2 : BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
# 137 : 2 : BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
# 138 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
# 139 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
# 140 : :
# 141 : 2 : set_foo(UniValue::VARR);
# 142 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "[]");
# 143 : 2 : BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
# 144 : 2 : BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
# 145 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
# 146 : 2 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
# 147 : :
# 148 : 2 : set_foo(UniValue::VNULL);
# 149 : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "null");
# 150 : 2 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "default");
# 151 : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 100);
# 152 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
# 153 : 2 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
# 154 : 2 : }
# 155 : :
# 156 : : BOOST_AUTO_TEST_CASE(boolarg)
# 157 : 2 : {
# 158 : 2 : ArgsManager local_args;
# 159 : :
# 160 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 161 : 2 : SetupArgs(local_args, {foo});
# 162 : 2 : ResetArgs(local_args, "-foo");
# 163 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
# 164 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
# 165 : :
# 166 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-fo", false));
# 167 : 2 : BOOST_CHECK(local_args.GetBoolArg("-fo", true));
# 168 : :
# 169 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-fooo", false));
# 170 : 2 : BOOST_CHECK(local_args.GetBoolArg("-fooo", true));
# 171 : :
# 172 : 2 : ResetArgs(local_args, "-foo=0");
# 173 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 174 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 175 : :
# 176 : 2 : ResetArgs(local_args, "-foo=1");
# 177 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
# 178 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
# 179 : :
# 180 : : // New 0.6 feature: auto-map -nosomething to !-something:
# 181 : 2 : ResetArgs(local_args, "-nofoo");
# 182 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 183 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 184 : :
# 185 : 2 : ResetArgs(local_args, "-nofoo=1");
# 186 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 187 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 188 : :
# 189 : 2 : ResetArgs(local_args, "-foo -nofoo"); // -nofoo should win
# 190 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 191 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 192 : :
# 193 : 2 : ResetArgs(local_args, "-foo=1 -nofoo=1"); // -nofoo should win
# 194 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 195 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 196 : :
# 197 : 2 : ResetArgs(local_args, "-foo=0 -nofoo=0"); // -nofoo=0 should win
# 198 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
# 199 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
# 200 : :
# 201 : : // New 0.6 feature: treat -- same as -:
# 202 : 2 : ResetArgs(local_args, "--foo=1");
# 203 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
# 204 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
# 205 : :
# 206 : 2 : ResetArgs(local_args, "--nofoo=1");
# 207 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 208 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 209 : 2 : }
# 210 : :
# 211 : : BOOST_AUTO_TEST_CASE(stringarg)
# 212 : 2 : {
# 213 : 2 : ArgsManager local_args;
# 214 : :
# 215 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 216 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 217 : 2 : SetupArgs(local_args, {foo, bar});
# 218 : 2 : ResetArgs(local_args, "");
# 219 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
# 220 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
# 221 : :
# 222 : 2 : ResetArgs(local_args, "-foo -bar");
# 223 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
# 224 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "");
# 225 : :
# 226 : 2 : ResetArgs(local_args, "-foo=");
# 227 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
# 228 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "");
# 229 : :
# 230 : 2 : ResetArgs(local_args, "-foo=11");
# 231 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "11");
# 232 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "11");
# 233 : :
# 234 : 2 : ResetArgs(local_args, "-foo=eleven");
# 235 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "eleven");
# 236 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
# 237 : 2 : }
# 238 : :
# 239 : : BOOST_AUTO_TEST_CASE(intarg)
# 240 : 2 : {
# 241 : 2 : ArgsManager local_args;
# 242 : :
# 243 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 244 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 245 : 2 : SetupArgs(local_args, {foo, bar});
# 246 : 2 : ResetArgs(local_args, "");
# 247 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 11);
# 248 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 0);
# 249 : :
# 250 : 2 : ResetArgs(local_args, "-foo -bar");
# 251 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 0);
# 252 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 0);
# 253 : :
# 254 : : // Check under-/overflow behavior.
# 255 : 2 : ResetArgs(local_args, "-foo=-9223372036854775809 -bar=9223372036854775808");
# 256 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), std::numeric_limits<int64_t>::min());
# 257 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), std::numeric_limits<int64_t>::max());
# 258 : :
# 259 : 2 : ResetArgs(local_args, "-foo=11 -bar=12");
# 260 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 11);
# 261 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 12);
# 262 : :
# 263 : 2 : ResetArgs(local_args, "-foo=NaN -bar=NotANumber");
# 264 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 1), 0);
# 265 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 0);
# 266 : 2 : }
# 267 : :
# 268 : : BOOST_AUTO_TEST_CASE(patharg)
# 269 : 2 : {
# 270 : 2 : ArgsManager local_args;
# 271 : :
# 272 : 2 : const auto dir = std::make_pair("-dir", ArgsManager::ALLOW_ANY);
# 273 : 2 : SetupArgs(local_args, {dir});
# 274 : 2 : ResetArgs(local_args, "");
# 275 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), fs::path{});
# 276 : :
# 277 : 2 : const fs::path root_path{"/"};
# 278 : 2 : ResetArgs(local_args, "-dir=/");
# 279 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
# 280 : :
# 281 : 2 : ResetArgs(local_args, "-dir=/.");
# 282 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
# 283 : :
# 284 : 2 : ResetArgs(local_args, "-dir=/./");
# 285 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
# 286 : :
# 287 : 2 : ResetArgs(local_args, "-dir=/.//");
# 288 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
# 289 : :
# 290 : : #ifdef WIN32
# 291 : : const fs::path win_root_path{"C:\\"};
# 292 : : ResetArgs(local_args, "-dir=C:\\");
# 293 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
# 294 : :
# 295 : : ResetArgs(local_args, "-dir=C:/");
# 296 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
# 297 : :
# 298 : : ResetArgs(local_args, "-dir=C:\\\\");
# 299 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
# 300 : :
# 301 : : ResetArgs(local_args, "-dir=C:\\.");
# 302 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
# 303 : :
# 304 : : ResetArgs(local_args, "-dir=C:\\.\\");
# 305 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
# 306 : :
# 307 : : ResetArgs(local_args, "-dir=C:\\.\\\\");
# 308 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
# 309 : : #endif
# 310 : :
# 311 : 2 : const fs::path absolute_path{"/home/user/.bitcoin"};
# 312 : 2 : ResetArgs(local_args, "-dir=/home/user/.bitcoin");
# 313 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 314 : :
# 315 : 2 : ResetArgs(local_args, "-dir=/root/../home/user/.bitcoin");
# 316 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 317 : :
# 318 : 2 : ResetArgs(local_args, "-dir=/home/./user/.bitcoin");
# 319 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 320 : :
# 321 : 2 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/");
# 322 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 323 : :
# 324 : 2 : ResetArgs(local_args, "-dir=/home/user/.bitcoin//");
# 325 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 326 : :
# 327 : 2 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/.");
# 328 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 329 : :
# 330 : 2 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/./");
# 331 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 332 : :
# 333 : 2 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/.//");
# 334 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
# 335 : :
# 336 : 2 : const fs::path relative_path{"user/.bitcoin"};
# 337 : 2 : ResetArgs(local_args, "-dir=user/.bitcoin");
# 338 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 339 : :
# 340 : 2 : ResetArgs(local_args, "-dir=somewhere/../user/.bitcoin");
# 341 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 342 : :
# 343 : 2 : ResetArgs(local_args, "-dir=user/./.bitcoin");
# 344 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 345 : :
# 346 : 2 : ResetArgs(local_args, "-dir=user/.bitcoin/");
# 347 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 348 : :
# 349 : 2 : ResetArgs(local_args, "-dir=user/.bitcoin//");
# 350 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 351 : :
# 352 : 2 : ResetArgs(local_args, "-dir=user/.bitcoin/.");
# 353 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 354 : :
# 355 : 2 : ResetArgs(local_args, "-dir=user/.bitcoin/./");
# 356 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 357 : :
# 358 : 2 : ResetArgs(local_args, "-dir=user/.bitcoin/.//");
# 359 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
# 360 : :
# 361 : : // Check negated and default argument handling. Specifying an empty argument
# 362 : : // is the same as not specifying the argument. This is convenient for
# 363 : : // scripting so later command line arguments can override earlier command
# 364 : : // line arguments or bitcoin.conf values. Currently the -dir= case cannot be
# 365 : : // distinguished from -dir case with no assignment, but #16545 would add the
# 366 : : // ability to distinguish these in the future (and treat the no-assign case
# 367 : : // like an imperative command or an error).
# 368 : 2 : ResetArgs(local_args, "");
# 369 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
# 370 : 2 : ResetArgs(local_args, "-dir=override");
# 371 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"override"});
# 372 : 2 : ResetArgs(local_args, "-dir=");
# 373 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
# 374 : 2 : ResetArgs(local_args, "-dir");
# 375 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
# 376 : 2 : ResetArgs(local_args, "-nodir");
# 377 : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{""});
# 378 : 2 : }
# 379 : :
# 380 : : BOOST_AUTO_TEST_CASE(doubledash)
# 381 : 2 : {
# 382 : 2 : ArgsManager local_args;
# 383 : :
# 384 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 385 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 386 : 2 : SetupArgs(local_args, {foo, bar});
# 387 : 2 : ResetArgs(local_args, "--foo");
# 388 : 2 : BOOST_CHECK_EQUAL(local_args.GetBoolArg("-foo", false), true);
# 389 : :
# 390 : 2 : ResetArgs(local_args, "--foo=verbose --bar=1");
# 391 : 2 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "verbose");
# 392 : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), 1);
# 393 : 2 : }
# 394 : :
# 395 : : BOOST_AUTO_TEST_CASE(boolargno)
# 396 : 2 : {
# 397 : 2 : ArgsManager local_args;
# 398 : :
# 399 : 2 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
# 400 : 2 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
# 401 : 2 : SetupArgs(local_args, {foo, bar});
# 402 : 2 : ResetArgs(local_args, "-nofoo");
# 403 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 404 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 405 : :
# 406 : 2 : ResetArgs(local_args, "-nofoo=1");
# 407 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 408 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 409 : :
# 410 : 2 : ResetArgs(local_args, "-nofoo=0");
# 411 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
# 412 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
# 413 : :
# 414 : 2 : ResetArgs(local_args, "-foo --nofoo"); // --nofoo should win
# 415 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
# 416 : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
# 417 : :
# 418 : 2 : ResetArgs(local_args, "-nofoo -foo"); // foo always wins:
# 419 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
# 420 : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
# 421 : 2 : }
# 422 : :
# 423 : : BOOST_AUTO_TEST_CASE(logargs)
# 424 : 2 : {
# 425 : 2 : ArgsManager local_args;
# 426 : :
# 427 : 2 : const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_ANY);
# 428 : 2 : const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_ANY);
# 429 : 2 : const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
# 430 : 2 : const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
# 431 : 2 : SetupArgs(local_args, {okaylog_bool, okaylog_negbool, okaylog, dontlog});
# 432 : 2 : ResetArgs(local_args, "-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
# 433 : :
# 434 : : // Everything logged to debug.log will also append to str
# 435 : 2 : std::string str;
# 436 : 2 : auto print_connection = LogInstance().PushBackCallback(
# 437 : 8 : [&str](const std::string& s) {
# 438 : 8 : str += s;
# 439 : 8 : });
# 440 : :
# 441 : : // Log the arguments
# 442 : 2 : local_args.LogArgs();
# 443 : :
# 444 : 2 : LogInstance().DeleteCallback(print_connection);
# 445 : : // Check that what should appear does, and what shouldn't doesn't.
# 446 : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
# 447 : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
# 448 : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
# 449 : 2 : BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
# 450 : 2 : BOOST_CHECK(str.find("private") == std::string::npos);
# 451 : 2 : }
# 452 : :
# 453 : : BOOST_AUTO_TEST_SUITE_END()
|