Branch data 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 <util/system.h> # 7 : : #include <univalue.h> # 8 : : # 9 : : #ifdef ENABLE_EXTERNAL_SIGNER # 10 : : #if defined(WIN32) && !defined(__kernel_entry) # 11 : : // A workaround for boost 1.71 incompatibility with mingw-w64 compiler. # 12 : : // For details see https://github.com/bitcoin/bitcoin/pull/22348. # 13 : : #define __kernel_entry # 14 : : #endif # 15 : : #if defined(__GNUC__) # 16 : : // Boost 1.78 requires the following workaround. # 17 : : // See: https://github.com/boostorg/process/issues/235 # 18 : : #pragma GCC diagnostic push # 19 : : #pragma GCC diagnostic ignored "-Wnarrowing" # 20 : : #endif # 21 : : #include <boost/process.hpp> # 22 : : #if defined(__GNUC__) # 23 : : #pragma GCC diagnostic pop # 24 : : #endif # 25 : : #endif // ENABLE_EXTERNAL_SIGNER # 26 : : # 27 : : #include <boost/test/unit_test.hpp> # 28 : : # 29 : : BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup) # 30 : : # 31 : : // At least one test is required (in case ENABLE_EXTERNAL_SIGNER is not defined). # 32 : : // Workaround for https://github.com/bitcoin/bitcoin/issues/19128 # 33 : : BOOST_AUTO_TEST_CASE(dummy) # 34 : 2 : { # 35 : 2 : BOOST_CHECK(true); # 36 : 2 : } # 37 : : # 38 : : #ifdef ENABLE_EXTERNAL_SIGNER # 39 : : # 40 : : BOOST_AUTO_TEST_CASE(run_command) # 41 : 2 : { # 42 : 2 : { # 43 : 2 : const UniValue result = RunCommandParseJSON(""); # 44 : 2 : BOOST_CHECK(result.isNull()); # 45 : 2 : } # 46 : 2 : { # 47 : : #ifdef WIN32 # 48 : : const UniValue result = RunCommandParseJSON("cmd.exe /c echo {\"success\": true}"); # 49 : : #else # 50 : 2 : const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\""); # 51 : 2 : #endif # 52 : 2 : BOOST_CHECK(result.isObject()); # 53 : 2 : const UniValue& success = find_value(result, "success"); # 54 : 2 : BOOST_CHECK(!success.isNull()); # 55 : 2 : BOOST_CHECK_EQUAL(success.getBool(), true); # 56 : 2 : } # 57 : 2 : { # 58 : : // An invalid command is handled by Boost # 59 : : #ifdef WIN32 # 60 : : const std::string expected{"The system cannot find the file specified."}; # 61 : : #else # 62 : 2 : const std::string expected{"No such file or directory"}; # 63 : 2 : #endif # 64 : 2 : BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, [&](const boost::process::process_error& e) { # 65 : 2 : const std::string what(e.what()); # 66 : 2 : BOOST_CHECK(what.find("RunCommandParseJSON error:") == std::string::npos); # 67 : 2 : BOOST_CHECK(what.find(expected) != std::string::npos); # 68 : 2 : return true; # 69 : 2 : }); # 70 : 2 : } # 71 : 2 : { # 72 : : // Return non-zero exit code, no output to stderr # 73 : : #ifdef WIN32 # 74 : : const std::string command{"cmd.exe /c call"}; # 75 : : #else # 76 : 2 : const std::string command{"false"}; # 77 : 2 : #endif # 78 : 2 : BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) { # 79 : 2 : BOOST_CHECK(std::string(e.what()).find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos); # 80 : 2 : return true; # 81 : 2 : }); # 82 : 2 : } # 83 : 2 : { # 84 : : // Return non-zero exit code, with error message for stderr # 85 : : #ifdef WIN32 # 86 : : const std::string command{"cmd.exe /c dir nosuchfile"}; # 87 : : const std::string expected{"File Not Found"}; # 88 : : #else # 89 : 2 : const std::string command{"ls nosuchfile"}; # 90 : 2 : const std::string expected{"No such file or directory"}; # 91 : 2 : #endif # 92 : 2 : BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) { # 93 : 2 : const std::string what(e.what()); # 94 : 2 : BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos); # 95 : 2 : BOOST_CHECK(what.find(expected) != std::string::npos); # 96 : 2 : return true; # 97 : 2 : }); # 98 : 2 : } # 99 : 2 : { # 100 : 2 : BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON # 101 : 2 : } # 102 : : // Test std::in, except for Windows # 103 : 2 : #ifndef WIN32 # 104 : 2 : { # 105 : 2 : const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}"); # 106 : 2 : BOOST_CHECK(result.isObject()); # 107 : 2 : const UniValue& success = find_value(result, "success"); # 108 : 2 : BOOST_CHECK(!success.isNull()); # 109 : 2 : BOOST_CHECK_EQUAL(success.getBool(), true); # 110 : 2 : } # 111 : 2 : #endif # 112 : 2 : } # 113 : : #endif // ENABLE_EXTERNAL_SIGNER # 114 : : # 115 : : BOOST_AUTO_TEST_SUITE_END()