Bitcoin Core  22.99.0
P2P Digital Currency
getarg_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 #include <util/strencodings.h>
7 #include <util/system.h>
8 
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 #include <boost/algorithm/string.hpp>
14 #include <boost/test/unit_test.hpp>
15 
16 namespace getarg_tests{
18  protected:
19  void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args);
20  void ResetArgs(const std::string& strArg);
22  };
23 }
24 
25 BOOST_FIXTURE_TEST_SUITE(getarg_tests, LocalTestingSetup)
26 
27 void LocalTestingSetup :: ResetArgs(const std::string& strArg)
28 {
29  std::vector<std::string> vecArg;
30  if (strArg.size())
31  boost::split(vecArg, strArg, IsSpace, boost::token_compress_on);
32 
33  // Insert dummy executable name:
34  vecArg.insert(vecArg.begin(), "testbitcoin");
35 
36  // Convert to char*:
37  std::vector<const char*> vecChar;
38  for (const std::string& s : vecArg)
39  vecChar.push_back(s.c_str());
40 
41  std::string error;
42  BOOST_CHECK(m_local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
43 }
44 
45 void LocalTestingSetup :: SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
46 {
47  m_local_args.ClearArgs();
48  for (const auto& arg : args) {
49  m_local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
50  }
51 }
52 
54 {
55  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
56  SetupArgs({foo});
57  ResetArgs("-foo");
58  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
59  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
60 
61  BOOST_CHECK(!m_local_args.GetBoolArg("-fo", false));
62  BOOST_CHECK(m_local_args.GetBoolArg("-fo", true));
63 
64  BOOST_CHECK(!m_local_args.GetBoolArg("-fooo", false));
65  BOOST_CHECK(m_local_args.GetBoolArg("-fooo", true));
66 
67  ResetArgs("-foo=0");
68  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
69  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
70 
71  ResetArgs("-foo=1");
72  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
73  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
74 
75  // New 0.6 feature: auto-map -nosomething to !-something:
76  ResetArgs("-nofoo");
77  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
78  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
79 
80  ResetArgs("-nofoo=1");
81  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
82  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
83 
84  ResetArgs("-foo -nofoo"); // -nofoo should win
85  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
86  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
87 
88  ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
89  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
90  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
91 
92  ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
93  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
94  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
95 
96  // New 0.6 feature: treat -- same as -:
97  ResetArgs("--foo=1");
98  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
99  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
100 
101  ResetArgs("--nofoo=1");
102  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
103  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
104 
105 }
106 
108 {
109  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
110  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
111  SetupArgs({foo, bar});
112  ResetArgs("");
113  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
114  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
115 
116  ResetArgs("-foo -bar");
117  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
118  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
119 
120  ResetArgs("-foo=");
121  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
122  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
123 
124  ResetArgs("-foo=11");
125  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "11");
126  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "11");
127 
128  ResetArgs("-foo=eleven");
129  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "eleven");
130  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
131 
132 }
133 
135 {
136  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
137  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
138  SetupArgs({foo, bar});
139  ResetArgs("");
140  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 11), 11);
141  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), 0);
142 
143  ResetArgs("-foo -bar");
144  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 11), 0);
145  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
146 
147  ResetArgs("-foo=11 -bar=12");
148  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), 11);
149  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 12);
150 
151  ResetArgs("-foo=NaN -bar=NotANumber");
152  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 1), 0);
153  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
154 }
155 
157 {
158  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
159  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
160  SetupArgs({foo, bar});
161  ResetArgs("--foo");
162  BOOST_CHECK_EQUAL(m_local_args.GetBoolArg("-foo", false), true);
163 
164  ResetArgs("--foo=verbose --bar=1");
165  BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "verbose");
166  BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 0), 1);
167 }
168 
170 {
171  const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
172  const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
173  SetupArgs({foo, bar});
174  ResetArgs("-nofoo");
175  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
176  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
177 
178  ResetArgs("-nofoo=1");
179  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
180  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
181 
182  ResetArgs("-nofoo=0");
183  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
184  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
185 
186  ResetArgs("-foo --nofoo"); // --nofoo should win
187  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
188  BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
189 
190  ResetArgs("-nofoo -foo"); // foo always wins:
191  BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
192  BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
193 }
194 
196 {
197  const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_BOOL);
198  const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_BOOL);
199  const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
200  const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
201  SetupArgs({okaylog_bool, okaylog_negbool, okaylog, dontlog});
202  ResetArgs("-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
203 
204  // Everything logged to debug.log will also append to str
205  std::string str;
206  auto print_connection = LogInstance().PushBackCallback(
207  [&str](const std::string& s) {
208  str += s;
209  });
210 
211  // Log the arguments
212  m_local_args.LogArgs();
213 
214  LogInstance().DeleteCallback(print_connection);
215  // Check that what should appear does, and what shouldn't doesn't.
216  BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
217  BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
218  BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
219  BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
220  BOOST_CHECK(str.find("private") == std::string::npos);
221 }
222 
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(boolarg)
Definition: getarg_tests.cpp:53
BCLog::Logger::DeleteCallback
void DeleteCallback(std::list< std::function< void(const std::string &)>>::iterator it)
Delete a connection.
Definition: logging.h:121
setup_common.h
ArgsManager::ALLOW_ANY
@ ALLOW_ANY
Definition: system.h:166
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:93
strencodings.h
getarg_tests::LocalTestingSetup::ResetArgs
void ResetArgs(const std::string &strArg)
Definition: getarg_tests.cpp:27
OptionsCategory::OPTIONS
@ OPTIONS
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
ArgsManager::SENSITIVE
@ SENSITIVE
Definition: system.h:175
system.h
ArgsManager
Definition: system.h:158
getarg_tests::LocalTestingSetup
Definition: getarg_tests.cpp:17
getarg_tests::LocalTestingSetup::m_local_args
ArgsManager m_local_args
Definition: getarg_tests.cpp:21
LogInstance
BCLog::Logger & LogInstance()
Definition: logging.cpp:17
BCLog::Logger::PushBackCallback
std::list< std::function< void(const std::string &)> >::iterator PushBackCallback(std::function< void(const std::string &)> fun)
Connect a slot to the print signal and return the connection.
Definition: logging.h:113
ArgsManager::ALLOW_BOOL
@ ALLOW_BOOL
Definition: system.h:163
getarg_tests
Definition: getarg_tests.cpp:16
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
getarg_tests::LocalTestingSetup::SetupArgs
void SetupArgs(const std::vector< std::pair< std::string, unsigned int >> &args)
Definition: getarg_tests.cpp:45
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18