| 52 | BOOST_FIXTURE_TEST_SUITE(settings_tests, BasicTestingSetup) |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(ReadWrite) |
| 55 | { |
| 56 | fs::path path = m_args.GetDataDirBase() / "settings.json"; |
| 57 | |
| 58 | WriteText(path, R"({ |
| 59 | "string": "string", |
| 60 | "num": 5, |
| 61 | "bool": true, |
| 62 | "null": null |
| 63 | })"); |
| 64 | |
| 65 | std::map<std::string, common::SettingsValue> expected{ |
| 66 | {"string", "string"}, |
| 67 | {"num", 5}, |
| 68 | {"bool", true}, |
| 69 | {"null", {}}, |
| 70 | }; |
| 71 | |
| 72 | // Check file read. |
| 73 | std::map<std::string, common::SettingsValue> values; |
| 74 | std::vector<std::string> errors; |
| 75 | BOOST_CHECK(common::ReadSettings(path, values, errors)); |
| 76 | BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end()); |
| 77 | BOOST_CHECK(errors.empty()); |
| 78 | |
| 79 | // Check no errors if file doesn't exist. |
| 80 | fs::remove(path); |
| 81 | BOOST_CHECK(common::ReadSettings(path, values, errors)); |
| 82 | BOOST_CHECK(values.empty()); |
| 83 | BOOST_CHECK(errors.empty()); |
| 84 | |
| 85 | // Check duplicate keys not allowed and that values returns empty if a duplicate is found. |
| 86 | WriteText(path, R"({ |
| 87 | "dupe": "string", |
| 88 | "dupe": "dupe" |
| 89 | })"); |
| 90 | BOOST_CHECK(!common::ReadSettings(path, values, errors)); |
| 91 | std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", fs::PathToString(path))}; |
| 92 | BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end()); |
| 93 | BOOST_CHECK(values.empty()); |
| 94 | |
| 95 | // Check non-kv json files not allowed |
| 96 | WriteText(path, R"("non-kv")"); |
| 97 | BOOST_CHECK(!common::ReadSettings(path, values, errors)); |
| 98 | std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s", fs::PathToString(path))}; |
| 99 | BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), non_kv.end()); |
| 100 | |
| 101 | // Check invalid json not allowed |
| 102 | WriteText(path, R"(invalid json)"); |
| 103 | BOOST_CHECK(!common::ReadSettings(path, values, errors)); |
| 104 | std::vector<std::string> fail_parse = {strprintf("Settings file %s does not contain valid JSON. This may be caused by a crash, power loss, full disk, or storage error, " |
| 105 | "and can be fixed by removing the file, which will reset settings to default values.", |
| 106 | fs::PathToString(path))}; |
| 107 | BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end()); |
| 108 | } |
| 109 | |
| 110 | //! Check settings struct contents against expected json strings. |
| 111 | static void CheckValues(const common::Settings& settings, const std::string& single_val, const std::string& list_val) |
nothing calls this directly
no test coverage detected