Branch data Line data Source code
1 : : // Copyright (c) 2012-present 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 <common/settings.h>
7 : : #include <logging.h>
8 : : #include <test/util/setup_common.h>
9 : : #include <univalue.h>
10 : : #include <util/strencodings.h>
11 : :
12 : : #include <limits>
13 : : #include <string>
14 : : #include <utility>
15 : : #include <vector>
16 : :
17 : : #include <boost/test/unit_test.hpp>
18 : :
19 : : using util::SplitString;
20 : :
21 : : BOOST_FIXTURE_TEST_SUITE(getarg_tests, BasicTestingSetup)
22 : :
23 : 54 : void ResetArgs(ArgsManager& local_args, const std::string& strArg)
24 : : {
25 : 54 : std::vector<std::string> vecArg;
26 [ - + + + ]: 54 : if (strArg.size()) {
27 [ + - ]: 50 : vecArg = SplitString(strArg, ' ');
28 : : }
29 : :
30 : : // Insert dummy executable name:
31 [ + - + - ]: 54 : vecArg.insert(vecArg.begin(), "testbitcoin");
32 : :
33 : : // Convert to char*:
34 : 54 : std::vector<const char*> vecChar;
35 [ - + + - ]: 54 : vecChar.reserve(vecArg.size());
36 [ + + ]: 172 : for (const std::string& s : vecArg)
37 [ + - ]: 118 : vecChar.push_back(s.c_str());
38 : :
39 [ + - ]: 54 : std::string error;
40 [ + - - + : 108 : BOOST_CHECK(local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
+ - + - ]
41 : 54 : }
42 : :
43 : 8 : void SetupArgs(ArgsManager& local_args, const std::vector<std::pair<std::string, unsigned int>>& args)
44 : : {
45 [ + + ]: 23 : for (const auto& arg : args) {
46 [ + - ]: 30 : local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
47 : : }
48 : 8 : }
49 : :
50 : : // Test behavior of GetArg functions when string, integer, and boolean types
51 : : // are specified in the settings.json file. GetArg functions are convenience
52 : : // functions. The GetSetting method can always be used instead of GetArg
53 : : // methods to retrieve original values, and there's not always an objective
54 : : // answer to what GetArg behavior is best in every case. This test makes sure
55 : : // there's test coverage for whatever the current behavior is, so it's not
56 : : // broken or changed unintentionally.
57 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(setting_args)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
58 : : {
59 : 1 : ArgsManager args;
60 [ + - + - : 3 : SetupArgs(args, {{"-foo", ArgsManager::ALLOW_ANY}});
+ + - - ]
61 : :
62 : 14 : auto set_foo = [&](const common::SettingsValue& value) {
63 [ + - + - : 23 : args.LockSettings([&](common::Settings& settings) {
+ - + - +
- + - + -
+ - + - +
- ]
64 [ + - + - ]: 13 : settings.rw_settings["foo"] = value;
65 : 13 : });
66 : 13 : };
67 : :
68 [ + - ]: 2 : set_foo("str");
69 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"str\"");
+ - + - +
- ]
70 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "str");
+ - + - +
- ]
71 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ - ]
72 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
+ - + - ]
73 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+ - + - ]
74 : :
75 [ + - ]: 2 : set_foo("99");
76 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"99\"");
+ - + - +
- ]
77 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
+ - + - +
- ]
78 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
+ - ]
79 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ - + - ]
80 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+ - + - ]
81 : :
82 [ + - ]: 2 : set_foo("3.25");
83 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"3.25\"");
+ - + - ]
84 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
+ - + - +
- ]
85 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 3);
+ - ]
86 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ - + - ]
87 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+ - + - ]
88 : :
89 [ + - ]: 2 : set_foo("0");
90 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"0\"");
+ - + - +
- ]
91 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
+ - + - +
- ]
92 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ - ]
93 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
+ - + - ]
94 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+ - + - ]
95 : :
96 [ + - ]: 2 : set_foo("");
97 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"\"");
+ - + - +
- ]
98 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "");
+ - + - +
- ]
99 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ - ]
100 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ - + - ]
101 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+ - + - ]
102 : :
103 [ + - ]: 2 : set_foo(99);
104 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "99");
+ - + - +
- ]
105 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
+ - + - +
- ]
106 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
+ - ]
107 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
- + - - -
- - + + -
+ - ]
108 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
- + - - -
- - + + -
+ - ]
109 : :
110 [ + - ]: 2 : set_foo(3.25);
111 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "3.25");
+ - + - +
- ]
112 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
+ - + - +
- ]
113 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
- - - - -
+ + - +
- ]
114 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
- + - - -
- - + + -
+ - ]
115 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
- + - - -
- - + + -
+ - ]
116 : :
117 [ + - ]: 2 : set_foo(0);
118 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "0");
+ - + - +
- ]
119 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
+ - + - +
- ]
120 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ - ]
121 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
- + - - -
- - + + -
+ - ]
122 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
- + - - -
- - + + -
+ - ]
123 : :
124 [ + - ]: 2 : set_foo(true);
125 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "true");
+ - + - +
- ]
126 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "1");
+ - + - +
- ]
127 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 1);
+ - ]
128 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ - + - ]
129 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+ - + - ]
130 : :
131 [ + - ]: 2 : set_foo(false);
132 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "false");
+ - + - +
- ]
133 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
+ - + - +
- ]
134 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ - ]
135 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
+ - + - ]
136 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+ - + - ]
137 : :
138 [ + - ]: 2 : set_foo(UniValue::VOBJ);
139 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "{}");
+ - + - +
- ]
140 [ + - + - : 4 : BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
+ - - + -
- - - - +
+ - + - ]
141 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
- - - - -
+ + - +
- ]
142 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
- + - - -
- - + + -
+ - ]
143 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
- + - - -
- - + + -
+ - ]
144 : :
145 [ + - ]: 2 : set_foo(UniValue::VARR);
146 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "[]");
+ - + - +
- ]
147 [ + - + - : 4 : BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
+ - - + -
- - - - +
+ - + - ]
148 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
- - - - -
+ + - +
- ]
149 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
- + - - -
- - + + -
+ - ]
150 [ + - + - : 3 : BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
- + - - -
- - + + -
+ - ]
151 : :
152 [ + - ]: 2 : set_foo(UniValue::VNULL);
153 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "null");
+ - + - +
- ]
154 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "default");
+ - + - +
- ]
155 [ + - + - : 2 : BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 100);
+ - ]
156 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ - + - ]
157 [ + - + - : 1 : BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+ - + - ]
158 [ + - ]: 2 : }
159 : :
160 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(boolarg)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
161 : : {
162 : 1 : ArgsManager local_args;
163 : :
164 : 1 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
165 [ + - + + : 2 : SetupArgs(local_args, {foo});
- - ]
166 [ + - + - ]: 1 : ResetArgs(local_args, "-foo");
167 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
168 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
169 : :
170 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-fo", false));
+ - + - +
- ]
171 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-fo", true));
+ - + - +
- ]
172 : :
173 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-fooo", false));
+ - + - +
- ]
174 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-fooo", true));
+ - + - +
- ]
175 : :
176 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=0");
177 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
178 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
179 : :
180 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=1");
181 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
182 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
183 : :
184 : : // New 0.6 feature: auto-map -nosomething to !-something:
185 [ + - + - ]: 1 : ResetArgs(local_args, "-nofoo");
186 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
187 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
188 : :
189 [ + - + - ]: 1 : ResetArgs(local_args, "-nofoo=1");
190 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
191 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
192 : :
193 [ + - + - ]: 1 : ResetArgs(local_args, "-foo -nofoo"); // -nofoo should win
194 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
195 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
196 : :
197 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=1 -nofoo=1"); // -nofoo should win
198 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
199 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
200 : :
201 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=0 -nofoo=0"); // -nofoo=0 should win
202 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
203 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
204 : :
205 : : // New 0.6 feature: treat -- same as -:
206 [ + - + - ]: 1 : ResetArgs(local_args, "--foo=1");
207 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
208 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
209 : :
210 [ + - + - ]: 1 : ResetArgs(local_args, "--nofoo=1");
211 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
212 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - ]
213 [ + - + - ]: 2 : }
214 : :
215 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(stringarg)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
216 : : {
217 : 1 : ArgsManager local_args;
218 : :
219 : 1 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
220 : 1 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
221 [ + - + + : 3 : SetupArgs(local_args, {foo, bar});
- - ]
222 [ + - + - ]: 1 : ResetArgs(local_args, "");
223 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
+ - + - +
- ]
224 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
+ - + - +
- ]
225 : :
226 [ + - + - ]: 1 : ResetArgs(local_args, "-foo -bar");
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=");
231 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
+ - + - +
- ]
232 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "");
+ - + - +
- ]
233 : :
234 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=11");
235 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "11");
+ - + - +
- ]
236 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "11");
+ - + - +
- ]
237 : :
238 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=eleven");
239 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "eleven");
+ - + - +
- ]
240 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
+ - + - +
- ]
241 [ + - + - : 2 : }
+ - - - ]
242 : :
243 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(intarg)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
244 : : {
245 : 1 : ArgsManager local_args;
246 : :
247 : 1 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
248 : 1 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
249 [ + - + + : 3 : SetupArgs(local_args, {foo, bar});
- - ]
250 : :
251 [ + - + - ]: 1 : ResetArgs(local_args, "");
252 [ + - + - : 2 : BOOST_CHECK(!local_args.GetArg<int64_t>("-foo").has_value());
+ - + - +
- ]
253 [ + - + - : 2 : BOOST_CHECK(!local_args.GetArg<uint8_t>("-bar").has_value());
+ - + - +
- ]
254 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 11);
+ - ]
255 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 0);
+ - ]
256 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-bar", uint8_t{222}), 222);
+ - + - ]
257 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-bar", uint8_t{0}), 0);
+ - + - ]
258 : :
259 [ + - + - ]: 1 : ResetArgs(local_args, "-foo -bar");
260 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<int64_t>("-foo"), 0);
+ - + - ]
261 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<uint8_t>("-bar"), 0);
+ - + - ]
262 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 0);
+ - ]
263 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-bar", uint8_t{222}), 0);
+ - + - ]
264 : :
265 : : // Check under-/overflow behavior.
266 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=-9223372036854775809 -bar=9223372036854775808");
267 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<int64_t>("-foo"), std::numeric_limits<int64_t>::min());
+ - + - ]
268 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<uint8_t>("-bar"), std::numeric_limits<uint8_t>::max());
+ - + - ]
269 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), std::numeric_limits<int64_t>::min());
+ - ]
270 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), std::numeric_limits<int64_t>::max());
+ - ]
271 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", uint8_t{0}), std::numeric_limits<uint8_t>::min());
+ - + - ]
272 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-bar", uint8_t{0}), std::numeric_limits<uint8_t>::max());
+ - + - ]
273 : :
274 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=11 -bar=12");
275 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<int64_t>("-foo"), 11);
+ - + - ]
276 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<uint8_t>("-bar"), 12);
+ - + - ]
277 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 11);
+ - ]
278 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-bar", uint8_t{11}), 12);
+ - + - ]
279 : :
280 [ + - + - ]: 1 : ResetArgs(local_args, "-foo=NaN -bar=NotANumber");
281 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<int64_t>("-foo"), 0);
+ - + - ]
282 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg<uint8_t>("-bar"), 0);
+ - + - ]
283 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 1), 0);
+ - ]
284 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-bar", uint8_t{11}), 0);
+ - + - ]
285 [ + - + - : 2 : }
+ - - - ]
286 : :
287 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(patharg)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
288 : : {
289 : 1 : ArgsManager local_args;
290 : :
291 : 1 : const auto dir = std::make_pair("-dir", ArgsManager::ALLOW_ANY);
292 [ + - + + : 2 : SetupArgs(local_args, {dir});
- - ]
293 [ + - + - ]: 1 : ResetArgs(local_args, "");
294 [ + - + - : 3 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), fs::path{});
+ - + - ]
295 : :
296 [ + - ]: 1 : const fs::path root_path{"/"};
297 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/");
298 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
+ - + - ]
299 : :
300 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/.");
301 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
+ - + - ]
302 : :
303 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/./");
304 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
+ - + - ]
305 : :
306 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/.//");
307 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
+ - + - ]
308 : :
309 : : #ifdef WIN32
310 : : const fs::path win_root_path{"C:\\"};
311 : : ResetArgs(local_args, "-dir=C:\\");
312 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
313 : :
314 : : ResetArgs(local_args, "-dir=C:/");
315 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
316 : :
317 : : ResetArgs(local_args, "-dir=C:\\\\");
318 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
319 : :
320 : : ResetArgs(local_args, "-dir=C:\\.");
321 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
322 : :
323 : : ResetArgs(local_args, "-dir=C:\\.\\");
324 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
325 : :
326 : : ResetArgs(local_args, "-dir=C:\\.\\\\");
327 : : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
328 : : #endif
329 : :
330 [ + - ]: 1 : const fs::path absolute_path{"/home/user/.bitcoin"};
331 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/home/user/.bitcoin");
332 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
333 : :
334 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/root/../home/user/.bitcoin");
335 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
336 : :
337 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/home/./user/.bitcoin");
338 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
339 : :
340 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/");
341 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
342 : :
343 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/home/user/.bitcoin//");
344 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
345 : :
346 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/.");
347 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
348 : :
349 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/./");
350 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
351 : :
352 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=/home/user/.bitcoin/.//");
353 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
+ - + - ]
354 : :
355 [ + - ]: 1 : const fs::path relative_path{"user/.bitcoin"};
356 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=user/.bitcoin");
357 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
358 : :
359 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=somewhere/../user/.bitcoin");
360 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
361 : :
362 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=user/./.bitcoin");
363 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
364 : :
365 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=user/.bitcoin/");
366 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
367 : :
368 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=user/.bitcoin//");
369 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
370 : :
371 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=user/.bitcoin/.");
372 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
373 : :
374 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=user/.bitcoin/./");
375 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
376 : :
377 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=user/.bitcoin/.//");
378 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+ - + - ]
379 : :
380 : : // Check negated and default argument handling. Specifying an empty argument
381 : : // is the same as not specifying the argument. This is convenient for
382 : : // scripting so later command line arguments can override earlier command
383 : : // line arguments or bitcoin.conf values. Currently the -dir= case cannot be
384 : : // distinguished from -dir case with no assignment, but #16545 would add the
385 : : // ability to distinguish these in the future (and treat the no-assign case
386 : : // like an imperative command or an error).
387 [ + - + - ]: 1 : ResetArgs(local_args, "");
388 [ + - + - : 3 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
+ - + - +
- + - ]
389 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=override");
390 [ + - + - : 3 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"override"});
+ - + - +
- + - ]
391 [ + - + - ]: 1 : ResetArgs(local_args, "-dir=");
392 [ + - + - : 3 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
+ - + - +
- + - ]
393 [ + - + - ]: 1 : ResetArgs(local_args, "-dir");
394 [ + - + - : 3 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
+ - + - +
- + - ]
395 [ + - + - ]: 1 : ResetArgs(local_args, "-nodir");
396 [ + - + - : 4 : BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{""});
+ - + - +
- + - ]
397 [ + - + - ]: 4 : }
398 : :
399 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(doubledash)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
400 : : {
401 : 1 : ArgsManager local_args;
402 : :
403 : 1 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
404 : 1 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
405 [ + - + + : 3 : SetupArgs(local_args, {foo, bar});
- - ]
406 [ + - + - ]: 1 : ResetArgs(local_args, "--foo");
407 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetBoolArg("-foo", false), true);
+ - + - ]
408 : :
409 [ + - + - ]: 1 : ResetArgs(local_args, "--foo=verbose --bar=1");
410 [ + - + - : 1 : BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "verbose");
+ - + - +
- ]
411 [ + - + - : 2 : BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), 1);
+ - ]
412 [ + - + - : 2 : }
+ - - - ]
413 : :
414 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(boolargno)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
415 : : {
416 : 1 : ArgsManager local_args;
417 : :
418 : 1 : const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
419 : 1 : const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
420 [ + - + + : 3 : SetupArgs(local_args, {foo, bar});
- - ]
421 [ + - + - ]: 1 : ResetArgs(local_args, "-nofoo");
422 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
423 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
424 : :
425 [ + - + - ]: 1 : ResetArgs(local_args, "-nofoo=1");
426 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
427 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
428 : :
429 [ + - + - ]: 1 : ResetArgs(local_args, "-nofoo=0");
430 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
431 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
432 : :
433 [ + - + - ]: 1 : ResetArgs(local_args, "-foo --nofoo"); // --nofoo should win
434 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
435 [ + - + - : 2 : BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ - + - +
- ]
436 : :
437 [ + - + - ]: 1 : ResetArgs(local_args, "-nofoo -foo"); // foo always wins:
438 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ - + - +
- ]
439 [ + - + - : 2 : BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ - + - ]
440 [ + - + - : 2 : }
+ - - - ]
441 : :
442 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(logargs)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
443 : : {
444 : 1 : ArgsManager local_args;
445 : :
446 : 1 : const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_ANY);
447 : 1 : const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_ANY);
448 : 1 : const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
449 : 1 : const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
450 [ + - + + : 5 : SetupArgs(local_args, {okaylog_bool, okaylog_negbool, okaylog, dontlog});
- - ]
451 [ + - + - ]: 1 : ResetArgs(local_args, "-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private42");
452 : :
453 : : // Everything logged to debug.log will also append to str
454 [ + - ]: 1 : std::string str;
455 [ + - + - ]: 1 : auto print_connection = LogInstance().PushBackCallback(
456 [ + - ]: 5 : [&str](const std::string& s) {
457 [ - + ]: 4 : str += s;
458 : : });
459 : :
460 : : // Log the arguments
461 [ + - ]: 1 : local_args.LogArgs();
462 : :
463 [ + - + - ]: 1 : LogInstance().DeleteCallback(print_connection);
464 : : // Check that what should appear does, and what shouldn't doesn't.
465 [ + - + - : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
+ - ]
466 [ + - + - : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
+ - ]
467 [ + - + - : 2 : BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
+ - ]
468 [ + - + - : 2 : BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
+ - ]
469 [ + - + - ]: 2 : BOOST_CHECK(str.find("private42") == std::string::npos);
470 [ + - + - : 2 : }
+ - + - +
- - - ]
471 : :
472 : : BOOST_AUTO_TEST_SUITE_END()
|