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