Changeset View
Changeset View
Standalone View
Standalone View
src/qt/test/rpcnestedtests.cpp
// Copyright (c) 2016 The Bitcoin Core developers | // Copyright (c) 2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "rpcnestedtests.h" | #include <test/rpcnestedtests.h> | ||||
#include "chainparams.h" | #include <chainparams.h> | ||||
#include "config.h" | #include <config.h> | ||||
#include "consensus/validation.h" | #include <consensus/validation.h> | ||||
#include "fs.h" | #include <fs.h> | ||||
#include "rpc/register.h" | #include <interfaces/node.h> | ||||
#include "rpc/server.h" | #include <qt/rpcconsole.h> | ||||
#include "rpcconsole.h" | #include <rpc/register.h> | ||||
#include "test/test_bitcoin.h" | #include <rpc/server.h> | ||||
#include "univalue.h" | #include <test/test_bitcoin.h> | ||||
#include "util.h" | #include <univalue.h> | ||||
#include "validation.h" | #include <util.h> | ||||
#include <validation.h> | |||||
#include <QDir> | #include <QDir> | ||||
#include <QtGlobal> | #include <QtGlobal> | ||||
static UniValue rpcNestedTest_rpc(const Config &config, | static UniValue rpcNestedTest_rpc(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
if (request.fHelp) { | if (request.fHelp) { | ||||
return "help message"; | return "help message"; | ||||
Show All 19 Lines | void RPCNestedTests::rpcNestedTests() { | ||||
gArgs.ForceSetArg("-datadir", path); | gArgs.ForceSetArg("-datadir", path); | ||||
// mempool.setSanityCheck(1.0); | // mempool.setSanityCheck(1.0); | ||||
TestingSetup test; | TestingSetup test; | ||||
std::string result; | std::string result; | ||||
std::string result2; | std::string result2; | ||||
std::string filtered; | std::string filtered; | ||||
auto node = interfaces::MakeNode(); | |||||
// Simple result filtering with path. | // Simple result filtering with path. | ||||
RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()[chain]", | RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
&filtered); | "getblockchaininfo()[chain]", &filtered); | ||||
QVERIFY(result == "main"); | QVERIFY(result == "main"); | ||||
QVERIFY(filtered == "getblockchaininfo()[chain]"); | QVERIFY(filtered == "getblockchaininfo()[chain]"); | ||||
// Simple 2 level nesting. | // Simple 2 level nesting. | ||||
RPCConsole::RPCExecuteCommandLine(result, "getblock(getbestblockhash())"); | RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
"getblock(getbestblockhash())"); | |||||
RPCConsole::RPCExecuteCommandLine( | RPCConsole::RPCExecuteCommandLine( | ||||
result, "getblock(getblock(getbestblockhash())[hash], true)"); | *node, result, "getblock(getblock(getbestblockhash())[hash], true)"); | ||||
// 4 level nesting with whitespace, filtering path and boolean parameter. | // 4 level nesting with whitespace, filtering path and boolean parameter. | ||||
RPCConsole::RPCExecuteCommandLine( | RPCConsole::RPCExecuteCommandLine( | ||||
result, "getblock( getblock( getblock(getbestblockhash())[hash] " | *node, result, | ||||
"getblock( getblock( getblock(getbestblockhash())[hash] " | |||||
")[hash], true)"); | ")[hash], true)"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo"); | RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo"); | ||||
QVERIFY(result.substr(0, 1) == "{"); | QVERIFY(result.substr(0, 1) == "{"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()"); | RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo()"); | ||||
QVERIFY(result.substr(0, 1) == "{"); | QVERIFY(result.substr(0, 1) == "{"); | ||||
// Whitespace at the end will be tolerated. | // Whitespace at the end will be tolerated. | ||||
RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo "); | RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo "); | ||||
QVERIFY(result.substr(0, 1) == "{"); | QVERIFY(result.substr(0, 1) == "{"); | ||||
// Quote path identifier are allowed, but look after a child contaning the | // Quote path identifier are allowed, but look after a child contaning the | ||||
// quotes in the key. | // quotes in the key. | ||||
(RPCConsole::RPCExecuteCommandLine(result, | (RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
"getblockchaininfo()[\"chain\"]")); | "getblockchaininfo()[\"chain\"]")); | ||||
QVERIFY(result == "null"); | QVERIFY(result == "null"); | ||||
// parameter not in brackets are allowed. | // parameter not in brackets are allowed. | ||||
(RPCConsole::RPCExecuteCommandLine(result, "createrawtransaction [] {} 0")); | (RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
"createrawtransaction [] {} 0")); | |||||
// Parameter in brackets are allowed. | // Parameter in brackets are allowed. | ||||
(RPCConsole::RPCExecuteCommandLine(result2, | (RPCConsole::RPCExecuteCommandLine(*node, result2, | ||||
"createrawtransaction([],{},0)")); | "createrawtransaction([],{},0)")); | ||||
QVERIFY(result == result2); | QVERIFY(result == result2); | ||||
// Whitespace between parametres is allowed. | // Whitespace between parametres is allowed. | ||||
(RPCConsole::RPCExecuteCommandLine( | (RPCConsole::RPCExecuteCommandLine( | ||||
result2, "createrawtransaction( [], {} , 0 )")); | *node, result2, "createrawtransaction( [], {} , 0 )")); | ||||
QVERIFY(result == result2); | QVERIFY(result == result2); | ||||
RPCConsole::RPCExecuteCommandLine( | RPCConsole::RPCExecuteCommandLine( | ||||
result, "getblock(getbestblockhash())[tx][0]", &filtered); | *node, result, "getblock(getbestblockhash())[tx][0]", &filtered); | ||||
QVERIFY(result == | QVERIFY(result == | ||||
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); | "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); | ||||
QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]"); | QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]"); | ||||
RPCConsole::RPCParseCommandLine(result, "importprivkey", false, &filtered); | RPCConsole::RPCParseCommandLine(nullptr, result, "importprivkey", false, | ||||
QVERIFY(filtered == "importprivkey(…)"); | |||||
RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc", false, | |||||
&filtered); | &filtered); | ||||
QVERIFY(filtered == "importprivkey(…)"); | |||||
RPCConsole::RPCParseCommandLine( | |||||
nullptr, result, "signmessagewithprivkey abc", false, &filtered); | |||||
QVERIFY(filtered == "signmessagewithprivkey(…)"); | QVERIFY(filtered == "signmessagewithprivkey(…)"); | ||||
RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc,def", | RPCConsole::RPCParseCommandLine( | ||||
false, &filtered); | nullptr, result, "signmessagewithprivkey abc,def", false, &filtered); | ||||
QVERIFY(filtered == "signmessagewithprivkey(…)"); | QVERIFY(filtered == "signmessagewithprivkey(…)"); | ||||
RPCConsole::RPCParseCommandLine(result, "signrawtransactionwithkey(abc)", | RPCConsole::RPCParseCommandLine( | ||||
false, &filtered); | nullptr, result, "signrawtransactionwithkey(abc)", false, &filtered); | ||||
QVERIFY(filtered == "signrawtransactionwithkey(…)"); | QVERIFY(filtered == "signrawtransactionwithkey(…)"); | ||||
RPCConsole::RPCParseCommandLine(result, "walletpassphrase(help())", false, | RPCConsole::RPCParseCommandLine(nullptr, result, "walletpassphrase(help())", | ||||
&filtered); | false, &filtered); | ||||
QVERIFY(filtered == "walletpassphrase(…)"); | QVERIFY(filtered == "walletpassphrase(…)"); | ||||
RPCConsole::RPCParseCommandLine( | RPCConsole::RPCParseCommandLine( | ||||
result, "walletpassphrasechange(help(walletpassphrasechange(abc)))", | nullptr, result, | ||||
false, &filtered); | "walletpassphrasechange(help(walletpassphrasechange(abc)))", false, | ||||
&filtered); | |||||
QVERIFY(filtered == "walletpassphrasechange(…)"); | QVERIFY(filtered == "walletpassphrasechange(…)"); | ||||
RPCConsole::RPCParseCommandLine(result, "help(encryptwallet(abc, def))", | RPCConsole::RPCParseCommandLine( | ||||
false, &filtered); | nullptr, result, "help(encryptwallet(abc, def))", false, &filtered); | ||||
QVERIFY(filtered == "help(encryptwallet(…))"); | QVERIFY(filtered == "help(encryptwallet(…))"); | ||||
RPCConsole::RPCParseCommandLine(result, "help(importprivkey())", false, | RPCConsole::RPCParseCommandLine(nullptr, result, "help(importprivkey())", | ||||
&filtered); | |||||
QVERIFY(filtered == "help(importprivkey(…))"); | |||||
RPCConsole::RPCParseCommandLine(result, "help(importprivkey(help()))", | |||||
false, &filtered); | false, &filtered); | ||||
QVERIFY(filtered == "help(importprivkey(…))"); | QVERIFY(filtered == "help(importprivkey(…))"); | ||||
RPCConsole::RPCParseCommandLine( | RPCConsole::RPCParseCommandLine( | ||||
result, "help(importprivkey(abc), walletpassphrase(def))", false, | nullptr, result, "help(importprivkey(help()))", false, &filtered); | ||||
&filtered); | QVERIFY(filtered == "help(importprivkey(…))"); | ||||
RPCConsole::RPCParseCommandLine( | |||||
nullptr, result, "help(importprivkey(abc), walletpassphrase(def))", | |||||
false, &filtered); | |||||
QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))"); | QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest"); | RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest"); | ||||
QVERIFY(result == "[]"); | QVERIFY(result == "[]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest ''"); | RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest ''"); | ||||
QVERIFY(result == "[\"\"]"); | QVERIFY(result == "[\"\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest \"\""); | RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest \"\""); | ||||
QVERIFY(result == "[\"\"]"); | QVERIFY(result == "[\"\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest '' abc"); | RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest '' abc"); | ||||
QVERIFY(result == "[\"\",\"abc\"]"); | QVERIFY(result == "[\"\",\"abc\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc '' abc"); | RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
"rpcNestedTest abc '' abc"); | |||||
QVERIFY(result == "[\"abc\",\"\",\"abc\"]"); | QVERIFY(result == "[\"abc\",\"\",\"abc\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc abc"); | RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest abc abc"); | ||||
QVERIFY(result == "[\"abc\",\"abc\"]"); | QVERIFY(result == "[\"abc\",\"abc\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc\t\tabc"); | RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
"rpcNestedTest abc\t\tabc"); | |||||
QVERIFY(result == "[\"abc\",\"abc\"]"); | QVERIFY(result == "[\"abc\",\"abc\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc )"); | RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest(abc )"); | ||||
QVERIFY(result == "[\"abc\"]"); | QVERIFY(result == "[\"abc\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest( abc )"); | RPCConsole::RPCExecuteCommandLine(*node, result, "rpcNestedTest( abc )"); | ||||
QVERIFY(result == "[\"abc\"]"); | QVERIFY(result == "[\"abc\"]"); | ||||
RPCConsole::RPCExecuteCommandLine(result, | RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
"rpcNestedTest( abc , cba )"); | "rpcNestedTest( abc , cba )"); | ||||
QVERIFY(result == "[\"abc\",\"cba\"]"); | QVERIFY(result == "[\"abc\",\"cba\"]"); | ||||
#if QT_VERSION >= 0x050300 | #if QT_VERSION >= 0x050300 | ||||
// do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher | // do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher | ||||
// (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3) | // (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3) | ||||
// invalid syntax | // invalid syntax | ||||
QVERIFY_EXCEPTION_THROWN( | QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine( | ||||
RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo() .\n"), | *node, result, "getblockchaininfo() .\n"), | ||||
std::runtime_error); | std::runtime_error); | ||||
// invalid syntax | // invalid syntax | ||||
QVERIFY_EXCEPTION_THROWN( | QVERIFY_EXCEPTION_THROWN( | ||||
RPCConsole::RPCExecuteCommandLine( | RPCConsole::RPCExecuteCommandLine( | ||||
result, "getblockchaininfo() getblockchaininfo()"), | *node, result, "getblockchaininfo() getblockchaininfo()"), | ||||
std::runtime_error); | std::runtime_error); | ||||
// tolerate non closing brackets if we have no arguments. | // tolerate non closing brackets if we have no arguments. | ||||
(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(")); | (RPCConsole::RPCExecuteCommandLine(*node, result, "getblockchaininfo(")); | ||||
// tolerate non command brackts | // tolerate non command brackts | ||||
(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()()()")); | (RPCConsole::RPCExecuteCommandLine(*node, result, | ||||
"getblockchaininfo()()()")); | |||||
// invalid argument | // invalid argument | ||||
QVERIFY_EXCEPTION_THROWN( | QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine( | ||||
RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(True)"), | *node, result, "getblockchaininfo(True)"), | ||||
UniValue); | UniValue); | ||||
// method not found | // method not found | ||||
QVERIFY_EXCEPTION_THROWN( | QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine( | ||||
RPCConsole::RPCExecuteCommandLine(result, "a(getblockchaininfo(True))"), | *node, result, "a(getblockchaininfo(True))"), | ||||
UniValue); | UniValue); | ||||
// don't tollerate empty arguments when using , | // don't tollerate empty arguments when using , | ||||
QVERIFY_EXCEPTION_THROWN( | QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine( | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc,,abc"), | *node, result, "rpcNestedTest abc,,abc"), | ||||
std::runtime_error); | std::runtime_error); | ||||
// don't tollerate empty arguments when using , | // don't tollerate empty arguments when using , | ||||
QVERIFY_EXCEPTION_THROWN( | QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine( | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,abc)"), | *node, result, "rpcNestedTest(abc,,abc)"), | ||||
std::runtime_error); | std::runtime_error); | ||||
// don't tollerate empty arguments when using , | // don't tollerate empty arguments when using , | ||||
QVERIFY_EXCEPTION_THROWN( | QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine( | ||||
RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,)"), | *node, result, "rpcNestedTest(abc,,)"), | ||||
std::runtime_error); | std::runtime_error); | ||||
#endif | #endif | ||||
fs::remove_all(fs::path(path)); | fs::remove_all(fs::path(path)); | ||||
} | } |