Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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#include <config/bitcoin-config.h> // IWYU pragma: keep
9#include <univalue.h>
10
11#ifdef ENABLE_EXTERNAL_SIGNER
12#include <util/subprocess.h>
13#endif // ENABLE_EXTERNAL_SIGNER
14
15#include <boost/test/unit_test.hpp>
16
17BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
18
19// At least one test is required (in case ENABLE_EXTERNAL_SIGNER is not defined).
20// Workaround for https://github.com/bitcoin/bitcoin/issues/19128
22{
23 BOOST_CHECK(true);
24}
25
26#ifdef ENABLE_EXTERNAL_SIGNER
27
29{
30 {
31 const UniValue result = RunCommandParseJSON("");
32 BOOST_CHECK(result.isNull());
33 }
34 {
35 const UniValue result = RunCommandParseJSON("echo {\"success\": true}");
36 BOOST_CHECK(result.isObject());
37 const UniValue& success = result.find_value("success");
38 BOOST_CHECK(!success.isNull());
39 BOOST_CHECK_EQUAL(success.get_bool(), true);
40 }
41 {
42 // An invalid command is handled by cpp-subprocess
43 const std::string expected{"execve failed: "};
44 BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), subprocess::CalledProcessError, HasReason(expected));
45 }
46 {
47 // Return non-zero exit code, no output to stderr
48 const std::string command{"false"};
49 BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
50 const std::string what{e.what()};
51 BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
52 return true;
53 });
54 }
55 {
56 // Return non-zero exit code, with error message for stderr
57 const std::string command{"sh -c 'echo err 1>&2 && false'"};
58 const std::string expected{"err"};
59 BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
60 const std::string what(e.what());
61 BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos);
62 BOOST_CHECK(what.find(expected) != std::string::npos);
63 return true;
64 });
65 }
66 {
67 // Unable to parse JSON
68 const std::string command{"echo {"};
69 BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, HasReason("Unable to parse JSON: {"));
70 }
71 // Test std::in
72 {
73 const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
74 BOOST_CHECK(result.isObject());
75 const UniValue& success = result.find_value("success");
76 BOOST_CHECK(!success.isNull());
77 BOOST_CHECK_EQUAL(success.get_bool(), true);
78 }
79}
80#endif // ENABLE_EXTERNAL_SIGNER
81
const auto command
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
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.
Basic testing setup.
BOOST_AUTO_TEST_CASE(dummy)
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...