Skip to content

Commit 31cf68a

Browse files
committed
[util] add RunCommandParseJSON
1 parent c17f54e commit 31cf68a

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ BITCOIN_TESTS =\
265265
test/skiplist_tests.cpp \
266266
test/streams_tests.cpp \
267267
test/sync_tests.cpp \
268+
test/system_tests.cpp \
268269
test/util_threadnames_tests.cpp \
269270
test/timedata_tests.cpp \
270271
test/torcontrol_tests.cpp \

src/test/system_tests.cpp

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

src/util/system.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <sync.h>
77
#include <util/system.h>
88

9+
#ifdef HAVE_BOOST_PROCESS
10+
#include <boost/process.hpp>
11+
#endif // HAVE_BOOST_PROCESS
12+
913
#include <chainparamsbase.h>
1014
#include <util/strencodings.h>
1115
#include <util/string.h>
@@ -1161,6 +1165,43 @@ void runCommand(const std::string& strCommand)
11611165
}
11621166
#endif
11631167

1168+
#ifdef HAVE_BOOST_PROCESS
1169+
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
1170+
{
1171+
namespace bp = boost::process;
1172+
1173+
UniValue result_json;
1174+
bp::opstream stdin_stream;
1175+
bp::ipstream stdout_stream;
1176+
bp::ipstream stderr_stream;
1177+
1178+
if (str_command.empty()) return UniValue::VNULL;
1179+
1180+
bp::child c(
1181+
str_command,
1182+
bp::std_out > stdout_stream,
1183+
bp::std_err > stderr_stream,
1184+
bp::std_in < stdin_stream
1185+
);
1186+
if (!str_std_in.empty()) {
1187+
stdin_stream << str_std_in << std::endl;
1188+
}
1189+
stdin_stream.pipe().close();
1190+
1191+
std::string result;
1192+
std::string error;
1193+
std::getline(stdout_stream, result);
1194+
std::getline(stderr_stream, error);
1195+
1196+
c.wait();
1197+
const int n_error = c.exit_code();
1198+
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
1199+
if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
1200+
1201+
return result_json;
1202+
}
1203+
#endif // HAVE_BOOST_PROCESS
1204+
11641205
void SetupEnvironment()
11651206
{
11661207
#ifdef HAVE_MALLOPT_ARENA_MAX

src/util/system.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
3939

40+
class UniValue;
41+
4042
// Application startup time (used for uptime calculation)
4143
int64_t GetStartupTime();
4244

@@ -96,6 +98,16 @@ std::string ShellEscape(const std::string& arg);
9698
#if HAVE_SYSTEM
9799
void runCommand(const std::string& strCommand);
98100
#endif
101+
#ifdef HAVE_BOOST_PROCESS
102+
/**
103+
* Execute a command which returns JSON, and parse the result.
104+
*
105+
* @param str_command The command to execute, including any arguments
106+
* @param str_std_in string to pass to stdin
107+
* @return parsed JSON
108+
*/
109+
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in="");
110+
#endif // HAVE_BOOST_PROCESS
99111

100112
/**
101113
* Most paths passed as configuration arguments are treated as relative to

0 commit comments

Comments
 (0)