11#include <boost/test/unit_test.hpp>
29inline std::ostream&
operator<<(std::ostream& os,
const std::pair<std::string, util::SettingsValue>& kv)
48 fs::path path = m_args.GetDataDirBase() /
"settings.json";
57 std::map<std::string, util::SettingsValue> expected{
65 std::map<std::string, util::SettingsValue>
values;
66 std::vector<std::string> errors;
68 BOOST_CHECK_EQUAL_COLLECTIONS(
values.begin(),
values.end(), expected.begin(), expected.end());
84 BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end());
89 std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s",
fs::PathToString(path))};
90 BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), non_kv.end());
96 BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end());
104 for (
const auto& item :
GetSettingsList(settings,
"section",
"name",
false)) {
117 settings.
ro_config[
"section"][
"name"].push_back(2);
120 CheckValues(settings, R
"("val2")", R"(["val1","val2",2])");
123 settings2.ro_config["section"][
"name"].push_back(
"val2");
124 settings2.
ro_config[
"section"][
"name"].push_back(
"val3");
127 CheckValues(settings2, R
"("val2")", R"(["val2","val3"])");
155 template <
typename Fn>
163 for (
bool force_set : {
false,
true}) {
164 for (
bool ignore_default_section_config : {
false,
true}) {
165 fn(arg_actions, conf_actions, force_set, ignore_default_section_config);
180 FILE* out_file =
nullptr;
181 if (
const char* out_path = getenv(
"SETTINGS_MERGE_TEST_OUT")) {
183 if (!out_file)
throw std::system_error(errno, std::generic_category(),
"fopen failed");
187 ForEachMergeSetup([&](
const ActionList& arg_actions,
const ActionList& conf_actions,
bool force_set,
188 bool ignore_default_section_config) {
190 int value_suffix = 0;
193 const std::string&
name = ignore_default_section_config ?
"wallet" :
"server";
194 auto push_values = [&](Action action,
const char* value_prefix,
const std::string& name_prefix,
195 std::vector<util::SettingsValue>& dest) {
196 if (action == SET || action == SECTION_SET) {
197 for (
int i = 0; i < 2; ++i) {
198 dest.push_back(value_prefix +
ToString(++value_suffix));
199 desc +=
" " + name_prefix +
name +
"=" + dest.back().get_str();
201 }
else if (action == NEGATE || action == SECTION_NEGATE) {
202 dest.push_back(
false);
203 desc +=
" " + name_prefix +
"no" +
name;
209 desc +=
" " +
name +
"=forced";
211 for (Action arg_action : arg_actions) {
214 for (Action conf_action : conf_actions) {
215 bool use_section = conf_action == SECTION_SET || conf_action == SECTION_NEGATE;
216 push_values(conf_action,
"c", use_section ? network +
"." :
"",
223 for (
const auto& s :
GetSettingsList(settings, network,
name, ignore_default_section_config)) {
233 BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
238 if (fclose(out_file))
throw std::system_error(errno, std::generic_category(),
"fclose failed");
244 std::string out_sha_hex =
HexStr(out_sha_bytes);
255 BOOST_CHECK_EQUAL(out_sha_hex,
"79db02d74e3e193196541b67c068b40ebd0c124a24b3ecbe9cbf7e85b1c4ba7a");
static const std::string MAIN
Chain name strings.
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
void Finalize(Span< unsigned char > output)
CHash256 & Write(Span< const unsigned char > input)
static const size_t OUTPUT_SIZE
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
void __pushKV(const std::string &key, const UniValue &val)
bool push_back(const UniValue &val)
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
BOOST_AUTO_TEST_SUITE_END()
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config)
Get combined setting value similar to GetSetting(), except if setting was specified multiple times,...
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string §ion, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
SettingsValue GetSetting(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config, bool get_chain_name)
Get settings value from combined sources: forced settings, command line arguments,...
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
bool operator==(const util::SettingsValue &a, const util::SettingsValue &b)
BOOST_AUTO_TEST_CASE(ReadWrite)
void WriteText(const fs::path &path, const std::string &text)
BOOST_FIXTURE_TEST_CASE(Merge, MergeTestingSetup)
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.
std::ostream & operator<<(std::ostream &os, const util::SettingsValue &value)
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
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.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
void ForEachMergeSetup(Fn &&fn)
Enumerate all possible test configurations.
Action[MAX_ACTIONS] ActionList
static constexpr int MAX_ACTIONS
Max number of actions to sequence together.
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
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.
std::map< std::string, std::vector< SettingsValue > > command_line_options
Map of setting name to list of command line values.