Bitcoin Core 28.99.0
P2P Digital Currency
optiontests.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <bitcoin-build-config.h> // IWYU pragma: keep
6
7#include <common/args.h>
8#include <init.h>
9#include <qt/bitcoin.h>
10#include <qt/guiutil.h>
11#include <qt/test/optiontests.h>
13
14#include <QSettings>
15#include <QTest>
16
17#include <univalue.h>
18
19#include <fstream>
20
22{
24}
25
27{
28 // reset args
31}
32
34{
35 // Set legacy QSettings and verify that they get cleared and migrated to
36 // settings.json
37 QSettings settings;
38 settings.setValue("nDatabaseCache", 600);
39 settings.setValue("nThreadsScriptVerif", 12);
40 settings.setValue("fListen", false);
41 settings.setValue("bPrune", true);
42 settings.setValue("nPruneSize", 3);
43 settings.setValue("fUseProxy", true);
44 settings.setValue("addrProxy", "proxy:123");
45 settings.setValue("fUseSeparateProxyTor", true);
46 settings.setValue("addrSeparateProxyTor", "onion:234");
47
48 settings.sync();
49
50 QVERIFY(settings.contains("nDatabaseCache"));
51 QVERIFY(settings.contains("nThreadsScriptVerif"));
52 QVERIFY(settings.contains("fListen"));
53 QVERIFY(settings.contains("bPrune"));
54 QVERIFY(settings.contains("nPruneSize"));
55 QVERIFY(settings.contains("fUseProxy"));
56 QVERIFY(settings.contains("addrProxy"));
57 QVERIFY(settings.contains("fUseSeparateProxyTor"));
58 QVERIFY(settings.contains("addrSeparateProxyTor"));
59
60 OptionsModel options{m_node};
61 bilingual_str error;
62 QVERIFY(options.Init(error));
63 QVERIFY(!settings.contains("nDatabaseCache"));
64 QVERIFY(!settings.contains("nThreadsScriptVerif"));
65 QVERIFY(!settings.contains("fListen"));
66 QVERIFY(!settings.contains("bPrune"));
67 QVERIFY(!settings.contains("nPruneSize"));
68 QVERIFY(!settings.contains("fUseProxy"));
69 QVERIFY(!settings.contains("addrProxy"));
70 QVERIFY(!settings.contains("fUseSeparateProxyTor"));
71 QVERIFY(!settings.contains("addrSeparateProxyTor"));
72
73 std::ifstream file(gArgs.GetDataDirNet() / "settings.json");
74 std::string default_warning = strprintf("This file is automatically generated and updated by %s. Please do not edit this file while the node "
75 "is running, as any changes might be ignored or overwritten.",
76 CLIENT_NAME);
77 QCOMPARE(std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()).c_str(), "{\n"
78 " \"_warning_\": \""+ default_warning+"\",\n"
79 " \"dbcache\": \"600\",\n"
80 " \"listen\": false,\n"
81 " \"onion\": \"onion:234\",\n"
82 " \"par\": \"12\",\n"
83 " \"proxy\": \"proxy:123\",\n"
84 " \"prune\": \"2861\"\n"
85 "}\n");
86}
87
89{
90 // Test regression https://github.com/bitcoin/bitcoin/issues/24457. Ensure
91 // that setting integer prune value doesn't cause an exception to be thrown
92 // in the OptionsModel constructor
93 gArgs.LockSettings([&](common::Settings& settings) {
94 settings.forced_settings.erase("prune");
95 settings.rw_settings["prune"] = 3814;
96 });
98 bilingual_str error;
99 QVERIFY(OptionsModel{m_node}.Init(error));
100 gArgs.LockSettings([&](common::Settings& settings) {
101 settings.rw_settings.erase("prune");
102 });
104}
105
107{
108 // Test that the bug https://github.com/bitcoin-core/gui/issues/567 does not resurface.
109 // It was fixed via https://github.com/bitcoin-core/gui/pull/568.
110 // With fListen=false in ~/.config/Bitcoin/Bitcoin-Qt.conf and all else left as default,
111 // bitcoin-qt should set both -listen and -listenonion to false and start successfully.
113 s.forced_settings.erase("listen");
114 s.forced_settings.erase("listenonion");
115 });
116 QVERIFY(!gArgs.IsArgSet("-listen"));
117 QVERIFY(!gArgs.IsArgSet("-listenonion"));
118
119 QSettings settings;
120 settings.setValue("fListen", false);
121
122 bilingual_str error;
123 QVERIFY(OptionsModel{m_node}.Init(error));
124
125 const bool expected{false};
126
127 QVERIFY(gArgs.IsArgSet("-listen"));
128 QCOMPARE(gArgs.GetBoolArg("-listen", !expected), expected);
129
130 QVERIFY(gArgs.IsArgSet("-listenonion"));
131 QCOMPARE(gArgs.GetBoolArg("-listenonion", !expected), expected);
132
134
135 // cleanup
136 settings.remove("fListen");
137 QVERIFY(!settings.contains("fListen"));
139}
140
142{
143 QString filter = QString("Partially Signed Transaction (Binary) (*.psbt)");
144 QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "psbt");
145
146 filter = QString("Image (*.png *.jpg)");
147 QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "png");
148}
ArgsManager gArgs
Definition: args.cpp:42
node::NodeContext m_node
Definition: bitcoin-gui.cpp:42
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:234
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: args.h:413
void ClearPathCache()
Clear cached directory paths.
Definition: args.cpp:333
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:371
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: args.cpp:425
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
common::Settings m_previous_settings
Definition: optiontests.h:29
void extractFilter()
OptionTests(interfaces::Node &node)
Definition: optiontests.cpp:21
void integerGetArgBug()
Definition: optiontests.cpp:88
interfaces::Node & m_node
Definition: optiontests.h:28
void parametersInteraction()
void migrateSettings()
Definition: optiontests.cpp:33
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:43
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:71
bool AppInitParameterInteraction(const ArgsManager &args)
Initialization: parameter interaction.
Definition: init.cpp:867
QString ExtractFirstSuffixFromFilter(const QString &filter)
Extract first suffix from filter pattern "Description (*.foo)" or "Description (*....
Definition: guiutil.cpp:302
Definition: messages.h:20
Bilingual messages:
Definition: translation.h:21
Stored settings.
Definition: settings.h:32
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:34
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:38
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165