1 // Copyright (c) 2019-2020 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 #include <boost/process.hpp>
16 #endif // ENABLE_EXTERNAL_SIGNER
17 
18 #include <boost/test/unit_test.hpp>
19 
BOOST_FIXTURE_TEST_SUITE(system_tests,BasicTestingSetup)20 BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
21 
22 // At least one test is required (in case ENABLE_EXTERNAL_SIGNER is not defined).
23 // Workaround for https://github.com/bitcoin/bitcoin/issues/19128
24 BOOST_AUTO_TEST_CASE(dummy)
25 {
26     BOOST_CHECK(true);
27 }
28 
29 #ifdef ENABLE_EXTERNAL_SIGNER
30 
checkMessage(const std::runtime_error & ex)31 bool checkMessage(const std::runtime_error& ex)
32 {
33     // On Linux & Mac: "No such file or directory"
34     // On Windows: "The system cannot find the file specified."
35     const std::string what(ex.what());
36     BOOST_CHECK(what.find("file") != std::string::npos);
37     return true;
38 }
39 
checkMessageFalse(const std::runtime_error & ex)40 bool checkMessageFalse(const std::runtime_error& ex)
41 {
42     BOOST_CHECK_EQUAL(ex.what(), std::string("RunCommandParseJSON error: process(false) returned 1: \n"));
43     return true;
44 }
45 
checkMessageStdErr(const std::runtime_error & ex)46 bool checkMessageStdErr(const std::runtime_error& ex)
47 {
48     const std::string what(ex.what());
49     BOOST_CHECK(what.find("RunCommandParseJSON error:") != std::string::npos);
50     return checkMessage(ex);
51 }
52 
BOOST_AUTO_TEST_CASE(run_command)53 BOOST_AUTO_TEST_CASE(run_command)
54 {
55     {
56         const UniValue result = RunCommandParseJSON("");
57         BOOST_CHECK(result.isNull());
58     }
59     {
60 #ifdef WIN32
61         // Windows requires single quotes to prevent escaping double quotes from the JSON...
62         const UniValue result = RunCommandParseJSON("echo '{\"success\": true}'");
63 #else
64         // ... but Linux and macOS echo a single quote if it's used
65         const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
66 #endif
67         BOOST_CHECK(result.isObject());
68         const UniValue& success = find_value(result, "success");
69         BOOST_CHECK(!success.isNull());
70         BOOST_CHECK_EQUAL(success.getBool(), true);
71     }
72     {
73         // An invalid command is handled by Boost
74         BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, checkMessage); // Command failed
75     }
76     {
77         // Return non-zero exit code, no output to stderr
78         BOOST_CHECK_EXCEPTION(RunCommandParseJSON("false"), std::runtime_error, checkMessageFalse);
79     }
80     {
81         // Return non-zero exit code, with error message for stderr
82         BOOST_CHECK_EXCEPTION(RunCommandParseJSON("ls nosuchfile"), std::runtime_error, checkMessageStdErr);
83     }
84     {
85         BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON
86     }
87     // Test std::in, except for Windows
88 #ifndef WIN32
89     {
90         const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
91         BOOST_CHECK(result.isObject());
92         const UniValue& success = find_value(result, "success");
93         BOOST_CHECK(!success.isNull());
94         BOOST_CHECK_EQUAL(success.getBool(), true);
95     }
96 #endif
97 }
98 #endif // ENABLE_EXTERNAL_SIGNER
99 
100 BOOST_AUTO_TEST_SUITE_END()
101