Bitcoin Core 28.99.0
P2P Digital Currency
argsman_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2022 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 <common/args.h>
6#include <sync.h>
7#include <test/util/logging.h>
9#include <test/util/str.h>
10#include <univalue.h>
11#include <util/chaintype.h>
12#include <util/fs.h>
13#include <util/strencodings.h>
14
15#include <array>
16#include <optional>
17#include <cstdint>
18#include <cstring>
19#include <vector>
20
21#include <boost/test/unit_test.hpp>
22
23using util::ToString;
24
26
28{
29 // Use local args variable instead of m_args to avoid making assumptions about test setup
31 args.ForceSetArg("-datadir", fs::PathToString(m_path_root));
32
33 const fs::path dd_norm = args.GetDataDirBase();
34
35 args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/");
38
39 args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/.");
42
43 args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/./");
46
47 args.ForceSetArg("-datadir", fs::PathToString(dd_norm) + "/.//");
50}
51
53{
54 TestArgsManager() { m_network_only_args.clear(); }
55 void ReadConfigString(const std::string str_config)
56 {
57 std::istringstream streamConfig(str_config);
58 {
60 m_settings.ro_config.clear();
61 m_config_sections.clear();
62 }
63 std::string error;
64 BOOST_REQUIRE(ReadConfigStream(streamConfig, "", error));
65 }
66 void SetNetworkOnlyArg(const std::string arg)
67 {
69 m_network_only_args.insert(arg);
70 }
71 void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
72 {
73 for (const auto& arg : args) {
74 AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
75 }
76 }
81 using ArgsManager::m_network;
82 using ArgsManager::m_settings;
83};
84
87{
88public:
89 struct Expect {
91 bool default_string = false;
92 bool default_int = false;
93 bool default_bool = false;
94 const char* string_value = nullptr;
95 std::optional<int64_t> int_value;
96 std::optional<bool> bool_value;
97 std::optional<std::vector<std::string>> list_value;
98 const char* error = nullptr;
99
100 explicit Expect(common::SettingsValue s) : setting(std::move(s)) {}
101 Expect& DefaultString() { default_string = true; return *this; }
102 Expect& DefaultInt() { default_int = true; return *this; }
103 Expect& DefaultBool() { default_bool = true; return *this; }
104 Expect& String(const char* s) { string_value = s; return *this; }
105 Expect& Int(int64_t i) { int_value = i; return *this; }
106 Expect& Bool(bool b) { bool_value = b; return *this; }
107 Expect& List(std::vector<std::string> m) { list_value = std::move(m); return *this; }
108 Expect& Error(const char* e) { error = e; return *this; }
109 };
110
111 void CheckValue(unsigned int flags, const char* arg, const Expect& expect)
112 {
113 TestArgsManager test;
114 test.SetupArgs({{"-value", flags}});
115 const char* argv[] = {"ignored", arg};
116 std::string error;
117 bool success = test.ParseParameters(arg ? 2 : 1, argv, error);
118
119 BOOST_CHECK_EQUAL(test.GetSetting("-value").write(), expect.setting.write());
120 auto settings_list = test.GetSettingsList("-value");
121 if (expect.setting.isNull() || expect.setting.isFalse()) {
122 BOOST_CHECK_EQUAL(settings_list.size(), 0U);
123 } else {
124 BOOST_CHECK_EQUAL(settings_list.size(), 1U);
125 BOOST_CHECK_EQUAL(settings_list[0].write(), expect.setting.write());
126 }
127
128 if (expect.error) {
129 BOOST_CHECK(!success);
130 BOOST_CHECK_NE(error.find(expect.error), std::string::npos);
131 } else {
132 BOOST_CHECK(success);
133 BOOST_CHECK_EQUAL(error, "");
134 }
135
136 if (expect.default_string) {
137 BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), "zzzzz");
138 } else if (expect.string_value) {
139 BOOST_CHECK_EQUAL(test.GetArg("-value", "zzzzz"), expect.string_value);
140 } else {
141 BOOST_CHECK(!success);
142 }
143
144 if (expect.default_int) {
145 BOOST_CHECK_EQUAL(test.GetIntArg("-value", 99999), 99999);
146 } else if (expect.int_value) {
147 BOOST_CHECK_EQUAL(test.GetIntArg("-value", 99999), *expect.int_value);
148 } else {
149 BOOST_CHECK(!success);
150 }
151
152 if (expect.default_bool) {
153 BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), false);
154 BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), true);
155 } else if (expect.bool_value) {
156 BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), *expect.bool_value);
157 BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), *expect.bool_value);
158 } else {
159 BOOST_CHECK(!success);
160 }
161
162 if (expect.list_value) {
163 auto l = test.GetArgs("-value");
164 BOOST_CHECK_EQUAL_COLLECTIONS(l.begin(), l.end(), expect.list_value->begin(), expect.list_value->end());
165 } else {
166 BOOST_CHECK(!success);
167 }
168 }
169};
170
172{
173 using M = ArgsManager;
174
175 CheckValue(M::ALLOW_ANY, nullptr, Expect{{}}.DefaultString().DefaultInt().DefaultBool().List({}));
176 CheckValue(M::ALLOW_ANY, "-novalue", Expect{false}.String("0").Int(0).Bool(false).List({}));
177 CheckValue(M::ALLOW_ANY, "-novalue=", Expect{false}.String("0").Int(0).Bool(false).List({}));
178 CheckValue(M::ALLOW_ANY, "-novalue=0", Expect{true}.String("1").Int(1).Bool(true).List({"1"}));
179 CheckValue(M::ALLOW_ANY, "-novalue=1", Expect{false}.String("0").Int(0).Bool(false).List({}));
180 CheckValue(M::ALLOW_ANY, "-novalue=2", Expect{false}.String("0").Int(0).Bool(false).List({}));
181 CheckValue(M::ALLOW_ANY, "-novalue=abc", Expect{true}.String("1").Int(1).Bool(true).List({"1"}));
182 CheckValue(M::ALLOW_ANY, "-value", Expect{""}.String("").Int(0).Bool(true).List({""}));
183 CheckValue(M::ALLOW_ANY, "-value=", Expect{""}.String("").Int(0).Bool(true).List({""}));
184 CheckValue(M::ALLOW_ANY, "-value=0", Expect{"0"}.String("0").Int(0).Bool(false).List({"0"}));
185 CheckValue(M::ALLOW_ANY, "-value=1", Expect{"1"}.String("1").Int(1).Bool(true).List({"1"}));
186 CheckValue(M::ALLOW_ANY, "-value=2", Expect{"2"}.String("2").Int(2).Bool(true).List({"2"}));
187 CheckValue(M::ALLOW_ANY, "-value=abc", Expect{"abc"}.String("abc").Int(0).Bool(false).List({"abc"}));
188}
189
191 std::string Parse(const char* arg)
192 {
193 TestArgsManager test;
194 test.SetupArgs({{"-includeconf", ArgsManager::ALLOW_ANY}});
195 std::array argv{"ignored", arg};
196 std::string error;
197 (void)test.ParseParameters(argv.size(), argv.data(), error);
198 return error;
199 }
200};
201
203{
204 BOOST_CHECK_EQUAL(Parse("-noincludeconf"), "");
205 BOOST_CHECK_EQUAL(Parse("-includeconf"), "-includeconf cannot be used from commandline; -includeconf=\"\"");
206 BOOST_CHECK_EQUAL(Parse("-includeconf=file"), "-includeconf cannot be used from commandline; -includeconf=\"file\"");
207}
208
209BOOST_AUTO_TEST_CASE(util_ParseParameters)
210{
211 TestArgsManager testArgs;
212 const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY);
213 const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY);
214 const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_ANY);
215 const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY);
216
217 const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
218
219 std::string error;
220 LOCK(testArgs.cs_args);
221 testArgs.SetupArgs({a, b, ccc, d});
222 BOOST_CHECK(testArgs.ParseParameters(0, argv_test, error));
223 BOOST_CHECK(testArgs.m_settings.command_line_options.empty() && testArgs.m_settings.ro_config.empty());
224
225 BOOST_CHECK(testArgs.ParseParameters(1, argv_test, error));
226 BOOST_CHECK(testArgs.m_settings.command_line_options.empty() && testArgs.m_settings.ro_config.empty());
227
228 BOOST_CHECK(testArgs.ParseParameters(7, argv_test, error));
229 // expectation: -ignored is ignored (program name argument),
230 // -a, -b and -ccc end up in map, -d ignored because it is after
231 // a non-option argument (non-GNU option parsing)
232 BOOST_CHECK(testArgs.m_settings.command_line_options.size() == 3 && testArgs.m_settings.ro_config.empty());
233 BOOST_CHECK(testArgs.IsArgSet("-a") && testArgs.IsArgSet("-b") && testArgs.IsArgSet("-ccc")
234 && !testArgs.IsArgSet("f") && !testArgs.IsArgSet("-d"));
235 BOOST_CHECK(testArgs.m_settings.command_line_options.count("a") && testArgs.m_settings.command_line_options.count("b") && testArgs.m_settings.command_line_options.count("ccc")
236 && !testArgs.m_settings.command_line_options.count("f") && !testArgs.m_settings.command_line_options.count("d"));
237
238 BOOST_CHECK(testArgs.m_settings.command_line_options["a"].size() == 1);
239 BOOST_CHECK(testArgs.m_settings.command_line_options["a"].front().get_str() == "");
240 BOOST_CHECK(testArgs.m_settings.command_line_options["ccc"].size() == 2);
241 BOOST_CHECK(testArgs.m_settings.command_line_options["ccc"].front().get_str() == "argument");
242 BOOST_CHECK(testArgs.m_settings.command_line_options["ccc"].back().get_str() == "multiple");
243 BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
244}
245
246BOOST_AUTO_TEST_CASE(util_ParseInvalidParameters)
247{
248 TestArgsManager test;
249 test.SetupArgs({{"-registered", ArgsManager::ALLOW_ANY}});
250
251 const char* argv[] = {"ignored", "-registered"};
252 std::string error;
253 BOOST_CHECK(test.ParseParameters(2, argv, error));
254 BOOST_CHECK_EQUAL(error, "");
255
256 argv[1] = "-unregistered";
257 BOOST_CHECK(!test.ParseParameters(2, argv, error));
258 BOOST_CHECK_EQUAL(error, "Invalid parameter -unregistered");
259
260 // Make sure registered parameters prefixed with a chain type trigger errors.
261 // (Previously, they were accepted and ignored.)
262 argv[1] = "-test.registered";
263 BOOST_CHECK(!test.ParseParameters(2, argv, error));
264 BOOST_CHECK_EQUAL(error, "Invalid parameter -test.registered");
265}
266
267static void TestParse(const std::string& str, bool expected_bool, int64_t expected_int)
268{
269 TestArgsManager test;
270 test.SetupArgs({{"-value", ArgsManager::ALLOW_ANY}});
271 std::string arg = "-value=" + str;
272 const char* argv[] = {"ignored", arg.c_str()};
273 std::string error;
274 BOOST_CHECK(test.ParseParameters(2, argv, error));
275 BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), expected_bool);
276 BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), expected_bool);
277 BOOST_CHECK_EQUAL(test.GetIntArg("-value", 99998), expected_int);
278 BOOST_CHECK_EQUAL(test.GetIntArg("-value", 99999), expected_int);
279}
280
281// Test bool and int parsing.
282BOOST_AUTO_TEST_CASE(util_ArgParsing)
283{
284 // Some of these cases could be ambiguous or surprising to users, and might
285 // be worth triggering errors or warnings in the future. But for now basic
286 // test coverage is useful to avoid breaking backwards compatibility
287 // unintentionally.
288 TestParse("", true, 0);
289 TestParse(" ", false, 0);
290 TestParse("0", false, 0);
291 TestParse("0 ", false, 0);
292 TestParse(" 0", false, 0);
293 TestParse("+0", false, 0);
294 TestParse("-0", false, 0);
295 TestParse("5", true, 5);
296 TestParse("5 ", true, 5);
297 TestParse(" 5", true, 5);
298 TestParse("+5", true, 5);
299 TestParse("-5", true, -5);
300 TestParse("0 5", false, 0);
301 TestParse("5 0", true, 5);
302 TestParse("050", true, 50);
303 TestParse("0.", false, 0);
304 TestParse("5.", true, 5);
305 TestParse("0.0", false, 0);
306 TestParse("0.5", false, 0);
307 TestParse("5.0", true, 5);
308 TestParse("5.5", true, 5);
309 TestParse("x", false, 0);
310 TestParse("x0", false, 0);
311 TestParse("x5", false, 0);
312 TestParse("0x", false, 0);
313 TestParse("5x", true, 5);
314 TestParse("0x5", false, 0);
315 TestParse("false", false, 0);
316 TestParse("true", false, 0);
317 TestParse("yes", false, 0);
318 TestParse("no", false, 0);
319}
320
321BOOST_AUTO_TEST_CASE(util_GetBoolArg)
322{
323 TestArgsManager testArgs;
324 const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY);
325 const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY);
326 const auto c = std::make_pair("-c", ArgsManager::ALLOW_ANY);
327 const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY);
328 const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY);
329 const auto f = std::make_pair("-f", ArgsManager::ALLOW_ANY);
330
331 const char *argv_test[] = {
332 "ignored", "-a", "-nob", "-c=0", "-d=1", "-e=false", "-f=true"};
333 std::string error;
334 LOCK(testArgs.cs_args);
335 testArgs.SetupArgs({a, b, c, d, e, f});
336 BOOST_CHECK(testArgs.ParseParameters(7, argv_test, error));
337
338 // Each letter should be set.
339 for (const char opt : "abcdef")
340 BOOST_CHECK(testArgs.IsArgSet({'-', opt}) || !opt);
341
342 // Nothing else should be in the map
343 BOOST_CHECK(testArgs.m_settings.command_line_options.size() == 6 &&
344 testArgs.m_settings.ro_config.empty());
345
346 // The -no prefix should get stripped on the way in.
347 BOOST_CHECK(!testArgs.IsArgSet("-nob"));
348
349 // The -b option is flagged as negated, and nothing else is
350 BOOST_CHECK(testArgs.IsArgNegated("-b"));
351 BOOST_CHECK(!testArgs.IsArgNegated("-a"));
352
353 // Check expected values.
354 BOOST_CHECK(testArgs.GetBoolArg("-a", false) == true);
355 BOOST_CHECK(testArgs.GetBoolArg("-b", true) == false);
356 BOOST_CHECK(testArgs.GetBoolArg("-c", true) == false);
357 BOOST_CHECK(testArgs.GetBoolArg("-d", false) == true);
358 BOOST_CHECK(testArgs.GetBoolArg("-e", true) == false);
359 BOOST_CHECK(testArgs.GetBoolArg("-f", true) == false);
360}
361
362BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases)
363{
364 // Test some awful edge cases that hopefully no user will ever exercise.
365 TestArgsManager testArgs;
366
367 // Params test
368 const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
369 const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
370 const char *argv_test[] = {"ignored", "-nofoo", "-foo", "-nobar=0"};
371 testArgs.SetupArgs({foo, bar});
372 std::string error;
373 BOOST_CHECK(testArgs.ParseParameters(4, argv_test, error));
374
375 // This was passed twice, second one overrides the negative setting.
376 BOOST_CHECK(!testArgs.IsArgNegated("-foo"));
377 BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "");
378
379 // A double negative is a positive, and not marked as negated.
380 BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
381 BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "1");
382
383 // Config test
384 const char *conf_test = "nofoo=1\nfoo=1\nnobar=0\n";
385 BOOST_CHECK(testArgs.ParseParameters(1, argv_test, error));
386 testArgs.ReadConfigString(conf_test);
387
388 // This was passed twice, second one overrides the negative setting,
389 // and the value.
390 BOOST_CHECK(!testArgs.IsArgNegated("-foo"));
391 BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "1");
392
393 // A double negative is a positive, and does not count as negated.
394 BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
395 BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "1");
396
397 // Combined test
398 const char *combo_test_args[] = {"ignored", "-nofoo", "-bar"};
399 const char *combo_test_conf = "foo=1\nnobar=1\n";
400 BOOST_CHECK(testArgs.ParseParameters(3, combo_test_args, error));
401 testArgs.ReadConfigString(combo_test_conf);
402
403 // Command line overrides, but doesn't erase old setting
404 BOOST_CHECK(testArgs.IsArgNegated("-foo"));
405 BOOST_CHECK(testArgs.GetArg("-foo", "xxx") == "0");
406 BOOST_CHECK(testArgs.GetArgs("-foo").size() == 0);
407
408 // Command line overrides, but doesn't erase old setting
409 BOOST_CHECK(!testArgs.IsArgNegated("-bar"));
410 BOOST_CHECK(testArgs.GetArg("-bar", "xxx") == "");
411 BOOST_CHECK(testArgs.GetArgs("-bar").size() == 1
412 && testArgs.GetArgs("-bar").front() == "");
413}
414
415BOOST_AUTO_TEST_CASE(util_ReadConfigStream)
416{
417 const char *str_config =
418 "a=\n"
419 "b=1\n"
420 "ccc=argument\n"
421 "ccc=multiple\n"
422 "d=e\n"
423 "nofff=1\n"
424 "noggg=0\n"
425 "h=1\n"
426 "noh=1\n"
427 "noi=1\n"
428 "i=1\n"
429 "sec1.ccc=extend1\n"
430 "\n"
431 "[sec1]\n"
432 "ccc=extend2\n"
433 "d=eee\n"
434 "h=1\n"
435 "[sec2]\n"
436 "ccc=extend3\n"
437 "iii=2\n";
438
439 TestArgsManager test_args;
440 LOCK(test_args.cs_args);
441 const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY);
442 const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY);
443 const auto ccc = std::make_pair("-ccc", ArgsManager::ALLOW_ANY);
444 const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY);
445 const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY);
446 const auto fff = std::make_pair("-fff", ArgsManager::ALLOW_ANY);
447 const auto ggg = std::make_pair("-ggg", ArgsManager::ALLOW_ANY);
448 const auto h = std::make_pair("-h", ArgsManager::ALLOW_ANY);
449 const auto i = std::make_pair("-i", ArgsManager::ALLOW_ANY);
450 const auto iii = std::make_pair("-iii", ArgsManager::ALLOW_ANY);
451 test_args.SetupArgs({a, b, ccc, d, e, fff, ggg, h, i, iii});
452
453 test_args.ReadConfigString(str_config);
454 // expectation: a, b, ccc, d, fff, ggg, h, i end up in map
455 // so do sec1.ccc, sec1.d, sec1.h, sec2.ccc, sec2.iii
456
457 BOOST_CHECK(test_args.m_settings.command_line_options.empty());
458 BOOST_CHECK(test_args.m_settings.ro_config.size() == 3);
459 BOOST_CHECK(test_args.m_settings.ro_config[""].size() == 8);
460 BOOST_CHECK(test_args.m_settings.ro_config["sec1"].size() == 3);
461 BOOST_CHECK(test_args.m_settings.ro_config["sec2"].size() == 2);
462
463 BOOST_CHECK(test_args.m_settings.ro_config[""].count("a"));
464 BOOST_CHECK(test_args.m_settings.ro_config[""].count("b"));
465 BOOST_CHECK(test_args.m_settings.ro_config[""].count("ccc"));
466 BOOST_CHECK(test_args.m_settings.ro_config[""].count("d"));
467 BOOST_CHECK(test_args.m_settings.ro_config[""].count("fff"));
468 BOOST_CHECK(test_args.m_settings.ro_config[""].count("ggg"));
469 BOOST_CHECK(test_args.m_settings.ro_config[""].count("h"));
470 BOOST_CHECK(test_args.m_settings.ro_config[""].count("i"));
471 BOOST_CHECK(test_args.m_settings.ro_config["sec1"].count("ccc"));
472 BOOST_CHECK(test_args.m_settings.ro_config["sec1"].count("h"));
473 BOOST_CHECK(test_args.m_settings.ro_config["sec2"].count("ccc"));
474 BOOST_CHECK(test_args.m_settings.ro_config["sec2"].count("iii"));
475
476 BOOST_CHECK(test_args.IsArgSet("-a"));
477 BOOST_CHECK(test_args.IsArgSet("-b"));
478 BOOST_CHECK(test_args.IsArgSet("-ccc"));
479 BOOST_CHECK(test_args.IsArgSet("-d"));
480 BOOST_CHECK(test_args.IsArgSet("-fff"));
481 BOOST_CHECK(test_args.IsArgSet("-ggg"));
482 BOOST_CHECK(test_args.IsArgSet("-h"));
483 BOOST_CHECK(test_args.IsArgSet("-i"));
484 BOOST_CHECK(!test_args.IsArgSet("-zzz"));
485 BOOST_CHECK(!test_args.IsArgSet("-iii"));
486
487 BOOST_CHECK_EQUAL(test_args.GetArg("-a", "xxx"), "");
488 BOOST_CHECK_EQUAL(test_args.GetArg("-b", "xxx"), "1");
489 BOOST_CHECK_EQUAL(test_args.GetArg("-ccc", "xxx"), "argument");
490 BOOST_CHECK_EQUAL(test_args.GetArg("-d", "xxx"), "e");
491 BOOST_CHECK_EQUAL(test_args.GetArg("-fff", "xxx"), "0");
492 BOOST_CHECK_EQUAL(test_args.GetArg("-ggg", "xxx"), "1");
493 BOOST_CHECK_EQUAL(test_args.GetArg("-h", "xxx"), "0");
494 BOOST_CHECK_EQUAL(test_args.GetArg("-i", "xxx"), "1");
495 BOOST_CHECK_EQUAL(test_args.GetArg("-zzz", "xxx"), "xxx");
496 BOOST_CHECK_EQUAL(test_args.GetArg("-iii", "xxx"), "xxx");
497
498 for (const bool def : {false, true}) {
499 BOOST_CHECK(test_args.GetBoolArg("-a", def));
500 BOOST_CHECK(test_args.GetBoolArg("-b", def));
501 BOOST_CHECK(!test_args.GetBoolArg("-ccc", def));
502 BOOST_CHECK(!test_args.GetBoolArg("-d", def));
503 BOOST_CHECK(!test_args.GetBoolArg("-fff", def));
504 BOOST_CHECK(test_args.GetBoolArg("-ggg", def));
505 BOOST_CHECK(!test_args.GetBoolArg("-h", def));
506 BOOST_CHECK(test_args.GetBoolArg("-i", def));
507 BOOST_CHECK(test_args.GetBoolArg("-zzz", def) == def);
508 BOOST_CHECK(test_args.GetBoolArg("-iii", def) == def);
509 }
510
511 BOOST_CHECK(test_args.GetArgs("-a").size() == 1
512 && test_args.GetArgs("-a").front() == "");
513 BOOST_CHECK(test_args.GetArgs("-b").size() == 1
514 && test_args.GetArgs("-b").front() == "1");
515 BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2
516 && test_args.GetArgs("-ccc").front() == "argument"
517 && test_args.GetArgs("-ccc").back() == "multiple");
518 BOOST_CHECK(test_args.GetArgs("-fff").size() == 0);
519 BOOST_CHECK(test_args.GetArgs("-nofff").size() == 0);
520 BOOST_CHECK(test_args.GetArgs("-ggg").size() == 1
521 && test_args.GetArgs("-ggg").front() == "1");
522 BOOST_CHECK(test_args.GetArgs("-noggg").size() == 0);
523 BOOST_CHECK(test_args.GetArgs("-h").size() == 0);
524 BOOST_CHECK(test_args.GetArgs("-noh").size() == 0);
525 BOOST_CHECK(test_args.GetArgs("-i").size() == 1
526 && test_args.GetArgs("-i").front() == "1");
527 BOOST_CHECK(test_args.GetArgs("-noi").size() == 0);
528 BOOST_CHECK(test_args.GetArgs("-zzz").size() == 0);
529
530 BOOST_CHECK(!test_args.IsArgNegated("-a"));
531 BOOST_CHECK(!test_args.IsArgNegated("-b"));
532 BOOST_CHECK(!test_args.IsArgNegated("-ccc"));
533 BOOST_CHECK(!test_args.IsArgNegated("-d"));
534 BOOST_CHECK(test_args.IsArgNegated("-fff"));
535 BOOST_CHECK(!test_args.IsArgNegated("-ggg"));
536 BOOST_CHECK(test_args.IsArgNegated("-h")); // last setting takes precedence
537 BOOST_CHECK(!test_args.IsArgNegated("-i")); // last setting takes precedence
538 BOOST_CHECK(!test_args.IsArgNegated("-zzz"));
539
540 // Test sections work
541 test_args.SelectConfigNetwork("sec1");
542
543 // same as original
544 BOOST_CHECK_EQUAL(test_args.GetArg("-a", "xxx"), "");
545 BOOST_CHECK_EQUAL(test_args.GetArg("-b", "xxx"), "1");
546 BOOST_CHECK_EQUAL(test_args.GetArg("-fff", "xxx"), "0");
547 BOOST_CHECK_EQUAL(test_args.GetArg("-ggg", "xxx"), "1");
548 BOOST_CHECK_EQUAL(test_args.GetArg("-zzz", "xxx"), "xxx");
549 BOOST_CHECK_EQUAL(test_args.GetArg("-iii", "xxx"), "xxx");
550 // d is overridden
551 BOOST_CHECK(test_args.GetArg("-d", "xxx") == "eee");
552 // section-specific setting
553 BOOST_CHECK(test_args.GetArg("-h", "xxx") == "1");
554 // section takes priority for multiple values
555 BOOST_CHECK(test_args.GetArg("-ccc", "xxx") == "extend1");
556 // check multiple values works
557 const std::vector<std::string> sec1_ccc_expected = {"extend1","extend2","argument","multiple"};
558 const auto& sec1_ccc_res = test_args.GetArgs("-ccc");
559 BOOST_CHECK_EQUAL_COLLECTIONS(sec1_ccc_res.begin(), sec1_ccc_res.end(), sec1_ccc_expected.begin(), sec1_ccc_expected.end());
560
561 test_args.SelectConfigNetwork("sec2");
562
563 // same as original
564 BOOST_CHECK(test_args.GetArg("-a", "xxx") == "");
565 BOOST_CHECK(test_args.GetArg("-b", "xxx") == "1");
566 BOOST_CHECK(test_args.GetArg("-d", "xxx") == "e");
567 BOOST_CHECK(test_args.GetArg("-fff", "xxx") == "0");
568 BOOST_CHECK(test_args.GetArg("-ggg", "xxx") == "1");
569 BOOST_CHECK(test_args.GetArg("-zzz", "xxx") == "xxx");
570 BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
571 // section-specific setting
572 BOOST_CHECK(test_args.GetArg("-iii", "xxx") == "2");
573 // section takes priority for multiple values
574 BOOST_CHECK(test_args.GetArg("-ccc", "xxx") == "extend3");
575 // check multiple values works
576 const std::vector<std::string> sec2_ccc_expected = {"extend3","argument","multiple"};
577 const auto& sec2_ccc_res = test_args.GetArgs("-ccc");
578 BOOST_CHECK_EQUAL_COLLECTIONS(sec2_ccc_res.begin(), sec2_ccc_res.end(), sec2_ccc_expected.begin(), sec2_ccc_expected.end());
579
580 // Test section only options
581
582 test_args.SetNetworkOnlyArg("-d");
583 test_args.SetNetworkOnlyArg("-ccc");
584 test_args.SetNetworkOnlyArg("-h");
585
587 BOOST_CHECK(test_args.GetArg("-d", "xxx") == "e");
588 BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2);
589 BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
590
591 test_args.SelectConfigNetwork("sec1");
592 BOOST_CHECK(test_args.GetArg("-d", "xxx") == "eee");
593 BOOST_CHECK(test_args.GetArgs("-d").size() == 1);
594 BOOST_CHECK(test_args.GetArgs("-ccc").size() == 2);
595 BOOST_CHECK(test_args.GetArg("-h", "xxx") == "1");
596
597 test_args.SelectConfigNetwork("sec2");
598 BOOST_CHECK(test_args.GetArg("-d", "xxx") == "xxx");
599 BOOST_CHECK(test_args.GetArgs("-d").size() == 0);
600 BOOST_CHECK(test_args.GetArgs("-ccc").size() == 1);
601 BOOST_CHECK(test_args.GetArg("-h", "xxx") == "0");
602}
603
605{
606 TestArgsManager testArgs;
607 LOCK(testArgs.cs_args);
608 testArgs.m_settings.command_line_options.clear();
609 testArgs.m_settings.command_line_options["strtest1"] = {"string..."};
610 // strtest2 undefined on purpose
611 testArgs.m_settings.command_line_options["inttest1"] = {"12345"};
612 testArgs.m_settings.command_line_options["inttest2"] = {"81985529216486895"};
613 // inttest3 undefined on purpose
614 testArgs.m_settings.command_line_options["booltest1"] = {""};
615 // booltest2 undefined on purpose
616 testArgs.m_settings.command_line_options["booltest3"] = {"0"};
617 testArgs.m_settings.command_line_options["booltest4"] = {"1"};
618
619 // priorities
620 testArgs.m_settings.command_line_options["pritest1"] = {"a", "b"};
621 testArgs.m_settings.ro_config[""]["pritest2"] = {"a", "b"};
622 testArgs.m_settings.command_line_options["pritest3"] = {"a"};
623 testArgs.m_settings.ro_config[""]["pritest3"] = {"b"};
624 testArgs.m_settings.command_line_options["pritest4"] = {"a","b"};
625 testArgs.m_settings.ro_config[""]["pritest4"] = {"c","d"};
626
627 BOOST_CHECK_EQUAL(testArgs.GetArg("strtest1", "default"), "string...");
628 BOOST_CHECK_EQUAL(testArgs.GetArg("strtest2", "default"), "default");
629 BOOST_CHECK_EQUAL(testArgs.GetIntArg("inttest1", -1), 12345);
630 BOOST_CHECK_EQUAL(testArgs.GetIntArg("inttest2", -1), 81985529216486895LL);
631 BOOST_CHECK_EQUAL(testArgs.GetIntArg("inttest3", -1), -1);
632 BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest1", false), true);
633 BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest2", false), false);
634 BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest3", false), false);
635 BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest4", false), true);
636
637 BOOST_CHECK_EQUAL(testArgs.GetArg("pritest1", "default"), "b");
638 BOOST_CHECK_EQUAL(testArgs.GetArg("pritest2", "default"), "a");
639 BOOST_CHECK_EQUAL(testArgs.GetArg("pritest3", "default"), "a");
640 BOOST_CHECK_EQUAL(testArgs.GetArg("pritest4", "default"), "b");
641}
642
643BOOST_AUTO_TEST_CASE(util_GetChainTypeString)
644{
645 TestArgsManager test_args;
646 const auto testnet = std::make_pair("-testnet", ArgsManager::ALLOW_ANY);
647 const auto testnet4 = std::make_pair("-testnet4", ArgsManager::ALLOW_ANY);
648 const auto regtest = std::make_pair("-regtest", ArgsManager::ALLOW_ANY);
649 test_args.SetupArgs({testnet, testnet4, regtest});
650
651 const char* argv_testnet[] = {"cmd", "-testnet"};
652 const char* argv_testnet4[] = {"cmd", "-testnet4"};
653 const char* argv_regtest[] = {"cmd", "-regtest"};
654 const char* argv_test_no_reg[] = {"cmd", "-testnet", "-noregtest"};
655 const char* argv_both[] = {"cmd", "-testnet", "-regtest"};
656
657 // equivalent to "-testnet"
658 // regtest in testnet section is ignored
659 const char* testnetconf = "testnet=1\nregtest=0\n[test]\nregtest=1";
660 std::string error;
661
662 BOOST_CHECK(test_args.ParseParameters(0, argv_testnet, error));
663 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "main");
664
665 BOOST_CHECK(test_args.ParseParameters(2, argv_testnet, error));
666 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
667
668 BOOST_CHECK(test_args.ParseParameters(0, argv_testnet4, error));
669 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "main");
670
671 BOOST_CHECK(test_args.ParseParameters(2, argv_testnet4, error));
672 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "testnet4");
673
674 BOOST_CHECK(test_args.ParseParameters(2, argv_regtest, error));
675 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "regtest");
676
677 BOOST_CHECK(test_args.ParseParameters(3, argv_test_no_reg, error));
678 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
679
680 BOOST_CHECK(test_args.ParseParameters(3, argv_both, error));
681 BOOST_CHECK_THROW(test_args.GetChainTypeString(), std::runtime_error);
682
683 BOOST_CHECK(test_args.ParseParameters(0, argv_testnet, error));
684 test_args.ReadConfigString(testnetconf);
685 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
686
687 BOOST_CHECK(test_args.ParseParameters(2, argv_testnet, error));
688 test_args.ReadConfigString(testnetconf);
689 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
690
691 BOOST_CHECK(test_args.ParseParameters(2, argv_regtest, error));
692 test_args.ReadConfigString(testnetconf);
693 BOOST_CHECK_THROW(test_args.GetChainTypeString(), std::runtime_error);
694
695 BOOST_CHECK(test_args.ParseParameters(3, argv_test_no_reg, error));
696 test_args.ReadConfigString(testnetconf);
697 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
698
699 BOOST_CHECK(test_args.ParseParameters(3, argv_both, error));
700 test_args.ReadConfigString(testnetconf);
701 BOOST_CHECK_THROW(test_args.GetChainTypeString(), std::runtime_error);
702
703 // check setting the network to test (and thus making
704 // [test] regtest=1 potentially relevant) doesn't break things
705 test_args.SelectConfigNetwork("test");
706
707 BOOST_CHECK(test_args.ParseParameters(0, argv_testnet, error));
708 test_args.ReadConfigString(testnetconf);
709 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
710
711 BOOST_CHECK(test_args.ParseParameters(2, argv_testnet, error));
712 test_args.ReadConfigString(testnetconf);
713 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
714
715 BOOST_CHECK(test_args.ParseParameters(2, argv_regtest, error));
716 test_args.ReadConfigString(testnetconf);
717 BOOST_CHECK_THROW(test_args.GetChainTypeString(), std::runtime_error);
718
719 BOOST_CHECK(test_args.ParseParameters(2, argv_test_no_reg, error));
720 test_args.ReadConfigString(testnetconf);
721 BOOST_CHECK_EQUAL(test_args.GetChainTypeString(), "test");
722
723 BOOST_CHECK(test_args.ParseParameters(3, argv_both, error));
724 test_args.ReadConfigString(testnetconf);
725 BOOST_CHECK_THROW(test_args.GetChainTypeString(), std::runtime_error);
726}
727
728// Test different ways settings can be merged, and verify results. This test can
729// be used to confirm that updates to settings code don't change behavior
730// unintentionally.
731//
732// The test covers:
733//
734// - Combining different setting actions. Possible actions are: configuring a
735// setting, negating a setting (adding "-no" prefix), and configuring/negating
736// settings in a network section (adding "main." or "test." prefixes).
737//
738// - Combining settings from command line arguments and a config file.
739//
740// - Combining SoftSet and ForceSet calls.
741//
742// - Testing "main" and "test" network values to make sure settings from network
743// sections are applied and to check for mainnet-specific behaviors like
744// inheriting settings from the default section.
745//
746// - Testing network-specific settings like "-wallet", that may be ignored
747// outside a network section, and non-network specific settings like "-server"
748// that aren't sensitive to the network.
749//
753 static constexpr int MAX_ACTIONS = 3;
754
757
759 template <typename Fn>
760 void ForEachMergeSetup(Fn&& fn)
761 {
762 ActionList arg_actions = {};
763 // command_line_options do not have sections. Only iterate over SET and NEGATE
764 ForEachNoDup(arg_actions, SET, NEGATE, [&] {
765 ActionList conf_actions = {};
766 ForEachNoDup(conf_actions, SET, SECTION_NEGATE, [&] {
767 for (bool soft_set : {false, true}) {
768 for (bool force_set : {false, true}) {
771 for (bool net_specific : {false, true}) {
772 fn(arg_actions, conf_actions, soft_set, force_set, section, network, net_specific);
773 }
774 }
775 }
776 }
777 }
778 });
779 });
780 }
781
783 std::vector<std::string> GetValues(const ActionList& actions,
784 const std::string& section,
785 const std::string& name,
786 const std::string& value_prefix)
787 {
788 std::vector<std::string> values;
789 int suffix = 0;
790 for (Action action : actions) {
791 if (action == NONE) break;
792 std::string prefix;
793 if (action == SECTION_SET || action == SECTION_NEGATE) prefix = section + ".";
794 if (action == SET || action == SECTION_SET) {
795 for (int i = 0; i < 2; ++i) {
796 values.push_back(prefix + name + "=" + value_prefix + ToString(++suffix));
797 }
798 }
799 if (action == NEGATE || action == SECTION_NEGATE) {
800 values.push_back(prefix + "no" + name + "=1");
801 }
802 }
803 return values;
804 }
805};
806
807// Regression test covering different ways config settings can be merged. The
808// test parses and merges settings, representing the results as strings that get
809// compared against an expected hash. To debug, the result strings can be dumped
810// to a file (see comments below).
812{
813 CHash256 out_sha;
814 FILE* out_file = nullptr;
815 if (const char* out_path = getenv("ARGS_MERGE_TEST_OUT")) {
816 out_file = fsbridge::fopen(out_path, "w");
817 if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");
818 }
819
820 ForEachMergeSetup([&](const ActionList& arg_actions, const ActionList& conf_actions, bool soft_set, bool force_set,
821 const std::string& section, const std::string& network, bool net_specific) {
822 TestArgsManager parser;
823 LOCK(parser.cs_args);
824
825 std::string desc = "net=";
826 desc += network;
827 parser.m_network = network;
828
829 const std::string& name = net_specific ? "wallet" : "server";
830 const std::string key = "-" + name;
832 if (net_specific) parser.SetNetworkOnlyArg(key);
833
834 auto args = GetValues(arg_actions, section, name, "a");
835 std::vector<const char*> argv = {"ignored"};
836 for (auto& arg : args) {
837 arg.insert(0, "-");
838 desc += " ";
839 desc += arg;
840 argv.push_back(arg.c_str());
841 }
842 std::string error;
843 BOOST_CHECK(parser.ParseParameters(argv.size(), argv.data(), error));
844 BOOST_CHECK_EQUAL(error, "");
845
846 std::string conf;
847 for (auto& conf_val : GetValues(conf_actions, section, name, "c")) {
848 desc += " ";
849 desc += conf_val;
850 conf += conf_val;
851 conf += "\n";
852 }
853 std::istringstream conf_stream(conf);
854 BOOST_CHECK(parser.ReadConfigStream(conf_stream, "filepath", error));
855 BOOST_CHECK_EQUAL(error, "");
856
857 if (soft_set) {
858 desc += " soft";
859 parser.SoftSetArg(key, "soft1");
860 parser.SoftSetArg(key, "soft2");
861 }
862
863 if (force_set) {
864 desc += " force";
865 parser.ForceSetArg(key, "force1");
866 parser.ForceSetArg(key, "force2");
867 }
868
869 desc += " || ";
870
871 if (!parser.IsArgSet(key)) {
872 desc += "unset";
873 BOOST_CHECK(!parser.IsArgNegated(key));
874 BOOST_CHECK_EQUAL(parser.GetArg(key, "default"), "default");
875 BOOST_CHECK(parser.GetArgs(key).empty());
876 } else if (parser.IsArgNegated(key)) {
877 desc += "negated";
878 BOOST_CHECK_EQUAL(parser.GetArg(key, "default"), "0");
879 BOOST_CHECK(parser.GetArgs(key).empty());
880 } else {
881 desc += parser.GetArg(key, "default");
882 desc += " |";
883 for (const auto& arg : parser.GetArgs(key)) {
884 desc += " ";
885 desc += arg;
886 }
887 }
888
889 std::set<std::string> ignored = parser.GetUnsuitableSectionOnlyArgs();
890 if (!ignored.empty()) {
891 desc += " | ignored";
892 for (const auto& arg : ignored) {
893 desc += " ";
894 desc += arg;
895 }
896 }
897
898 desc += "\n";
899
900 out_sha.Write(MakeUCharSpan(desc));
901 if (out_file) {
902 BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
903 }
904 });
905
906 if (out_file) {
907 if (fclose(out_file)) throw std::system_error(errno, std::generic_category(), "fclose failed");
908 out_file = nullptr;
909 }
910
911 unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
912 out_sha.Finalize(out_sha_bytes);
913 std::string out_sha_hex = HexStr(out_sha_bytes);
914
915 // If check below fails, should manually dump the results with:
916 //
917 // ARGS_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=argsman_tests/util_ArgsMerge
918 //
919 // And verify diff against previous results to make sure the changes are expected.
920 //
921 // Results file is formatted like:
922 //
923 // <input> || <IsArgSet/IsArgNegated/GetArg output> | <GetArgs output> | <GetUnsuitable output>
924 BOOST_CHECK_EQUAL(out_sha_hex, "f1ee5ab094cc43d16a6086fa7f2c10389e0f99902616b31bbf29189972ad1473");
925}
926
927// Similar test as above, but for ArgsManager::GetChainTypeString function.
929 static constexpr int MAX_ACTIONS = 2;
930
933
935 template <typename Fn>
936 void ForEachMergeSetup(Fn&& fn)
937 {
938 ActionList arg_actions = {};
939 ForEachNoDup(arg_actions, ENABLE_TEST, NEGATE_REG, [&] {
940 ActionList conf_actions = {};
941 ForEachNoDup(conf_actions, ENABLE_TEST, NEGATE_REG, [&] { fn(arg_actions, conf_actions); });
942 });
943 }
944};
945
947{
948 CHash256 out_sha;
949 FILE* out_file = nullptr;
950 if (const char* out_path = getenv("CHAIN_MERGE_TEST_OUT")) {
951 out_file = fsbridge::fopen(out_path, "w");
952 if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");
953 }
954
955 ForEachMergeSetup([&](const ActionList& arg_actions, const ActionList& conf_actions) {
956 TestArgsManager parser;
957 LOCK(parser.cs_args);
958 parser.AddArg("-regtest", "regtest", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
959 parser.AddArg("-testnet", "testnet", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
960
961 auto arg = [](Action action) { return action == ENABLE_TEST ? "-testnet=1" :
962 action == DISABLE_TEST ? "-testnet=0" :
963 action == NEGATE_TEST ? "-notestnet=1" :
964 action == ENABLE_REG ? "-regtest=1" :
965 action == DISABLE_REG ? "-regtest=0" :
966 action == NEGATE_REG ? "-noregtest=1" : nullptr; };
967
968 std::string desc;
969 std::vector<const char*> argv = {"ignored"};
970 for (Action action : arg_actions) {
971 const char* argstr = arg(action);
972 if (!argstr) break;
973 argv.push_back(argstr);
974 desc += " ";
975 desc += argv.back();
976 }
977 std::string error;
978 BOOST_CHECK(parser.ParseParameters(argv.size(), argv.data(), error));
979 BOOST_CHECK_EQUAL(error, "");
980
981 std::string conf;
982 for (Action action : conf_actions) {
983 const char* argstr = arg(action);
984 if (!argstr) break;
985 desc += " ";
986 desc += argstr + 1;
987 conf += argstr + 1;
988 conf += "\n";
989 }
990 std::istringstream conf_stream(conf);
991 BOOST_CHECK(parser.ReadConfigStream(conf_stream, "filepath", error));
992 BOOST_CHECK_EQUAL(error, "");
993
994 desc += " || ";
995 try {
996 desc += parser.GetChainTypeString();
997 } catch (const std::runtime_error& e) {
998 desc += "error: ";
999 desc += e.what();
1000 }
1001 desc += "\n";
1002
1003 out_sha.Write(MakeUCharSpan(desc));
1004 if (out_file) {
1005 BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
1006 }
1007 });
1008
1009 if (out_file) {
1010 if (fclose(out_file)) throw std::system_error(errno, std::generic_category(), "fclose failed");
1011 out_file = nullptr;
1012 }
1013
1014 unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
1015 out_sha.Finalize(out_sha_bytes);
1016 std::string out_sha_hex = HexStr(out_sha_bytes);
1017
1018 // If check below fails, should manually dump the results with:
1019 //
1020 // CHAIN_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=argsman_tests/util_ChainMerge
1021 //
1022 // And verify diff against previous results to make sure the changes are expected.
1023 //
1024 // Results file is formatted like:
1025 //
1026 // <input> || <output>
1027 BOOST_CHECK_EQUAL(out_sha_hex, "9e60306e1363528bbc19a47f22bcede88e5d6815212f18ec8e6cdc4638dddab4");
1028}
1029
1030BOOST_AUTO_TEST_CASE(util_ReadWriteSettings)
1031{
1032 // Test writing setting.
1033 TestArgsManager args1;
1034 args1.ForceSetArg("-datadir", fs::PathToString(m_path_root));
1035 args1.LockSettings([&](common::Settings& settings) { settings.rw_settings["name"] = "value"; });
1036 args1.WriteSettingsFile();
1037
1038 // Test reading setting.
1039 TestArgsManager args2;
1040 args2.ForceSetArg("-datadir", fs::PathToString(m_path_root));
1041 args2.ReadSettingsFile();
1042 args2.LockSettings([&](common::Settings& settings) { BOOST_CHECK_EQUAL(settings.rw_settings["name"].get_str(), "value"); });
1043
1044 // Test error logging, and remove previously written setting.
1045 {
1046 ASSERT_DEBUG_LOG("Failed renaming settings file");
1047 fs::remove(args1.GetDataDirBase() / "settings.json");
1048 fs::create_directory(args1.GetDataDirBase() / "settings.json");
1049 args2.WriteSettingsFile();
1050 fs::remove(args1.GetDataDirBase() / "settings.json");
1051 }
1052}
1053
BOOST_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest)
static void TestParse(const std::string &str, bool expected_bool, int64_t expected_int)
BOOST_AUTO_TEST_CASE(util_datadir)
int flags
Definition: bitcoin-tx.cpp:536
ArgsManager & args
Definition: bitcoind.cpp:277
std::string ChainTypeToString(ChainType chain)
Definition: chaintype.cpp:11
std::set< std::string > GetUnsuitableSectionOnlyArgs() const
Log warnings for options in m_section_only_args when they are specified in the default section but no...
Definition: args.cpp:136
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
Definition: args.cpp:452
@ ALLOW_ANY
disable validation
Definition: args.h:106
common::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
Definition: args.cpp:825
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr)
Read settings file.
Definition: args.cpp:402
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: args.cpp:546
std::string GetChainTypeString() const
Returns the appropriate chain type string from the program arguments.
Definition: args.cpp:781
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:179
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: args.cpp:362
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: args.h:413
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: args.cpp:530
void SelectConfigNetwork(const std::string &network)
Select the network in use.
Definition: args.cpp:173
void ClearPathCache()
Clear cached directory paths.
Definition: args.cpp:333
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:371
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: args.cpp:425
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:482
fs::path GetDataDirBase() const
Get data directory path.
Definition: args.h:227
std::vector< common::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
Definition: args.cpp:833
RecursiveMutex cs_args
Definition: args.h:134
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:457
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:93
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:564
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:24
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
Test GetSetting and GetArg type coercion, negation, and default value handling.
void CheckValue(unsigned int flags, const char *arg, const Expect &expect)
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:327
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:233
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
const char * prefix
Definition: rest.cpp:1009
const char * name
Definition: rest.cpp:49
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:297
void ForEachNoDup(CharType(&string)[StringLength], CharType min_char, CharType max_char, Fn &&fn)
Iterate over string values and call function for each string without successive duplicate characters.
Definition: str.h:32
void ForEachMergeSetup(Fn &&fn)
Enumerate all possible test configurations.
static constexpr int MAX_ACTIONS
Max number of actions to sequence together.
std::vector< std::string > GetValues(const ActionList &actions, const std::string &section, const std::string &name, const std::string &value_prefix)
Translate actions into a list of <key>=setting strings.
Action[MAX_ACTIONS] ActionList
Basic testing setup.
Definition: setup_common.h:63
void ForEachMergeSetup(Fn &&fn)
Enumerate all possible test configurations.
Action[MAX_ACTIONS] ActionList
static constexpr int MAX_ACTIONS
Expect(common::SettingsValue s)
Expect & List(std::vector< std::string > m)
Expect & Bool(bool b)
Expect & String(const char *s)
std::optional< int64_t > int_value
std::optional< std::vector< std::string > > list_value
Expect & Error(const char *e)
common::SettingsValue setting
std::optional< bool > bool_value
Expect & Int(int64_t i)
std::string Parse(const char *arg)
common::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
Definition: args.cpp:825
void SetupArgs(const std::vector< std::pair< std::string, unsigned int > > &args)
void SetNetworkOnlyArg(const std::string arg)
std::vector< common::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
Definition: args.cpp:833
void ReadConfigString(const std::string str_config)
RecursiveMutex cs_args
Definition: args.h:134
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:93
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:139
Stored settings.
Definition: settings.h:32
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:38
#define LOCK(cs)
Definition: sync.h:257
#define ASSERT_DEBUG_LOG(message)
Definition: logging.h:39
#define expect(bit)