LCOV - code coverage report
Current view: top level - src/common - run_command.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 21 21
Test Date: 2026-02-25 05:45:00 Functions: 100.0 % 1 1
Branches: 60.0 % 60 36

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022-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 <bitcoin-build-config.h> // IWYU pragma: keep
       6                 :             : 
       7                 :             : #include <common/run_command.h>
       8                 :             : 
       9                 :             : #include <tinyformat.h>
      10                 :             : #include <univalue.h>
      11                 :             : #include <util/string.h>
      12                 :             : 
      13                 :             : #ifdef ENABLE_EXTERNAL_SIGNER
      14                 :             : #include <util/subprocess.h>
      15                 :             : #endif // ENABLE_EXTERNAL_SIGNER
      16                 :             : 
      17                 :          35 : UniValue RunCommandParseJSON(const std::vector<std::string>& cmd_args, const std::string& str_std_in)
      18                 :             : {
      19                 :             : #ifdef ENABLE_EXTERNAL_SIGNER
      20                 :          35 :     namespace sp = subprocess;
      21                 :             : 
      22         [ +  - ]:          35 :     UniValue result_json;
      23         [ +  - ]:          35 :     std::istringstream stdout_stream;
      24         [ +  - ]:          35 :     std::istringstream stderr_stream;
      25                 :             : 
      26         [ +  + ]:          35 :     if (cmd_args.empty()) return UniValue::VNULL;
      27                 :             : 
      28   [ +  -  +  -  :          34 :     auto c = sp::Popen(cmd_args, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});
          +  -  +  -  +  
                      + ]
      29         [ +  + ]:          32 :     if (!str_std_in.empty()) {
      30         [ -  + ]:           4 :         c.send(str_std_in);
      31                 :             :     }
      32   [ +  -  +  - ]:          32 :     auto [out_res, err_res] = c.communicate();
      33   [ +  -  +  - ]:          64 :     stdout_stream.str(std::string{out_res.buf.begin(), out_res.buf.end()});
      34   [ +  -  +  - ]:          64 :     stderr_stream.str(std::string{err_res.buf.begin(), err_res.buf.end()});
      35                 :             : 
      36         [ +  - ]:          32 :     std::string result;
      37                 :          32 :     std::string error;
      38         [ +  - ]:          32 :     std::getline(stdout_stream, result);
      39         [ +  - ]:          32 :     std::getline(stderr_stream, error);
      40                 :             : 
      41         [ +  + ]:          32 :     const int n_error = c.retcode();
      42   [ +  +  +  -  :          36 :     if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", util::Join(cmd_args, " "), n_error, error));
             +  -  +  - ]
      43   [ -  +  +  -  :          30 :     if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
          +  +  +  -  +  
                      - ]
      44                 :             : 
      45                 :          26 :     return result_json;
      46                 :             : #else
      47                 :             :     throw std::runtime_error("Compiled without external signing support (required for external signing).");
      48                 :             : #endif // ENABLE_EXTERNAL_SIGNER
      49                 :          95 : }
        

Generated by: LCOV version 2.0-1