Bitcoin Core  27.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 //
5 
6 #if defined(HAVE_CONFIG_H)
8 #endif
10 #include <common/run_command.h>
11 #include <univalue.h>
12 
13 #ifdef ENABLE_EXTERNAL_SIGNER
14 #include <util/subprocess.hpp>
15 #endif // ENABLE_EXTERNAL_SIGNER
16 
17 #include <boost/test/unit_test.hpp>
18 
19 BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
20 
21 // At least one test is required (in case ENABLE_EXTERNAL_SIGNER is not defined).
22 // Workaround for https://github.com/bitcoin/bitcoin/issues/19128
24 {
25  BOOST_CHECK(true);
26 }
27 
28 #ifdef ENABLE_EXTERNAL_SIGNER
29 
31 {
32  {
33  const UniValue result = RunCommandParseJSON("");
34  BOOST_CHECK(result.isNull());
35  }
36  {
37  const UniValue result = RunCommandParseJSON("echo {\"success\": true}");
38  BOOST_CHECK(result.isObject());
39  const UniValue& success = result.find_value("success");
40  BOOST_CHECK(!success.isNull());
41  BOOST_CHECK_EQUAL(success.get_bool(), true);
42  }
43  {
44  // An invalid command is handled by cpp-subprocess
45  const std::string expected{"execve failed: "};
46  BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), subprocess::CalledProcessError, HasReason(expected));
47  }
48  {
49  // Return non-zero exit code, no output to stderr
50  const std::string command{"false"};
51  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
52  const std::string what{e.what()};
53  BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
54  return true;
55  });
56  }
57  {
58  // Return non-zero exit code, with error message for stderr
59  const std::string command{"ls nosuchfile"};
60  const std::string expected{"No such file or directory"};
61  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
62  const std::string what(e.what());
63  BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos);
64  BOOST_CHECK(what.find(expected) != std::string::npos);
65  return true;
66  });
67  }
68  {
69  // Unable to parse JSON
70  const std::string command{"echo {"};
71  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, HasReason("Unable to parse JSON: {"));
72  }
73  // Test std::in
74  {
75  const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
76  BOOST_CHECK(result.isObject());
77  const UniValue& success = result.find_value("success");
78  BOOST_CHECK(!success.isNull());
79  BOOST_CHECK_EQUAL(success.get_bool(), true);
80  }
81 }
82 #endif // ENABLE_EXTERNAL_SIGNER
83 
const auto command
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:244
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:233
bool isNull() const
Definition: univalue.h:79
bool get_bool() const
bool isObject() const
Definition: univalue.h:86
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:18
Basic testing setup.
Definition: setup_common.h:52
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