Bitcoin Core  27.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/guiconstants.h>
13 #include <qt/test/apptests.h>
14 #include <qt/test/optiontests.h>
15 #include <qt/test/rpcnestedtests.h>
16 #include <qt/test/uritests.h>
17 #include <test/util/setup_common.h>
18 #include <util/chaintype.h>
19 
20 #ifdef ENABLE_WALLET
22 #include <qt/test/wallettests.h>
23 #endif // ENABLE_WALLET
24 
25 #include <QApplication>
26 #include <QDebug>
27 #include <QObject>
28 #include <QSettings>
29 #include <QTest>
30 
31 #include <functional>
32 
33 #if defined(QT_STATICPLUGIN)
34 #include <QtPlugin>
35 #if defined(QT_QPA_PLATFORM_MINIMAL)
36 Q_IMPORT_PLUGIN(QMinimalIntegrationPlugin);
37 #endif
38 #if defined(QT_QPA_PLATFORM_XCB)
39 Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
40 #elif defined(QT_QPA_PLATFORM_WINDOWS)
41 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
42 #elif defined(QT_QPA_PLATFORM_COCOA)
43 Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin);
44 #elif defined(QT_QPA_PLATFORM_ANDROID)
45 Q_IMPORT_PLUGIN(QAndroidPlatformIntegrationPlugin)
46 #endif
47 #endif
48 
49 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
50 
51 const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{};
52 
53 const std::function<std::string()> G_TEST_GET_FULL_NAME{};
54 
55 // This is all you need to run all the tests
56 int main(int argc, char* argv[])
57 {
58  // Initialize persistent globals with the testing setup state for sanity.
59  // E.g. -datadir in gArgs is set to a temp directory dummy value (instead
60  // of defaulting to the default datadir), or globalChainParams is set to
61  // regtest params.
62  //
63  // All tests must use their own testing setup (if needed).
66  return gArgs.GetDataDirNet() / "blocks";
67  }());
68 
69  std::unique_ptr<interfaces::Init> init = interfaces::MakeGuiInit(argc, argv);
70  gArgs.ForceSetArg("-listen", "0");
71  gArgs.ForceSetArg("-listenonion", "0");
72  gArgs.ForceSetArg("-discover", "0");
73  gArgs.ForceSetArg("-dnsseed", "0");
74  gArgs.ForceSetArg("-fixedseeds", "0");
75  gArgs.ForceSetArg("-upnp", "0");
76  gArgs.ForceSetArg("-natpmp", "0");
77 
78  std::string error;
79  if (!gArgs.ReadConfigFiles(error, true)) QWARN(error.c_str());
80 
81  // Prefer the "minimal" platform for the test instead of the normal default
82  // platform ("xcb", "windows", or "cocoa") so tests can't unintentionally
83  // interfere with any background GUIs and don't require extra resources.
84  #if defined(WIN32)
85  if (getenv("QT_QPA_PLATFORM") == nullptr) _putenv_s("QT_QPA_PLATFORM", "minimal");
86  #else
87  setenv("QT_QPA_PLATFORM", "minimal", 0 /* overwrite */);
88  #endif
89 
90 
91  QCoreApplication::setOrganizationName(QAPP_ORG_NAME);
92  QCoreApplication::setApplicationName(QAPP_APP_NAME_DEFAULT "-test");
93 
94  int num_test_failures{0};
95 
96  {
98  app.createNode(*init);
99 
100  AppTests app_tests(app);
101  num_test_failures += QTest::qExec(&app_tests);
102 
103  OptionTests options_tests(app.node());
104  num_test_failures += QTest::qExec(&options_tests);
105 
106  URITests test1;
107  num_test_failures += QTest::qExec(&test1);
108 
109  RPCNestedTests test3(app.node());
110  num_test_failures += QTest::qExec(&test3);
111 
112 #ifdef ENABLE_WALLET
113  WalletTests test5(app.node());
114  num_test_failures += QTest::qExec(&test5);
115 
116  AddressBookTests test6(app.node());
117  num_test_failures += QTest::qExec(&test6);
118 #endif
119 
120  if (num_test_failures) {
121  qWarning("\nFailed tests: %d\n", num_test_failures);
122  } else {
123  qDebug("\nAll tests passed.\n");
124  }
125  }
126 
127  QSettings settings;
128  settings.clear();
129 
130  return num_test_failures;
131 }
ArgsManager gArgs
Definition: args.cpp:41
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: args.cpp:544
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:232
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:118
Main Bitcoin application object.
Definition: bitcoin.h:37
void createNode(interfaces::Init &init)
Create or spawn node.
Definition: bitcoin.cpp:304
interfaces::Node & node() const
Definition: bitcoin.h:71
const std::string test1
#define QAPP_ORG_NAME
Definition: guiconstants.h:49
#define QAPP_APP_NAME_DEFAULT
Definition: guiconstants.h:51
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:190
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:52
int main(int argc, char *argv[])
Definition: test_main.cpp:56
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: test_main.cpp:49
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: test_main.cpp:51
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition: test_main.cpp:53