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 6.9KB

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