Bitcoin Core  24.99.0
P2P Digital Currency
optiontests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018 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 <init.h>
6 #include <qt/bitcoin.h>
7 #include <qt/guiutil.h>
8 #include <qt/test/optiontests.h>
10 #include <util/system.h>
11 
12 #include <QSettings>
13 #include <QTest>
14 
15 #include <univalue.h>
16 
17 #include <fstream>
18 
20 {
22 }
23 
25 {
26  // reset args
29 }
30 
32 {
33  // Set legacy QSettings and verify that they get cleared and migrated to
34  // settings.json
35  QSettings settings;
36  settings.setValue("nDatabaseCache", 600);
37  settings.setValue("nThreadsScriptVerif", 12);
38  settings.setValue("fUseUPnP", false);
39  settings.setValue("fListen", false);
40  settings.setValue("bPrune", true);
41  settings.setValue("nPruneSize", 3);
42  settings.setValue("fUseProxy", true);
43  settings.setValue("addrProxy", "proxy:123");
44  settings.setValue("fUseSeparateProxyTor", true);
45  settings.setValue("addrSeparateProxyTor", "onion:234");
46 
47  settings.sync();
48 
49  OptionsModel options{m_node};
51  QVERIFY(options.Init(error));
52  QVERIFY(!settings.contains("nDatabaseCache"));
53  QVERIFY(!settings.contains("nThreadsScriptVerif"));
54  QVERIFY(!settings.contains("fUseUPnP"));
55  QVERIFY(!settings.contains("fListen"));
56  QVERIFY(!settings.contains("bPrune"));
57  QVERIFY(!settings.contains("nPruneSize"));
58  QVERIFY(!settings.contains("fUseProxy"));
59  QVERIFY(!settings.contains("addrProxy"));
60  QVERIFY(!settings.contains("fUseSeparateProxyTor"));
61  QVERIFY(!settings.contains("addrSeparateProxyTor"));
62 
63  std::ifstream file(gArgs.GetDataDirNet() / "settings.json");
64  QCOMPARE(std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()).c_str(), "{\n"
65  " \"dbcache\": \"600\",\n"
66  " \"listen\": false,\n"
67  " \"onion\": \"onion:234\",\n"
68  " \"par\": \"12\",\n"
69  " \"proxy\": \"proxy:123\",\n"
70  " \"prune\": \"2861\"\n"
71  "}\n");
72 }
73 
75 {
76  // Test regression https://github.com/bitcoin/bitcoin/issues/24457. Ensure
77  // that setting integer prune value doesn't cause an exception to be thrown
78  // in the OptionsModel constructor
79  gArgs.LockSettings([&](util::Settings& settings) {
80  settings.forced_settings.erase("prune");
81  settings.rw_settings["prune"] = 3814;
82  });
85  QVERIFY(OptionsModel{m_node}.Init(error));
86  gArgs.LockSettings([&](util::Settings& settings) {
87  settings.rw_settings.erase("prune");
88  });
90 }
91 
93 {
94  // Test that the bug https://github.com/bitcoin-core/gui/issues/567 does not resurface.
95  // It was fixed via https://github.com/bitcoin-core/gui/pull/568.
96  // With fListen=false in ~/.config/Bitcoin/Bitcoin-Qt.conf and all else left as default,
97  // bitcoin-qt should set both -listen and -listenonion to false and start successfully.
99  s.forced_settings.erase("listen");
100  s.forced_settings.erase("listenonion");
101  });
102  QVERIFY(!gArgs.IsArgSet("-listen"));
103  QVERIFY(!gArgs.IsArgSet("-listenonion"));
104 
105  QSettings settings;
106  settings.setValue("fListen", false);
107 
109  QVERIFY(OptionsModel{m_node}.Init(error));
110 
111  const bool expected{false};
112 
113  QVERIFY(gArgs.IsArgSet("-listen"));
114  QCOMPARE(gArgs.GetBoolArg("-listen", !expected), expected);
115 
116  QVERIFY(gArgs.IsArgSet("-listenonion"));
117  QCOMPARE(gArgs.GetBoolArg("-listenonion", !expected), expected);
118 
120 
121  // cleanup
122  settings.remove("fListen");
123  QVERIFY(!settings.contains("fListen"));
125 }
126 
128 {
129  QString filter = QString("Partially Signed Transaction (Binary) (*.psbt)");
130  QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "psbt");
131 
132  filter = QString("Image (*.png *.jpg)");
133  QCOMPARE(GUIUtil::ExtractFirstSuffixFromFilter(filter), "png");
134 }
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: system.h:474
void ClearPathCache()
Clear cached directory paths.
Definition: system.cpp:462
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:500
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:303
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: system.cpp:572
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:654
void extractFilter()
OptionTests(interfaces::Node &node)
Definition: optiontests.cpp:19
void integerGetArgBug()
Definition: optiontests.cpp:74
interfaces::Node & m_node
Definition: optiontests.h:28
void parametersInteraction()
Definition: optiontests.cpp:92
util::Settings m_previous_settings
Definition: optiontests.h:29
void migrateSettings()
Definition: optiontests.cpp:31
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:41
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:70
bool AppInitParameterInteraction(const ArgsManager &args, bool use_syscall_sandbox)
Initialization: parameter interaction.
Definition: init.cpp:820
QString ExtractFirstSuffixFromFilter(const QString &filter)
Extract first suffix from filter pattern "Description (*.foo)" or "Description (*....
Definition: guiutil.cpp:293
Definition: init.h:25
Bilingual messages:
Definition: translation.h:18
Stored settings.
Definition: settings.h:31
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:37
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:33
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
ArgsManager gArgs
Definition: system.cpp:86