Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
qt
test
rpcnestedtests.cpp
Go to the documentation of this file.
1
// Copyright (c) 2016-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
#include <
qt/test/rpcnestedtests.h
>
6
7
#include <
common/system.h
>
8
#include <
interfaces/node.h
>
9
#include <
qt/rpcconsole.h
>
10
#include <
rpc/server.h
>
11
#include <
test/util/setup_common.h
>
12
#include <
univalue.h
>
13
14
#include <QTest>
15
16
#include <string>
17
#include <stdexcept>
18
19
static
RPCHelpMan
rpcNestedTest_rpc
()
20
{
21
return
RPCHelpMan
{
22
"rpcNestedTest"
,
23
"echo the passed string(s)"
,
24
{
25
{
"arg1"
,
RPCArg::Type::STR
,
RPCArg::Optional::OMITTED
,
""
},
26
{
"arg2"
,
RPCArg::Type::STR
,
RPCArg::Optional::OMITTED
,
""
},
27
{
"arg3"
,
RPCArg::Type::STR
,
RPCArg::Optional::OMITTED
,
""
},
28
},
29
RPCResult
{
RPCResult::Type::ANY
,
""
,
""
},
30
RPCExamples
{
""
},
31
[&](
const
RPCHelpMan
& self,
const
JSONRPCRequest
& request) ->
UniValue
{
32
return
request.params.write(0, 0);
33
},
34
};
35
}
36
37
static
const
CRPCCommand
vRPCCommands
[] = {
38
{
"rpcNestedTest"
, &
rpcNestedTest_rpc
},
39
};
40
41
void
RPCNestedTests::rpcNestedTests
()
42
{
43
// do some test setup
44
// could be moved to a more generic place when we add more tests on QT level
45
for
(
const
auto
& c :
vRPCCommands
) {
46
tableRPC
.appendCommand(c.name, &c);
47
}
48
49
TestingSetup
test;
50
m_node
.setContext(&test.
m_node
);
51
52
if
(
RPCIsInWarmup
(
nullptr
))
SetRPCWarmupFinished
();
53
54
std::string result;
55
std::string result2;
56
std::string filtered;
57
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo()[chain]"
, &filtered);
//simple result filtering with path
58
QVERIFY(result==
"main"
);
59
QVERIFY(filtered ==
"getblockchaininfo()[chain]"
);
60
61
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblock(getbestblockhash())"
);
//simple 2 level nesting
62
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblock(getblock(getbestblockhash())[hash], true)"
);
63
64
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true)"
);
//4 level nesting with whitespace, filtering path and boolean parameter
65
66
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo"
);
67
QVERIFY(result.starts_with(
"{"
));
68
69
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo()"
);
70
QVERIFY(result.starts_with(
"{"
));
71
72
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo "
);
//whitespace at the end will be tolerated
73
QVERIFY(result.starts_with(
"{"
));
74
75
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo()[\"chain\"]"
);
//Quote path identifier are allowed, but look after a child containing the quotes in the key
76
QVERIFY(result ==
"null"
);
77
78
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"createrawtransaction [] {} 0"
);
//parameter not in brackets are allowed
79
RPCConsole::RPCExecuteCommandLine
(
m_node
, result2,
"createrawtransaction([],{},0)"
);
//parameter in brackets are allowed
80
QVERIFY(result == result2);
81
RPCConsole::RPCExecuteCommandLine
(
m_node
, result2,
"createrawtransaction( [], {} , 0 )"
);
//whitespace between parameters is allowed
82
QVERIFY(result == result2);
83
84
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblock(getbestblockhash())[tx][0]"
, &filtered);
85
QVERIFY(result ==
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
);
86
QVERIFY(filtered ==
"getblock(getbestblockhash())[tx][0]"
);
87
88
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"createwallet test true"
,
false
, &filtered);
89
QVERIFY(filtered ==
"createwallet(…)"
);
90
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"createwalletdescriptor abc"
,
false
, &filtered);
91
QVERIFY(filtered ==
"createwalletdescriptor(…)"
);
92
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"migratewallet abc abc"
,
false
, &filtered);
93
QVERIFY(filtered ==
"migratewallet(…)"
);
94
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"signmessagewithprivkey abc"
,
false
, &filtered);
95
QVERIFY(filtered ==
"signmessagewithprivkey(…)"
);
96
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"signmessagewithprivkey abc,def"
,
false
, &filtered);
97
QVERIFY(filtered ==
"signmessagewithprivkey(…)"
);
98
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"signrawtransactionwithkey(abc)"
,
false
, &filtered);
99
QVERIFY(filtered ==
"signrawtransactionwithkey(…)"
);
100
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"walletpassphrase(help())"
,
false
, &filtered);
101
QVERIFY(filtered ==
"walletpassphrase(…)"
);
102
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"walletpassphrasechange(help(walletpassphrasechange(abc)))"
,
false
, &filtered);
103
QVERIFY(filtered ==
"walletpassphrasechange(…)"
);
104
RPCConsole::RPCParseCommandLine
(
nullptr
, result,
"help(encryptwallet(abc, def))"
,
false
, &filtered);
105
QVERIFY(filtered ==
"help(encryptwallet(…))"
);
106
107
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest"
);
108
QVERIFY(result ==
"[]"
);
109
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest ''"
);
110
QVERIFY(result ==
"[\"\"]"
);
111
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest \"\""
);
112
QVERIFY(result ==
"[\"\"]"
);
113
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest '' abc"
);
114
QVERIFY(result ==
"[\"\",\"abc\"]"
);
115
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest abc '' abc"
);
116
QVERIFY(result ==
"[\"abc\",\"\",\"abc\"]"
);
117
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest abc abc"
);
118
QVERIFY(result ==
"[\"abc\",\"abc\"]"
);
119
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest abc\t\tabc"
);
120
QVERIFY(result ==
"[\"abc\",\"abc\"]"
);
121
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest(abc )"
);
122
QVERIFY(result ==
"[\"abc\"]"
);
123
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest( abc )"
);
124
QVERIFY(result ==
"[\"abc\"]"
);
125
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest( abc , cba )"
);
126
QVERIFY(result ==
"[\"abc\",\"cba\"]"
);
127
128
// Handle deprecated macro, can be removed once minimum Qt is at least 6.3.0.
129
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
130
#undef QVERIFY_EXCEPTION_THROWN
131
#define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) QVERIFY_THROWS_EXCEPTION(exceptiontype, expression)
132
#endif
133
QVERIFY_EXCEPTION_THROWN
(
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo() .\n"
), std::runtime_error);
//invalid syntax
134
QVERIFY_EXCEPTION_THROWN
(
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo() getblockchaininfo()"
), std::runtime_error);
//invalid syntax
135
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo("
);
//tolerate non closing brackets if we have no arguments
136
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo()()()"
);
//tolerate non command brackets
137
QVERIFY_EXCEPTION_THROWN
(
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"getblockchaininfo(True)"
),
UniValue
);
//invalid argument
138
QVERIFY_EXCEPTION_THROWN
(
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"a(getblockchaininfo(True))"
),
UniValue
);
//method not found
139
QVERIFY_EXCEPTION_THROWN
(
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest abc,,abc"
), std::runtime_error);
//don't tolerate empty arguments when using ,
140
QVERIFY_EXCEPTION_THROWN
(
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest(abc,,abc)"
), std::runtime_error);
//don't tolerate empty arguments when using ,
141
QVERIFY_EXCEPTION_THROWN
(
RPCConsole::RPCExecuteCommandLine
(
m_node
, result,
"rpcNestedTest(abc,,)"
), std::runtime_error);
//don't tolerate empty arguments when using ,
142
}
CRPCCommand
Definition
server.h:42
JSONRPCRequest
Definition
request.h:53
RPCConsole::RPCExecuteCommandLine
static bool RPCExecuteCommandLine(interfaces::Node &node, std::string &strResult, const std::string &strCommand, std::string *const pstrFilteredOut=nullptr, const QString &wallet_name={})
Definition
rpcconsole.h:52
RPCConsole::RPCParseCommandLine
static bool RPCParseCommandLine(interfaces::Node *node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string *pstrFilteredOut=nullptr, const QString &wallet_name={})
Split shell command line into a list of arguments and optionally execute the command(s).
Definition
rpcconsole.cpp:141
RPCHelpMan
Definition
util.h:419
RPCNestedTests::rpcNestedTests
void rpcNestedTests()
Definition
rpcnestedtests.cpp:41
RPCNestedTests::m_node
interfaces::Node & m_node
Definition
rpcnestedtests.h:19
UniValue
Definition
univalue.h:22
node.h
rpcconsole.h
QVERIFY_EXCEPTION_THROWN
#define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype)
rpcNestedTest_rpc
static RPCHelpMan rpcNestedTest_rpc()
Definition
rpcnestedtests.cpp:19
vRPCCommands
static const CRPCCommand vRPCCommands[]
Definition
rpcnestedtests.cpp:37
rpcnestedtests.h
SetRPCWarmupFinished
void SetRPCWarmupFinished()
Definition
server.cpp:324
RPCIsInWarmup
bool RPCIsInWarmup(std::string *outStatus)
Definition
server.cpp:331
tableRPC
CRPCTable tableRPC
Definition
server.cpp:544
server.h
setup_common.h
BasicTestingSetup::m_node
node::NodeContext m_node
Definition
setup_common.h:66
RPCArg::Type::STR
@ STR
Definition
util.h:190
RPCArg::Optional::OMITTED
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
Definition
util.h:217
RPCExamples
Definition
util.h:408
RPCResult
Definition
util.h:296
RPCResult::Type::ANY
@ ANY
Special type to disable type checks (for testing only).
Definition
util.h:304
TestingSetup
Testing setup that configures a complete environment.
Definition
setup_common.h:121
system.h
univalue.h
Generated on
for Bitcoin Core by
1.17.0