Bitcoin Core  27.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 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <common/args.h>
10 #include <init.h>
11 #include <qt/bitcoin.h>
12 #include <qt/guiutil.h>
13 #include <qt/test/optiontests.h>
14 #include <test/util/setup_common.h>
15 
16 #include <QSettings>
17 #include <QTest>
18 
19 #include <univalue.h>
20 
21 #include <fstream>
22 
24 {
26 }
27 
29 {
30  // reset args
33 }
34 
36 {
37  // Set legacy QSettings and verify that they get cleared and migrated to
38  // settings.json
39  QSettings settings;
40  settings.setValue("nDatabaseCache", 600);
41  settings.setValue("nThreadsScriptVerif", 12);
42  settings.setValue("fUseUPnP", false);
43  settings.setValue("fListen", false);
44  settings.setValue("bPrune", true);
45  settings.setValue("nPruneSize", 3);
46  settings.setValue("fUseProxy", true);
47  settings.setValue("addrProxy", "proxy:123");
48  settings.setValue("fUseSeparateProxyTor", true);
49  settings.setValue("addrSeparateProxyTor", "onion:234");
50 
51  settings.sync();
52 
53  QVERIFY(settings.contains("nDatabaseCache"));
54  QVERIFY(settings.contains("nThreadsScriptVerif"));
55  QVERIFY(settings.contains("fUseUPnP"));
56  QVERIFY(settings.contains("fListen"));
57  QVERIFY(settings.contains("bPrune"));
58  QVERIFY(settings.contains("nPruneSize"));
59  QVERIFY(settings.contains("fUseProxy"));
60  QVERIFY(settings.contains("addrProxy"));
61  QVERIFY(settings.contains("fUseSeparateProxyTor"));
62  QVERIFY(settings.contains("addrSeparateProxyTor"));
63 
64  OptionsModel options{m_node};
65  bilingual_str error;
66  QVERIFY(options.Init(error));
67  QVERIFY(!settings.contains("nDatabaseCache"));
68  QVERIFY(!settings.contains("nThreadsScriptVerif"));
69  QVERIFY(!settings.contains("fUseUPnP"));
70  QVERIFY(!settings.contains("fListen"));
71  QVERIFY(!settings.contains("bPrune"));
72  QVERIFY(!settings.contains("nPruneSize"));
73  QVERIFY(!settings.contains("fUseProxy"));
74  QVERIFY(!settings.contains("addrProxy"));
75  QVERIFY(!settings.contains("fUseSeparateProxyTor"));
76  QVERIFY(!settings.contains("addrSeparateProxyTor"));
77 
78  std::ifstream file(gArgs.GetDataDirNet() / "settings.json");
79  std::string default_warning = strprintf("This file is automatically generated and updated by %s. Please do not edit this file while the node "
80  "is running, as any changes might be ignored or overwritten.",
81  PACKAGE_NAME);
82  QCOMPARE(std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()).c_str(), "{\n"
83  " \"_warning_\": \""+ default_warning+"\",\n"
84  " \"dbcache\": \"600\",\n"
85  " \"listen\": false,\n"
86  " \"onion\": \"onion:234\",\n"
87  " \"par\": \"12\",\n"
88  " \"proxy\": \"proxy:123\",\n"
89  " \"prune\": \"2861\"\n"
90  "}\n");
91 }
92 
94 {
95  // Test regression https://github.com/bitcoin/bitcoin/issues/24457. Ensure
96  // that setting integer prune value doesn't cause an exception to be thrown
97  // in the OptionsModel constructor
98  gArgs.LockSettings([&](common::Settings& settings) {
99  settings.forced_settings.erase("prune");
100  settings.rw_settings["prune"] = 3814;
101  });
103  bilingual_str error;
104  QVERIFY(OptionsModel{m_node}.Init(error));
105  gArgs.LockSettings([&](common::Settings& settings) {
106  settings.rw_settings.erase("prune");
107  });
109 }
110 
112 {
113  // Test that the bug https://github.com/bitcoin-core/gui/issues/567 does not resurface.
114  // It was fixed via https://github.com/bitcoin-core/gui/pull/568.
115  // With fListen=false in ~/.config/Bitcoin/Bitcoin-Qt.conf and all else left as default,
116  // bitcoin-qt should set both -listen and -listenonion to false and start successfully.
118  s.forced_settings.erase("listen");
119  s.forced_settings.erase("listenonion");
120  });
121  QVERIFY(!gArgs.IsArgSet("-listen"));
122  QVERIFY(!gArgs.IsArgSet("-listenonion"));
123 
124  QSettings settings;
125  settings.setValue("fListen", false);
126 
127  bilingual_str error;
128  QVERIFY(OptionsModel{m_node}.Init(error));
129 
130  const bool expected{false};
131 
132  QVERIFY(gArgs.IsArgSet("-listen"));
133  QCOMPARE(gArgs.GetBoolArg("-listen", !expected), expected);
134 
135  QVERIFY(gArgs.IsArgSet("-listenonion"));
136  QCOMPARE(gArgs.GetBoolArg("-listenonion", !expected), expected);
137 
139 
140  // cleanup
141  settings.remove("fListen");
142  QVERIFY(!settings.contains("fListen"));
144 }
145 
147 {
148  QString filter = QString("Partially Signed Transaction (Binary) (*.psbt)");
149  QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "psbt");
150 
151  filter = QString("Image (*.png *.jpg)");
152  QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "png");
153 }
ArgsManager gArgs
Definition: args.cpp:41
#define PACKAGE_NAME
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:232
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: args.h:404
void ClearPathCache()
Clear cached directory paths.
Definition: args.cpp:331
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:369
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: args.cpp:423
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:505
common::Settings m_previous_settings
Definition: optiontests.h:29
void extractFilter()
OptionTests(interfaces::Node &node)
Definition: optiontests.cpp:23
void integerGetArgBug()
Definition: optiontests.cpp:93
interfaces::Node & m_node
Definition: optiontests.h:28
void parametersInteraction()
void migrateSettings()
Definition: optiontests.cpp:35
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:70
bool AppInitParameterInteraction(const ArgsManager &args)
Initialization: parameter interaction.
Definition: init.cpp:882
QString ExtractFirstSuffixFromFilter(const QString &filter)
Extract first suffix from filter pattern "Description (*.foo)" or "Description (*....
Definition: guiutil.cpp:301
Definition: init.h:25
Bilingual messages:
Definition: translation.h:18
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:1162