You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpcnestedtests.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2016 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "rpcnestedtests.h"
  5. #include "chainparams.h"
  6. #include "consensus/validation.h"
  7. #include "fs.h"
  8. #include "validation.h"
  9. #include "rpc/register.h"
  10. #include "rpc/server.h"
  11. #include "rpcconsole.h"
  12. #include "test/testutil.h"
  13. #include "test/test_bitcoin.h"
  14. #include "univalue.h"
  15. #include "util.h"
  16. #include <QDir>
  17. #include <QtGlobal>
  18. static UniValue rpcNestedTest_rpc(const JSONRPCRequest& request)
  19. {
  20. if (request.fHelp) {
  21. return "help message";
  22. }
  23. return request.params.write(0, 0);
  24. }
  25. static const CRPCCommand vRPCCommands[] =
  26. {
  27. { "test", "rpcNestedTest", &rpcNestedTest_rpc, true, {} },
  28. };
  29. void RPCNestedTests::rpcNestedTests()
  30. {
  31. // do some test setup
  32. // could be moved to a more generic place when we add more tests on QT level
  33. tableRPC.appendCommand("rpcNestedTest", &vRPCCommands[0]);
  34. //mempool.setSanityCheck(1.0);
  35. TestingSetup test;
  36. SetRPCWarmupFinished();
  37. std::string result;
  38. std::string result2;
  39. std::string filtered;
  40. RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()[chain]", &filtered); //simple result filtering with path
  41. QVERIFY(result=="main");
  42. QVERIFY(filtered == "getblockchaininfo()[chain]");
  43. RPCConsole::RPCExecuteCommandLine(result, "getblock(getbestblockhash())"); //simple 2 level nesting
  44. RPCConsole::RPCExecuteCommandLine(result, "getblock(getblock(getbestblockhash())[hash], true)");
  45. RPCConsole::RPCExecuteCommandLine(result, "getblock( getblock( getblock(getbestblockhash())[hash] )[hash], true)"); //4 level nesting with whitespace, filtering path and boolean parameter
  46. RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo");
  47. QVERIFY(result.substr(0,1) == "{");
  48. RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()");
  49. QVERIFY(result.substr(0,1) == "{");
  50. RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo "); //whitespace at the end will be tolerated
  51. QVERIFY(result.substr(0,1) == "{");
  52. (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()[\"chain\"]")); //Quote path identifier are allowed, but look after a child containing the quotes in the key
  53. QVERIFY(result == "null");
  54. (RPCConsole::RPCExecuteCommandLine(result, "createrawtransaction [] {} 0")); //parameter not in brackets are allowed
  55. (RPCConsole::RPCExecuteCommandLine(result2, "createrawtransaction([],{},0)")); //parameter in brackets are allowed
  56. QVERIFY(result == result2);
  57. (RPCConsole::RPCExecuteCommandLine(result2, "createrawtransaction( [], {} , 0 )")); //whitespace between parameters is allowed
  58. QVERIFY(result == result2);
  59. RPCConsole::RPCExecuteCommandLine(result, "getblock(getbestblockhash())[tx][0]", &filtered);
  60. QVERIFY(result == "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
  61. QVERIFY(filtered == "getblock(getbestblockhash())[tx][0]");
  62. RPCConsole::RPCParseCommandLine(result, "importprivkey", false, &filtered);
  63. QVERIFY(filtered == "importprivkey(…)");
  64. RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc", false, &filtered);
  65. QVERIFY(filtered == "signmessagewithprivkey(…)");
  66. RPCConsole::RPCParseCommandLine(result, "signmessagewithprivkey abc,def", false, &filtered);
  67. QVERIFY(filtered == "signmessagewithprivkey(…)");
  68. RPCConsole::RPCParseCommandLine(result, "signrawtransaction(abc)", false, &filtered);
  69. QVERIFY(filtered == "signrawtransaction(…)");
  70. RPCConsole::RPCParseCommandLine(result, "walletpassphrase(help())", false, &filtered);
  71. QVERIFY(filtered == "walletpassphrase(…)");
  72. RPCConsole::RPCParseCommandLine(result, "walletpassphrasechange(help(walletpassphrasechange(abc)))", false, &filtered);
  73. QVERIFY(filtered == "walletpassphrasechange(…)");
  74. RPCConsole::RPCParseCommandLine(result, "help(encryptwallet(abc, def))", false, &filtered);
  75. QVERIFY(filtered == "help(encryptwallet(…))");
  76. RPCConsole::RPCParseCommandLine(result, "help(importprivkey())", false, &filtered);
  77. QVERIFY(filtered == "help(importprivkey(…))");
  78. RPCConsole::RPCParseCommandLine(result, "help(importprivkey(help()))", false, &filtered);
  79. QVERIFY(filtered == "help(importprivkey(…))");
  80. RPCConsole::RPCParseCommandLine(result, "help(importprivkey(abc), walletpassphrase(def))", false, &filtered);
  81. QVERIFY(filtered == "help(importprivkey(…), walletpassphrase(…))");
  82. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest");
  83. QVERIFY(result == "[]");
  84. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest ''");
  85. QVERIFY(result == "[\"\"]");
  86. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest \"\"");
  87. QVERIFY(result == "[\"\"]");
  88. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest '' abc");
  89. QVERIFY(result == "[\"\",\"abc\"]");
  90. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc '' abc");
  91. QVERIFY(result == "[\"abc\",\"\",\"abc\"]");
  92. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc abc");
  93. QVERIFY(result == "[\"abc\",\"abc\"]");
  94. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc\t\tabc");
  95. QVERIFY(result == "[\"abc\",\"abc\"]");
  96. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc )");
  97. QVERIFY(result == "[\"abc\"]");
  98. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest( abc )");
  99. QVERIFY(result == "[\"abc\"]");
  100. RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest( abc , cba )");
  101. QVERIFY(result == "[\"abc\",\"cba\"]");
  102. #if QT_VERSION >= 0x050300
  103. // do the QVERIFY_EXCEPTION_THROWN checks only with Qt5.3 and higher (QVERIFY_EXCEPTION_THROWN was introduced in Qt5.3)
  104. QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo() .\n"), std::runtime_error); //invalid syntax
  105. QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo() getblockchaininfo()"), std::runtime_error); //invalid syntax
  106. (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(")); //tolerate non closing brackets if we have no arguments
  107. (RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo()()()")); //tolerate non command brackts
  108. QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "getblockchaininfo(True)"), UniValue); //invalid argument
  109. QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "a(getblockchaininfo(True))"), UniValue); //method not found
  110. QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest abc,,abc"), std::runtime_error); //don't tollerate empty arguments when using ,
  111. QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,abc)"), std::runtime_error); //don't tollerate empty arguments when using ,
  112. QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,)"), std::runtime_error); //don't tollerate empty arguments when using ,
  113. #endif
  114. }