Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2016-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <qt/test/rpcnestedtests.h>7#include <common/system.h>8#include <interfaces/node.h>9#include <qt/rpcconsole.h>10#include <rpc/server.h>11#include <test/util/setup_common.h>12#include <univalue.h>14#include <QTest>16#include <string>17#include <stdexcept>19static RPCMethod rpcNestedTest_rpc()20{21return RPCMethod{22"rpcNestedTest",23"echo the passed string(s)",24{25{"arg1", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},26{"arg2", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},27{"arg3", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},28},29RPCResult{RPCResult::Type::ANY, "", ""},30RPCExamples{""},31[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue {32return request.params.write(0, 0);33},34};35}37static const CRPCCommand vRPCCommands[] = {38{"rpcNestedTest", &rpcNestedTest_rpc},39};41void RPCNestedTests::rpcNestedTests()42{43// do some test setup44// could be moved to a more generic place when we add more tests on QT level45for (const auto& c : vRPCCommands) {46tableRPC.appendCommand(c.name, &c);47}49TestingSetup test;50m_node.setContext(&test.m_node);52if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();54std::string result;55std::string result2;56std::string filtered;57RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo()[chain]", &filtered); //simple result filtering with path58QVERIFY(result=="main");59QVERIFY(filtered == "getblockchaininfo()[chain]");61RPCConsole::RPCExecuteCommandLine(m_node, result, "getblock(getbestblockhash())"); //simple 2 level nesting62RPCConsole::RPCExecuteCommandLine(m_node, result, "getblock(getblock(getbestblockhash())[hash], true)");64RPCConsole::RPCExecuteCommandLine(m_node, result, "getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true)"); //4 level nesting with whitespace, filtering path and boolean parameter66RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo");67QVERIFY(result.starts_with("{"));69RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo()");70QVERIFY(result.starts_with("{"));72RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo "); //whitespace at the end will be tolerated73QVERIFY(result.starts_with("{"));75RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo()[\"chain\"]"); //Quote path identifier are allowed, but look after a child containing the quotes in the key76QVERIFY(result == "null");78RPCConsole::RPCExecuteCommandLine(m_node, result, "createrawtransaction [] {} 0"); //parameter not in brackets are allowed79RPCConsole::RPCExecuteCommandLine(m_node, result2, "createrawtransaction([],{},0)"); //parameter in brackets are allowed80QVERIFY(result == result2);81RPCConsole::RPCExecuteCommandLine(m_node, result2, "createrawtransaction( [], {} , 0 )"); //whitespace between parameters is allowed82QVERIFY(result == result2);84RPCConsole::RPCExecuteCommandLine(m_node, result, "getblock(getbestblockhash())[tx][0]", &filtered);85QVERIFY(result == "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");86QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]");88RPCConsole::RPCParseCommandLine(nullptr, result, "createwallet test true", false, &filtered);89QVERIFY(filtered == "createwallet(…)");90RPCConsole::RPCParseCommandLine(nullptr, result, "createwalletdescriptor abc", false, &filtered);91QVERIFY(filtered == "createwalletdescriptor(…)");92RPCConsole::RPCParseCommandLine(nullptr, result, "migratewallet abc abc", false, &filtered);93QVERIFY(filtered == "migratewallet(…)");94RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc", false, &filtered);95QVERIFY(filtered == "signmessagewithprivkey(…)");96RPCConsole::RPCParseCommandLine(nullptr, result, "signmessagewithprivkey abc,def", false, &filtered);97QVERIFY(filtered == "signmessagewithprivkey(…)");98RPCConsole::RPCParseCommandLine(nullptr, result, "signrawtransactionwithkey(abc)", false, &filtered);99QVERIFY(filtered == "signrawtransactionwithkey(…)");100RPCConsole::RPCParseCommandLine(nullptr, result, "walletpassphrase(help())", false, &filtered);101QVERIFY(filtered == "walletpassphrase(…)");102RPCConsole::RPCParseCommandLine(nullptr, result, "walletpassphrasechange(help(walletpassphrasechange(abc)))", false, &filtered);103QVERIFY(filtered == "walletpassphrasechange(…)");104RPCConsole::RPCParseCommandLine(nullptr, result, "help(encryptwallet(abc, def))", false, &filtered);105QVERIFY(filtered == "help(encryptwallet(…))");107RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest");108QVERIFY(result == "[]");109RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest ''");110QVERIFY(result == "[\"\"]");111RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest \"\"");112QVERIFY(result == "[\"\"]");113RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest '' abc");114QVERIFY(result == "[\"\",\"abc\"]");115RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc '' abc");116QVERIFY(result == "[\"abc\",\"\",\"abc\"]");117RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc abc");118QVERIFY(result == "[\"abc\",\"abc\"]");119RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc\t\tabc");120QVERIFY(result == "[\"abc\",\"abc\"]");121RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest(abc )");122QVERIFY(result == "[\"abc\"]");123RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest( abc )");124QVERIFY(result == "[\"abc\"]");125RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest( abc , cba )");126QVERIFY(result == "[\"abc\",\"cba\"]");128// Handle deprecated macro, can be removed once minimum Qt is at least 6.3.0.129#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))130#undef QVERIFY_EXCEPTION_THROWN131#define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) QVERIFY_THROWS_EXCEPTION(exceptiontype, expression)132#endif133QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo() .\n"), std::runtime_error); //invalid syntax134QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo() getblockchaininfo()"), std::runtime_error); //invalid syntax135RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo("); //tolerate non closing brackets if we have no arguments136RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo()()()"); //tolerate non command brackets137RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo)"); //tolerate a closing bracket after a command138QVERIFY(!RPCConsole::RPCExecuteCommandLine(m_node, result, ")")); //reject a closing bracket with no command (empty argument stack)139QVERIFY(!RPCConsole::RPCExecuteCommandLine(m_node, result, "()")); //reject empty brackets with no command140QVERIFY(!RPCConsole::RPCExecuteCommandLine(m_node, result, "(")); //reject an opening bracket with no command141QVERIFY(!RPCConsole::RPCExecuteCommandLine(m_node, result, ",")); //reject a comma with no command142QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "getblockchaininfo(True)"), UniValue); //invalid argument143QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "a(getblockchaininfo(True))"), UniValue); //method not found144QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest abc,,abc"), std::runtime_error); //don't tolerate empty arguments when using ,145QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest(abc,,abc)"), std::runtime_error); //don't tolerate empty arguments when using ,146QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(m_node, result, "rpcNestedTest(abc,,)"), std::runtime_error); //don't tolerate empty arguments when using ,147}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.