Changeset View
Changeset View
Standalone View
Standalone View
src/test/settings_tests.cpp
// Copyright (c) 2011-2019 The Bitcoin Core developers | // Copyright (c) 2011-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <util/settings.h> | #include <util/settings.h> | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <test/util/str.h> | #include <test/util/str.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/system.h> | |||||
#include <univalue.h> | #include <univalue.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <vector> | #include <vector> | ||||
inline bool operator==(const util::SettingsValue &a, | |||||
const util::SettingsValue &b) { | |||||
return a.write() == b.write(); | |||||
} | |||||
inline std::ostream &operator<<(std::ostream &os, | |||||
const util::SettingsValue &value) { | |||||
os << value.write(); | |||||
return os; | |||||
} | |||||
inline std::ostream & | |||||
operator<<(std::ostream &os, | |||||
const std::pair<std::string, util::SettingsValue> &kv) { | |||||
util::SettingsValue out(util::SettingsValue::VOBJ); | |||||
out.__pushKV(kv.first, kv.second); | |||||
os << out.write(); | |||||
return os; | |||||
} | |||||
inline void WriteText(const fs::path &path, const std::string &text) { | |||||
fsbridge::ofstream file; | |||||
file.open(path); | |||||
file << text; | |||||
} | |||||
BOOST_FIXTURE_TEST_SUITE(settings_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(settings_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(ReadWrite) { | |||||
fs::path path = GetDataDir() / "settings.json"; | |||||
WriteText(path, R"({ | |||||
"string": "string", | |||||
"num": 5, | |||||
"bool": true, | |||||
"null": null | |||||
})"); | |||||
std::map<std::string, util::SettingsValue> expected{ | |||||
{"string", "string"}, | |||||
{"num", 5}, | |||||
{"bool", true}, | |||||
{"null", {}}, | |||||
}; | |||||
// Check file read. | |||||
std::map<std::string, util::SettingsValue> values; | |||||
std::vector<std::string> errors; | |||||
BOOST_CHECK(util::ReadSettings(path, values, errors)); | |||||
BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), | |||||
expected.begin(), expected.end()); | |||||
BOOST_CHECK(errors.empty()); | |||||
// Check no errors if file doesn't exist. | |||||
fs::remove(path); | |||||
BOOST_CHECK(util::ReadSettings(path, values, errors)); | |||||
BOOST_CHECK(values.empty()); | |||||
BOOST_CHECK(errors.empty()); | |||||
// Check duplicate keys not allowed | |||||
WriteText(path, R"({ | |||||
"dupe": "string", | |||||
"dupe": "dupe" | |||||
})"); | |||||
BOOST_CHECK(!util::ReadSettings(path, values, errors)); | |||||
std::vector<std::string> dup_keys = {strprintf( | |||||
"Found duplicate key dupe in settings file %s", path.string())}; | |||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), | |||||
dup_keys.begin(), dup_keys.end()); | |||||
// Check non-kv json files not allowed | |||||
WriteText(path, R"("non-kv")"); | |||||
BOOST_CHECK(!util::ReadSettings(path, values, errors)); | |||||
std::vector<std::string> non_kv = { | |||||
strprintf("Found non-object value \"non-kv\" in settings file %s", | |||||
path.string())}; | |||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), | |||||
non_kv.end()); | |||||
// Check invalid json not allowed | |||||
WriteText(path, R"(invalid json)"); | |||||
BOOST_CHECK(!util::ReadSettings(path, values, errors)); | |||||
std::vector<std::string> fail_parse = { | |||||
strprintf("Unable to parse settings file %s", path.string())}; | |||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), | |||||
fail_parse.begin(), fail_parse.end()); | |||||
} | |||||
//! Check settings struct contents against expected json strings. | //! Check settings struct contents against expected json strings. | ||||
static void CheckValues(const util::Settings &settings, | static void CheckValues(const util::Settings &settings, | ||||
const std::string &single_val, | const std::string &single_val, | ||||
const std::string &list_val) { | const std::string &list_val) { | ||||
util::SettingsValue single_value = | util::SettingsValue single_value = | ||||
GetSetting(settings, "section", "name", false, false); | GetSetting(settings, "section", "name", false, false); | ||||
util::SettingsValue list_value(util::SettingsValue::VARR); | util::SettingsValue list_value(util::SettingsValue::VARR); | ||||
for (const auto &item : | for (const auto &item : | ||||
▲ Show 20 Lines • Show All 182 Lines • Show Last 20 Lines |