2016-08-20 11:19:35 +02:00
// Copyright (c) 2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
# include "rpcnestedtests.h"
# include "chainparams.h"
# include "consensus/validation.h"
2017-03-01 16:54:22 +01:00
# include "fs.h"
2016-12-02 01:06:41 +01:00
# include "validation.h"
2016-08-20 11:19:35 +02:00
# include "rpc/register.h"
# include "rpc/server.h"
# include "rpcconsole.h"
2017-04-27 02:13:24 +02:00
# include "test/test_bitcoin.h"
2016-08-20 11:19:35 +02:00
# include "univalue.h"
# include "util.h"
# include <QDir>
2016-12-14 08:59:09 +01:00
# include <QtGlobal>
2016-08-20 11:19:35 +02:00
2016-12-13 00:04:10 +01: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-09 02:38:23 +02:00
{ " test " , " rpcNestedTest " , & rpcNestedTest_rpc , { } } ,
2016-12-13 00:04:10 +01: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-13 00:04:10 +01:00
tableRPC . appendCommand ( " rpcNestedTest " , & vRPCCommands [ 0 ] ) ;
2016-08-20 11:19:35 +02:00
ClearDatadirCache ( ) ;
std : : string path = QDir : : tempPath ( ) . toStdString ( ) + " / " + strprintf ( " test_bitcoin_qt_%lu_%i " , ( unsigned long ) GetTime ( ) , ( int ) ( GetRand ( 100000 ) ) ) ;
QDir dir ( QString : : fromStdString ( path ) ) ;
dir . mkpath ( " . " ) ;
2017-08-01 21:17:40 +02:00
gArgs . ForceSetArg ( " -datadir " , path ) ;
2016-08-20 11:19:35 +02:00
//mempool.setSanityCheck(1.0);
2017-04-27 02:13:24 +02:00
TestingSetup test ;
2016-08-20 11:19:35 +02:00
SetRPCWarmupFinished ( ) ;
std : : string result ;
std : : string result2 ;
2016-11-16 13:31:44 +01:00
std : : string filtered ;
RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo()[chain] " , & filtered ) ; //simple result filtering with path
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = " main " ) ;
2016-11-16 13:31:44 +01:00
QVERIFY ( filtered = = " getblockchaininfo()[chain] " ) ;
2016-08-20 11:19:35 +02:00
RPCConsole : : RPCExecuteCommandLine ( result , " getblock(getbestblockhash()) " ) ; //simple 2 level nesting
RPCConsole : : RPCExecuteCommandLine ( result , " getblock(getblock(getbestblockhash())[hash], true) " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true) " ) ; //4 level nesting with whitespace, filtering path and boolean parameter
RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo " ) ;
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo() " ) ;
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo " ) ; //whitespace at the end will be tolerated
QVERIFY ( result . substr ( 0 , 1 ) = = " { " ) ;
2017-08-16 00:24:39 +02:00
( RPCConsole : : RPCExecuteCommandLine ( 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 " ) ;
( RPCConsole : : RPCExecuteCommandLine ( result , " createrawtransaction [] {} 0 " ) ) ; //parameter not in brackets are allowed
( RPCConsole : : RPCExecuteCommandLine ( result2 , " createrawtransaction([],{},0) " ) ) ; //parameter in brackets are allowed
QVERIFY ( result = = result2 ) ;
2017-07-30 21:42:17 +02:00
( RPCConsole : : RPCExecuteCommandLine ( result2 , " createrawtransaction( [], {} , 0 ) " ) ) ; //whitespace between parameters is allowed
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = result2 ) ;
2016-11-16 13:31:44 +01:00
RPCConsole : : RPCExecuteCommandLine ( result , " getblock(getbestblockhash())[tx][0] " , & filtered ) ;
2016-08-20 11:19:35 +02:00
QVERIFY ( result = = " 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b " ) ;
2016-11-16 13:31:44 +01:00
QVERIFY ( filtered = = " getblock(getbestblockhash())[tx][0] " ) ;
RPCConsole : : RPCParseCommandLine ( result , " importprivkey " , false , & filtered ) ;
QVERIFY ( filtered = = " importprivkey(…) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " signmessagewithprivkey abc " , false , & filtered ) ;
QVERIFY ( filtered = = " signmessagewithprivkey(…) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " signmessagewithprivkey abc,def " , false , & filtered ) ;
QVERIFY ( filtered = = " signmessagewithprivkey(…) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " signrawtransaction(abc) " , false , & filtered ) ;
QVERIFY ( filtered = = " signrawtransaction(…) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " walletpassphrase(help()) " , false , & filtered ) ;
QVERIFY ( filtered = = " walletpassphrase(…) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " walletpassphrasechange(help(walletpassphrasechange(abc))) " , false , & filtered ) ;
QVERIFY ( filtered = = " walletpassphrasechange(…) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " help(encryptwallet(abc, def)) " , false , & filtered ) ;
QVERIFY ( filtered = = " help(encryptwallet(…)) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " help(importprivkey()) " , false , & filtered ) ;
QVERIFY ( filtered = = " help(importprivkey(…)) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " help(importprivkey(help())) " , false , & filtered ) ;
QVERIFY ( filtered = = " help(importprivkey(…)) " ) ;
RPCConsole : : RPCParseCommandLine ( result , " help(importprivkey(abc), walletpassphrase(def)) " , false , & filtered ) ;
QVERIFY ( filtered = = " help(importprivkey(…), walletpassphrase(…)) " ) ;
2016-08-20 11:19:35 +02:00
2016-12-13 00:04:10 +01:00
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest " ) ;
QVERIFY ( result = = " [] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest '' " ) ;
QVERIFY ( result = = " [ \" \" ] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest \" \" " ) ;
QVERIFY ( result = = " [ \" \" ] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest '' abc " ) ;
QVERIFY ( result = = " [ \" \" , \" abc \" ] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest abc '' abc " ) ;
QVERIFY ( result = = " [ \" abc \" , \" \" , \" abc \" ] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest abc abc " ) ;
QVERIFY ( result = = " [ \" abc \" , \" abc \" ] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest abc \t \t abc " ) ;
QVERIFY ( result = = " [ \" abc \" , \" abc \" ] " ) ;
2016-12-14 08:59:09 +01:00
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest(abc ) " ) ;
QVERIFY ( result = = " [ \" abc \" ] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest( abc ) " ) ;
QVERIFY ( result = = " [ \" abc \" ] " ) ;
RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest( abc , cba ) " ) ;
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)
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo() . \n " ) , std : : runtime_error ) ; //invalid syntax
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo() getblockchaininfo() " ) , std : : runtime_error ) ; //invalid syntax
( RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo( " ) ) ; //tolerate non closing brackets if we have no arguments
( RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo()()() " ) ) ; //tolerate non command brackts
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( result , " getblockchaininfo(True) " ) , UniValue ) ; //invalid argument
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( result , " a(getblockchaininfo(True)) " ) , UniValue ) ; //method not found
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest abc,,abc " ) , std : : runtime_error ) ; //don't tollerate empty arguments when using ,
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest(abc,,abc) " ) , std : : runtime_error ) ; //don't tollerate empty arguments when using ,
QVERIFY_EXCEPTION_THROWN ( RPCConsole : : RPCExecuteCommandLine ( result , " rpcNestedTest(abc,,) " ) , std : : runtime_error ) ; //don't tollerate empty arguments when using ,
# endif
2016-12-13 00:04:10 +01:00
2017-03-01 17:05:50 +01:00
fs : : remove_all ( fs : : path ( path ) ) ;
2016-08-20 11:19:35 +02:00
}