Bitcoin Core  24.99.0
P2P Digital Currency
test_main.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-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 <interfaces/init.h>
10 #include <interfaces/node.h>
11 #include <qt/bitcoin.h>
12 #include <qt/test/apptests.h>
13 #include <qt/test/optiontests.h>
14 #include <qt/test/rpcnestedtests.h>
15 #include <qt/test/uritests.h>
16 #include <test/util/setup_common.h>
17 
18 #ifdef ENABLE_WALLET
20 #include <qt/test/wallettests.h>
21 #endif // ENABLE_WALLET
22 
23 #include <QApplication>
24 #include <QDebug>
25 #include <QObject>
26 #include <QTest>
27 
28 #include <functional>
29 
30 #if defined(QT_STATICPLUGIN)
31 #include <QtPlugin>
32 #if defined(QT_QPA_PLATFORM_MINIMAL)
33 Q_IMPORT_PLUGIN(QMinimalIntegrationPlugin);
34 #endif
35 #if defined(QT_QPA_PLATFORM_XCB)
36 Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
37 #elif defined(QT_QPA_PLATFORM_WINDOWS)
38 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
39 #elif defined(QT_QPA_PLATFORM_COCOA)
40 Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin);
41 #elif defined(QT_QPA_PLATFORM_ANDROID)
42 Q_IMPORT_PLUGIN(QAndroidPlatformIntegrationPlugin)
43 #endif
44 #endif
45 
46 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
47 
48 const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{};
49 
50 // This is all you need to run all the tests
51 int main(int argc, char* argv[])
52 {
53  // Initialize persistent globals with the testing setup state for sanity.
54  // E.g. -datadir in gArgs is set to a temp directory dummy value (instead
55  // of defaulting to the default datadir), or globalChainParams is set to
56  // regtest params.
57  //
58  // All tests must use their own testing setup (if needed).
61  return gArgs.GetDataDirNet() / "blocks";
62  }());
63 
64  std::unique_ptr<interfaces::Init> init = interfaces::MakeGuiInit(argc, argv);
65  gArgs.ForceSetArg("-listen", "0");
66  gArgs.ForceSetArg("-listenonion", "0");
67  gArgs.ForceSetArg("-discover", "0");
68  gArgs.ForceSetArg("-dnsseed", "0");
69  gArgs.ForceSetArg("-fixedseeds", "0");
70  gArgs.ForceSetArg("-upnp", "0");
71  gArgs.ForceSetArg("-natpmp", "0");
72 
73  // Prefer the "minimal" platform for the test instead of the normal default
74  // platform ("xcb", "windows", or "cocoa") so tests can't unintentionally
75  // interfere with any background GUIs and don't require extra resources.
76  #if defined(WIN32)
77  if (getenv("QT_QPA_PLATFORM") == nullptr) _putenv_s("QT_QPA_PLATFORM", "minimal");
78  #else
79  setenv("QT_QPA_PLATFORM", "minimal", 0 /* overwrite */);
80  #endif
81 
83  app.setApplicationName("Bitcoin-Qt-test");
84  app.createNode(*init);
85 
86  int num_test_failures{0};
87 
88  AppTests app_tests(app);
89  num_test_failures += QTest::qExec(&app_tests);
90 
91  OptionTests options_tests(app.node());
92  num_test_failures += QTest::qExec(&options_tests);
93 
95  num_test_failures += QTest::qExec(&test1);
96 
97  RPCNestedTests test3(app.node());
98  num_test_failures += QTest::qExec(&test3);
99 
100 #ifdef ENABLE_WALLET
101  WalletTests test5(app.node());
102  num_test_failures += QTest::qExec(&test5);
103 
104  AddressBookTests test6(app.node());
105  num_test_failures += QTest::qExec(&test6);
106 #endif
107 
108  if (num_test_failures) {
109  qWarning("\nFailed tests: %d\n", num_test_failures);
110  } else {
111  qDebug("\nAll tests passed.\n");
112  }
113  return num_test_failures;
114 }
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:654
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:289
Main Bitcoin application object.
Definition: bitcoin.h:37
void createNode(interfaces::Init &init)
Create or spawn node.
Definition: bitcoin.cpp:296
interfaces::Node & node() const
Definition: bitcoin.h:74
static const std::string REGTEST
const std::string test1
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:188
std::unique_ptr< Init > MakeGuiInit(int argc, char *argv[])
Return implementation of Init interface for the gui process.
Definition: bitcoin-gui.cpp:44
Basic testing setup.
Definition: setup_common.h:79
int main(int argc, char *argv[])
Definition: test_main.cpp:51
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: test_main.cpp:46
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: test_main.cpp:48
ArgsManager gArgs
Definition: system.cpp:73