Bitcoin Core 30.99.0
P2P Digital Currency
system_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-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
6#include <bitcoin-build-config.h> // IWYU pragma: keep
7
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#include <boost/test/unit_test.hpp>
18
20
21#ifdef ENABLE_EXTERNAL_SIGNER
22
24{
25 {
26 const UniValue result = RunCommandParseJSON({});
27 BOOST_CHECK(result.isNull());
28 }
29 {
30#ifdef WIN32
31 const UniValue result = RunCommandParseJSON({"cmd.exe", "/c", "echo", "{\"success\":", "true}"}); // The command is intentionally split "incorrectly", to exactly preserve previous behavior. This is due to the cmd.exe internal echo quoting strings with spaces in it, unlike the normal 'echo' below.
32#else
33 const UniValue result = RunCommandParseJSON({"echo", "{\"success\": true}"});
34#endif
35 BOOST_CHECK(result.isObject());
36 const UniValue& success = result.find_value("success");
37 BOOST_CHECK(!success.isNull());
38 BOOST_CHECK_EQUAL(success.get_bool(), true);
39 }
40 {
41 // An invalid command is handled by cpp-subprocess
42#ifdef WIN32
43 const std::string expected{"CreateProcess failed: "};
44#else
45 const std::string expected{"execve failed: "};
46#endif
47 BOOST_CHECK_EXCEPTION(RunCommandParseJSON({"invalid_command"}), subprocess::CalledProcessError, HasReason(expected));
48 }
49 {
50 // Return non-zero exit code, no output to stderr
51#ifdef WIN32
52 const std::vector<std::string> command = {"cmd.exe", "/c", "exit 1"};
53#else
54 const std::vector<std::string> command = {"false"};
55#endif
56 BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
57 const std::string what{e.what()};
58 BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", util::Join(command, " "))) != std::string::npos);
59 return true;
60 });
61 }
62 {
63 // Return non-zero exit code, with error message for stderr
64#ifdef WIN32
65 const std::vector<std::string> command = {"cmd.exe", "/c", "echo err 1>&2 && exit 1"};
66#else
67 const std::vector<std::string> command = {"sh", "-c", "echo err 1>&2 && false"};
68#endif
69 const std::string expected{"err"};
70 BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
71 const std::string what(e.what());
72 BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned %s: %s", util::Join(command, " "), 1, "err")) != std::string::npos);
73 BOOST_CHECK(what.find(expected) != std::string::npos);
74 return true;
75 });
76 }
77 {
78 // Unable to parse JSON
79#ifdef WIN32
80 const std::vector<std::string> command = {"cmd.exe", "/c", "echo {"};
81#else
82 const std::vector<std::string> command = {"echo", "{"};
83#endif
84 BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, HasReason("Unable to parse JSON: {"));
85 }
86#ifndef WIN32
87 {
88 // Test stdin
89 const UniValue result = RunCommandParseJSON({"cat"}, "{\"success\": true}");
90 BOOST_CHECK(result.isObject());
91 const UniValue& success = result.find_value("success");
92 BOOST_CHECK(!success.isNull());
93 BOOST_CHECK_EQUAL(success.get_bool(), true);
94 }
95#endif
96}
97#endif // ENABLE_EXTERNAL_SIGNER
98
const auto command
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:293
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:232
bool isNull() const
Definition: univalue.h:81
bool get_bool() const
bool isObject() const
Definition: univalue.h:88
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:205
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
UniValue RunCommandParseJSON(const std::vector< std::string > &cmd_args, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition: run_command.cpp:17
Basic testing setup.
Definition: setup_common.h:64
BOOST_AUTO_TEST_CASE(run_command)
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172