2018-01-03 02:12:05 +09:00
// Copyright (c) 2016-2017 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
2017-11-10 13:57:53 +13:00
# include <chainparams.h>
# include <consensus/validation.h>
# include <fs.h>
2018-04-07 03:42:02 -04:00
# include <interfaces/node.h>
2017-11-10 13:57:53 +13:00
# include <validation.h>
# include <rpc/register.h>
# include <rpc/server.h>
2017-08-15 17:31:26 +02:00
# include <qt/rpcconsole.h>
2017-11-10 13:57:53 +13:00
# include <test/test_bitcoin.h>
# include <univalue.h>
# include <util.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
2016-12-12 23:04:10 +00:00
static UniValue rpcNestedTest_rpc ( const JSONRPCRequest & request )
{
if ( request . fHelp ) {
return " help message " ;
}
return request . params . write ( 0 , 0 ) ;
}
static const CRPCCommand vRPCCommands [ ] =
{
2017-06-08 17:38:23 -07:00
{ " test " , " rpcNestedTest " , & rpcNestedTest_rpc , { } } ,
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 ] ) ;
2016-08-20 11:19:35 +02:00
//mempool.setSanityCheck(1.0);
2017-04-26 20:13:24 -04:00
TestingSetup test ;
2016-08-20 11:19:35 +02:00
SetRPCWarmupFinished ( ) ;
std : : string result ;
std : : string result2 ;
2016-11-16 12:31:44 +00:00
std : : string filtered ;
2018-04-07 03:42:02 -04:00
auto node = interfaces : : MakeNode ( ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * 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
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblock(getbestblockhash()) " ) ; //simple 2 level nesting
RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblock(getblock(getbestblockhash())[hash], true) " ) ;
2016-08-20 11:19:35 +02:00
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * 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
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo " ) ;
2016-08-20 11:19:35 +02:00
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo() " ) ;
2016-08-20 11:19:35 +02:00
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo " ) ; //whitespace at the end will be tolerated
2016-08-20 11:19:35 +02:00
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
2017-04-17 16:38:51 -04:00
( RPCConsole : : RPCExecuteCommandLine ( * 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 " ) ;
2017-04-17 16:38:51 -04:00
( RPCConsole : : RPCExecuteCommandLine ( * node , result , " createrawtransaction [] {} 0 " ) ) ; //parameter not in brackets are allowed
( RPCConsole : : RPCExecuteCommandLine ( * node , result2 , " createrawtransaction([],{},0) " ) ) ; //parameter in brackets are allowed
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = result2 ) ;
2017-04-17 16:38:51 -04:00
( RPCConsole : : RPCExecuteCommandLine ( * node , result2 , " createrawtransaction( [], {} , 0 ) " ) ) ; //whitespace between parameters is allowed
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = result2 ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * 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
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest '' " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest \" \" " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest '' abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" \" , \" abc \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest abc '' abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" abc \" , \" \" , \" abc \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest abc abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" abc \" , \" abc \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest abc \t \t abc " ) ;
2016-12-12 23:04:10 +00:00
QVERIFY ( result = = " [ \" abc \" , \" abc \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest(abc ) " ) ;
2016-12-14 08:59:09 +01:00
QVERIFY ( result = = " [ \" abc \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest( abc ) " ) ;
2016-12-14 08:59:09 +01:00
QVERIFY ( result = = " [ \" abc \" ] " ) ;
2017-04-17 16:38:51 -04:00
RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest( abc , cba ) " ) ;
2016-12-14 08:59:09 +01:00
QVERIFY ( result = = " [ \" abc \" , \" cba \" ] " ) ;
# if QT_VERSION >= 0x050300
// do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3)
2017-04-17 16:38:51 -04:00
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo() . \n " ) , std : : runtime_error ) ; //invalid syntax
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo() getblockchaininfo() " ) , std : : runtime_error ) ; //invalid syntax
( RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo( " ) ) ; //tolerate non closing brackets if we have no arguments
( RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo()()() " ) ) ; //tolerate non command brackts
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( * node , result , " getblockchaininfo(True) " ) , UniValue ) ; //invalid argument
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( * node , result , " a(getblockchaininfo(True)) " ) , UniValue ) ; //method not found
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest abc,,abc " ) , std : : runtime_error ) ; //don't tollerate empty arguments when using ,
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest(abc,,abc) " ) , std : : runtime_error ) ; //don't tollerate empty arguments when using ,
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( * node , result , " rpcNestedTest(abc,,) " ) , std : : runtime_error ) ; //don't tollerate empty arguments when using ,
2016-12-14 08:59:09 +01:00
# endif
2016-08-20 11:19:35 +02:00
}