Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1002// Similar test as above, but for ArgsManager::GetChainTypeString function.1003struct ChainMergeTestingSetup : public BasicTestingSetup {1004static constexpr int MAX_ACTIONS = 2;1006enum Action { NONE, ENABLE_TEST, DISABLE_TEST, NEGATE_TEST, ENABLE_REG, DISABLE_REG, NEGATE_REG };1007using ActionList = Action[MAX_ACTIONS];1009//! Enumerate all possible test configurations.1010template <typename Fn>1011void ForEachMergeSetup(Fn&& fn)1012{1013ActionList arg_actions = {};1014ForEachNoDup(arg_actions, ENABLE_TEST, NEGATE_REG, [&] {1015ActionList conf_actions = {};1016ForEachNoDup(conf_actions, ENABLE_TEST, NEGATE_REG, [&] { fn(arg_actions, conf_actions); });1017});1018}1019};1021BOOST_FIXTURE_TEST_CASE(util_ChainMerge, ChainMergeTestingSetup)1022{1023CHash256 out_sha;1024FILE* out_file = nullptr;1025if (const char* out_path = getenv("CHAIN_MERGE_TEST_OUT")) {1026out_file = fsbridge::fopen(out_path, "w");1027if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");1028}1030ForEachMergeSetup([&](const ActionList& arg_actions, const ActionList& conf_actions) {1031TestArgsManager parser;1032parser.AddArg("-regtest", "regtest", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);1033parser.AddArg("-testnet4", "testnet4", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);1035auto arg = [](Action action) { return action == ENABLE_TEST ? "-testnet4=1" :1036action == DISABLE_TEST ? "-testnet4=0" :1037action == NEGATE_TEST ? "-notestnet4=1" :1038action == ENABLE_REG ? "-regtest=1" :1039action == DISABLE_REG ? "-regtest=0" :1040action == NEGATE_REG ? "-noregtest=1" : nullptr; };1042std::string desc;1043std::vector<const char*> argv = {"ignored"};1044for (Action action : arg_actions) {1045const char* argstr = arg(action);1046if (!argstr) break;1047argv.push_back(argstr);1048desc += " ";1049desc += argv.back();1050}1051std::string error;1052BOOST_CHECK(parser.ParseParameters(argv.size(), argv.data(), error));1053BOOST_CHECK_EQUAL(error, "");1055std::string conf;1056for (Action action : conf_actions) {1057const char* argstr = arg(action);1058if (!argstr) break;1059desc += " ";1060desc += argstr + 1;1061conf += argstr + 1;1062conf += "\n";1063}1064std::istringstream conf_stream(conf);1065BOOST_CHECK(parser.ReadConfigStream(conf_stream, "filepath", error));1066BOOST_CHECK_EQUAL(error, "");1068desc += " || ";1069try {1070desc += parser.GetChainTypeString();1071} catch (const std::runtime_error& e) {1072desc += "error: ";1073desc += e.what();1074}1075desc += "\n";1077out_sha.Write(MakeUCharSpan(desc));1078if (out_file) {1079BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());1080}1081});1083if (out_file) {1084if (fclose(out_file)) throw std::system_error(errno, std::generic_category(), "fclose failed");1085out_file = nullptr;1086}1088unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];1089out_sha.Finalize(out_sha_bytes);1090std::string out_sha_hex = HexStr(out_sha_bytes);1092// If check below fails, should manually dump the results with:1093//1094// CHAIN_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=argsman_tests/util_ChainMerge1095//1096// And verify diff against previous results to make sure the changes are expected.1097//1098// Results file is formatted like:1099//1100// <input> || <output>1101BOOST_CHECK_EQUAL(out_sha_hex, "c0e33aab0c74e040ddcee9edad59e8148d8e1cacb3cccd9ea1a1f485cb6bad21");1102}1104BOOST_AUTO_TEST_CASE(util_ReadWriteSettings)1105{1106// Test writing setting.1107TestArgsManager args1;1108args1.ForceSetArg("-datadir", fs::PathToString(m_path_root));1109args1.LockSettings([&](common::Settings& s) { s.rw_settings["name"] = "value"; });1110args1.WriteSettingsFile();1112// Test reading setting.1113TestArgsManager args2;1114args2.ForceSetArg("-datadir", fs::PathToString(m_path_root));1115args2.ReadSettingsFile();1116args2.LockSettings([&](common::Settings& s) { BOOST_CHECK_EQUAL(s.rw_settings["name"].get_str(), "value"); });1118// Test error logging, and remove previously written setting.1119{1120ASSERT_DEBUG_LOG("Failed renaming settings file");1121fs::remove(args1.GetDataDirBase() / "settings.json");1122fs::create_directory(args1.GetDataDirBase() / "settings.json");1123args2.WriteSettingsFile();1124fs::remove(args1.GetDataDirBase() / "settings.json");1125}1126}1128BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.