2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2012-2020 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 06:11:00 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <test/util/setup_common.h>
|
2018-12-06 08:12:15 -03:00
|
|
|
#include <util/strencodings.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/system.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
|
|
|
#include <string>
|
2019-07-27 13:27:08 -04:00
|
|
|
#include <utility>
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <vector>
|
|
|
|
|
2012-02-06 14:37:49 -03:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2015-03-12 05:34:42 -03:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(getarg_tests, BasicTestingSetup)
|
2012-02-06 14:37:49 -03:00
|
|
|
|
2013-04-28 11:37:50 -04:00
|
|
|
static void ResetArgs(const std::string& strArg)
|
2012-02-06 14:37:49 -03:00
|
|
|
{
|
|
|
|
std::vector<std::string> vecArg;
|
2014-09-23 17:53:34 -03:00
|
|
|
if (strArg.size())
|
2018-11-05 13:22:09 -03:00
|
|
|
boost::split(vecArg, strArg, IsSpace, boost::token_compress_on);
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
// Insert dummy executable name:
|
|
|
|
vecArg.insert(vecArg.begin(), "testbitcoin");
|
|
|
|
|
|
|
|
// Convert to char*:
|
|
|
|
std::vector<const char*> vecChar;
|
2018-06-18 01:58:28 -04:00
|
|
|
for (const std::string& s : vecArg)
|
2012-02-06 14:37:49 -03:00
|
|
|
vecChar.push_back(s.c_str());
|
|
|
|
|
2018-04-28 20:40:51 -03:00
|
|
|
std::string error;
|
2018-08-02 10:31:10 -04:00
|
|
|
BOOST_CHECK(gArgs.ParseParameters(vecChar.size(), vecChar.data(), error));
|
2018-04-28 20:40:51 -03:00
|
|
|
}
|
|
|
|
|
2019-07-27 13:27:08 -04:00
|
|
|
static void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
|
2018-04-28 20:40:51 -03:00
|
|
|
{
|
|
|
|
gArgs.ClearArgs();
|
2019-07-27 13:27:08 -04:00
|
|
|
for (const auto& arg : args) {
|
|
|
|
gArgs.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
|
2018-04-28 20:40:51 -03:00
|
|
|
}
|
2012-02-06 14:37:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(boolarg)
|
|
|
|
{
|
2019-11-12 19:01:00 -03:00
|
|
|
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
|
2019-07-27 13:27:08 -04:00
|
|
|
SetupArgs({foo});
|
2012-02-06 14:37:49 -03:00
|
|
|
ResetArgs("-foo");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
|
2012-02-06 14:37:49 -03:00
|
|
|
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-fo", false));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-fo", true));
|
2012-02-06 14:37:49 -03:00
|
|
|
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-fooo", false));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-fooo", true));
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo=0");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo=1");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
|
|
|
// New 0.6 feature: auto-map -nosomething to !-something:
|
|
|
|
ResetArgs("-nofoo");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
|
|
|
ResetArgs("-nofoo=1");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
2015-06-15 12:17:23 -03:00
|
|
|
ResetArgs("-foo -nofoo"); // -nofoo should win
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
2015-06-15 12:17:23 -03:00
|
|
|
ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
2015-06-15 12:17:23 -03:00
|
|
|
ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
|
2015-06-15 12:17:23 -03:00
|
|
|
|
2012-02-06 15:55:11 -03:00
|
|
|
// New 0.6 feature: treat -- same as -:
|
|
|
|
ResetArgs("--foo=1");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
|
|
|
ResetArgs("--nofoo=1");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
2012-02-06 14:37:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(stringarg)
|
|
|
|
{
|
2019-11-12 19:01:00 -03:00
|
|
|
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
|
|
|
|
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
|
2019-07-27 13:27:08 -04:00
|
|
|
SetupArgs({foo, bar});
|
2012-02-06 14:37:49 -03:00
|
|
|
ResetArgs("");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "");
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven");
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo -bar");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "");
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "");
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo=");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "");
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "");
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo=11");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "11");
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "11");
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo=eleven");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "eleven");
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", "eleven"), "eleven");
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(intarg)
|
|
|
|
{
|
2019-11-12 19:01:00 -03:00
|
|
|
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
|
|
|
|
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
|
2019-07-27 13:27:08 -04:00
|
|
|
SetupArgs({foo, bar});
|
2012-02-06 14:37:49 -03:00
|
|
|
ResetArgs("");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 11);
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 0);
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo -bar");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 11), 0);
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0);
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo=11 -bar=12");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 0), 11);
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 12);
|
2012-02-06 14:37:49 -03:00
|
|
|
|
|
|
|
ResetArgs("-foo=NaN -bar=NotANumber");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", 1), 0);
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 11), 0);
|
2012-02-06 14:37:49 -03:00
|
|
|
}
|
|
|
|
|
2012-02-06 15:55:11 -03:00
|
|
|
BOOST_AUTO_TEST_CASE(doubledash)
|
|
|
|
{
|
2019-07-27 13:27:08 -04:00
|
|
|
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
|
|
|
|
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
|
|
|
|
SetupArgs({foo, bar});
|
2012-02-06 15:55:11 -03:00
|
|
|
ResetArgs("--foo");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetBoolArg("-foo", false), true);
|
2012-02-06 15:55:11 -03:00
|
|
|
|
|
|
|
ResetArgs("--foo=verbose --bar=1");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-foo", ""), "verbose");
|
|
|
|
BOOST_CHECK_EQUAL(gArgs.GetArg("-bar", 0), 1);
|
2012-02-06 15:55:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(boolargno)
|
|
|
|
{
|
2019-11-12 19:01:00 -03:00
|
|
|
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
|
|
|
|
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
|
2019-07-27 13:27:08 -04:00
|
|
|
SetupArgs({foo, bar});
|
2012-02-06 15:55:11 -03:00
|
|
|
ResetArgs("-nofoo");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
|
|
|
ResetArgs("-nofoo=1");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
|
|
|
ResetArgs("-nofoo=0");
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
2015-06-15 12:17:23 -03:00
|
|
|
ResetArgs("-foo --nofoo"); // --nofoo should win
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", true));
|
|
|
|
BOOST_CHECK(!gArgs.GetBoolArg("-foo", false));
|
2012-02-06 15:55:11 -03:00
|
|
|
|
|
|
|
ResetArgs("-nofoo -foo"); // foo always wins:
|
2017-08-01 15:17:40 -04:00
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", true));
|
|
|
|
BOOST_CHECK(gArgs.GetBoolArg("-foo", false));
|
2012-02-06 15:55:11 -03:00
|
|
|
}
|
|
|
|
|
2020-01-20 12:32:42 -03:00
|
|
|
BOOST_AUTO_TEST_CASE(logargs)
|
|
|
|
{
|
|
|
|
const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_BOOL);
|
|
|
|
const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_BOOL);
|
|
|
|
const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
|
|
|
|
const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
|
|
|
|
SetupArgs({okaylog_bool, okaylog_negbool, okaylog, dontlog});
|
|
|
|
ResetArgs("-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
|
|
|
|
|
|
|
|
// Everything logged to debug.log will also append to str
|
|
|
|
std::string str;
|
|
|
|
auto print_connection = LogInstance().PushBackCallback(
|
|
|
|
[&str](const std::string& s) {
|
|
|
|
str += s;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Log the arguments
|
|
|
|
gArgs.LogArgs();
|
|
|
|
|
|
|
|
LogInstance().DeleteCallback(print_connection);
|
|
|
|
// Check that what should appear does, and what shouldn't doesn't.
|
|
|
|
BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
|
|
|
|
BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
|
|
|
|
BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
|
|
|
|
BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
|
|
|
|
BOOST_CHECK(str.find("private") == std::string::npos);
|
|
|
|
}
|
|
|
|
|
2012-02-06 14:37:49 -03:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|