Bitcoin Core  22.99.0
P2P Digital Currency
test_main.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2020 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/initexecutor.h>
13 #include <qt/test/apptests.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 <QObject>
25 #include <QTest>
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 #endif
39 #endif
40 
41 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
42 
43 // This is all you need to run all the tests
44 int main(int argc, char* argv[])
45 {
46  // Initialize persistent globals with the testing setup state for sanity.
47  // E.g. -datadir in gArgs is set to a temp directory dummy value (instead
48  // of defaulting to the default datadir), or globalChainParams is set to
49  // regtest params.
50  //
51  // All tests must use their own testing setup (if needed).
52  {
54  }
55 
56  NodeContext node_context;
57  int unused_exit_status;
58  std::unique_ptr<interfaces::Init> init = interfaces::MakeNodeInit(node_context, argc, argv, unused_exit_status);
59  gArgs.ForceSetArg("-listen", "0");
60  gArgs.ForceSetArg("-listenonion", "0");
61  gArgs.ForceSetArg("-discover", "0");
62  gArgs.ForceSetArg("-dnsseed", "0");
63  gArgs.ForceSetArg("-fixedseeds", "0");
64  gArgs.ForceSetArg("-upnp", "0");
65  gArgs.ForceSetArg("-natpmp", "0");
66 
67  bool fInvalid = false;
68 
69  // Prefer the "minimal" platform for the test instead of the normal default
70  // platform ("xcb", "windows", or "cocoa") so tests can't unintentionally
71  // interfere with any background GUIs and don't require extra resources.
72  #if defined(WIN32)
73  if (getenv("QT_QPA_PLATFORM") == nullptr) _putenv_s("QT_QPA_PLATFORM", "minimal");
74  #else
75  setenv("QT_QPA_PLATFORM", "minimal", /* overwrite */ 0);
76  #endif
77 
78  // Don't remove this, it's needed to access
79  // QApplication:: and QCoreApplication:: in the tests
81  app.setApplicationName("Bitcoin-Qt-test");
82  app.createNode(*init);
83 
84  AppTests app_tests(app);
85  if (QTest::qExec(&app_tests) != 0) {
86  fInvalid = true;
87  }
89  if (QTest::qExec(&test1) != 0) {
90  fInvalid = true;
91  }
92  RPCNestedTests test3(app.node());
93  if (QTest::qExec(&test3) != 0) {
94  fInvalid = true;
95  }
96 #ifdef ENABLE_WALLET
97  WalletTests test5(app.node());
98  if (QTest::qExec(&test5) != 0) {
99  fInvalid = true;
100  }
101  AddressBookTests test6(app.node());
102  if (QTest::qExec(&test6) != 0) {
103  fInvalid = true;
104  }
105 #endif
106 
107  return fInvalid;
108 }
wallettests.h
BitcoinApplication::node
interfaces::Node & node() const
Definition: bitcoin.h:76
setup_common.h
bitcoin.h
bitcoin-config.h
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:41
AddressBookTests
Definition: addressbooktests.h:15
main
int main(int argc, char *argv[])
Definition: test_main.cpp:44
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:622
init.h
BitcoinApplication::createNode
void createNode(interfaces::Init &init)
Create or spawn node.
Definition: bitcoin.cpp:279
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
test1
const std::string test1
Definition: crypto_tests.cpp:205
gArgs
ArgsManager gArgs
Definition: system.cpp:84
apptests.h
URITests
Definition: uritests.h:11
WalletTests
Definition: wallettests.h:15
rpcnestedtests.h
init
Definition: bitcoin-node.cpp:16
initexecutor.h
uritests.h
interfaces::MakeNodeInit
std::unique_ptr< Init > MakeNodeInit(NodeContext &node, int argc, char *argv[], int &exit_status)
Return implementation of Init interface for the node process.
Definition: bitcoin-node.cpp:45
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:39
BitcoinApplication
Main Bitcoin application object.
Definition: bitcoin.h:36
addressbooktests.h