test: Add tests for GetArg methods / settings.json type coercion

Just add tests. No changes to application behavior. Tests will be
updated in the next commit changing & improving current behavior.

Include a Qt test for GUI startup crash reported by Rspigler in
https://github.com/bitcoin/bitcoin/issues/24457 caused by GetArg
behavior that happens if settings.json contains an integer value for any
of the configuration options which GUI settings can currently clash with
(-dbcache, -par, -spendzeroconfchange, -signer, -upnp, -natpmp, -listen,
-server, -proxy, -proxy, -onion, -onion, -lang, and -prune).
This commit is contained in:
Ryan Ofsky 2022-03-07 13:29:46 -05:00
parent c9ed9927bb
commit 84b0973e35
6 changed files with 179 additions and 0 deletions

View File

@ -12,6 +12,7 @@
<ClCompile Include="..\..\src\test\util\setup_common.cpp" />
<ClCompile Include="..\..\src\qt\test\addressbooktests.cpp" />
<ClCompile Include="..\..\src\qt\test\apptests.cpp" />
<ClCompile Include="..\..\src\qt\test\optiontests.cpp" />
<ClCompile Include="..\..\src\qt\test\rpcnestedtests.cpp" />
<ClCompile Include="..\..\src\qt\test\test_main.cpp" />
<ClCompile Include="..\..\src\qt\test\uritests.cpp" />
@ -20,6 +21,7 @@
<ClCompile Include="..\..\src\wallet\test\wallet_test_fixture.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_addressbooktests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_apptests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_optiontests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_rpcnestedtests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_uritests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_wallettests.cpp" />
@ -88,6 +90,7 @@
<ItemGroup>
<MocTestFiles Include="..\..\src\qt\test\addressbooktests.h" />
<MocTestFiles Include="..\..\src\qt\test\apptests.h" />
<MocTestFiles Include="..\..\src\qt\test\optiontests.h" />
<MocTestFiles Include="..\..\src\qt\test\rpcnestedtests.h" />
<MocTestFiles Include="..\..\src\qt\test\uritests.h" />
<MocTestFiles Include="..\..\src\qt\test\wallettests.h" />

View File

@ -7,6 +7,7 @@ TESTS += qt/test/test_bitcoin-qt
TEST_QT_MOC_CPP = \
qt/test/moc_apptests.cpp \
qt/test/moc_optiontests.cpp \
qt/test/moc_rpcnestedtests.cpp \
qt/test/moc_uritests.cpp
@ -19,6 +20,7 @@ endif # ENABLE_WALLET
TEST_QT_H = \
qt/test/addressbooktests.h \
qt/test/apptests.h \
qt/test/optiontests.h \
qt/test/rpcnestedtests.h \
qt/test/uritests.h \
qt/test/util.h \
@ -30,6 +32,7 @@ qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_
qt_test_test_bitcoin_qt_SOURCES = \
init/bitcoin-qt.cpp \
qt/test/apptests.cpp \
qt/test/optiontests.cpp \
qt/test/rpcnestedtests.cpp \
qt/test/test_main.cpp \
qt/test/uritests.cpp \

View File

@ -0,0 +1,31 @@
// Copyright (c) 2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qt/bitcoin.h>
#include <qt/test/optiontests.h>
#include <test/util/setup_common.h>
#include <util/system.h>
#include <QSettings>
#include <QTest>
#include <univalue.h>
//! Entry point for BitcoinApplication tests.
void OptionTests::optionTests()
{
// Test regression https://github.com/bitcoin/bitcoin/issues/24457. Check
// if setting an integer prune value causes an exception to be thrown in
// the OptionsModel constructor.
gArgs.LockSettings([&](util::Settings& settings) {
settings.forced_settings.erase("prune");
settings.rw_settings["prune"] = 3814;
});
gArgs.WriteSettingsFile();
QVERIFY_EXCEPTION_THROWN(OptionsModel{}, std::runtime_error);
gArgs.LockSettings([&](util::Settings& settings) {
settings.rw_settings.erase("prune");
});
gArgs.WriteSettingsFile();
}

25
src/qt/test/optiontests.h Normal file
View File

@ -0,0 +1,25 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_QT_TEST_OPTIONTESTS_H
#define BITCOIN_QT_TEST_OPTIONTESTS_H
#include <qt/optionsmodel.h>
#include <QObject>
class OptionTests : public QObject
{
Q_OBJECT
public:
explicit OptionTests(interfaces::Node& node) : m_node(node) {}
private Q_SLOTS:
void optionTests();
private:
interfaces::Node& m_node;
};
#endif // BITCOIN_QT_TEST_OPTIONTESTS_H

View File

@ -10,6 +10,7 @@
#include <interfaces/node.h>
#include <qt/bitcoin.h>
#include <qt/test/apptests.h>
#include <qt/test/optiontests.h>
#include <qt/test/rpcnestedtests.h>
#include <qt/test/uritests.h>
#include <test/util/setup_common.h>
@ -89,6 +90,10 @@ int main(int argc, char* argv[])
if (QTest::qExec(&app_tests) != 0) {
fInvalid = true;
}
OptionTests options_tests(app.node());
if (QTest::qExec(&options_tests) != 0) {
fInvalid = true;
}
URITests test1;
if (QTest::qExec(&test1) != 0) {
fInvalid = true;

View File

@ -3,6 +3,8 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/setup_common.h>
#include <univalue.h>
#include <util/settings.h>
#include <util/strencodings.h>
#include <util/system.h>
@ -41,6 +43,116 @@ void SetupArgs(ArgsManager& local_args, const std::vector<std::pair<std::string,
}
}
// Test behavior of GetArg functions when string, integer, and boolean types
// are specified in the settings.json file. GetArg functions are convenience
// functions. The GetSetting method can always be used instead of GetArg
// methods to retrieve original values, and there's not always an objective
// answer to what GetArg behavior is best in every case. This test makes sure
// there's test coverage for whatever the current behavior is, so it's not
// broken or changed unintentionally.
BOOST_AUTO_TEST_CASE(setting_args)
{
ArgsManager args;
SetupArgs(args, {{"-foo", ArgsManager::ALLOW_ANY}});
auto set_foo = [&](const util::SettingsValue& value) {
args.LockSettings([&](util::Settings& settings) {
settings.rw_settings["foo"] = value;
});
};
set_foo("str");
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"str\"");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "str");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
set_foo("99");
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"99\"");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
set_foo("3.25");
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"3.25\"");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 3);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
set_foo("0");
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"0\"");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
set_foo("");
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"\"");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
set_foo(99);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "99");
BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
set_foo(3.25);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "3.25");
BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
set_foo(0);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "0");
BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
set_foo(true);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "true");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "1");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 1);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
set_foo(false);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "false");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
set_foo(UniValue::VOBJ);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "{}");
BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
set_foo(UniValue::VARR);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "[]");
BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
set_foo(UniValue::VNULL);
BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "null");
BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "default");
BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 100);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
}
BOOST_AUTO_TEST_CASE(boolarg)
{
ArgsManager local_args;