Skip to content

Commit eb682c5

Browse files
committed
util: Add ReadSettings and WriteSettings functions
Currently unused, but includes tests.
1 parent a426317 commit eb682c5

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

src/test/settings_tests.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,90 @@
1212
#include <univalue.h>
1313
#include <util/strencodings.h>
1414
#include <util/string.h>
15+
#include <util/system.h>
1516
#include <vector>
1617

18+
inline bool operator==(const util::SettingsValue& a, const util::SettingsValue& b)
19+
{
20+
return a.write() == b.write();
21+
}
22+
23+
inline std::ostream& operator<<(std::ostream& os, const util::SettingsValue& value)
24+
{
25+
os << value.write();
26+
return os;
27+
}
28+
29+
inline std::ostream& operator<<(std::ostream& os, const std::pair<std::string, util::SettingsValue>& kv)
30+
{
31+
util::SettingsValue out(util::SettingsValue::VOBJ);
32+
out.__pushKV(kv.first, kv.second);
33+
os << out.write();
34+
return os;
35+
}
36+
37+
inline void WriteText(const fs::path& path, const std::string& text)
38+
{
39+
fsbridge::ofstream file;
40+
file.open(path);
41+
file << text;
42+
}
43+
1744
BOOST_FIXTURE_TEST_SUITE(settings_tests, BasicTestingSetup)
1845

46+
BOOST_AUTO_TEST_CASE(ReadWrite)
47+
{
48+
fs::path path = GetDataDir() / "settings.json";
49+
50+
WriteText(path, R"({
51+
"string": "string",
52+
"num": 5,
53+
"bool": true,
54+
"null": null
55+
})");
56+
57+
std::map<std::string, util::SettingsValue> expected{
58+
{"string", "string"},
59+
{"num", 5},
60+
{"bool", true},
61+
{"null", {}},
62+
};
63+
64+
// Check file read.
65+
std::map<std::string, util::SettingsValue> values;
66+
std::vector<std::string> errors;
67+
BOOST_CHECK(util::ReadSettings(path, values, errors));
68+
BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end());
69+
BOOST_CHECK(errors.empty());
70+
71+
// Check no errors if file doesn't exist.
72+
fs::remove(path);
73+
BOOST_CHECK(util::ReadSettings(path, values, errors));
74+
BOOST_CHECK(values.empty());
75+
BOOST_CHECK(errors.empty());
76+
77+
// Check duplicate keys not allowed
78+
WriteText(path, R"({
79+
"dupe": "string",
80+
"dupe": "dupe"
81+
})");
82+
BOOST_CHECK(!util::ReadSettings(path, values, errors));
83+
std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", path.string())};
84+
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end());
85+
86+
// Check non-kv json files not allowed
87+
WriteText(path, R"("non-kv")");
88+
BOOST_CHECK(!util::ReadSettings(path, values, errors));
89+
std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s", path.string())};
90+
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), non_kv.end());
91+
92+
// Check invalid json not allowed
93+
WriteText(path, R"(invalid json)");
94+
BOOST_CHECK(!util::ReadSettings(path, values, errors));
95+
std::vector<std::string> fail_parse = {strprintf("Unable to parse settings file %s", path.string())};
96+
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end());
97+
}
98+
1999
//! Check settings struct contents against expected json strings.
20100
static void CheckValues(const util::Settings& settings, const std::string& single_val, const std::string& list_val)
21101
{

src/util/settings.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <util/settings.h>
66

7+
#include <tinyformat.h>
78
#include <univalue.h>
89

910
namespace util {
@@ -49,6 +50,62 @@ static void MergeSettings(const Settings& settings, const std::string& section,
4950
}
5051
} // namespace
5152

53+
bool ReadSettings(const fs::path& path, std::map<std::string, SettingsValue>& values, std::vector<std::string>& errors)
54+
{
55+
values.clear();
56+
errors.clear();
57+
58+
fsbridge::ifstream file;
59+
file.open(path);
60+
if (!file.is_open()) return true; // Ok for file not to exist.
61+
62+
SettingsValue in;
63+
if (!in.read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
64+
errors.emplace_back(strprintf("Unable to parse settings file %s", path.string()));
65+
return false;
66+
}
67+
68+
if (file.fail()) {
69+
errors.emplace_back(strprintf("Failed reading settings file %s", path.string()));
70+
return false;
71+
}
72+
file.close(); // Done with file descriptor. Release while copying data.
73+
74+
if (!in.isObject()) {
75+
errors.emplace_back(strprintf("Found non-object value %s in settings file %s", in.write(), path.string()));
76+
return false;
77+
}
78+
79+
const std::vector<std::string>& in_keys = in.getKeys();
80+
const std::vector<SettingsValue>& in_values = in.getValues();
81+
for (size_t i = 0; i < in_keys.size(); ++i) {
82+
auto inserted = values.emplace(in_keys[i], in_values[i]);
83+
if (!inserted.second) {
84+
errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], path.string()));
85+
}
86+
}
87+
return errors.empty();
88+
}
89+
90+
bool WriteSettings(const fs::path& path,
91+
const std::map<std::string, SettingsValue>& values,
92+
std::vector<std::string>& errors)
93+
{
94+
SettingsValue out(SettingsValue::VOBJ);
95+
for (const auto& value : values) {
96+
out.__pushKV(value.first, value.second);
97+
}
98+
fsbridge::ofstream file;
99+
file.open(path);
100+
if (file.fail()) {
101+
errors.emplace_back(strprintf("Error: Unable to open settings file %s for writing", path.string()));
102+
return false;
103+
}
104+
file << out.write(/* prettyIndent= */ 1, /* indentLevel= */ 4) << std::endl;
105+
file.close();
106+
return true;
107+
}
108+
52109
SettingsValue GetSetting(const Settings& settings,
53110
const std::string& section,
54111
const std::string& name,

src/util/settings.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_UTIL_SETTINGS_H
66
#define BITCOIN_UTIL_SETTINGS_H
77

8+
#include <fs.h>
9+
810
#include <map>
911
#include <string>
1012
#include <vector>
@@ -35,6 +37,16 @@ struct Settings {
3537
std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
3638
};
3739

40+
//! Read settings file.
41+
bool ReadSettings(const fs::path& path,
42+
std::map<std::string, SettingsValue>& values,
43+
std::vector<std::string>& errors);
44+
45+
//! Write settings file.
46+
bool WriteSettings(const fs::path& path,
47+
const std::map<std::string, SettingsValue>& values,
48+
std::vector<std::string>& errors);
49+
3850
//! Get settings value from combined sources: forced settings, command line
3951
//! arguments and the read-only config file.
4052
//!

0 commit comments

Comments
 (0)