Skip to content

Commit f6bb11f

Browse files
committed
Add test for ArgsManager::GetChainName
There was some test coverage previously, but it was limited and didn't test conflicting and negated arguments.
1 parent 4b33115 commit f6bb11f

File tree

1 file changed

+106
-4
lines changed

1 file changed

+106
-4
lines changed

src/test/util_tests.cpp

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ BOOST_AUTO_TEST_CASE(util_GetChainName)
601601
// outside a network section, and non-network specific settings like "-server"
602602
// that aren't sensitive to the network.
603603
//
604-
struct SettingsMergeTestingSetup : public BasicTestingSetup {
604+
struct ArgsMergeTestingSetup : public BasicTestingSetup {
605605
//! Max number of actions to sequence together. Can decrease this when
606606
//! debugging to make test results easier to understand.
607607
static constexpr int MAX_ACTIONS = 3;
@@ -661,11 +661,11 @@ struct SettingsMergeTestingSetup : public BasicTestingSetup {
661661
// test parses and merges settings, representing the results as strings that get
662662
// compared against an expected hash. To debug, the result strings can be dumped
663663
// to a file (see comments below).
664-
BOOST_FIXTURE_TEST_CASE(util_SettingsMerge, SettingsMergeTestingSetup)
664+
BOOST_FIXTURE_TEST_CASE(util_ArgsMerge, ArgsMergeTestingSetup)
665665
{
666666
CHash256 out_sha;
667667
FILE* out_file = nullptr;
668-
if (const char* out_path = getenv("SETTINGS_MERGE_TEST_OUT")) {
668+
if (const char* out_path = getenv("ARGS_MERGE_TEST_OUT")) {
669669
out_file = fsbridge::fopen(out_path, "w");
670670
if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");
671671
}
@@ -767,7 +767,7 @@ BOOST_FIXTURE_TEST_CASE(util_SettingsMerge, SettingsMergeTestingSetup)
767767

768768
// If check below fails, should manually dump the results with:
769769
//
770-
// SETTINGS_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=util_tests/util_SettingsMerge
770+
// ARGS_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=util_tests/util_ArgsMerge
771771
//
772772
// And verify diff against previous results to make sure the changes are expected.
773773
//
@@ -777,6 +777,108 @@ BOOST_FIXTURE_TEST_CASE(util_SettingsMerge, SettingsMergeTestingSetup)
777777
BOOST_CHECK_EQUAL(out_sha_hex, "b835eef5977d69114eb039a976201f8c7121f34fe2b7ea2b73cafb516e5c9dc8");
778778
}
779779

780+
// Similar test as above, but for ArgsManager::GetChainName function.
781+
struct ChainMergeTestingSetup : public BasicTestingSetup {
782+
static constexpr int MAX_ACTIONS = 2;
783+
784+
enum Action { NONE, ENABLE_TEST, DISABLE_TEST, NEGATE_TEST, ENABLE_REG, DISABLE_REG, NEGATE_REG };
785+
using ActionList = Action[MAX_ACTIONS];
786+
787+
//! Enumerate all possible test configurations.
788+
template <typename Fn>
789+
void ForEachMergeSetup(Fn&& fn)
790+
{
791+
ActionList arg_actions = {};
792+
ForEachNoDup(arg_actions, ENABLE_TEST, NEGATE_REG, [&] {
793+
ActionList conf_actions = {};
794+
ForEachNoDup(conf_actions, ENABLE_TEST, NEGATE_REG, [&] { fn(arg_actions, conf_actions); });
795+
});
796+
}
797+
};
798+
799+
BOOST_FIXTURE_TEST_CASE(util_ChainMerge, ChainMergeTestingSetup)
800+
{
801+
CHash256 out_sha;
802+
FILE* out_file = nullptr;
803+
if (const char* out_path = getenv("CHAIN_MERGE_TEST_OUT")) {
804+
out_file = fsbridge::fopen(out_path, "w");
805+
if (!out_file) throw std::system_error(errno, std::generic_category(), "fopen failed");
806+
}
807+
808+
ForEachMergeSetup([&](const ActionList& arg_actions, const ActionList& conf_actions) {
809+
TestArgsManager parser;
810+
LOCK(parser.cs_args);
811+
parser.AddArg("-regtest", "regtest", false, OptionsCategory::OPTIONS);
812+
parser.AddArg("-testnet", "testnet", false, OptionsCategory::OPTIONS);
813+
814+
auto arg = [](Action action) { return action == ENABLE_TEST ? "-testnet=1" :
815+
action == DISABLE_TEST ? "-testnet=0" :
816+
action == NEGATE_TEST ? "-notestnet=1" :
817+
action == ENABLE_REG ? "-regtest=1" :
818+
action == DISABLE_REG ? "-regtest=0" :
819+
action == NEGATE_REG ? "-noregtest=1" : nullptr; };
820+
821+
std::string desc;
822+
std::vector<const char*> argv = {"ignored"};
823+
for (Action action : arg_actions) {
824+
const char* argstr = arg(action);
825+
if (!argstr) break;
826+
argv.push_back(argstr);
827+
desc += " ";
828+
desc += argv.back();
829+
}
830+
std::string error;
831+
BOOST_CHECK(parser.ParseParameters(argv.size(), argv.data(), error));
832+
BOOST_CHECK_EQUAL(error, "");
833+
834+
std::string conf;
835+
for (Action action : conf_actions) {
836+
const char* argstr = arg(action);
837+
if (!argstr) break;
838+
desc += " ";
839+
desc += argstr + 1;
840+
conf += argstr + 1;
841+
}
842+
std::istringstream conf_stream(conf);
843+
BOOST_CHECK(parser.ReadConfigStream(conf_stream, "filepath", error));
844+
BOOST_CHECK_EQUAL(error, "");
845+
846+
desc += " || ";
847+
try {
848+
desc += parser.GetChainName();
849+
} catch (const std::runtime_error& e) {
850+
desc += "error: ";
851+
desc += e.what();
852+
}
853+
desc += "\n";
854+
855+
out_sha.Write((const unsigned char*)desc.data(), desc.size());
856+
if (out_file) {
857+
BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
858+
}
859+
});
860+
861+
if (out_file) {
862+
if (fclose(out_file)) throw std::system_error(errno, std::generic_category(), "fclose failed");
863+
out_file = nullptr;
864+
}
865+
866+
unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
867+
out_sha.Finalize(out_sha_bytes);
868+
std::string out_sha_hex = HexStr(std::begin(out_sha_bytes), std::end(out_sha_bytes));
869+
870+
// If check below fails, should manually dump the results with:
871+
//
872+
// CHAIN_MERGE_TEST_OUT=results.txt ./test_bitcoin --run_test=util_tests/util_ChainMerge
873+
//
874+
// And verify diff against previous results to make sure the changes are expected.
875+
//
876+
// Results file is formatted like:
877+
//
878+
// <input> || <output>
879+
BOOST_CHECK_EQUAL(out_sha_hex, "b284f4b4a15dd6bf8c06213a69a004b1960388e1d9917173927db52ac220927f");
880+
}
881+
780882
BOOST_AUTO_TEST_CASE(util_FormatMoney)
781883
{
782884
BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");

0 commit comments

Comments
 (0)