LCOV - code coverage report
Current view: top level - src/test - system_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 66.1 % 62 41
Test Date: 2026-08-25 06:16:57 Functions: 100.0 % 6 6
Branches: 44.6 % 242 108

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-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                 :             : 
       6                 :             : #include <common/run_command.h>
       7                 :             : #include <test/util/common.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : #include <univalue.h>
      10                 :             : #include <util/string.h>
      11                 :             : 
      12                 :             : #include <cstdlib>
      13                 :             : #include <iostream>
      14                 :             : #include <string_view>
      15                 :             : 
      16                 :             : #include <util/subprocess.h>
      17                 :             : 
      18                 :             : #include <boost/test/unit_test.hpp>
      19                 :             : 
      20                 :             : #include <string>
      21                 :             : 
      22                 :             : namespace {
      23                 :             : // When set in the environment, test_bitcoin acts as a mock subprocess for the
      24                 :             : // run_command test below instead of running unit tests.
      25                 :             : constexpr const char* MOCK_PROCESS_ENV = "BITCOIN_TEST_MOCK_PROCESS";
      26                 :             : 
      27                 :         155 : const bool g_maybe_run_mock_dispatcher_before_main{[]() {
      28                 :         155 :     const char* name = std::getenv(MOCK_PROCESS_ENV);
      29         [ +  - ]:         155 :     if (!name) return false;
      30         [ #  # ]:           0 :     const std::string_view n{name};
      31         [ #  # ]:           0 :     if (n == "valid_json") {
      32                 :           0 :         std::cout << R"({"success": true})" << std::endl;
      33                 :           0 :         std::_Exit(EXIT_SUCCESS);
      34                 :             :     }
      35         [ #  # ]:           0 :     if (n == "nonzeroexit_nooutput") {
      36                 :           0 :         std::_Exit(EXIT_FAILURE);
      37                 :             :     }
      38         [ #  # ]:           0 :     if (n == "nonzeroexit_stderroutput") {
      39                 :           0 :         std::cerr << "err" << std::endl;
      40                 :           0 :         std::_Exit(EXIT_FAILURE);
      41                 :             :     }
      42         [ #  # ]:           0 :     if (n == "invalid_json") {
      43                 :           0 :         std::cout << "{" << std::endl;
      44                 :           0 :         std::_Exit(EXIT_SUCCESS);
      45                 :             :     }
      46         [ #  # ]:           0 :     if (n == "pass_stdin_to_stdout") {
      47         [ #  # ]:           0 :         std::string s;
      48         [ #  # ]:           0 :         std::getline(std::cin, s);
      49   [ #  #  #  # ]:           0 :         std::cout << s << std::endl;
      50                 :           0 :         std::_Exit(EXIT_SUCCESS);
      51                 :           0 :     }
      52                 :           0 :     std::cerr << "Unknown mock process: " << n << std::endl;
      53                 :           0 :     std::_Exit(EXIT_FAILURE);
      54                 :             : }()};
      55                 :             : } // namespace
      56                 :             : 
      57                 :             : BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
      58                 :             : 
      59                 :           5 : static std::vector<std::string> mock_executable(const std::string& name)
      60                 :             : {
      61                 :             : #if defined(WIN32)
      62                 :             :     _putenv_s(MOCK_PROCESS_ENV, name.c_str());
      63                 :             : #else
      64                 :           5 :     setenv(MOCK_PROCESS_ENV, name.c_str(), /*overwrite=*/1);
      65                 :             : #endif
      66   [ +  -  +  +  :          20 :     return {boost::unit_test::framework::master_test_suite().argv[0]};
                   -  - ]
      67                 :           5 : }
      68                 :             : 
      69   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(run_command)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      70                 :             : {
      71                 :           1 :     {
      72         [ +  - ]:           1 :         const UniValue result = RunCommandParseJSON({});
      73   [ +  -  +  - ]:           2 :         BOOST_CHECK(result.isNull());
      74                 :           1 :     }
      75                 :           1 :     {
      76   [ +  -  +  -  :           1 :         const UniValue result = RunCommandParseJSON(mock_executable("valid_json"));
                   +  - ]
      77   [ +  -  +  -  :           2 :         BOOST_CHECK(result.isObject());
                   +  - ]
      78         [ +  - ]:           1 :         const UniValue& success = result.find_value("success");
      79   [ +  -  +  -  :           2 :         BOOST_CHECK(!success.isNull());
                   +  - ]
      80   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(success.get_bool(), true);
                   +  - ]
      81                 :           1 :     }
      82                 :           1 :     {
      83                 :             :         // An invalid command is handled by cpp-subprocess
      84                 :             : #ifdef WIN32
      85                 :             :         const std::string expected{"CreateProcess failed: "};
      86                 :             : #else
      87                 :           1 :         const std::string expected{"execve failed: "};
      88                 :             : #endif
      89   [ +  -  +  -  :           3 :         BOOST_CHECK_EXCEPTION(RunCommandParseJSON({"invalid_command"}), subprocess::CalledProcessError, HasReason(expected));
          +  -  -  +  -  
          -  -  -  -  +  
          +  -  -  +  +  
                -  +  - ]
      90                 :           0 :     }
      91                 :           1 :     {
      92                 :             :         // Return non-zero exit code, no output to stderr
      93         [ +  - ]:           1 :         const std::vector<std::string> command = mock_executable("nonzeroexit_nooutput");
      94   [ +  -  +  -  :           6 :         BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
          +  +  +  -  -  
          -  -  +  +  -  
             +  -  +  - ]
      95                 :             :             const std::string what{e.what()};
      96                 :             :             BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned %d: \n", util::Join(command, " "), EXIT_FAILURE)) != std::string::npos);
      97                 :             :             return true;
      98                 :             :         });
      99                 :           1 :     }
     100                 :           1 :     {
     101                 :             :         // Return non-zero exit code, with error message for stderr
     102         [ +  - ]:           1 :         const std::vector<std::string> command = mock_executable("nonzeroexit_stderroutput");
     103         [ +  - ]:           1 :         const std::string expected{"err"};
     104   [ +  -  +  -  :           8 :         BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
          +  +  +  -  +  
          -  -  +  +  -  
             +  -  +  - ]
     105                 :             :             const std::string what(e.what());
     106                 :             :             BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned %s: %s", util::Join(command, " "), EXIT_FAILURE, "err")) != std::string::npos);
     107                 :             :             BOOST_CHECK(what.find(expected) != std::string::npos);
     108                 :             :             return true;
     109                 :             :         });
     110                 :           1 :     }
     111                 :           1 :     {
     112                 :             :         // Unable to parse JSON
     113   [ +  -  +  -  :           4 :         BOOST_CHECK_EXCEPTION(RunCommandParseJSON(mock_executable("invalid_json")), std::runtime_error, HasReason("Unable to parse JSON: {"));
          +  -  +  -  -  
          +  -  -  -  -  
          -  +  +  -  +  
                -  +  - ]
     114                 :             :     }
     115                 :           1 :     {
     116                 :             :         // Test stdin
     117   [ +  -  +  -  :           1 :         const UniValue result = RunCommandParseJSON(mock_executable("pass_stdin_to_stdout"), "{\"success\": true}");
                   +  - ]
     118   [ +  -  +  -  :           2 :         BOOST_CHECK(result.isObject());
                   +  - ]
     119         [ +  - ]:           1 :         const UniValue& success = result.find_value("success");
     120   [ +  -  +  -  :           2 :         BOOST_CHECK(!success.isNull());
                   +  - ]
     121   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(success.get_bool(), true);
                   +  - ]
     122                 :           1 :     }
     123                 :           1 : }
     124                 :             : 
     125                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1