Bitcoin Core  24.1.0
P2P Digital Currency
system_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 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 <util/system.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  {
38  const UniValue result = RunCommandParseJSON("");
39  BOOST_CHECK(result.isNull());
40  }
41  {
42 #ifdef WIN32
43  const UniValue result = RunCommandParseJSON("cmd.exe /c echo {\"success\": true}");
44 #else
45  const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
46 #endif
47  BOOST_CHECK(result.isObject());
48  const UniValue& success = find_value(result, "success");
49  BOOST_CHECK(!success.isNull());
50  BOOST_CHECK_EQUAL(success.getBool(), true);
51  }
52  {
53  // An invalid command is handled by Boost
54 #ifdef WIN32
55  const std::string expected{"The system cannot find the file specified."};
56 #else
57  const std::string expected{"No such file or directory"};
58 #endif
59  BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, [&](const boost::process::process_error& e) {
60  const std::string what(e.what());
61  BOOST_CHECK(what.find("RunCommandParseJSON error:") == std::string::npos);
62  BOOST_CHECK(what.find(expected) != std::string::npos);
63  return true;
64  });
65  }
66  {
67  // Return non-zero exit code, no output to stderr
68 #ifdef WIN32
69  const std::string command{"cmd.exe /c call"};
70 #else
71  const std::string command{"false"};
72 #endif
73  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
74  BOOST_CHECK(std::string(e.what()).find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
75  return true;
76  });
77  }
78  {
79  // Return non-zero exit code, with error message for stderr
80 #ifdef WIN32
81  const std::string command{"cmd.exe /c dir nosuchfile"};
82  const std::string expected{"File Not Found"};
83 #else
84  const std::string command{"ls nosuchfile"};
85  const std::string expected{"No such file or directory"};
86 #endif
87  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
88  const std::string what(e.what());
89  BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos);
90  BOOST_CHECK(what.find(expected) != std::string::npos);
91  return true;
92  });
93  }
94  {
95  BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON
96  }
97  // Test std::in, except for Windows
98 #ifndef WIN32
99  {
100  const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
101  BOOST_CHECK(result.isObject());
102  const UniValue& success = find_value(result, "success");
103  BOOST_CHECK(!success.isNull());
104  BOOST_CHECK_EQUAL(success.getBool(), true);
105  }
106 #endif
107 }
108 #endif // ENABLE_EXTERNAL_SIGNER
109 
UniValue RunCommandParseJSON(const std::string &str_command, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition: system.cpp:1335
bool isObject() const
Definition: univalue.h:81
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:233
Basic testing setup.
Definition: setup_common.h:83
BOOST_AUTO_TEST_SUITE_END()
bool isNull() const
Definition: univalue.h:74
const auto command
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
BOOST_AUTO_TEST_CASE(dummy)
#define BOOST_CHECK(expr)
Definition: object.cpp:16