Moving tests and adding wallet merge function.

This commit is contained in:
clemahieu 2014-12-28 15:04:08 -06:00
commit 46a32b2392
6 changed files with 692 additions and 636 deletions

View file

@ -60,9 +60,10 @@ add_executable (core_test
rai/core_test/message_parser.cpp
rai/core_test/processor_service.cpp
rai/core_test/peer_container.cpp
rai/core_test/rpc.cpp
rai/core_test/network.cpp
rai/core_test/uint256_union.cpp
rai/core_test/wallet_test.cpp
rai/core_test/wallet.cpp
rai/core_test/wallets.cpp
)

View file

@ -745,6 +745,24 @@ void rai::wallet_store::serialize_json (std::string & string_a)
string_a = ostream.str ();
}
bool rai::wallet_store::merge (rai::wallet_store & other_a)
{
assert (valid_password ());
assert (other_a.valid_password ());
auto result (false);
for (auto i (other_a.begin ()), n (other_a.end ()); i != n; ++i)
{
rai::private_key prv;
auto error (other_a.fetch (i->first, prv));
result = result | error;
if (!result)
{
insert (prv);
}
}
return result;
}
rai::wallet::wallet (bool & init_a, rai::client & client_a, boost::filesystem::path const & path_a) :
store (init_a, path_a),
client (client_a)
@ -851,6 +869,22 @@ bool rai::wallet::send (rai::account const & account_a, rai::uint128_t const & a
return result;
}
bool rai::wallet::import (std::string const & json_a, std::string const & password_a)
{
auto result (!store.valid_password ());
rai::wallet_store store_l (result, boost::filesystem::unique_path (), json_a);
if (!result)
{
store_l.enter_password (password_a);
result = !store_l.valid_password ();
if (!result)
{
result = store.merge (store_l);
}
}
return result;
}
rai::wallets::wallets (rai::client & client_a, boost::filesystem::path const & path_a) :
path (path_a),
client (client_a)

View file

@ -299,6 +299,7 @@ public:
rai::uint256_union derive_key (std::string const &);
rai::uint128_t balance (rai::ledger &);
void serialize_json (std::string &);
bool merge (rai::wallet_store &);
rai::fan password;
static rai::uint256_union const version_1;
static rai::uint256_union const version_current;
@ -321,6 +322,7 @@ public:
wallet (bool &, rai::client &, boost::filesystem::path const &, std::string const &);
bool receive (rai::send_block const &, rai::private_key const &, rai::account const &);
bool send (rai::account const &, rai::uint128_t const &);
bool import (std::string const &, std::string const &);
std::mutex mutex;
rai::wallet_store store;
rai::client & client;

View file

@ -1,8 +1,6 @@
#include <gtest/gtest.h>
#include <boost/thread.hpp>
#include <rai/core/core.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
TEST (network, tcp_connection)
{
@ -358,518 +356,6 @@ TEST (receivable_processor, send_with_receive)
ASSERT_EQ (100, system.clients [1]->ledger.account_balance (key2.pub));
}
TEST (rpc, account_create)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_create");
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
auto account_text (response_tree.get <std::string> ("account"));
rai::uint256_union account;
ASSERT_FALSE (account.decode_base58check (account_text));
ASSERT_NE (system.wallet (0)->store.end (), system.wallet (0)->store.find (account));
}
TEST (rpc, account_balance_exact)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_balance_exact");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("balance"));
ASSERT_EQ ("340282366920938463463374607431768211455", balance_text);
}
TEST (rpc, account_balance)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_balance");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("balance"));
ASSERT_EQ ("3402823669209384634", balance_text);
}
TEST (rpc, account_weight_exact)
{
rai::keypair key;
rai::system system (24000, 1);
rai::frontier frontier;
ASSERT_FALSE (system.clients [0]->store.latest_get (rai::test_genesis_key.pub, frontier));
rai::change_block block (key.pub, frontier.hash, rai::test_genesis_key.prv, rai::test_genesis_key.pub);
block.work = system.clients [0]->create_work (block);
ASSERT_EQ (rai::process_result::progress, system.clients [0]->ledger.process (block));
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_weight_exact");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("weight"));
ASSERT_EQ ("340282366920938463463374607431768211455", balance_text);
}
TEST (rpc, account_weight)
{
rai::keypair key;
rai::system system (24000, 1);
rai::frontier frontier;
ASSERT_FALSE (system.clients [0]->store.latest_get (rai::test_genesis_key.pub, frontier));
rai::change_block block (key.pub, frontier.hash, rai::test_genesis_key.prv, rai::test_genesis_key.pub);
block.work = system.clients [0]->create_work (block);
ASSERT_EQ (rai::process_result::progress, system.clients [0]->ledger.process (block));
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_weight");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("weight"));
ASSERT_EQ ("3402823669209384634", balance_text);
}
TEST (rpc, wallet_contains)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_contains");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("exists"));
ASSERT_EQ ("1", exists_text);
}
TEST (rpc, wallet_doesnt_contain)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_contains");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("exists"));
ASSERT_EQ ("0", exists_text);
}
TEST (rpc, validate_account_number)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "validate_account_number");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("valid"));
ASSERT_EQ ("1", exists_text);
}
TEST (rpc, validate_account_invalid)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
account [0] ^= 0x1;
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "validate_account_number");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("valid"));
ASSERT_EQ ("0", exists_text);
}
TEST (rpc, send_exact)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
rai::keypair key1;
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "send_exact");
request_tree.put ("account", account);
request_tree.put ("amount", "100");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string sent_text (response_tree.get <std::string> ("sent"));
ASSERT_EQ ("1", sent_text);
}
TEST (rpc, send)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
rai::keypair key1;
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "send");
request_tree.put ("account", account);
request_tree.put ("amount", "1");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
auto balance1 (system.clients [0]->ledger.account_balance (rai::test_genesis_key.pub));
rpc (request, response);
ASSERT_EQ (balance1 - rai::scale_64bit_base10, system.clients [0]->ledger.account_balance (rai::test_genesis_key.pub));
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string sent_text (response_tree.get <std::string> ("sent"));
ASSERT_EQ ("1", sent_text);
}
TEST (rpc, send_fail)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
rai::keypair key1;
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "send");
request_tree.put ("account", account);
request_tree.put ("amount", "100");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string sent_text (response_tree.get <std::string> ("sent"));
ASSERT_EQ ("0", sent_text);
}
TEST (rpc, wallet_add)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
rai::keypair key1;
std::string key_text;
key1.prv.encode_hex (key_text);
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_add");
request_tree.put ("key", key_text);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("account"));
std::string account_text2;
key1.pub.encode_base58check (account_text2);
ASSERT_EQ (account_text1, account_text2);
}
TEST (rpc, wallet_password_valid)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "password_valid");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("valid"));
ASSERT_EQ (account_text1, "1");
}
TEST (rpc, wallet_password_change)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "password_change");
request_tree.put ("password", "test");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("changed"));
ASSERT_EQ (account_text1, "1");
ASSERT_TRUE (system.wallet (0)->store.valid_password ());
system.wallet (0)->store.enter_password ("");
ASSERT_FALSE (system.wallet (0)->store.valid_password ());
system.wallet (0)->store.enter_password ("test");
ASSERT_TRUE (system.wallet (0)->store.valid_password ());
}
TEST (rpc, wallet_password_enter)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "password_enter");
request_tree.put ("password", "");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("valid"));
ASSERT_EQ (account_text1, "1");
}
TEST (rpc, representative)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "representative");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("representative"));
std::string representative;
rai::genesis_account.encode_base58check (representative);
ASSERT_EQ (account_text1, representative);
}
TEST (rpc, representative_set)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
rai::keypair key;
std::string representative_l;
key.pub.encode_base58check (representative_l);
request_tree.put ("action", "representative_set");
request_tree.put ("representative", representative_l);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
ASSERT_EQ (key.pub, system.clients [0]->wallets.items.begin ()->second->store.representative ());
}
TEST (network, receive_weight_change)
{
rai::system system (24000, 2);
@ -888,127 +374,6 @@ TEST (network, receive_weight_change)
}
}
TEST (rpc, wallet_list)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
rai::keypair key2;
system.wallet (0)->store.insert (key2.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_list");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
auto & accounts_node (response_tree.get_child ("accounts"));
std::vector <rai::uint256_union> accounts;
for (auto i (accounts_node.begin ()), j (accounts_node.end ()); i != j; ++i)
{
auto account (i->second.get <std::string> (""));
rai::uint256_union number;
ASSERT_FALSE (number.decode_base58check (account));
accounts.push_back (number);
}
ASSERT_EQ (2, accounts.size ());
for (auto i (accounts.begin ()), j (accounts.end ()); i != j; ++i)
{
ASSERT_NE (system.wallet (0)->store.end (), system.wallet (0)->store.find (*i));
}
}
TEST (rpc, wallet_key_valid)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_key_valid");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("valid"));
ASSERT_EQ ("1", exists_text);
}
TEST (rpc, wallet_create)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "wallet_create");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string wallet_text (response_tree.get <std::string> ("wallet"));
rai::uint256_union wallet_id;
ASSERT_FALSE (wallet_id.decode_hex (wallet_text));
ASSERT_NE (system.clients [0]->wallets.items.end (), system.clients [0]->wallets.items.find (wallet_id));
}
TEST (rpc, wallet_export)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "wallet_export");
request_tree.put ("wallet", system.clients [0]->wallets.items.begin ()->first.to_string ());
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string wallet_json (response_tree.get <std::string> ("json"));
bool error (false);
rai::wallet_store store (error, boost::filesystem::unique_path (), wallet_json);
ASSERT_FALSE (error);
ASSERT_TRUE (store.exists (rai::test_genesis_key.pub));
}
TEST (parse_endpoint, valid)
{
std::string string ("127.0.0.1:24000");

638
rai/core_test/rpc.cpp Normal file
View file

@ -0,0 +1,638 @@
#include <gtest/gtest.h>
#include <boost/thread.hpp>
#include <rai/core/core.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
TEST (rpc, account_create)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_create");
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
auto account_text (response_tree.get <std::string> ("account"));
rai::uint256_union account;
ASSERT_FALSE (account.decode_base58check (account_text));
ASSERT_NE (system.wallet (0)->store.end (), system.wallet (0)->store.find (account));
}
TEST (rpc, account_balance_exact)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_balance_exact");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("balance"));
ASSERT_EQ ("340282366920938463463374607431768211455", balance_text);
}
TEST (rpc, account_balance)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_balance");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("balance"));
ASSERT_EQ ("3402823669209384634", balance_text);
}
TEST (rpc, account_weight_exact)
{
rai::keypair key;
rai::system system (24000, 1);
rai::frontier frontier;
ASSERT_FALSE (system.clients [0]->store.latest_get (rai::test_genesis_key.pub, frontier));
rai::change_block block (key.pub, frontier.hash, rai::test_genesis_key.prv, rai::test_genesis_key.pub);
block.work = system.clients [0]->create_work (block);
ASSERT_EQ (rai::process_result::progress, system.clients [0]->ledger.process (block));
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_weight_exact");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("weight"));
ASSERT_EQ ("340282366920938463463374607431768211455", balance_text);
}
TEST (rpc, account_weight)
{
rai::keypair key;
rai::system system (24000, 1);
rai::frontier frontier;
ASSERT_FALSE (system.clients [0]->store.latest_get (rai::test_genesis_key.pub, frontier));
rai::change_block block (key.pub, frontier.hash, rai::test_genesis_key.prv, rai::test_genesis_key.pub);
block.work = system.clients [0]->create_work (block);
ASSERT_EQ (rai::process_result::progress, system.clients [0]->ledger.process (block));
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "account_weight");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string balance_text (response_tree.get <std::string> ("weight"));
ASSERT_EQ ("3402823669209384634", balance_text);
}
TEST (rpc, wallet_contains)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_contains");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("exists"));
ASSERT_EQ ("1", exists_text);
}
TEST (rpc, wallet_doesnt_contain)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_contains");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("exists"));
ASSERT_EQ ("0", exists_text);
}
TEST (rpc, validate_account_number)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "validate_account_number");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("valid"));
ASSERT_EQ ("1", exists_text);
}
TEST (rpc, validate_account_invalid)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
account [0] ^= 0x1;
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "validate_account_number");
request_tree.put ("account", account);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("valid"));
ASSERT_EQ ("0", exists_text);
}
TEST (rpc, send_exact)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
rai::keypair key1;
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "send_exact");
request_tree.put ("account", account);
request_tree.put ("amount", "100");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string sent_text (response_tree.get <std::string> ("sent"));
ASSERT_EQ ("1", sent_text);
}
TEST (rpc, send)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
rai::keypair key1;
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "send");
request_tree.put ("account", account);
request_tree.put ("amount", "1");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
auto balance1 (system.clients [0]->ledger.account_balance (rai::test_genesis_key.pub));
rpc (request, response);
ASSERT_EQ (balance1 - rai::scale_64bit_base10, system.clients [0]->ledger.account_balance (rai::test_genesis_key.pub));
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string sent_text (response_tree.get <std::string> ("sent"));
ASSERT_EQ ("1", sent_text);
}
TEST (rpc, send_fail)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
rai::keypair key1;
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "send");
request_tree.put ("account", account);
request_tree.put ("amount", "100");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string sent_text (response_tree.get <std::string> ("sent"));
ASSERT_EQ ("0", sent_text);
}
TEST (rpc, wallet_add)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
rai::keypair key1;
std::string key_text;
key1.prv.encode_hex (key_text);
system.wallet (0)->store.insert (key1.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_add");
request_tree.put ("key", key_text);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("account"));
std::string account_text2;
key1.pub.encode_base58check (account_text2);
ASSERT_EQ (account_text1, account_text2);
}
TEST (rpc, wallet_password_valid)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "password_valid");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("valid"));
ASSERT_EQ (account_text1, "1");
}
TEST (rpc, wallet_password_change)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "password_change");
request_tree.put ("password", "test");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("changed"));
ASSERT_EQ (account_text1, "1");
ASSERT_TRUE (system.wallet (0)->store.valid_password ());
system.wallet (0)->store.enter_password ("");
ASSERT_FALSE (system.wallet (0)->store.valid_password ());
system.wallet (0)->store.enter_password ("test");
ASSERT_TRUE (system.wallet (0)->store.valid_password ());
}
TEST (rpc, wallet_password_enter)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "password_enter");
request_tree.put ("password", "");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("valid"));
ASSERT_EQ (account_text1, "1");
}
TEST (rpc, representative)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "representative");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string account_text1 (response_tree.get <std::string> ("representative"));
std::string representative;
rai::genesis_account.encode_base58check (representative);
ASSERT_EQ (account_text1, representative);
}
TEST (rpc, representative_set)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
rai::keypair key;
std::string representative_l;
key.pub.encode_base58check (representative_l);
request_tree.put ("action", "representative_set");
request_tree.put ("representative", representative_l);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
ASSERT_EQ (key.pub, system.clients [0]->wallets.items.begin ()->second->store.representative ());
}
TEST (rpc, wallet_list)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
rai::keypair key2;
system.wallet (0)->store.insert (key2.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_list");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
auto & accounts_node (response_tree.get_child ("accounts"));
std::vector <rai::uint256_union> accounts;
for (auto i (accounts_node.begin ()), j (accounts_node.end ()); i != j; ++i)
{
auto account (i->second.get <std::string> (""));
rai::uint256_union number;
ASSERT_FALSE (number.decode_base58check (account));
accounts.push_back (number);
}
ASSERT_EQ (2, accounts.size ());
for (auto i (accounts.begin ()), j (accounts.end ()); i != j; ++i)
{
ASSERT_NE (system.wallet (0)->store.end (), system.wallet (0)->store.find (*i));
}
}
TEST (rpc, wallet_key_valid)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
std::string account;
rai::test_genesis_key.pub.encode_base58check (account);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
std::string wallet;
system.clients [0]->wallets.items.begin ()->first.encode_hex (wallet);
request_tree.put ("wallet", wallet);
request_tree.put ("action", "wallet_key_valid");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string exists_text (response_tree.get <std::string> ("valid"));
ASSERT_EQ ("1", exists_text);
}
TEST (rpc, wallet_create)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "wallet_create");
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string wallet_text (response_tree.get <std::string> ("wallet"));
rai::uint256_union wallet_id;
ASSERT_FALSE (wallet_id.decode_hex (wallet_text));
ASSERT_NE (system.clients [0]->wallets.items.end (), system.clients [0]->wallets.items.find (wallet_id));
}
TEST (rpc, wallet_export)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
boost::network::http::server <rai::rpc>::request request;
boost::network::http::server <rai::rpc>::response response;
request.method = "POST";
boost::property_tree::ptree request_tree;
request_tree.put ("action", "wallet_export");
request_tree.put ("wallet", system.clients [0]->wallets.items.begin ()->first.to_string ());
std::stringstream ostream;
boost::property_tree::write_json (ostream, request_tree);
request.body = ostream.str ();
rpc (request, response);
ASSERT_EQ (boost::network::http::server <rai::rpc>::response::ok, response.status);
boost::property_tree::ptree response_tree;
std::stringstream istream (response.content);
boost::property_tree::read_json (istream, response_tree);
std::string wallet_json (response_tree.get <std::string> ("json"));
bool error (false);
rai::wallet_store store (error, boost::filesystem::unique_path (), wallet_json);
ASSERT_FALSE (error);
ASSERT_TRUE (store.exists (rai::test_genesis_key.pub));
}

View file

@ -394,4 +394,20 @@ TEST (wallet, serialize_json_password)
rai::private_key prv;
wallet2.fetch (key.pub, prv);
ASSERT_EQ (key.prv, prv);
}
TEST (wallet_store, merge)
{
auto error (false);
rai::wallet_store wallet1 (error, boost::filesystem::unique_path ());
ASSERT_FALSE (error);
rai::keypair key1;
wallet1.insert (key1.prv);
rai::wallet_store wallet2 (error, boost::filesystem::unique_path ());
ASSERT_FALSE (error);
rai::keypair key2;
wallet2.insert (key2.prv);
ASSERT_FALSE (wallet1.exists (key2.pub));
ASSERT_FALSE (wallet1.merge (wallet2));
ASSERT_TRUE (wallet1.exists (key2.pub));
}