Bitcoin Core  22.99.0
P2P Digital Currency
system_tests.cpp
Go to the documentation of this file.
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 //
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 
32 {
33  {
34  const UniValue result = RunCommandParseJSON("");
35  BOOST_CHECK(result.isNull());
36  }
37  {
38 #ifdef WIN32
39  const UniValue result = RunCommandParseJSON("cmd.exe /c echo {\"success\": true}");
40 #else
41  const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
42 #endif
43  BOOST_CHECK(result.isObject());
44  const UniValue& success = find_value(result, "success");
45  BOOST_CHECK(!success.isNull());
46  BOOST_CHECK_EQUAL(success.getBool(), true);
47  }
48  {
49  // An invalid command is handled by Boost
50 #ifdef WIN32
51  const std::string expected{"The system cannot find the file specified."};
52 #else
53  const std::string expected{"No such file or directory"};
54 #endif
55  BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, [&](const boost::process::process_error& e) {
56  const std::string what(e.what());
57  BOOST_CHECK(what.find("RunCommandParseJSON error:") == std::string::npos);
58  BOOST_CHECK(what.find(expected) != std::string::npos);
59  return true;
60  });
61  }
62  {
63  // Return non-zero exit code, no output to stderr
64 #ifdef WIN32
65  const std::string command{"cmd.exe /c call"};
66 #else
67  const std::string command{"false"};
68 #endif
69  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
70  BOOST_CHECK(std::string(e.what()).find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
71  return true;
72  });
73  }
74  {
75  // Return non-zero exit code, with error message for stderr
76 #ifdef WIN32
77  const std::string command{"cmd.exe /c dir nosuchfile"};
78  const std::string expected{"File Not Found"};
79 #else
80  const std::string command{"ls nosuchfile"};
81  const std::string expected{"No such file or directory"};
82 #endif
83  BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
84  const std::string what(e.what());
85  BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos);
86  BOOST_CHECK(what.find(expected) != std::string::npos);
87  return true;
88  });
89  }
90  {
91  BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON
92  }
93  // Test std::in, except for Windows
94 #ifndef WIN32
95  {
96  const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
97  BOOST_CHECK(result.isObject());
98  const UniValue& success = find_value(result, "success");
99  BOOST_CHECK(!success.isNull());
100  BOOST_CHECK_EQUAL(success.getBool(), true);
101  }
102 #endif
103 }
104 #endif // ENABLE_EXTERNAL_SIGNER
105 
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:1262
UniValue::isNull
bool isNull() const
Definition: univalue.h:75
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
UniValue::getBool
bool getBool() const
Definition: univalue.h:68
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
UniValue
Definition: univalue.h:17
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(dummy)
Definition: system_tests.cpp:24
univalue.h
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:236
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:82