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