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 dir = std::make_pair("-dir", ArgsManager::ALLOW_ANY);
165  SetupArgs({dir});
166  ResetArgs("");
167  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), fs::path{});
168 
169  const fs::path root_path{"/"};
170  ResetArgs("-dir=/");
171  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
172 
173  ResetArgs("-dir=/.");
174  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
175 
176  ResetArgs("-dir=/./");
177  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
178 
179  ResetArgs("-dir=/.//");
180  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
181 
182 #ifdef WIN32
183  const fs::path win_root_path{"C:\\"};
184  ResetArgs("-dir=C:\\");
185  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
186 
187  ResetArgs("-dir=C:/");
188  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
189 
190  ResetArgs("-dir=C:\\\\");
191  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
192 
193  ResetArgs("-dir=C:\\.");
194  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
195 
196  ResetArgs("-dir=C:\\.\\");
197  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
198 
199  ResetArgs("-dir=C:\\.\\\\");
200  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
201 #endif
202 
203  const fs::path absolute_path{"/home/user/.bitcoin"};
204  ResetArgs("-dir=/home/user/.bitcoin");
205  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
206 
207  ResetArgs("-dir=/root/../home/user/.bitcoin");
208  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
209 
210  ResetArgs("-dir=/home/./user/.bitcoin");
211  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
212 
213  ResetArgs("-dir=/home/user/.bitcoin/");
214  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
215 
216  ResetArgs("-dir=/home/user/.bitcoin//");
217  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
218 
219  ResetArgs("-dir=/home/user/.bitcoin/.");
220  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
221 
222  ResetArgs("-dir=/home/user/.bitcoin/./");
223  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
224 
225  ResetArgs("-dir=/home/user/.bitcoin/.//");
226  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
227 
228  const fs::path relative_path{"user/.bitcoin"};
229  ResetArgs("-dir=user/.bitcoin");
230  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
231 
232  ResetArgs("-dir=somewhere/../user/.bitcoin");
233  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
234 
235  ResetArgs("-dir=user/./.bitcoin");
236  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
237 
238  ResetArgs("-dir=user/.bitcoin/");
239  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
240 
241  ResetArgs("-dir=user/.bitcoin//");
242  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
243 
244  ResetArgs("-dir=user/.bitcoin/.");
245  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
246 
247  ResetArgs("-dir=user/.bitcoin/./");
248  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
249 
250  ResetArgs("-dir=user/.bitcoin/.//");
251  BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
252 }
253 
255 {
256  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
257  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
258  SetupArgs({foo, bar});
259  ResetArgs("--foo");
260  BOOST_CHECK_EQUAL(m_local_args.GetBoolArg("-foo", false), true);
261 
262  ResetArgs("--foo=verbose --bar=1");
263  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "verbose");
264  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 0), 1);
265 }
266 
268 {
269  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
270  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
271  SetupArgs({foo, bar});
272  ResetArgs("-nofoo");
273  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
274  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
275 
276  ResetArgs("-nofoo=1");
277  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
278  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
279 
280  ResetArgs("-nofoo=0");
281  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
282  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
283 
284  ResetArgs("-foo --nofoo"); // --nofoo should win
285  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
286  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
287 
288  ResetArgs("-nofoo -foo"); // foo always wins:
289  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
290  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
291 }
292 
294 {
295  const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_ANY);
296  const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_ANY);
297  const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
298  const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
299  SetupArgs({okaylog_bool, okaylog_negbool, okaylog, dontlog});
300  ResetArgs("-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
301 
302  // Everything logged to debug.log will also append to str
303  std::string str;
304  auto print_connection = LogInstance().PushBackCallback(
305  [&str](const std::string& s) {
306  str += s;
307  });
308 
309  // Log the arguments
310  m_local_args.LogArgs();
311 
312  LogInstance().DeleteCallback(print_connection);
313  // Check that what should appear does, and what shouldn't doesn't.
314  BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
315  BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
316  BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
317  BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
318  BOOST_CHECK(str.find("private") == std::string::npos);
319 }
320 
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:124
setup_common.h
ArgsManager::ALLOW_ANY
@ ALLOW_ANY
disable validation
Definition: system.h:172
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
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
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:187
system.h
ArgsManager
Definition: system.h:164
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:18
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:116
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