|
12 | 12 | #include <univalue.h>
|
13 | 13 | #include <util/strencodings.h>
|
14 | 14 | #include <util/string.h>
|
| 15 | +#include <util/system.h> |
15 | 16 | #include <vector>
|
16 | 17 |
|
| 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 | + |
17 | 44 | BOOST_FIXTURE_TEST_SUITE(settings_tests, BasicTestingSetup)
|
18 | 45 |
|
| 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 | + |
19 | 99 | //! Check settings struct contents against expected json strings.
|
20 | 100 | static void CheckValues(const util::Settings& settings, const std::string& single_val, const std::string& list_val)
|
21 | 101 | {
|
|
0 commit comments