Merge #10705: Trivial: spelling fixes
[bitcoinplatinum.git] / src / qt / test / rpcnestedtests.cpp
blob8b8f4f0528be0c2c9ad3f93a9e29022d5249196f
1 // Copyright (c) 2016 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.
5 #include "rpcnestedtests.h"
7 #include "chainparams.h"
8 #include "consensus/validation.h"
9 #include "fs.h"
10 #include "validation.h"
11 #include "rpc/register.h"
12 #include "rpc/server.h"
13 #include "rpcconsole.h"
14 #include "test/testutil.h"
15 #include "test/test_bitcoin.h"
16 #include "univalue.h"
17 #include "util.h"
19 #include <QDir>
20 #include <QtGlobal>
22 static UniValue rpcNestedTest_rpc(const JSONRPCRequest& request)
24 if (request.fHelp) {
25 return "help message";
27 return request.params.write(0, 0);
30 static const CRPCCommand vRPCCommands[] =
32 { "test", "rpcNestedTest", &rpcNestedTest_rpc, true, {} },
35 void RPCNestedTests::rpcNestedTests()
37 // do some test setup
38 // could be moved to a more generic place when we add more tests on QT level
39 tableRPC.appendCommand("rpcNestedTest", &vRPCCommands[0]);
40 ClearDatadirCache();
41 std::string path = QDir::tempPath().toStdString() + "/" + strprintf("test_bitcoin_qt_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
42 QDir dir(QString::fromStdString(path));
43 dir.mkpath(".");
44 gArgs.ForceSetArg("-datadir", path);
45 //mempool.setSanityCheck(1.0);
47 TestingSetup test;
49 SetRPCWarmupFinished();
51 std::string result;
52 std::string result2;
53 std::string filtered;
54 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()[chain]", &filtered); //simple result filtering with path
55 QVERIFY(result=="main");
56 QVERIFY(filtered == "getblockchaininfo()[chain]");
58 RPCConsole::RPCExecuteCommandLine(result, "getblock(getbestblockhash())"); //simple 2 level nesting
59 RPCConsole::RPCExecuteCommandLine(result, "getblock(getblock(getbestblockhash())[hash], true)");
61 RPCConsole::RPCExecuteCommandLine(result, "getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true)"); //4 level nesting with whitespace, filtering path and boolean parameter
63 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo");
64 QVERIFY(result.substr(0,1) == "{");
66 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()");
67 QVERIFY(result.substr(0,1) == "{");
69 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo "); //whitespace at the end will be tolerated
70 QVERIFY(result.substr(0,1) == "{");
72 (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()[\"chain\"]")); //Quote path identifier are allowed, but look after a child containing the quotes in the key
73 QVERIFY(result == "null");
75 (RPCConsole::RPCExecuteCommandLine(result, "createrawtransaction [] {} 0")); //parameter not in brackets are allowed
76 (RPCConsole::RPCExecuteCommandLine(result2, "createrawtransaction([],{},0)")); //parameter in brackets are allowed
77 QVERIFY(result == result2);
78 (RPCConsole::RPCExecuteCommandLine(result2, "createrawtransaction( [], {} , 0 )")); //whitespace between parametres is allowed
79 QVERIFY(result == result2);
81 RPCConsole::RPCExecuteCommandLine(result, "getblock(getbestblockhash())[tx][0]", &filtered);
82 QVERIFY(result == "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
83 QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]");
85 RPCConsole::RPCParseCommandLine(result, "importprivkey", false, &filtered);
86 QVERIFY(filtered == "importprivkey(…)");
87 RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc", false, &filtered);
88 QVERIFY(filtered == "signmessagewithprivkey(…)");
89 RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc,def", false, &filtered);
90 QVERIFY(filtered == "signmessagewithprivkey(…)");
91 RPCConsole::RPCParseCommandLine(result, "signrawtransaction(abc)", false, &filtered);
92 QVERIFY(filtered == "signrawtransaction(…)");
93 RPCConsole::RPCParseCommandLine(result, "walletpassphrase(help())", false, &filtered);
94 QVERIFY(filtered == "walletpassphrase(…)");
95 RPCConsole::RPCParseCommandLine(result, "walletpassphrasechange(help(walletpassphrasechange(abc)))", false, &filtered);
96 QVERIFY(filtered == "walletpassphrasechange(…)");
97 RPCConsole::RPCParseCommandLine(result, "help(encryptwallet(abc, def))", false, &filtered);
98 QVERIFY(filtered == "help(encryptwallet(…))");
99 RPCConsole::RPCParseCommandLine(result, "help(importprivkey())", false, &filtered);
100 QVERIFY(filtered == "help(importprivkey(…))");
101 RPCConsole::RPCParseCommandLine(result, "help(importprivkey(help()))", false, &filtered);
102 QVERIFY(filtered == "help(importprivkey(…))");
103 RPCConsole::RPCParseCommandLine(result, "help(importprivkey(abc), walletpassphrase(def))", false, &filtered);
104 QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))");
106 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest");
107 QVERIFY(result == "[]");
108 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest ''");
109 QVERIFY(result == "[\"\"]");
110 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest \"\"");
111 QVERIFY(result == "[\"\"]");
112 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest '' abc");
113 QVERIFY(result == "[\"\",\"abc\"]");
114 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc '' abc");
115 QVERIFY(result == "[\"abc\",\"\",\"abc\"]");
116 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc abc");
117 QVERIFY(result == "[\"abc\",\"abc\"]");
118 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc\t\tabc");
119 QVERIFY(result == "[\"abc\",\"abc\"]");
120 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc )");
121 QVERIFY(result == "[\"abc\"]");
122 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest( abc )");
123 QVERIFY(result == "[\"abc\"]");
124 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest( abc , cba )");
125 QVERIFY(result == "[\"abc\",\"cba\"]");
127 #if QT_VERSION >= 0x050300
128 // do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3)
129 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo() .\n"), std::runtime_error); //invalid syntax
130 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo() getblockchaininfo()"), std::runtime_error); //invalid syntax
131 (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(")); //tolerate non closing brackets if we have no arguments
132 (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()()()")); //tolerate non command brackts
133 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(True)"), UniValue); //invalid argument
134 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "a(getblockchaininfo(True))"), UniValue); //method not found
135 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc,,abc"), std::runtime_error); //don't tollerate empty arguments when using ,
136 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,abc)"), std::runtime_error); //don't tollerate empty arguments when using ,
137 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,)"), std::runtime_error); //don't tollerate empty arguments when using ,
138 #endif
140 fs::remove_all(fs::path(path));