Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
system_tests.cpp
Go to the documentation of this file.
1
// Copyright (c) 2019-present 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
6
#include <bitcoin-build-config.h>
// IWYU pragma: keep
7
8
#include <
common/run_command.h
>
9
#include <
test/util/common.h
>
10
#include <
test/util/setup_common.h
>
11
#include <
univalue.h
>
12
#include <
util/string.h
>
13
14
#ifdef ENABLE_EXTERNAL_SIGNER
15
#include <
util/subprocess.h
>
16
#endif
// ENABLE_EXTERNAL_SIGNER
17
18
#include <boost/cstdlib.hpp>
19
#include <boost/test/unit_test.hpp>
20
21
#include <string>
22
23
BOOST_FIXTURE_TEST_SUITE
(system_tests,
BasicTestingSetup
)
24
25
#ifdef ENABLE_EXTERNAL_SIGNER
26
27
static
std::vector<std::string>
mock_executable
(std::string
name
)
28
{
29
return
{boost::unit_test::framework::master_test_suite().argv[0],
"--log_level=nothing"
,
"--report_level=no"
,
"--run_test=mock_process/"
+
name
};
30
}
31
32
BOOST_AUTO_TEST_CASE
(run_command)
33
{
34
{
35
const
UniValue
result =
RunCommandParseJSON
({});
36
BOOST_CHECK
(result.
isNull
());
37
}
38
{
39
const
UniValue
result =
RunCommandParseJSON
(
mock_executable
(
"valid_json"
));
40
BOOST_CHECK
(result.
isObject
());
41
const
UniValue
& success = result.
find_value
(
"success"
);
42
BOOST_CHECK
(!success.
isNull
());
43
BOOST_CHECK_EQUAL
(success.
get_bool
(),
true
);
44
}
45
{
46
// An invalid command is handled by cpp-subprocess
47
#ifdef WIN32
48
const
std::string expected{
"CreateProcess failed: "
};
49
#else
50
const
std::string expected{
"execve failed: "
};
51
#endif
52
BOOST_CHECK_EXCEPTION(
RunCommandParseJSON
({
"invalid_command"
}),
subprocess::CalledProcessError
,
HasReason
(expected));
53
}
54
{
55
// Return non-zero exit code, no output to stderr
56
const
std::vector<std::string>
command
=
mock_executable
(
"nonzeroexit_nooutput"
);
57
BOOST_CHECK_EXCEPTION(
RunCommandParseJSON
(
command
), std::runtime_error, [&](
const
std::runtime_error& e) {
58
const
std::string what{e.what()};
59
BOOST_CHECK
(what.find(
strprintf
(
"RunCommandParseJSON error: process(%s) returned %d: \n"
,
util::Join
(
command
,
" "
), boost::exit_test_failure)) != std::string::npos);
60
return
true
;
61
});
62
}
63
{
64
// Return non-zero exit code, with error message for stderr
65
const
std::vector<std::string>
command
=
mock_executable
(
"nonzeroexit_stderroutput"
);
66
const
std::string expected{
"err"
};
67
BOOST_CHECK_EXCEPTION(
RunCommandParseJSON
(
command
), std::runtime_error, [&](
const
std::runtime_error& e) {
68
const
std::string what(e.what());
69
BOOST_CHECK
(what.find(
strprintf
(
"RunCommandParseJSON error: process(%s) returned %s: %s"
,
util::Join
(
command
,
" "
), boost::exit_test_failure,
"err"
)) != std::string::npos);
70
BOOST_CHECK
(what.find(expected) != std::string::npos);
71
return
true
;
72
});
73
}
74
{
75
// Unable to parse JSON
76
BOOST_CHECK_EXCEPTION(
RunCommandParseJSON
(
mock_executable
(
"invalid_json"
)), std::runtime_error,
HasReason
(
"Unable to parse JSON: {"
));
77
}
78
{
79
// Test stdin
80
const
UniValue
result =
RunCommandParseJSON
(
mock_executable
(
"pass_stdin_to_stdout"
),
"{\"success\": true}"
);
81
BOOST_CHECK
(result.
isObject
());
82
const
UniValue
& success = result.
find_value
(
"success"
);
83
BOOST_CHECK
(!success.
isNull
());
84
BOOST_CHECK_EQUAL
(success.
get_bool
(),
true
);
85
}
86
}
87
#endif
// ENABLE_EXTERNAL_SIGNER
88
89
BOOST_AUTO_TEST_SUITE_END
()
command
const auto command
Definition
bitcoin-wallet.cpp:116
HasReason
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition
common.h:18
UniValue
Definition
univalue.h:22
UniValue::find_value
const UniValue & find_value(std::string_view key) const
Definition
univalue.cpp:232
UniValue::isNull
bool isNull() const
Definition
univalue.h:81
UniValue::get_bool
bool get_bool() const
Definition
univalue_get.cpp:56
UniValue::isObject
bool isObject() const
Definition
univalue.h:88
subprocess::CalledProcessError
Definition
subprocess.h:140
BOOST_FIXTURE_TEST_SUITE
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
util::Join
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition
string.h:205
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition
object.cpp:17
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition
object.cpp:16
name
const char * name
Definition
rest.cpp:48
RunCommandParseJSON
UniValue RunCommandParseJSON(const std::vector< std::string > &cmd_args, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition
run_command.cpp:17
run_command.h
setup_common.h
string.h
BasicTestingSetup
Basic testing setup.
Definition
setup_common.h:64
subprocess.h
mock_executable
static std::vector< std::string > mock_executable(std::string name)
Definition
system_tests.cpp:27
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(run_command)
Definition
system_tests.cpp:32
common.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition
tinyformat.h:1172
univalue.h
Generated on
for Bitcoin Core by
1.17.0