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