node_init_tests.cpp raw
1 // Copyright (c) 2025-present 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 <init.h>
6 #include <interfaces/init.h>
7 #include <logging.h>
8 #include <rpc/server.h>
9
10 #include <boost/test/unit_test.hpp>
11 #include <test/util/common.h>
12 #include <test/util/setup_common.h>
13
14 using node::NodeContext;
15
16 //! Like BasicTestingSetup, but using regtest network instead of mainnet.
17 struct InitTestSetup : BasicTestingSetup {
18 InitTestSetup() : BasicTestingSetup{ChainType::REGTEST} {}
19 };
20
21 BOOST_FIXTURE_TEST_SUITE(node_init_tests, InitTestSetup)
22
23 //! Custom implementation of interfaces::Init for testing.
24 class TestInit : public interfaces::Init
25 {
26 public:
27 TestInit(NodeContext& node) : m_node(node)
28 {
29 InitContext(m_node);
30 m_node.init = this;
31 }
32 std::unique_ptr<interfaces::Chain> makeChain() override { return interfaces::MakeChain(m_node); }
33 std::unique_ptr<interfaces::WalletLoader> makeWalletLoader(interfaces::Chain& chain) override
34 {
35 return MakeWalletLoader(chain, *Assert(m_node.args));
36 }
37 NodeContext& m_node;
38 };
39
40 BOOST_AUTO_TEST_CASE(init_test)
41 {
42 // Clear state set by BasicTestingSetup that AppInitMain assumes is unset.
43 LogInstance().DisconnectTestLogger();
44 m_node.args->SetConfigFilePath({});
45
46 // Prevent the test from trying to listen on ports 8332 and 8333.
47 m_node.args->ForceSetArg("-server", "0");
48 m_node.args->ForceSetArg("-listen", "0");
49
50 // Run through initialization and shutdown code.
51 TestInit init{m_node};
52 BOOST_CHECK(AppInitInterfaces(m_node));
53 BOOST_CHECK(AppInitMain(m_node));
54 Interrupt(m_node);
55 Shutdown(m_node);
56 }
57
58 BOOST_AUTO_TEST_SUITE_END()
59