Bitcoin Core  22.99.0
P2P Digital Currency
getarg_tests.cpp
Go to the documentation of this file.
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 
6 #include <util/strencodings.h>
7 #include <util/system.h>
8 
9 #include <limits>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include <boost/algorithm/string.hpp>
15 #include <boost/test/unit_test.hpp>
16 
17 namespace getarg_tests{
19  protected:
20  void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args);
21  void ResetArgs(const std::string& strArg);
23  };
24 }
25 
26 BOOST_FIXTURE_TEST_SUITE(getarg_tests, LocalTestingSetup)
27 
28 void LocalTestingSetup :: ResetArgs(const std::string& strArg)
29 {
30  std::vector<std::string> vecArg;
31  if (strArg.size())
32  boost::split(vecArg, strArg, IsSpace, boost::token_compress_on);
33 
34  // Insert dummy executable name:
35  vecArg.insert(vecArg.begin(), "testbitcoin");
36 
37  // Convert to char*:
38  std::vector<const char*> vecChar;
39  for (const std::string& s : vecArg)
40  vecChar.push_back(s.c_str());
41 
42  std::string error;
43  BOOST_CHECK(m_local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
44 }
45 
46 void LocalTestingSetup :: SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
47 {
48  m_local_args.ClearArgs();
49  for (const auto& arg : args) {
50  m_local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
51  }
52 }
53 
55 {
56  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
57  SetupArgs({foo});
58  ResetArgs("-foo");
59  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
60  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
61 
62  BOOST_CHECK(!m_local_args.GetBoolArg("-fo", false));
63  BOOST_CHECK(m_local_args.GetBoolArg("-fo", true));
64 
65  BOOST_CHECK(!m_local_args.GetBoolArg("-fooo", false));
66  BOOST_CHECK(m_local_args.GetBoolArg("-fooo", true));
67 
68  ResetArgs("-foo=0");
69  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
70  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
71 
72  ResetArgs("-foo=1");
73  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
74  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
75 
76  // New 0.6 feature: auto-map -nosomething to !-something:
77  ResetArgs("-nofoo");
78  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
79  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
80 
81  ResetArgs("-nofoo=1");
82  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
83  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
84 
85  ResetArgs("-foo -nofoo"); // -nofoo should win
86  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
87  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
88 
89  ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
90  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
91  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
92 
93  ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
94  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
95  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
96 
97  // New 0.6 feature: treat -- same as -:
98  ResetArgs("--foo=1");
99  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
100  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
101 
102  ResetArgs("--nofoo=1");
103  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
104  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
105 
106 }
107 
109 {
110  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
111  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
112  SetupArgs({foo, bar});
113  ResetArgs("");
114  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
115  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
116 
117  ResetArgs("-foo -bar");
118  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
119  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
120 
121  ResetArgs("-foo=");
122  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
123  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
124 
125  ResetArgs("-foo=11");
126  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "11");
127  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "11");
128 
129  ResetArgs("-foo=eleven");
130  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "eleven");
131  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
132 
133 }
134 
136 {
137  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
138  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
139  SetupArgs({foo, bar});
140  ResetArgs("");
141  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 11), 11);
142  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), 0);
143 
144  ResetArgs("-foo -bar");
145  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 11), 0);
146  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
147 
148  // Check under-/overflow behavior.
149  ResetArgs("-foo=-9223372036854775809 -bar=9223372036854775808");
150  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), std::numeric_limits<int64_t>::min());
151  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 0), std::numeric_limits<int64_t>::max());
152 
153  ResetArgs("-foo=11 -bar=12");
154  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), 11);
155  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 12);
156 
157  ResetArgs("-foo=NaN -bar=NotANumber");
158  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 1), 0);
159  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
160 }
161 
163 {
164  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
165  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
166  SetupArgs({foo, bar});
167  ResetArgs("--foo");
168  BOOST_CHECK_EQUAL(m_local_args.GetBoolArg("-foo", false), true);
169 
170  ResetArgs("--foo=verbose --bar=1");
171  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "verbose");
172  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 0), 1);
173 }
174 
176 {
177  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
178  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
179  SetupArgs({foo, bar});
180  ResetArgs("-nofoo");
181  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
182  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
183 
184  ResetArgs("-nofoo=1");
185  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
186  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
187 
188  ResetArgs("-nofoo=0");
189  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
190  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
191 
192  ResetArgs("-foo --nofoo"); // --nofoo should win
193  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
194  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
195 
196  ResetArgs("-nofoo -foo"); // foo always wins:
197  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
198  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
199 }
200 
202 {
203  const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_ANY);
204  const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_ANY);
205  const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
206  const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
207  SetupArgs({okaylog_bool, okaylog_negbool, okaylog, dontlog});
208  ResetArgs("-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
209 
210  // Everything logged to debug.log will also append to str
211  std::string str;
212  auto print_connection = LogInstance().PushBackCallback(
213  [&str](const std::string& s) {
214  str += s;
215  });
216 
217  // Log the arguments
218  m_local_args.LogArgs();
219 
220  LogInstance().DeleteCallback(print_connection);
221  // Check that what should appear does, and what shouldn't doesn't.
222  BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
223  BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
224  BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
225  BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
226  BOOST_CHECK(str.find("private") == std::string::npos);
227 }
228 
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(boolarg)
Definition: getarg_tests.cpp:54
BCLog::Logger::DeleteCallback
void DeleteCallback(std::list< std::function< void(const std::string &)>>::iterator it)
Delete a connection.
Definition: logging.h:123
setup_common.h
ArgsManager::ALLOW_ANY
@ ALLOW_ANY
disable validation
Definition: system.h:166
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:152
strencodings.h
getarg_tests::LocalTestingSetup::ResetArgs
void ResetArgs(const std::string &strArg)
Definition: getarg_tests.cpp:28
OptionsCategory::OPTIONS
@ OPTIONS
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
ArgsManager::SENSITIVE
@ SENSITIVE
Definition: system.h:181
system.h
ArgsManager
Definition: system.h:158
getarg_tests::LocalTestingSetup
Definition: getarg_tests.cpp:18
std
Definition: setup_common.h:33
getarg_tests::LocalTestingSetup::m_local_args
ArgsManager m_local_args
Definition: getarg_tests.cpp:22
LogInstance
BCLog::Logger & LogInstance()
Definition: logging.cpp:17
BCLog::Logger::PushBackCallback
std::list< std::function< void(const std::string &)> >::iterator PushBackCallback(std::function< void(const std::string &)> fun)
Connect a slot to the print signal and return the connection.
Definition: logging.h:115
getarg_tests
Definition: getarg_tests.cpp:17
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
getarg_tests::LocalTestingSetup::SetupArgs
void SetupArgs(const std::vector< std::pair< std::string, unsigned int >> &args)
Definition: getarg_tests.cpp:46
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
args
ArgsManager args
Definition: notifications.cpp:36