Branch data Line data Source code
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
7 : : #include <test/util/setup_common.h>
8 : : #include <common/run_command.h>
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 : :
17 : : BOOST_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
21 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dummy)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
22 : : {
23 [ + - ]: 2 : BOOST_CHECK(true);
24 : 1 : }
25 : :
26 : : #ifdef ENABLE_EXTERNAL_SIGNER
27 : :
28 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(run_command)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
29 : : {
30 : 1 : {
31 [ + - + - ]: 2 : const UniValue result = RunCommandParseJSON("");
32 [ + - + - ]: 2 : BOOST_CHECK(result.isNull());
33 : 1 : }
34 : 1 : {
35 [ + - + - ]: 2 : const UniValue result = RunCommandParseJSON("echo {\"success\": true}");
36 [ + - + - : 2 : BOOST_CHECK(result.isObject());
+ - ]
37 [ + - ]: 1 : const UniValue& success = result.find_value("success");
38 [ + - + - : 2 : BOOST_CHECK(!success.isNull());
+ - ]
39 [ + - + - : 1 : BOOST_CHECK_EQUAL(success.get_bool(), true);
+ - ]
40 : 1 : }
41 : 1 : {
42 : : // An invalid command is handled by cpp-subprocess
43 : 1 : const std::string expected{"execve failed: "};
44 [ + - + - : 4 : BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), subprocess::CalledProcessError, HasReason(expected));
+ - - + -
- - - - +
+ - + - +
- + - ]
45 : 0 : }
46 : 1 : {
47 : : // Return non-zero exit code, no output to stderr
48 : 1 : const std::string command{"false"};
49 [ + - + - : 6 : 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 : 0 : }
55 : 1 : {
56 : : // Return non-zero exit code, with error message for stderr
57 : 1 : const std::string command{"ls nosuchfile"};
58 [ + - ]: 1 : const std::string expected{"No such file or directory"};
59 [ + - + - : 7 : 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 : 1 : }
66 : 1 : {
67 : : // Unable to parse JSON
68 : 1 : const std::string command{"echo {"};
69 [ + - + - : 4 : BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, HasReason("Unable to parse JSON: {"));
- + - - -
- - + + -
+ - + - +
- ]
70 : 0 : }
71 : : // Test std::in
72 : 1 : {
73 [ + - + - ]: 2 : const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
74 [ + - + - : 2 : BOOST_CHECK(result.isObject());
+ - ]
75 [ + - ]: 1 : const UniValue& success = result.find_value("success");
76 [ + - + - : 2 : BOOST_CHECK(!success.isNull());
+ - ]
77 [ + - + - : 1 : BOOST_CHECK_EQUAL(success.get_bool(), true);
+ - ]
78 : 1 : }
79 : 1 : }
80 : : #endif // ENABLE_EXTERNAL_SIGNER
81 : :
82 : : BOOST_AUTO_TEST_SUITE_END()
|