Line data Source code
1 : // Copyright (c) 2016-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 <qt/test/rpcnestedtests.h>
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 <QDir>
15 : #include <QtGlobal>
16 :
17 10 : static UniValue rpcNestedTest_rpc(const JSONRPCRequest& request)
18 : {
19 10 : if (request.fHelp) {
20 0 : return "help message";
21 : }
22 10 : return request.params.write(0, 0);
23 10 : }
24 :
25 3 : static const CRPCCommand vRPCCommands[] =
26 1 : {
27 1 : { "test", "rpcNestedTest", &rpcNestedTest_rpc, {} },
28 : };
29 :
30 1 : void RPCNestedTests::rpcNestedTests()
31 : {
32 : // do some test setup
33 : // could be moved to a more generic place when we add more tests on QT level
34 1 : tableRPC.appendCommand("rpcNestedTest", &vRPCCommands[0]);
35 :
36 1 : TestingSetup test;
37 :
38 1 : if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();
39 :
40 1 : std::string result;
41 1 : std::string result2;
42 1 : std::string filtered;
43 1 : interfaces::Node* node = &m_node;
44 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo()[chain]", &filtered); //simple result filtering with path
45 1 : QVERIFY(result=="main");
46 1 : QVERIFY(filtered == "getblockchaininfo()[chain]");
47 :
48 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblock(getbestblockhash())"); //simple 2 level nesting
49 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblock(getblock(getbestblockhash())[hash], true)");
50 :
51 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true)"); //4 level nesting with whitespace, filtering path and boolean parameter
52 :
53 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo");
54 1 : QVERIFY(result.substr(0,1) == "{");
55 :
56 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo()");
57 1 : QVERIFY(result.substr(0,1) == "{");
58 :
59 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo "); //whitespace at the end will be tolerated
60 1 : QVERIFY(result.substr(0,1) == "{");
61 :
62 1 : (RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo()[\"chain\"]")); //Quote path identifier are allowed, but look after a child containing the quotes in the key
63 1 : QVERIFY(result == "null");
64 :
65 1 : (RPCConsole::RPCExecuteCommandLine(*node, result, "createrawtransaction [] {} 0")); //parameter not in brackets are allowed
66 1 : (RPCConsole::RPCExecuteCommandLine(*node, result2, "createrawtransaction([],{},0)")); //parameter in brackets are allowed
67 1 : QVERIFY(result == result2);
68 1 : (RPCConsole::RPCExecuteCommandLine(*node, result2, "createrawtransaction( [], {} , 0 )")); //whitespace between parameters is allowed
69 1 : QVERIFY(result == result2);
70 :
71 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "getblock(getbestblockhash())[tx][0]", &filtered);
72 1 : QVERIFY(result == "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
73 1 : QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]");
74 :
75 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "importprivkey", false, &filtered);
76 1 : QVERIFY(filtered == "importprivkey(…)");
77 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc", false, &filtered);
78 1 : QVERIFY(filtered == "signmessagewithprivkey(…)");
79 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc,def", false, &filtered);
80 1 : QVERIFY(filtered == "signmessagewithprivkey(…)");
81 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "signrawtransactionwithkey(abc)", false, &filtered);
82 1 : QVERIFY(filtered == "signrawtransactionwithkey(…)");
83 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "walletpassphrase(help())", false, &filtered);
84 1 : QVERIFY(filtered == "walletpassphrase(…)");
85 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "walletpassphrasechange(help(walletpassphrasechange(abc)))", false, &filtered);
86 1 : QVERIFY(filtered == "walletpassphrasechange(…)");
87 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "help(encryptwallet(abc, def))", false, &filtered);
88 1 : QVERIFY(filtered == "help(encryptwallet(…))");
89 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey())", false, &filtered);
90 1 : QVERIFY(filtered == "help(importprivkey(…))");
91 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey(help()))", false, &filtered);
92 1 : QVERIFY(filtered == "help(importprivkey(…))");
93 1 : RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey(abc), walletpassphrase(def))", false, &filtered);
94 1 : QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))");
95 :
96 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest");
97 1 : QVERIFY(result == "[]");
98 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest ''");
99 1 : QVERIFY(result == "[\"\"]");
100 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest \"\"");
101 1 : QVERIFY(result == "[\"\"]");
102 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest '' abc");
103 1 : QVERIFY(result == "[\"\",\"abc\"]");
104 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest abc '' abc");
105 1 : QVERIFY(result == "[\"abc\",\"\",\"abc\"]");
106 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest abc abc");
107 1 : QVERIFY(result == "[\"abc\",\"abc\"]");
108 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest abc\t\tabc");
109 1 : QVERIFY(result == "[\"abc\",\"abc\"]");
110 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest(abc )");
111 1 : QVERIFY(result == "[\"abc\"]");
112 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest( abc )");
113 1 : QVERIFY(result == "[\"abc\"]");
114 1 : RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest( abc , cba )");
115 1 : QVERIFY(result == "[\"abc\",\"cba\"]");
116 :
117 : // do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3)
118 1 : QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo() .\n"), std::runtime_error); //invalid syntax
119 1 : QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo() getblockchaininfo()"), std::runtime_error); //invalid syntax
120 1 : (RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo(")); //tolerate non closing brackets if we have no arguments
121 1 : (RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo()()()")); //tolerate non command brackts
122 1 : QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo(True)"), UniValue); //invalid argument
123 1 : QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(*node, result, "a(getblockchaininfo(True))"), UniValue); //method not found
124 1 : QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest abc,,abc"), std::runtime_error); //don't tollerate empty arguments when using ,
125 1 : QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest(abc,,abc)"), std::runtime_error); //don't tollerate empty arguments when using ,
126 1 : QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest(abc,,)"), std::runtime_error); //don't tollerate empty arguments when using ,
127 8 : }
|