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 
26 #if defined(QT_STATICPLUGIN)
27 #include <QtPlugin>
28 #if defined(QT_QPA_PLATFORM_MINIMAL)
29 Q_IMPORT_PLUGIN(QMinimalIntegrationPlugin);
30 #endif
31 #if defined(QT_QPA_PLATFORM_XCB)
32 Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
33 #elif defined(QT_QPA_PLATFORM_WINDOWS)
34 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
35 #elif defined(QT_QPA_PLATFORM_COCOA)
36 Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin);
37 #elif defined(QT_QPA_PLATFORM_ANDROID)
38 Q_IMPORT_PLUGIN(QAndroidPlatformIntegrationPlugin)
39 #endif
40 #endif
41 
42 using node::NodeContext;
43 
44 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
45 
46 // This is all you need to run all the tests
47 int main(int argc, char* argv[])
48 {
49  // Initialize persistent globals with the testing setup state for sanity.
50  // E.g. -datadir in gArgs is set to a temp directory dummy value (instead
51  // of defaulting to the default datadir), or globalChainParams is set to
52  // regtest params.
53  //
54  // All tests must use their own testing setup (if needed).
55  {
57  }
58 
59  std::unique_ptr<interfaces::Init> init = interfaces::MakeGuiInit(argc, argv);
60  gArgs.ForceSetArg("-listen", "0");
61  gArgs.ForceSetArg("-listenonion", "0");
62  gArgs.ForceSetArg("-discover", "0");
63  gArgs.ForceSetArg("-dnsseed", "0");
64  gArgs.ForceSetArg("-fixedseeds", "0");
65  gArgs.ForceSetArg("-upnp", "0");
66  gArgs.ForceSetArg("-natpmp", "0");
67 
68  bool fInvalid = false;
69 
70  // Prefer the "minimal" platform for the test instead of the normal default
71  // platform ("xcb", "windows", or "cocoa") so tests can't unintentionally
72  // interfere with any background GUIs and don't require extra resources.
73  #if defined(WIN32)
74  if (getenv("QT_QPA_PLATFORM") == nullptr) _putenv_s("QT_QPA_PLATFORM", "minimal");
75  #else
76  setenv("QT_QPA_PLATFORM", "minimal", 0 /* overwrite */);
77  #endif
78 
79  // Don't remove this, it's needed to access
80  // QApplication:: and QCoreApplication:: in the tests
82  app.setApplicationName("Bitcoin-Qt-test");
83  app.createNode(*init);
84 
85  AppTests app_tests(app);
86  if (QTest::qExec(&app_tests) != 0) {
87  fInvalid = true;
88  }
90  if (QTest::qExec(&test1) != 0) {
91  fInvalid = true;
92  }
93  RPCNestedTests test3(app.node());
94  if (QTest::qExec(&test3) != 0) {
95  fInvalid = true;
96  }
97 #ifdef ENABLE_WALLET
98  WalletTests test5(app.node());
99  if (QTest::qExec(&test5) != 0) {
100  fInvalid = true;
101  }
102  AddressBookTests test6(app.node());
103  if (QTest::qExec(&test6) != 0) {
104  fInvalid = true;
105  }
106 #endif
107 
108  return fInvalid;
109 }
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:44
AddressBookTests
Definition: addressbooktests.h:15
main
int main(int argc, char *argv[])
Definition: test_main.cpp:47
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:624
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:79
test1
const std::string test1
Definition: crypto_tests.cpp:205
gArgs
ArgsManager gArgs
Definition: system.cpp:85
apptests.h
URITests
Definition: uritests.h:11
WalletTests
Definition: wallettests.h:15
rpcnestedtests.h
init
Definition: bitcoin-gui.cpp:16
uritests.h
BitcoinApplication
Main Bitcoin application object.
Definition: bitcoin.h:36
addressbooktests.h