Bitcoin Core  22.99.0
P2P Digital Currency
rpcnestedtests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-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 
7 #include <interfaces/node.h>
8 #include <rpc/server.h>
9 #include <qt/rpcconsole.h>
10 #include <test/util/setup_common.h>
11 #include <univalue.h>
12 #include <util/system.h>
13 
14 #include <QTest>
15 
16 #include <string>
17 #include <stdexcept>
18 
20 {
21  return RPCHelpMan{
22  "rpcNestedTest",
23  "echo the passed string(s)",
24  {
28  },
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 
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.substr(0,1) == "{");
68 
69  RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo()");
70  QVERIFY(result.substr(0,1) == "{");
71 
72  RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo "); //whitespace at the end will be tolerated
73  QVERIFY(result.substr(0,1) == "{");
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, "importprivkey", false, &filtered);
89  QVERIFY(filtered == "importprivkey(…)");
90  RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc", false, &filtered);
91  QVERIFY(filtered == "signmessagewithprivkey(…)");
92  RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc,def", false, &filtered);
93  QVERIFY(filtered == "signmessagewithprivkey(…)");
94  RPCConsole::RPCParseCommandLine(nullptr, result, "signrawtransactionwithkey(abc)", false, &filtered);
95  QVERIFY(filtered == "signrawtransactionwithkey(…)");
96  RPCConsole::RPCParseCommandLine(nullptr, result, "walletpassphrase(help())", false, &filtered);
97  QVERIFY(filtered == "walletpassphrase(…)");
98  RPCConsole::RPCParseCommandLine(nullptr, result, "walletpassphrasechange(help(walletpassphrasechange(abc)))", false, &filtered);
99  QVERIFY(filtered == "walletpassphrasechange(…)");
100  RPCConsole::RPCParseCommandLine(nullptr, result, "help(encryptwallet(abc, def))", false, &filtered);
101  QVERIFY(filtered == "help(encryptwallet(…))");
102  RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey())", false, &filtered);
103  QVERIFY(filtered == "help(importprivkey(…))");
104  RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey(help()))", false, &filtered);
105  QVERIFY(filtered == "help(importprivkey(…))");
106  RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey(abc), walletpassphrase(def))", false, &filtered);
107  QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))");
108 
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 \"\"");
114  QVERIFY(result == "[\"\"]");
115  RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest '' abc");
116  QVERIFY(result == "[\"\",\"abc\"]");
117  RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc '' abc");
118  QVERIFY(result == "[\"abc\",\"\",\"abc\"]");
119  RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc abc");
120  QVERIFY(result == "[\"abc\",\"abc\"]");
121  RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc\t\tabc");
122  QVERIFY(result == "[\"abc\",\"abc\"]");
123  RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest(abc )");
124  QVERIFY(result == "[\"abc\"]");
125  RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest( abc )");
126  QVERIFY(result == "[\"abc\"]");
127  RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest( abc , cba )");
128  QVERIFY(result == "[\"abc\",\"cba\"]");
129 
130  QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo() .\n"), std::runtime_error); //invalid syntax
131  QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo() getblockchaininfo()"), std::runtime_error); //invalid syntax
132  RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo("); //tolerate non closing brackets if we have no arguments
133  RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo()()()"); //tolerate non command brackets
134  QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo(True)"), UniValue); //invalid argument
135  QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "a(getblockchaininfo(True))"), UniValue); //method not found
136  QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc,,abc"), std::runtime_error); //don't tolerate empty arguments when using ,
137  QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest(abc,,abc)"), std::runtime_error); //don't tolerate empty arguments when using ,
138  QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest(abc,,)"), std::runtime_error); //don't tolerate empty arguments when using ,
139 }
RPCNestedTests::rpcNestedTests
void rpcNestedTests()
Definition: rpcnestedtests.cpp:41
setup_common.h
RPCHelpMan
Definition: util.h:345
RPCArg::Type::STR
@ STR
vRPCCommands
static const CRPCCommand vRPCCommands[]
Definition: rpcnestedtests.cpp:37
RPCConsole::RPCParseCommandLine
static bool RPCParseCommandLine(interfaces::Node *node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string *const pstrFilteredOut=nullptr, const WalletModel *wallet_model=nullptr)
Split shell command line into a list of arguments and optionally execute the command(s).
Definition: rpcconsole.cpp:173
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:29
rpcNestedTest_rpc
static RPCHelpMan rpcNestedTest_rpc()
Definition: rpcnestedtests.cpp:19
UniValue
Definition: univalue.h:19
rpcconsole.h
SetRPCWarmupFinished
void SetRPCWarmupFinished()
Definition: server.cpp:337
CRPCCommand
Definition: server.h:90
node.h
univalue.h
tableRPC
CRPCTable tableRPC
Definition: server.cpp:548
RPCExamples
Definition: util.h:335
interfaces::Node::setContext
virtual void setContext(NodeContext *context)
Definition: node.h:229
RPCNestedTests::m_node
interfaces::Node & m_node
Definition: rpcnestedtests.h:19
RPCResult::Type::ANY
@ ANY
Special type to disable type checks (for testing only)
system.h
CRPCTable::appendCommand
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:270
BasicTestingSetup::m_node
NodeContext m_node
Definition: setup_common.h:78
rpcnestedtests.h
RPCArg::Optional::OMITTED
@ OMITTED
Optional argument with default value omitted because they are implicitly clear.
RPCConsole::RPCExecuteCommandLine
static bool RPCExecuteCommandLine(interfaces::Node &node, std::string &strResult, const std::string &strCommand, std::string *const pstrFilteredOut=nullptr, const WalletModel *wallet_model=nullptr)
Definition: rpcconsole.h:47
TestingSetup
Testing setup that configures a complete environment.
Definition: setup_common.h:99
JSONRPCRequest
Definition: request.h:28
RPCResult
Definition: util.h:231
RPCIsInWarmup
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:344
server.h