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