2020-12-31 09:48:25 +01:00
// Copyright (c) 2016-2020 The Bitcoin Core developers
2016-08-20 11:19:35 +02:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 13:57:53 +13:00
# include <qt/test/rpcnestedtests.h>
2016-08-20 11:19:35 +02:00
2018-04-07 03:42:02 -04:00
# include <interfaces/node.h>
2017-11-10 13:57:53 +13:00
# include <rpc/server.h>
2017-08-15 17:31:26 +02:00
# include <qt/rpcconsole.h>
2019-11-05 15:18:59 -05:00
# include <test/util/setup_common.h>
2017-11-10 13:57:53 +13:00
# include <univalue.h>
2018-10-22 15:51:11 -07:00
# include <util/system.h>
2016-08-20 11:19:35 +02:00
# include <QDir>
2016-12-14 08:59:09 +01:00
# include <QtGlobal>
2016-08-20 11:19:35 +02:00
2020-04-01 02:55:59 +02:00
static RPCHelpMan rpcNestedTest_rpc ( )
2016-12-12 23:04:10 +00:00
{
2020-04-01 02:55:59 +02:00
return RPCHelpMan {
" rpcNestedTest " ,
" echo the passed string(s) " ,
{
{ " arg1 " , RPCArg : : Type : : STR , RPCArg : : Optional : : OMITTED , " " } ,
{ " arg2 " , RPCArg : : Type : : STR , RPCArg : : Optional : : OMITTED , " " } ,
{ " arg3 " , RPCArg : : Type : : STR , RPCArg : : Optional : : OMITTED , " " } ,
} ,
{ } ,
RPCExamples { " " } ,
[ ] ( const RPCHelpMan & self , const JSONRPCRequest & request ) - > UniValue {
return request . params . write ( 0 , 0 ) ;
} ,
} ;
2016-12-12 23:04:10 +00:00
}
2020-04-01 02:55:59 +02:00
static const CRPCCommand vRPCCommands [ ] = {
{ " test " , " rpcNestedTest " , & rpcNestedTest_rpc , { " arg1 " , " arg2 " , " arg3 " } } ,
2016-12-12 23:04:10 +00:00
} ;
2016-08-20 11:19:35 +02:00
void RPCNestedTests : : rpcNestedTests ( )
{
// do some test setup
// could be moved to a more generic place when we add more tests on QT level
2016-12-12 23:04:10 +00:00
tableRPC . appendCommand ( " rpcNestedTest " , & vRPCCommands [ 0 ] ) ;
2017-04-26 20:13:24 -04:00
TestingSetup test ;
2020-08-28 16:27:32 -04:00
m_node . setContext ( & test . m_node ) ;
2016-08-20 11:19:35 +02:00
2017-11-06 20:11:43 -05:00
if ( RPCIsInWarmup ( nullptr ) ) SetRPCWarmupFinished ( ) ;
2016-08-20 11:19:35 +02:00
std : : string result ;
std : : string result2 ;
2016-11-16 12:31:44 +00:00
std : : string filtered ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo()[chain] " , & filtered ) ; //simple result filtering with path
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = " main " ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " getblockchaininfo()[chain] " ) ;
2016-08-20 11:19:35 +02:00
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblock(getbestblockhash()) " ) ; //simple 2 level nesting
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblock(getblock(getbestblockhash())[hash], true) " ) ;
2016-08-20 11:19:35 +02:00
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true) " ) ; //4 level nesting with whitespace, filtering path and boolean parameter
2016-08-20 11:19:35 +02:00
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo " ) ;
2016-08-20 11:19:35 +02:00
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo() " ) ;
2016-08-20 11:19:35 +02:00
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo " ) ; //whitespace at the end will be tolerated
2016-08-20 11:19:35 +02:00
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
2020-08-28 16:27:32 -04:00
( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo()[ \" chain \" ] " ) ) ; //Quote path identifier are allowed, but look after a child containing the quotes in the key
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = " null " ) ;
2020-08-28 16:27:32 -04:00
( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " createrawtransaction [] {} 0 " ) ) ; //parameter not in brackets are allowed
( RPCConsole : : RPCExecuteCommandLine ( m_node , result2 , " createrawtransaction([],{},0) " ) ) ; //parameter in brackets are allowed
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = result2 ) ;
2020-08-28 16:27:32 -04:00
( RPCConsole : : RPCExecuteCommandLine ( m_node , result2 , " createrawtransaction( [], {} , 0 ) " ) ) ; //whitespace between parameters is allowed
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = result2 ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblock(getbestblockhash())[tx][0] " , & filtered ) ;
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = " 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b " ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " getblock(getbestblockhash())[tx][0] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " importprivkey " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " importprivkey(…) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " signmessagewithprivkey abc " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " signmessagewithprivkey(…) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " signmessagewithprivkey abc,def " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " signmessagewithprivkey(…) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " signrawtransactionwithkey(abc) " , false , & filtered ) ;
2017-06-12 12:23:02 -07:00
QVERIFY ( filtered = = " signrawtransactionwithkey(…) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " walletpassphrase(help()) " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " walletpassphrase(…) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " walletpassphrasechange(help(walletpassphrasechange(abc))) " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " walletpassphrasechange(…) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " help(encryptwallet(abc, def)) " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " help(encryptwallet(…)) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " help(importprivkey()) " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " help(importprivkey(…)) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " help(importprivkey(help())) " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " help(importprivkey(…)) " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCParseCommandLine ( nullptr , result , " help(importprivkey(abc), walletpassphrase(def)) " , false , & filtered ) ;
2016-11-16 12:31:44 +00:00
QVERIFY ( filtered = = " help(importprivkey(…), walletpassphrase(…)) " ) ;
2016-08-20 11:19:35 +02:00
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest '' " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest \" \" " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest '' abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" \" , \" abc \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest abc '' abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" abc \" , \" \" , \" abc \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest abc abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" abc \" , \" abc \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest abc \t \t abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" abc \" , \" abc \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest(abc ) " ) ;
2016-12-14 08:59:09 +01:00
QVERIFY ( result = = " [ \" abc \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest( abc ) " ) ;
2016-12-14 08:59:09 +01:00
QVERIFY ( result = = " [ \" abc \" ] " ) ;
2020-08-28 16:27:32 -04:00
RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest( abc , cba ) " ) ;
2016-12-14 08:59:09 +01:00
QVERIFY ( result = = " [ \" abc \" , \" cba \" ] " ) ;
// do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3)
2020-08-28 16:27:32 -04:00
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo() . \n " ) , std : : runtime_error ) ; //invalid syntax
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo() getblockchaininfo() " ) , std : : runtime_error ) ; //invalid syntax
( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo( " ) ) ; //tolerate non closing brackets if we have no arguments
2020-12-24 22:37:17 +09:00
( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo()()() " ) ) ; //tolerate non command brackets
2020-08-28 16:27:32 -04:00
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " getblockchaininfo(True) " ) , UniValue ) ; //invalid argument
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " a(getblockchaininfo(True)) " ) , UniValue ) ; //method not found
2020-12-24 22:37:17 +09:00
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest abc,,abc " ) , std : : runtime_error ) ; //don't tolerate empty arguments when using ,
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest(abc,,abc) " ) , std : : runtime_error ) ; //don't tolerate empty arguments when using ,
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( m_node , result , " rpcNestedTest(abc,,) " ) , std : : runtime_error ) ; //don't tolerate empty arguments when using ,
2016-08-20 11:19:35 +02:00
}