Bitcoin Core  22.99.0
P2P Digital Currency
system_tests.cpp
Go to the documentation of this file.
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 //
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 
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
25 {
26  BOOST_CHECK(true);
27 }
28 
29 #ifdef ENABLE_EXTERNAL_SIGNER
30 
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 
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 
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 
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 
checkMessageStdErr
bool checkMessageStdErr(const std::runtime_error &ex)
Definition: system_tests.cpp:46
setup_common.h
RunCommandParseJSON
UniValue RunCommandParseJSON(const std::string &str_command, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition: system.cpp:1256
UniValue::isNull
bool isNull() const
Definition: univalue.h:77
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
UniValue::getBool
bool getBool() const
Definition: univalue.h:70
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
UniValue
Definition: univalue.h:19
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(dummy)
Definition: system_tests.cpp:24
univalue.h
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
checkMessage
bool checkMessage(const std::runtime_error &ex)
Definition: system_tests.cpp:31
checkMessageFalse
bool checkMessageFalse(const std::runtime_error &ex)
Definition: system_tests.cpp:40
system.h
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
UniValue::isObject
bool isObject() const
Definition: univalue.h:84