Remove unreachable or otherwise redundant code
[bitcoinplatinum.git] / src / qt / test / rpcnestedtests.cpp
blob26dec3c61087acca5b9a261d48c2338bcb209a26
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 "univalue.h"
16 #include "util.h"
18 #include <QDir>
19 #include <QtGlobal>
21 static UniValue rpcNestedTest_rpc(const JSONRPCRequest& request)
23 if (request.fHelp) {
24 return "help message";
26 return request.params.write(0, 0);
29 static const CRPCCommand vRPCCommands[] =
31 { "test", "rpcNestedTest", &rpcNestedTest_rpc, true, {} },
34 void RPCNestedTests::rpcNestedTests()
36 // do some test setup
37 // could be moved to a more generic place when we add more tests on QT level
38 const CChainParams& chainparams = Params();
39 RegisterAllCoreRPCCommands(tableRPC);
40 tableRPC.appendCommand("rpcNestedTest", &vRPCCommands[0]);
41 ClearDatadirCache();
42 std::string path = QDir::tempPath().toStdString() + "/" + strprintf("test_bitcoin_qt_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
43 QDir dir(QString::fromStdString(path));
44 dir.mkpath(".");
45 ForceSetArg("-datadir", path);
46 //mempool.setSanityCheck(1.0);
47 pblocktree = new CBlockTreeDB(1 << 20, true);
48 pcoinsdbview = new CCoinsViewDB(1 << 23, true);
49 pcoinsTip = new CCoinsViewCache(pcoinsdbview);
50 InitBlockIndex(chainparams);
52 CValidationState state;
53 bool ok = ActivateBestChain(state, chainparams);
54 QVERIFY(ok);
57 SetRPCWarmupFinished();
59 std::string result;
60 std::string result2;
61 std::string filtered;
62 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()[chain]", &filtered); //simple result filtering with path
63 QVERIFY(result=="main");
64 QVERIFY(filtered == "getblockchaininfo()[chain]");
66 RPCConsole::RPCExecuteCommandLine(result, "getblock(getbestblockhash())"); //simple 2 level nesting
67 RPCConsole::RPCExecuteCommandLine(result, "getblock(getblock(getbestblockhash())[hash], true)");
69 RPCConsole::RPCExecuteCommandLine(result, "getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true)"); //4 level nesting with whitespace, filtering path and boolean parameter
71 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo");
72 QVERIFY(result.substr(0,1) == "{");
74 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()");
75 QVERIFY(result.substr(0,1) == "{");
77 RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo "); //whitespace at the end will be tolerated
78 QVERIFY(result.substr(0,1) == "{");
80 (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()[\"chain\"]")); //Quote path identifier are allowed, but look after a child contaning the quotes in the key
81 QVERIFY(result == "null");
83 (RPCConsole::RPCExecuteCommandLine(result, "createrawtransaction [] {} 0")); //parameter not in brackets are allowed
84 (RPCConsole::RPCExecuteCommandLine(result2, "createrawtransaction([],{},0)")); //parameter in brackets are allowed
85 QVERIFY(result == result2);
86 (RPCConsole::RPCExecuteCommandLine(result2, "createrawtransaction( [], {} , 0 )")); //whitespace between parametres is allowed
87 QVERIFY(result == result2);
89 RPCConsole::RPCExecuteCommandLine(result, "getblock(getbestblockhash())[tx][0]", &filtered);
90 QVERIFY(result == "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
91 QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]");
93 RPCConsole::RPCParseCommandLine(result, "importprivkey", false, &filtered);
94 QVERIFY(filtered == "importprivkey(…)");
95 RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc", false, &filtered);
96 QVERIFY(filtered == "signmessagewithprivkey(…)");
97 RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc,def", false, &filtered);
98 QVERIFY(filtered == "signmessagewithprivkey(…)");
99 RPCConsole::RPCParseCommandLine(result, "signrawtransaction(abc)", false, &filtered);
100 QVERIFY(filtered == "signrawtransaction(…)");
101 RPCConsole::RPCParseCommandLine(result, "walletpassphrase(help())", false, &filtered);
102 QVERIFY(filtered == "walletpassphrase(…)");
103 RPCConsole::RPCParseCommandLine(result, "walletpassphrasechange(help(walletpassphrasechange(abc)))", false, &filtered);
104 QVERIFY(filtered == "walletpassphrasechange(…)");
105 RPCConsole::RPCParseCommandLine(result, "help(encryptwallet(abc, def))", false, &filtered);
106 QVERIFY(filtered == "help(encryptwallet(…))");
107 RPCConsole::RPCParseCommandLine(result, "help(importprivkey())", false, &filtered);
108 QVERIFY(filtered == "help(importprivkey(…))");
109 RPCConsole::RPCParseCommandLine(result, "help(importprivkey(help()))", false, &filtered);
110 QVERIFY(filtered == "help(importprivkey(…))");
111 RPCConsole::RPCParseCommandLine(result, "help(importprivkey(abc), walletpassphrase(def))", false, &filtered);
112 QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))");
114 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest");
115 QVERIFY(result == "[]");
116 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest ''");
117 QVERIFY(result == "[\"\"]");
118 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest \"\"");
119 QVERIFY(result == "[\"\"]");
120 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest '' abc");
121 QVERIFY(result == "[\"\",\"abc\"]");
122 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc '' abc");
123 QVERIFY(result == "[\"abc\",\"\",\"abc\"]");
124 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc abc");
125 QVERIFY(result == "[\"abc\",\"abc\"]");
126 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc\t\tabc");
127 QVERIFY(result == "[\"abc\",\"abc\"]");
128 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc )");
129 QVERIFY(result == "[\"abc\"]");
130 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest( abc )");
131 QVERIFY(result == "[\"abc\"]");
132 RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest( abc , cba )");
133 QVERIFY(result == "[\"abc\",\"cba\"]");
135 #if QT_VERSION >= 0x050300
136 // do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3)
137 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo() .\n"), std::runtime_error); //invalid syntax
138 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo() getblockchaininfo()"), std::runtime_error); //invalid syntax
139 (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(")); //tolerate non closing brackets if we have no arguments
140 (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()()()")); //tolerate non command brackts
141 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(True)"), UniValue); //invalid argument
142 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "a(getblockchaininfo(True))"), UniValue); //method not found
143 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc,,abc"), std::runtime_error); //don't tollerate empty arguments when using ,
144 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,abc)"), std::runtime_error); //don't tollerate empty arguments when using ,
145 QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,)"), std::runtime_error); //don't tollerate empty arguments when using ,
146 #endif
148 UnloadBlockIndex();
149 delete pcoinsTip;
150 pcoinsTip = nullptr;
151 delete pcoinsdbview;
152 pcoinsdbview = nullptr;
153 delete pblocktree;
154 pblocktree = nullptr;
156 fs::remove_all(fs::path(path));