Bitcoin Core  24.99.0
P2P Digital Currency
system_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-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 //
6 #include <common/run_command.h>
7 #include <univalue.h>
8 
9 #ifdef ENABLE_EXTERNAL_SIGNER
10 #if defined(__GNUC__)
11 // Boost 1.78 requires the following workaround.
12 // See: https://github.com/boostorg/process/issues/235
13 #pragma GCC diagnostic push
14 #pragma GCC diagnostic ignored "-Wnarrowing"
15 #endif
16 #include <boost/process.hpp>
17 #if defined(__GNUC__)
18 #pragma GCC diagnostic pop
19 #endif
20 #endif // ENABLE_EXTERNAL_SIGNER
21 
22 #include <boost/test/unit_test.hpp>
23 
24 BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
25 
26 // At least one test is required (in case ENABLE_EXTERNAL_SIGNER is not defined).
27 // Workaround for https://github.com/bitcoin/bitcoin/issues/19128
29 {
30  BOOST_CHECK(true);
31 }
32 
33 #ifdef ENABLE_EXTERNAL_SIGNER
34 
36 {
37 #ifdef WIN32
38  // https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html
39  auto hntdll = GetModuleHandleA("ntdll.dll");
40  assert(hntdll);
41  const bool wine_runtime = GetProcAddress(hntdll, "wine_get_version");
42 #endif
43 
44  {
45  const UniValue result = RunCommandParseJSON("");
46  BOOST_CHECK(result.isNull());
47  }
48  {
49 #ifdef WIN32
50  const UniValue result = RunCommandParseJSON("cmd.exe /c echo {\"success\": true}");
51 #else
52  const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
53 #endif
54  BOOST_CHECK(result.isObject());
55  const UniValue& success = find_value(result, "success");
56  BOOST_CHECK(!success.isNull());
57  BOOST_CHECK_EQUAL(success.get_bool(), true);
58  }
59  {
60  // An invalid command is handled by Boost
61 #ifdef WIN32
62  const int expected_error{wine_runtime ? 6 : 2};
63 #else
64  const int expected_error{2};
65 #endif
66  BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, [&](const boost::process::process_error& e) {
67  BOOST_CHECK(std::string(e.what()).find("RunCommandParseJSON error:") == std::string::npos);
68  BOOST_CHECK_EQUAL(e.code().value(), expected_error);
69  return true;
70  });
71  }
72  {
73  // Return non-zero exit code, no output to stderr
74 #ifdef WIN32
75  const std::string command{"cmd.exe /c exit 1"};
76 #else
77  const std::string command{"false"};
78 #endif
79  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
80  const std::string what{e.what()};
81  BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
82  return true;
83  });
84  }
85  {
86  // Return non-zero exit code, with error message for stderr
87 #ifdef WIN32
88  const std::string command{"cmd.exe /c dir nosuchfile"};
89  const std::string expected{wine_runtime ? "File not found." : "File Not Found"};
90 #else
91  const std::string command{"ls nosuchfile"};
92  const std::string expected{"No such file or directory"};
93 #endif
94  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", command)) != std::string::npos);
97  BOOST_CHECK(what.find(expected) != std::string::npos);
98  return true;
99  });
100  }
101  {
102  BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON
103  }
104  // Test std::in, except for Windows
105 #ifndef WIN32
106  {
107  const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
108  BOOST_CHECK(result.isObject());
109  const UniValue& success = find_value(result, "success");
110  BOOST_CHECK(!success.isNull());
111  BOOST_CHECK_EQUAL(success.get_bool(), true);
112  }
113 #endif
114 }
115 #endif // ENABLE_EXTERNAL_SIGNER
116 
const auto command
bool isNull() const
Definition: univalue.h:76
bool get_bool() const
bool isObject() const
Definition: univalue.h:83
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
UniValue RunCommandParseJSON(const std::string &str_command, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition: run_command.cpp:27
Basic testing setup.
Definition: setup_common.h:79
BOOST_AUTO_TEST_CASE(dummy)
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:233
assert(!tx.IsCoinBase())