Bitcoin Core  29.1.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 <bitcoin-build-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 const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
32 
33 const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS{};
34 
35 const std::function<std::string()> G_TEST_GET_FULL_NAME{};
36 
37 // This is all you need to run all the tests
38 int main(int argc, char* argv[])
39 {
40  // Initialize persistent globals with the testing setup state for sanity.
41  // E.g. -datadir in gArgs is set to a temp directory dummy value (instead
42  // of defaulting to the default datadir), or globalChainParams is set to
43  // regtest params.
44  //
45  // All tests must use their own testing setup (if needed).
48  return gArgs.GetDataDirNet() / "blocks";
49  }());
50 
51  std::unique_ptr<interfaces::Init> init = interfaces::MakeGuiInit(argc, argv);
52  gArgs.ForceSetArg("-listen", "0");
53  gArgs.ForceSetArg("-listenonion", "0");
54  gArgs.ForceSetArg("-discover", "0");
55  gArgs.ForceSetArg("-dnsseed", "0");
56  gArgs.ForceSetArg("-fixedseeds", "0");
57  gArgs.ForceSetArg("-natpmp", "0");
58 
59  std::string error;
60  if (!gArgs.ReadConfigFiles(error, true)) qWarning() << error.c_str();
61 
62  // Prefer the "minimal" platform for the test instead of the normal default
63  // platform ("xcb", "windows", or "cocoa") so tests can't unintentionally
64  // interfere with any background GUIs and don't require extra resources.
65  #if defined(WIN32)
66  if (getenv("QT_QPA_PLATFORM") == nullptr) _putenv_s("QT_QPA_PLATFORM", "minimal");
67  #else
68  setenv("QT_QPA_PLATFORM", "minimal", 0 /* overwrite */);
69  #endif
70 
71 
72  QCoreApplication::setOrganizationName(QAPP_ORG_NAME);
73  QCoreApplication::setApplicationName(QAPP_APP_NAME_DEFAULT "-test");
74 
75  int num_test_failures{0};
76 
77  {
79  app.createNode(*init);
80 
81  AppTests app_tests(app);
82  num_test_failures += QTest::qExec(&app_tests);
83 
84  OptionTests options_tests(app.node());
85  num_test_failures += QTest::qExec(&options_tests);
86 
88  num_test_failures += QTest::qExec(&test1);
89 
90  RPCNestedTests test3(app.node());
91  num_test_failures += QTest::qExec(&test3);
92 
93 #ifdef ENABLE_WALLET
94  WalletTests test5(app.node());
95  num_test_failures += QTest::qExec(&test5);
96 
97  AddressBookTests test6(app.node());
98  num_test_failures += QTest::qExec(&test6);
99 #endif
100 
101  if (num_test_failures) {
102  qWarning("\nFailed tests: %d\n", num_test_failures);
103  } else {
104  qDebug("\nAll tests passed.\n");
105  }
106  }
107 
108  QSettings settings;
109  settings.clear();
110 
111  return num_test_failures;
112 }
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:122
int main(int argc, char *argv[])
Definition: test_main.cpp:38
const std::string test1
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: args.cpp:546
interfaces::Node & node() const
Definition: bitcoin.h:69
Basic testing setup.
Definition: setup_common.h:64
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: test_main.cpp:33
#define QAPP_ORG_NAME
Definition: guiconstants.h:49
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:234
void createNode(interfaces::Init &init)
Create or spawn node.
Definition: bitcoin.cpp:289
Main Bitcoin application object.
Definition: bitcoin.h:34
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
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition: test_main.cpp:35
ArgsManager gArgs
Definition: args.cpp:42
#define QAPP_APP_NAME_DEFAULT
Definition: guiconstants.h:51
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: test_main.cpp:31
std::unique_ptr< Init > MakeGuiInit(int argc, char *argv[])
Return implementation of Init interface for the gui process.
Definition: bitcoin-gui.cpp:49