Bitcoin Core  22.99.0
P2P Digital Currency
test_main.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2021 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/rpcnestedtests.h>
14 #include <qt/test/uritests.h>
15 #include <test/util/setup_common.h>
16 
17 #ifdef ENABLE_WALLET
19 #include <qt/test/wallettests.h>
20 #endif // ENABLE_WALLET
21 
22 #include <QApplication>
23 #include <QObject>
24 #include <QTest>
25 #include <functional>
26 
27 #if defined(QT_STATICPLUGIN)
28 #include <QtPlugin>
29 #if defined(QT_QPA_PLATFORM_MINIMAL)
30 Q_IMPORT_PLUGIN(QMinimalIntegrationPlugin);
31 #endif
32 #if defined(QT_QPA_PLATFORM_XCB)
33 Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
34 #elif defined(QT_QPA_PLATFORM_WINDOWS)
35 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
36 #elif defined(QT_QPA_PLATFORM_COCOA)
37 Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin);
38 #elif defined(QT_QPA_PLATFORM_ANDROID)
39 Q_IMPORT_PLUGIN(QAndroidPlatformIntegrationPlugin)
40 #endif
41 #endif
42 
43 using node::NodeContext;
44 
45 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
46 
47 const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{};
48 
49 // This is all you need to run all the tests
50 int main(int argc, char* argv[])
51 {
52  // Initialize persistent globals with the testing setup state for sanity.
53  // E.g. -datadir in gArgs is set to a temp directory dummy value (instead
54  // of defaulting to the default datadir), or globalChainParams is set to
55  // regtest params.
56  //
57  // All tests must use their own testing setup (if needed).
58  {
60  }
61 
62  std::unique_ptr<interfaces::Init> init = interfaces::MakeGuiInit(argc, argv);
63  gArgs.ForceSetArg("-listen", "0");
64  gArgs.ForceSetArg("-listenonion", "0");
65  gArgs.ForceSetArg("-discover", "0");
66  gArgs.ForceSetArg("-dnsseed", "0");
67  gArgs.ForceSetArg("-fixedseeds", "0");
68  gArgs.ForceSetArg("-upnp", "0");
69  gArgs.ForceSetArg("-natpmp", "0");
70 
71  bool fInvalid = false;
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 
82  // Don't remove this, it's needed to access
83  // QApplication:: and QCoreApplication:: in the tests
85  app.setApplicationName("Bitcoin-Qt-test");
86  app.createNode(*init);
87 
88  AppTests app_tests(app);
89  if (QTest::qExec(&app_tests) != 0) {
90  fInvalid = true;
91  }
93  if (QTest::qExec(&test1) != 0) {
94  fInvalid = true;
95  }
96  RPCNestedTests test3(app.node());
97  if (QTest::qExec(&test3) != 0) {
98  fInvalid = true;
99  }
100 #ifdef ENABLE_WALLET
101  WalletTests test5(app.node());
102  if (QTest::qExec(&test5) != 0) {
103  fInvalid = true;
104  }
105  AddressBookTests test6(app.node());
106  if (QTest::qExec(&test6) != 0) {
107  fInvalid = true;
108  }
109 #endif
110 
111  return fInvalid;
112 }
wallettests.h
BitcoinApplication::node
interfaces::Node & node() const
Definition: bitcoin.h:74
setup_common.h
node::NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:40
bitcoin.h
bitcoin-config.h
interfaces::MakeGuiInit
std::unique_ptr< Init > MakeGuiInit(int argc, char *argv[])
Return implementation of Init interface for the gui process.
Definition: bitcoin-gui.cpp:43
AppTests
Definition: apptests.h:17
G_TEST_LOG_FUN
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: test_main.cpp:45
AddressBookTests
Definition: addressbooktests.h:15
main
int main(int argc, char *argv[])
Definition: test_main.cpp:50
RPCNestedTests
Definition: rpcnestedtests.h:15
node.h
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
ArgsManager::ForceSetArg
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:619
init.h
BitcoinApplication::createNode
void createNode(interfaces::Init &init)
Create or spawn node.
Definition: bitcoin.cpp:285
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
test1
const std::string test1
Definition: crypto_tests.cpp:205
gArgs
ArgsManager gArgs
Definition: system.cpp:80
apptests.h
URITests
Definition: uritests.h:11
WalletTests
Definition: wallettests.h:15
rpcnestedtests.h
init
Definition: bitcoin-gui.cpp:16
uritests.h
G_TEST_COMMAND_LINE_ARGUMENTS
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: test_main.cpp:47
BitcoinApplication
Main Bitcoin application object.
Definition: bitcoin.h:36
addressbooktests.h