Bitcoin Core  22.99.0
P2P Digital Currency
settings_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <util/settings.h>
6 
7 #include <fs.h>
9 #include <test/util/str.h>
10 
11 
12 #include <boost/test/unit_test.hpp>
13 #include <univalue.h>
14 #include <util/strencodings.h>
15 #include <util/string.h>
16 #include <util/system.h>
17 
18 #include <fstream>
19 #include <map>
20 #include <string>
21 #include <system_error>
22 #include <vector>
23 
24 inline bool operator==(const util::SettingsValue& a, const util::SettingsValue& b)
25 {
26  return a.write() == b.write();
27 }
28 
29 inline std::ostream& operator<<(std::ostream& os, const util::SettingsValue& value)
30 {
31  os << value.write();
32  return os;
33 }
34 
35 inline std::ostream& operator<<(std::ostream& os, const std::pair<std::string, util::SettingsValue>& kv)
36 {
38  out.__pushKV(kv.first, kv.second);
39  os << out.write();
40  return os;
41 }
42 
43 inline void WriteText(const fs::path& path, const std::string& text)
44 {
45  std::ofstream file;
46  file.open(path);
47  file << text;
48 }
49 
51 
53 {
54  fs::path path = m_args.GetDataDirBase() / "settings.json";
55 
56  WriteText(path, R"({
57  "string": "string",
58  "num": 5,
59  "bool": true,
60  "null": null
61  })");
62 
63  std::map<std::string, util::SettingsValue> expected{
64  {"string", "string"},
65  {"num", 5},
66  {"bool", true},
67  {"null", {}},
68  };
69 
70  // Check file read.
71  std::map<std::string, util::SettingsValue> values;
72  std::vector<std::string> errors;
73  BOOST_CHECK(util::ReadSettings(path, values, errors));
74  BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end());
75  BOOST_CHECK(errors.empty());
76 
77  // Check no errors if file doesn't exist.
78  fs::remove(path);
79  BOOST_CHECK(util::ReadSettings(path, values, errors));
80  BOOST_CHECK(values.empty());
81  BOOST_CHECK(errors.empty());
82 
83  // Check duplicate keys not allowed
84  WriteText(path, R"({
85  "dupe": "string",
86  "dupe": "dupe"
87  })");
88  BOOST_CHECK(!util::ReadSettings(path, values, errors));
89  std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", fs::PathToString(path))};
90  BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end());
91 
92  // Check non-kv json files not allowed
93  WriteText(path, R"("non-kv")");
94  BOOST_CHECK(!util::ReadSettings(path, values, errors));
95  std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s", fs::PathToString(path))};
96  BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), non_kv.end());
97 
98  // Check invalid json not allowed
99  WriteText(path, R"(invalid json)");
100  BOOST_CHECK(!util::ReadSettings(path, values, errors));
101  std::vector<std::string> fail_parse = {strprintf("Unable to parse settings file %s", fs::PathToString(path))};
102  BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end());
103 }
104 
106 static void CheckValues(const util::Settings& settings, const std::string& single_val, const std::string& list_val)
107 {
108  util::SettingsValue single_value = GetSetting(settings, "section", "name", false, false);
110  for (const auto& item : GetSettingsList(settings, "section", "name", false)) {
111  list_value.push_back(item);
112  }
113  BOOST_CHECK_EQUAL(single_value.write().c_str(), single_val);
114  BOOST_CHECK_EQUAL(list_value.write().c_str(), list_val);
115 };
116 
117 // Simple settings merge test case.
119 {
120  util::Settings settings;
121  settings.command_line_options["name"].push_back("val1");
122  settings.command_line_options["name"].push_back("val2");
123  settings.ro_config["section"]["name"].push_back(2);
124 
125  // The last given arg takes precedence when specified via commandline.
126  CheckValues(settings, R"("val2")", R"(["val1","val2",2])");
127 
128  util::Settings settings2;
129  settings2.ro_config["section"]["name"].push_back("val2");
130  settings2.ro_config["section"]["name"].push_back("val3");
131 
132  // The first given arg takes precedence when specified via config file.
133  CheckValues(settings2, R"("val2")", R"(["val2","val3"])");
134 }
135 
136 // Confirm that a high priority setting overrides a lower priority setting even
137 // if the high priority setting is null. This behavior is useful for a high
138 // priority setting source to be able to effectively reset any setting back to
139 // its default value.
140 BOOST_AUTO_TEST_CASE(NullOverride)
141 {
142  util::Settings settings;
143  settings.command_line_options["name"].push_back("value");
144  BOOST_CHECK_EQUAL(R"("value")", GetSetting(settings, "section", "name", false, false).write().c_str());
145  settings.forced_settings["name"] = {};
146  BOOST_CHECK_EQUAL(R"(null)", GetSetting(settings, "section", "name", false, false).write().c_str());
147 }
148 
149 // Test different ways settings can be merged, and verify results. This test can
150 // be used to confirm that updates to settings code don't change behavior
151 // unintentionally.
155  static constexpr int MAX_ACTIONS = 3;
156 
159 
161  template <typename Fn>
162  void ForEachMergeSetup(Fn&& fn)
163  {
164  ActionList arg_actions = {};
165  // command_line_options do not have sections. Only iterate over SET and NEGATE
166  ForEachNoDup(arg_actions, SET, NEGATE, [&]{
167  ActionList conf_actions = {};
168  ForEachNoDup(conf_actions, SET, SECTION_NEGATE, [&]{
169  for (bool force_set : {false, true}) {
170  for (bool ignore_default_section_config : {false, true}) {
171  fn(arg_actions, conf_actions, force_set, ignore_default_section_config);
172  }
173  }
174  });
175  });
176  }
177 };
178 
179 // Regression test covering different ways config settings can be merged. The
180 // test parses and merges settings, representing the results as strings that get
181 // compared against an expected hash. To debug, the result strings can be dumped
182 // to a file (see comments below).
184 {
185  CHash256 out_sha;
186  FILE* out_file = nullptr;
187  if (const char* out_path = getenv("SETTINGS_MERGE_TEST_OUT")) {
188  out_file = fsbridge::fopen(out_path, "w");
189  if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");
190  }
191 
192  const std::string& network = CBaseChainParams::MAIN;
193  ForEachMergeSetup([&](const ActionList& arg_actions, const ActionList& conf_actions, bool force_set,
194  bool ignore_default_section_config) {
195  std::string desc;
196  int value_suffix = 0;
197  util::Settings settings;
198 
199  const std::string& name = ignore_default_section_config ? "wallet" : "server";
200  auto push_values = [&](Action action, const char* value_prefix, const std::string& name_prefix,
201  std::vector<util::SettingsValue>& dest) {
202  if (action == SET || action == SECTION_SET) {
203  for (int i = 0; i < 2; ++i) {
204  dest.push_back(value_prefix + ToString(++value_suffix));
205  desc += " " + name_prefix + name + "=" + dest.back().get_str();
206  }
207  } else if (action == NEGATE || action == SECTION_NEGATE) {
208  dest.push_back(false);
209  desc += " " + name_prefix + "no" + name;
210  }
211  };
212 
213  if (force_set) {
214  settings.forced_settings[name] = "forced";
215  desc += " " + name + "=forced";
216  }
217  for (Action arg_action : arg_actions) {
218  push_values(arg_action, "a", "-", settings.command_line_options[name]);
219  }
220  for (Action conf_action : conf_actions) {
221  bool use_section = conf_action == SECTION_SET || conf_action == SECTION_NEGATE;
222  push_values(conf_action, "c", use_section ? network + "." : "",
223  settings.ro_config[use_section ? network : ""][name]);
224  }
225 
226  desc += " || ";
227  desc += GetSetting(settings, network, name, ignore_default_section_config, /* get_chain_name= */ false).write();
228  desc += " |";
229  for (const auto& s : GetSettingsList(settings, network, name, ignore_default_section_config)) {
230  desc += " ";
231  desc += s.write();
232  }
233  desc += " |";
234  if (OnlyHasDefaultSectionSetting(settings, network, name)) desc += " ignored";
235  desc += "\n";
236 
237  out_sha.Write(MakeUCharSpan(desc));
238  if (out_file) {
239  BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
240  }
241  });
242 
243  if (out_file) {
244  if (fclose(out_file)) throw std::system_error(errno, std::generic_category(), "fclose failed");
245  out_file = nullptr;
246  }
247 
248  unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
249  out_sha.Finalize(out_sha_bytes);
250  std::string out_sha_hex = HexStr(out_sha_bytes);
251 
252  // If check below fails, should manually dump the results with:
253  //
254  // SETTINGS_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=settings_tests/Merge
255  //
256  // And verify diff against previous results to make sure the changes are expected.
257  //
258  // Results file is formatted like:
259  //
260  // <input> || GetSetting() | GetSettingsList() | OnlyHasDefaultSectionSetting()
261  BOOST_CHECK_EQUAL(out_sha_hex, "79db02d74e3e193196541b67c068b40ebd0c124a24b3ecbe9cbf7e85b1c4ba7a");
262 }
263 
ForEachNoDup
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
MergeTestingSetup::SET
@ SET
Definition: settings_tests.cpp:157
str.h
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:19
util::Settings
Stored settings.
Definition: settings.h:31
fs.h
CHash256::Write
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:27
setup_common.h
CheckValues
static void CheckValues(const util::Settings &settings, const std::string &single_val, const std::string &list_val)
Check settings struct contents against expected json strings.
Definition: settings_tests.cpp:106
util::Settings::forced_settings
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:33
string.h
operator==
bool operator==(const util::SettingsValue &a, const util::SettingsValue &b)
Definition: settings_tests.cpp:24
operator<<
std::ostream & operator<<(std::ostream &os, const util::SettingsValue &value)
Definition: settings_tests.cpp:29
Merge
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
Definition: signingprovider.cpp:67
util::Settings::ro_config
std::map< std::string, std::map< std::string, std::vector< SettingsValue > > > ro_config
Map of config section name and setting name to list of config file values.
Definition: settings.h:39
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:112
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:28
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()
MergeTestingSetup
Definition: settings_tests.cpp:152
UniValue
Definition: univalue.h:17
util::ReadSettings
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:64
strencodings.h
settings.h
MergeTestingSetup::SECTION_SET
@ SECTION_SET
Definition: settings_tests.cpp:157
values
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Definition: scriptnum_tests.cpp:17
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
MergeTestingSetup::ForEachMergeSetup
void ForEachMergeSetup(Fn &&fn)
Enumerate all possible test configurations.
Definition: settings_tests.cpp:162
univalue.h
MergeTestingSetup::NEGATE
@ NEGATE
Definition: settings_tests.cpp:157
MergeTestingSetup::END
@ END
Definition: settings_tests.cpp:157
UniValue::__pushKV
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:127
CHash256::Finalize
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
MergeTestingSetup::MAX_ACTIONS
static constexpr int MAX_ACTIONS
Max number of actions to sequence together.
Definition: settings_tests.cpp:155
MergeTestingSetup::ActionList
Action[MAX_ACTIONS] ActionList
Definition: settings_tests.cpp:158
CBaseChainParams::MAIN
static const std::string MAIN
Chain name strings.
Definition: chainparamsbase.h:22
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(Merge, MergeTestingSetup)
Definition: settings_tests.cpp:183
name
const char * name
Definition: rest.cpp:52
CSHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
util::OnlyHasDefaultSectionSetting
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string &section, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
Definition: settings.cpp:224
util::GetSetting
SettingsValue GetSetting(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config, bool get_chain_name)
Get settings value from combined sources: forced settings, command line arguments,...
Definition: settings.cpp:126
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
CHash256
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:24
MergeTestingSetup::Action
Action
Definition: settings_tests.cpp:157
util::Settings::command_line_options
std::map< std::string, std::vector< SettingsValue > > command_line_options
Map of setting name to list of command line values.
Definition: settings.h:35
util::GetSettingsList
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config)
Get combined setting value similar to GetSetting(), except if setting was specified multiple times,...
Definition: settings.cpp:179
UniValue::VARR
@ VARR
Definition: univalue.h:19
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:511
WriteText
void WriteText(const fs::path &path, const std::string &text)
Definition: settings_tests.cpp:43
MergeTestingSetup::SECTION_NEGATE
@ SECTION_NEGATE
Definition: settings_tests.cpp:157
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(ReadWrite)
Definition: settings_tests.cpp:52