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