From 92dc37c43a75b634cc829fb97d4dccdb42911e87 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Wed, 7 Aug 2019 13:11:22 +0100 Subject: [PATCH] Add test for retrieving non-existing account (#2206) * Add test for non-existing account in rpc.account_info * Fix assert for conf height db * Avoid using hardcoded error codes --- nano/rpc_test/rpc.cpp | 139 +++++++++++++++++------------ nano/secure/blockstore_partial.hpp | 2 +- 2 files changed, 82 insertions(+), 59 deletions(-) diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index caf7f19a..7ccf4f65 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -223,7 +223,7 @@ TEST (rpc, account_create) ASSERT_NO_ERROR (system.poll ()); } ASSERT_EQ (200, response2.status); - ASSERT_EQ (response2.json.get ("error"), "Invalid index"); + ASSERT_EQ (std::error_code (nano::error_common::invalid_index).message (), response2.json.get ("error")); } TEST (rpc, account_weight) @@ -440,7 +440,7 @@ TEST (rpc, send_fail) ASSERT_NO_ERROR (system.poll ()); } done = true; - ASSERT_EQ (response.json.get ("error"), "Account not found in wallet"); + ASSERT_EQ (std::error_code (nano::error_common::account_not_found_wallet).message (), response.json.get ("error")); thread2.join (); } @@ -471,7 +471,7 @@ TEST (rpc, send_work) { ASSERT_NO_ERROR (system.poll ()); } - ASSERT_EQ (response.json.get ("error"), "Invalid work"); + ASSERT_EQ (std::error_code (nano::error_common::invalid_work).message (), response.json.get ("error")); request.erase ("work"); request.put ("work", nano::to_string_hex (system.nodes[0]->work_generate_blocking (system.nodes[0]->latest (nano::test_genesis_key.pub)))); test_response response2 (request, rpc.config.port, system.io_ctx); @@ -540,7 +540,7 @@ TEST (rpc, send_idempotent) ASSERT_NO_ERROR (system.poll ()); } ASSERT_EQ (200, response3.status); - ASSERT_EQ (response3.json.get ("error"), "Insufficient balance"); + ASSERT_EQ (std::error_code (nano::error_common::insufficient_balance).message (), response3.json.get ("error")); } TEST (rpc, stop) @@ -4426,19 +4426,8 @@ TEST (rpc, account_info) nano::system system (24000, 1); nano::keypair key; nano::genesis genesis; - system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); - system.wallet (0)->insert_adhoc (key.prv); + auto & node1 (*system.nodes[0]); - auto latest (system.nodes[0]->latest (nano::test_genesis_key.pub)); - nano::send_block send (latest, key.pub, 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, node1.work_generate_blocking (latest)); - system.nodes[0]->process (send); - auto time (nano::seconds_since_epoch ()); - - { - auto transaction = node1.store.tx_begin_write (); - node1.store.confirmation_height_put (transaction, nano::test_genesis_key.pub, 1); - } - enable_ipc_transport_tcp (node1.config.ipc_config.transport_tcp); nano::node_rpc_config node_rpc_config; nano::ipc::ipc_server ipc_server (node1, node_rpc_config); @@ -4446,53 +4435,87 @@ TEST (rpc, account_info) nano::ipc_rpc_processor ipc_rpc_processor (system.io_ctx, rpc_config); nano::rpc rpc (system.io_ctx, rpc_config, ipc_rpc_processor); rpc.start (); + boost::property_tree::ptree request; request.put ("action", "account_info"); - request.put ("account", nano::test_genesis_key.pub.to_account ()); - test_response response (request, rpc.config.port, system.io_ctx); - system.deadline_set (5s); - while (response.status == 0) + request.put ("account", nano::account ().to_account ()); + + // Test for a non existing account { - ASSERT_NO_ERROR (system.poll ()); + test_response response (request, rpc.config.port, system.io_ctx); + system.deadline_set (5s); + while (response.status == 0) + { + ASSERT_NO_ERROR (system.poll ()); + } + + auto error (response.json.get_optional ("error")); + ASSERT_TRUE (error.is_initialized ()); + ASSERT_EQ (error.get (), std::error_code (nano::error_common::account_not_found).message ()); } - ASSERT_EQ (200, response.status); - std::string frontier (response.json.get ("frontier")); - ASSERT_EQ (send.hash ().to_string (), frontier); - std::string open_block (response.json.get ("open_block")); - ASSERT_EQ (genesis.hash ().to_string (), open_block); - std::string representative_block (response.json.get ("representative_block")); - ASSERT_EQ (genesis.hash ().to_string (), representative_block); - std::string balance (response.json.get ("balance")); - ASSERT_EQ ("100", balance); - std::string modified_timestamp (response.json.get ("modified_timestamp")); - ASSERT_LT (std::abs ((long)time - stol (modified_timestamp)), 5); - std::string block_count (response.json.get ("block_count")); - ASSERT_EQ ("2", block_count); - std::string confirmation_height (response.json.get ("confirmation_height")); - ASSERT_EQ ("1", confirmation_height); - ASSERT_EQ (0, response.json.get ("account_version")); - boost::optional weight (response.json.get_optional ("weight")); - ASSERT_FALSE (weight.is_initialized ()); - boost::optional pending (response.json.get_optional ("pending")); - ASSERT_FALSE (pending.is_initialized ()); - boost::optional representative (response.json.get_optional ("representative")); - ASSERT_FALSE (representative.is_initialized ()); + + system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); + system.wallet (0)->insert_adhoc (key.prv); + auto latest (system.nodes[0]->latest (nano::test_genesis_key.pub)); + nano::send_block send (latest, key.pub, 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, node1.work_generate_blocking (latest)); + system.nodes[0]->process (send); + auto time (nano::seconds_since_epoch ()); + { + auto transaction = node1.store.tx_begin_write (); + node1.store.confirmation_height_put (transaction, nano::test_genesis_key.pub, 1); + } + + request.put ("account", nano::test_genesis_key.pub.to_account ()); + { + test_response response (request, rpc.config.port, system.io_ctx); + system.deadline_set (5s); + while (response.status == 0) + { + ASSERT_NO_ERROR (system.poll ()); + } + + ASSERT_EQ (200, response.status); + std::string frontier (response.json.get ("frontier")); + ASSERT_EQ (send.hash ().to_string (), frontier); + std::string open_block (response.json.get ("open_block")); + ASSERT_EQ (genesis.hash ().to_string (), open_block); + std::string representative_block (response.json.get ("representative_block")); + ASSERT_EQ (genesis.hash ().to_string (), representative_block); + std::string balance (response.json.get ("balance")); + ASSERT_EQ ("100", balance); + std::string modified_timestamp (response.json.get ("modified_timestamp")); + ASSERT_LT (std::abs ((long)time - stol (modified_timestamp)), 5); + std::string block_count (response.json.get ("block_count")); + ASSERT_EQ ("2", block_count); + std::string confirmation_height (response.json.get ("confirmation_height")); + ASSERT_EQ ("1", confirmation_height); + ASSERT_EQ (0, response.json.get ("account_version")); + boost::optional weight (response.json.get_optional ("weight")); + ASSERT_FALSE (weight.is_initialized ()); + boost::optional pending (response.json.get_optional ("pending")); + ASSERT_FALSE (pending.is_initialized ()); + boost::optional representative (response.json.get_optional ("representative")); + ASSERT_FALSE (representative.is_initialized ()); + } + // Test for optional values request.put ("weight", "true"); request.put ("pending", "1"); request.put ("representative", "1"); - test_response response2 (request, rpc.config.port, system.io_ctx); - system.deadline_set (5s); - while (response2.status == 0) { - ASSERT_NO_ERROR (system.poll ()); + test_response response (request, rpc.config.port, system.io_ctx); + system.deadline_set (5s); + while (response.status == 0) + { + ASSERT_NO_ERROR (system.poll ()); + } + std::string weight2 (response.json.get ("weight")); + ASSERT_EQ ("100", weight2); + std::string pending2 (response.json.get ("pending")); + ASSERT_EQ ("0", pending2); + std::string representative2 (response.json.get ("representative")); + ASSERT_EQ (nano::test_genesis_key.pub.to_account (), representative2); } - std::string weight2 (response2.json.get ("weight")); - ASSERT_EQ ("100", weight2); - std::string pending2 (response2.json.get ("pending")); - ASSERT_EQ ("0", pending2); - std::string representative2 (response2.json.get ("representative")); - ASSERT_EQ (nano::test_genesis_key.pub.to_account (), representative2); } /** Make sure we can use json block literals instead of string as input */ @@ -4633,7 +4656,7 @@ TEST (rpc, blocks_info) ASSERT_NO_ERROR (system.poll ()); } ASSERT_EQ (200, response.status); - ASSERT_EQ ("Block not found", response.json.get ("error")); + ASSERT_EQ (std::error_code (nano::error_blocks::not_found).message (), response.json.get ("error")); } request.put ("include_not_found", "true"); { @@ -5356,7 +5379,7 @@ TEST (rpc, wallet_create_fail) { ASSERT_NO_ERROR (system.poll ()); } - ASSERT_EQ ("Failed to create wallet. Increase lmdb_max_dbs in node config", response.json.get ("error")); + ASSERT_EQ (std::error_code (nano::error_common::wallet_lmdb_max_dbs).message (), response.json.get ("error")); } TEST (rpc, wallet_ledger) @@ -5807,7 +5830,7 @@ TEST (rpc, block_confirm_absent) ASSERT_NO_ERROR (system.poll ()); } ASSERT_EQ (200, response.status); - ASSERT_EQ ("Block not found", response.json.get ("error")); + ASSERT_EQ (std::error_code (nano::error_blocks::not_found).message (), response.json.get ("error")); } TEST (rpc, block_confirm_confirmed) @@ -6294,7 +6317,7 @@ TEST (rpc, block_confirmed) ASSERT_NO_ERROR (system.poll ()); } ASSERT_EQ (200, response.status); - ASSERT_EQ ("Invalid block hash", response.json.get ("error")); + ASSERT_EQ (std::error_code (nano::error_blocks::invalid_block_hash).message (), response.json.get ("error")); request.put ("hash", "0"); test_response response1 (request, rpc.config.port, system.io_ctx); @@ -6304,7 +6327,7 @@ TEST (rpc, block_confirmed) system.poll (); } ASSERT_EQ (200, response1.status); - ASSERT_EQ ("Block not found", response1.json.get ("error")); + ASSERT_EQ (std::error_code (nano::error_blocks::not_found).message (), response1.json.get ("error")); system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); nano::keypair key; diff --git a/nano/secure/blockstore_partial.hpp b/nano/secure/blockstore_partial.hpp index 1c58ad1c..55c44e98 100644 --- a/nano/secure/blockstore_partial.hpp +++ b/nano/secure/blockstore_partial.hpp @@ -824,7 +824,7 @@ public: { nano::db_val value; auto status = get (transaction_a, tables::confirmation_height, nano::db_val (account_a), value); - release_assert (success (status) || !not_found (status)); + release_assert (success (status) || not_found (status)); confirmation_height_a = 0; if (success (status)) {