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.

rpc_tests.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2012-2013 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 "rpcserver.h"
  5. #include "rpcclient.h"
  6. #include "base58.h"
  7. #include "netbase.h"
  8. #include "test/test_bitcoin.h"
  9. #include <boost/algorithm/string.hpp>
  10. #include <boost/test/unit_test.hpp>
  11. using namespace std;
  12. using namespace json_spirit;
  13. Array
  14. createArgs(int nRequired, const char* address1=NULL, const char* address2=NULL)
  15. {
  16. Array result;
  17. result.push_back(nRequired);
  18. Array addresses;
  19. if (address1) addresses.push_back(address1);
  20. if (address2) addresses.push_back(address2);
  21. result.push_back(addresses);
  22. return result;
  23. }
  24. Value CallRPC(string args)
  25. {
  26. vector<string> vArgs;
  27. boost::split(vArgs, args, boost::is_any_of(" \t"));
  28. string strMethod = vArgs[0];
  29. vArgs.erase(vArgs.begin());
  30. Array params = RPCConvertValues(strMethod, vArgs);
  31. rpcfn_type method = tableRPC[strMethod]->actor;
  32. try {
  33. Value result = (*method)(params, false);
  34. return result;
  35. }
  36. catch (const Object& objError) {
  37. throw runtime_error(find_value(objError, "message").get_str());
  38. }
  39. }
  40. BOOST_FIXTURE_TEST_SUITE(rpc_tests, TestingSetup)
  41. BOOST_AUTO_TEST_CASE(rpc_rawparams)
  42. {
  43. // Test raw transaction API argument handling
  44. Value r;
  45. BOOST_CHECK_THROW(CallRPC("getrawtransaction"), runtime_error);
  46. BOOST_CHECK_THROW(CallRPC("getrawtransaction not_hex"), runtime_error);
  47. BOOST_CHECK_THROW(CallRPC("getrawtransaction a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed not_int"), runtime_error);
  48. BOOST_CHECK_THROW(CallRPC("createrawtransaction"), runtime_error);
  49. BOOST_CHECK_THROW(CallRPC("createrawtransaction null null"), runtime_error);
  50. BOOST_CHECK_THROW(CallRPC("createrawtransaction not_array"), runtime_error);
  51. BOOST_CHECK_THROW(CallRPC("createrawtransaction [] []"), runtime_error);
  52. BOOST_CHECK_THROW(CallRPC("createrawtransaction {} {}"), runtime_error);
  53. BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [] {}"));
  54. BOOST_CHECK_THROW(CallRPC("createrawtransaction [] {} extra"), runtime_error);
  55. BOOST_CHECK_THROW(CallRPC("decoderawtransaction"), runtime_error);
  56. BOOST_CHECK_THROW(CallRPC("decoderawtransaction null"), runtime_error);
  57. BOOST_CHECK_THROW(CallRPC("decoderawtransaction DEADBEEF"), runtime_error);
  58. string rawtx = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
  59. BOOST_CHECK_NO_THROW(r = CallRPC(string("decoderawtransaction ")+rawtx));
  60. BOOST_CHECK_EQUAL(find_value(r.get_obj(), "version").get_int(), 1);
  61. BOOST_CHECK_EQUAL(find_value(r.get_obj(), "locktime").get_int(), 0);
  62. BOOST_CHECK_THROW(r = CallRPC(string("decoderawtransaction ")+rawtx+" extra"), runtime_error);
  63. BOOST_CHECK_THROW(CallRPC("signrawtransaction"), runtime_error);
  64. BOOST_CHECK_THROW(CallRPC("signrawtransaction null"), runtime_error);
  65. BOOST_CHECK_THROW(CallRPC("signrawtransaction ff00"), runtime_error);
  66. BOOST_CHECK_NO_THROW(CallRPC(string("signrawtransaction ")+rawtx));
  67. BOOST_CHECK_NO_THROW(CallRPC(string("signrawtransaction ")+rawtx+" null null NONE|ANYONECANPAY"));
  68. BOOST_CHECK_NO_THROW(CallRPC(string("signrawtransaction ")+rawtx+" [] [] NONE|ANYONECANPAY"));
  69. BOOST_CHECK_THROW(CallRPC(string("signrawtransaction ")+rawtx+" null null badenum"), runtime_error);
  70. // Only check failure cases for sendrawtransaction, there's no network to send to...
  71. BOOST_CHECK_THROW(CallRPC("sendrawtransaction"), runtime_error);
  72. BOOST_CHECK_THROW(CallRPC("sendrawtransaction null"), runtime_error);
  73. BOOST_CHECK_THROW(CallRPC("sendrawtransaction DEADBEEF"), runtime_error);
  74. BOOST_CHECK_THROW(CallRPC(string("sendrawtransaction ")+rawtx+" extra"), runtime_error);
  75. }
  76. BOOST_AUTO_TEST_CASE(rpc_rawsign)
  77. {
  78. Value r;
  79. // input is a 1-of-2 multisig (so is output):
  80. string prevout =
  81. "[{\"txid\":\"b4cc287e58f87cdae59417329f710f3ecd75a4ee1d2872b7248f50977c8493f3\","
  82. "\"vout\":1,\"scriptPubKey\":\"a914b10c9df5f7edf436c697f02f1efdba4cf399615187\","
  83. "\"redeemScript\":\"512103debedc17b3df2badbcdd86d5feb4562b86fe182e5998abd8bcd4f122c6155b1b21027e940bb73ab8732bfdf7f9216ecefca5b94d6df834e77e108f68e66f126044c052ae\"}]";
  84. r = CallRPC(string("createrawtransaction ")+prevout+" "+
  85. "{\"3HqAe9LtNBjnsfM4CyYaWTnvCaUYT7v4oZ\":11}");
  86. string notsigned = r.get_str();
  87. string privkey1 = "\"KzsXybp9jX64P5ekX1KUxRQ79Jht9uzW7LorgwE65i5rWACL6LQe\"";
  88. string privkey2 = "\"Kyhdf5LuKTRx4ge69ybABsiUAWjVRK4XGxAKk2FQLp2HjGMy87Z4\"";
  89. r = CallRPC(string("signrawtransaction ")+notsigned+" "+prevout+" "+"[]");
  90. BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == false);
  91. r = CallRPC(string("signrawtransaction ")+notsigned+" "+prevout+" "+"["+privkey1+","+privkey2+"]");
  92. BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == true);
  93. }
  94. BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
  95. {
  96. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(0LL), false), "0.00000000");
  97. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(1LL), false), "0.00000001");
  98. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(17622195LL), false), "0.17622195");
  99. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(50000000LL), false), "0.50000000");
  100. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(89898989LL), false), "0.89898989");
  101. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(100000000LL), false), "1.00000000");
  102. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(2099999999999990LL), false), "20999999.99999990");
  103. BOOST_CHECK_EQUAL(write_string(ValueFromAmount(2099999999999999LL), false), "20999999.99999999");
  104. }
  105. static Value ValueFromString(const std::string &str)
  106. {
  107. Value value;
  108. BOOST_CHECK(read_string(str, value));
  109. return value;
  110. }
  111. BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
  112. {
  113. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000001")), 1LL);
  114. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.17622195")), 17622195LL);
  115. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.5")), 50000000LL);
  116. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.50000000")), 50000000LL);
  117. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.89898989")), 89898989LL);
  118. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1.00000000")), 100000000LL);
  119. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.9999999")), 2099999999999990LL);
  120. BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.99999999")), 2099999999999999LL);
  121. }
  122. BOOST_AUTO_TEST_CASE(rpc_boostasiotocnetaddr)
  123. {
  124. // Check IPv4 addresses
  125. BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("1.2.3.4")).ToString(), "1.2.3.4");
  126. BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("127.0.0.1")).ToString(), "127.0.0.1");
  127. // Check IPv6 addresses
  128. BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::1")).ToString(), "::1");
  129. BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("123:4567:89ab:cdef:123:4567:89ab:cdef")).ToString(),
  130. "123:4567:89ab:cdef:123:4567:89ab:cdef");
  131. // v4 compatible must be interpreted as IPv4
  132. BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::0:127.0.0.1")).ToString(), "127.0.0.1");
  133. // v4 mapped must be interpreted as IPv4
  134. BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::ffff:127.0.0.1")).ToString(), "127.0.0.1");
  135. }
  136. BOOST_AUTO_TEST_SUITE_END()