diff options
author | Antonio Juarez <antonio.maria.juarez@live.com> | 2014-04-02 17:00:17 +0100 |
---|---|---|
committer | Antonio Juarez <antonio.maria.juarez@live.com> | 2014-04-02 17:00:17 +0100 |
commit | 29c2859a3e8a935ef605534c6c36333894980d50 (patch) | |
tree | 1d31daa09741cc82ecc24c0c89f15b84e74cb12d /tests | |
parent | some fixes (diff) | |
download | monero-29c2859a3e8a935ef605534c6c36333894980d50.tar.xz |
json rpc for wallet and bugfix
Diffstat (limited to '')
-rw-r--r-- | tests/core_tests/block_reward.cpp | 2 | ||||
-rw-r--r-- | tests/core_tests/chaingen.cpp | 15 | ||||
-rw-r--r-- | tests/core_tests/transaction_tests.cpp | 18 | ||||
-rw-r--r-- | tests/difficulty/difficulty.cpp | 1 | ||||
-rw-r--r-- | tests/functional_tests/transactions_flow_test.cpp | 53 | ||||
-rw-r--r-- | tests/net_load_tests/clt.cpp | 2 | ||||
-rw-r--r-- | tests/performance_tests/multi_tx_test_base.h | 3 | ||||
-rw-r--r-- | tests/performance_tests/single_tx_test_base.h | 3 | ||||
-rw-r--r-- | tests/unit_tests/block_reward.cpp | 76 | ||||
-rw-r--r-- | tests/unit_tests/get_xtype_from_string.cpp | 3 | ||||
-rw-r--r-- | tests/unit_tests/parse_amount.cpp | 124 | ||||
-rw-r--r-- | tests/unit_tests/test_format_utils.cpp | 18 |
12 files changed, 219 insertions, 99 deletions
diff --git a/tests/core_tests/block_reward.cpp b/tests/core_tests/block_reward.cpp index 14d181d75..7950738a6 100644 --- a/tests/core_tests/block_reward.cpp +++ b/tests/core_tests/block_reward.cpp @@ -16,7 +16,7 @@ namespace const account_public_address& miner_address, std::vector<size_t>& block_sizes, size_t target_tx_size, size_t target_block_size, uint64_t fee = 0) { - if (!construct_miner_tx(height, already_generated_coins, miner_address, miner_tx, fee, block_sizes, target_block_size)) + if (!construct_miner_tx(height, misc_utils::median(block_sizes), already_generated_coins, target_block_size, fee, miner_address, miner_tx)) return false; size_t current_size = get_object_blobsize(miner_tx); diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index c876e3a75..f00323518 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -71,8 +71,8 @@ uint64_t test_generator::get_already_generated_coins(const cryptonote::block& bl void test_generator::add_block(const cryptonote::block& blk, size_t tsx_size, std::vector<size_t>& block_sizes, uint64_t already_generated_coins) { const size_t block_size = tsx_size + get_object_blobsize(blk.miner_tx); - bool block_too_big; - uint64_t block_reward = get_block_reward(block_sizes, block_size, block_too_big, already_generated_coins); + uint64_t block_reward; + get_block_reward(misc_utils::median(block_sizes), block_size, already_generated_coins, block_reward); m_blocks_info[get_block_hash(blk)] = block_info(blk.prev_id, already_generated_coins + block_reward, block_size); } @@ -108,7 +108,7 @@ bool test_generator::construct_block(cryptonote::block& blk, uint64_t height, co size_t target_block_size = txs_size + get_object_blobsize(blk.miner_tx); while (true) { - if (!construct_miner_tx(height, already_generated_coins, miner_acc.get_keys().m_account_address, blk.miner_tx, total_fee, block_sizes, target_block_size, 10)) + if (!construct_miner_tx(height, misc_utils::median(block_sizes), already_generated_coins, target_block_size, total_fee, miner_acc.get_keys().m_account_address, blk.miner_tx, blobdata(), 10)) return false; size_t actual_block_size = txs_size + get_object_blobsize(blk.miner_tx); @@ -209,7 +209,7 @@ bool test_generator::construct_block_manually(block& blk, const block& prev_bloc { size_t current_block_size = txs_sizes + get_object_blobsize(blk.miner_tx); // TODO: This will work, until size of constructed block is less then CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - if (!construct_miner_tx(height, already_generated_coins, miner_acc.get_keys().m_account_address, blk.miner_tx, 0, block_sizes, current_block_size, 1)) + if (!construct_miner_tx(height, misc_utils::median(block_sizes), already_generated_coins, current_block_size, 0, miner_acc.get_keys().m_account_address, blk.miner_tx, blobdata(), 1)) return false; } @@ -496,14 +496,13 @@ bool construct_miner_tx_manually(size_t height, uint64_t already_generated_coins tx.vin.push_back(in); // This will work, until size of constructed block is less then CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - std::vector<size_t> block_sizes; - bool block_too_big = false; - uint64_t block_reward = get_block_reward(block_sizes, 0, block_too_big, already_generated_coins) + fee; - if (block_too_big) + uint64_t block_reward; + if (!get_block_reward(0, 0, already_generated_coins, block_reward)) { LOG_PRINT_L0("Block is too big"); return false; } + block_reward += fee; crypto::key_derivation derivation; crypto::public_key out_eph_public_key; diff --git a/tests/core_tests/transaction_tests.cpp b/tests/core_tests/transaction_tests.cpp index 4da9446cd..07e6661d8 100644 --- a/tests/core_tests/transaction_tests.cpp +++ b/tests/core_tests/transaction_tests.cpp @@ -7,6 +7,7 @@ #include "cryptonote_core/cryptonote_basic_impl.h" #include "cryptonote_core/account.h" #include "cryptonote_core/cryptonote_format_utils.h" +#include "misc_language.h" using namespace cryptonote; @@ -35,19 +36,18 @@ bool test_transaction_generation_and_ring_signature() rv_acc.generate(); account_base rv_acc2; rv_acc2.generate(); - std::vector<size_t> b; transaction tx_mine_1; - construct_miner_tx(0, 0, miner_acc1.get_keys().m_account_address, tx_mine_1, 0, b, 10); + construct_miner_tx(0, 0, 0, 10, 0, miner_acc1.get_keys().m_account_address, tx_mine_1); transaction tx_mine_2; - construct_miner_tx(0, 0, miner_acc2.get_keys().m_account_address, tx_mine_2, 0, b, 0); + construct_miner_tx(0, 0, 0, 0, 0, miner_acc2.get_keys().m_account_address, tx_mine_2); transaction tx_mine_3; - construct_miner_tx(0, 0, miner_acc3.get_keys().m_account_address, tx_mine_3, 0, b, 0); + construct_miner_tx(0, 0, 0, 0, 0, miner_acc3.get_keys().m_account_address, tx_mine_3); transaction tx_mine_4; - construct_miner_tx(0, 0, miner_acc4.get_keys().m_account_address, tx_mine_4, 0, b, 0); + construct_miner_tx(0, 0, 0, 0, 0, miner_acc4.get_keys().m_account_address, tx_mine_4); transaction tx_mine_5; - construct_miner_tx(0, 0, miner_acc5.get_keys().m_account_address, tx_mine_5, 0, b, 0); + construct_miner_tx(0, 0, 0, 0, 0, miner_acc5.get_keys().m_account_address, tx_mine_5); transaction tx_mine_6; - construct_miner_tx(0, 0, miner_acc6.get_keys().m_account_address, tx_mine_6, 0, b, 0); + construct_miner_tx(0, 0, 0, 0, 0, miner_acc6.get_keys().m_account_address, tx_mine_6); //fill inputs entry typedef tx_source_entry::output_entry tx_output_entry; @@ -111,7 +111,7 @@ bool test_transaction_generation_and_ring_signature() std::vector<size_t> outs; uint64_t money = 0; - + r = lookup_acc_outs(rv_acc.get_keys(), tx_rc1, get_tx_pub_key_from_extra(tx_rc1), outs, money); CHECK_AND_ASSERT_MES(r, false, "failed to lookup_acc_outs"); CHECK_AND_ASSERT_MES(td.amount == money, false, "wrong money amount in new transaction"); @@ -130,7 +130,7 @@ bool test_block_creation() bool r = get_account_address_from_str(adr, "0099be99c70ef10fd534c43c88e9d13d1c8853213df7e362afbec0e4ee6fec4948d0c190b58f4b356cd7feaf8d9d0a76e7c7e5a9a0a497a6b1faf7a765882dd08ac2"); CHECK_AND_ASSERT_MES(r, false, "failed to import"); block b; - r = construct_miner_tx(90, 3553616528562147, adr, b.miner_tx, 10000000, szs, 33094, 11); + r = construct_miner_tx(90, epee::misc_utils::median(szs), 3553616528562147, 33094, 10000000, adr, b.miner_tx, blobdata(), 11); return r; } diff --git a/tests/difficulty/difficulty.cpp b/tests/difficulty/difficulty.cpp index 85c469391..2af2f7976 100644 --- a/tests/difficulty/difficulty.cpp +++ b/tests/difficulty/difficulty.cpp @@ -7,6 +7,7 @@ #include <fstream> #include <iostream> #include <vector> +#include <algorithm> #include "cryptonote_config.h" #include "cryptonote_core/difficulty.h" diff --git a/tests/functional_tests/transactions_flow_test.cpp b/tests/functional_tests/transactions_flow_test.cpp index 3683c2160..ce65c8f20 100644 --- a/tests/functional_tests/transactions_flow_test.cpp +++ b/tests/functional_tests/transactions_flow_test.cpp @@ -50,7 +50,15 @@ bool do_send_money(tools::wallet2& w1, tools::wallet2& w2, size_t mix_in_factor, dsts.push_back(de); } - return w1.transfer(dsts, mix_in_factor, 0, DEFAULT_FEE, tools::detail::null_split_strategy, tools::wallet2::tx_dust_policy(DEFAULT_FEE), tx); + try + { + w1.transfer(dsts, mix_in_factor, 0, DEFAULT_FEE, tools::detail::null_split_strategy, tools::tx_dust_policy(DEFAULT_FEE), tx); + return true; + } + catch (const std::exception&) + { + return false; + } } uint64_t get_money_in_first_transfers(const tools::wallet2::transfer_container& incoming_transfers, size_t n_transfers) @@ -84,36 +92,30 @@ bool transactions_flow_test(std::string& working_folder, path_terget_wallet = generate_random_wallet_name(); - if(!w1.generate(working_folder + "/" + path_source_wallet, "")) + try { - LOG_ERROR("failed to load source wallet from " << path_source_wallet); - return false; + w1.generate(working_folder + "/" + path_source_wallet, ""); + w2.generate(working_folder + "/" + path_terget_wallet, ""); } - - if(!w2.generate(working_folder + "/" + path_terget_wallet, "")) + catch (const std::exception& e) { - LOG_ERROR( "failed to generate target load wallet from " << path_source_wallet ); + LOG_ERROR("failed to generate wallet: " << e.what()); return false; } - if(!w1.init(daemon_addr_a)) - { - LOG_ERROR("failed to init source wallet from " << daemon_addr_a ); - return false; - } + w1.init(daemon_addr_a); + size_t blocks_fetched = 0; - tools::wallet2::fail_details fd; - if(!w1.refresh(blocks_fetched, fd)) + bool received_money; + bool ok; + if(!w1.refresh(blocks_fetched, received_money, ok)) { LOG_ERROR( "failed to refresh source wallet from " << daemon_addr_a ); return false; } - if(!w2.init(daemon_addr_b)) - { - LOG_ERROR( "failed to init target wallet from " << daemon_addr_b ); - return false; - } + w2.init(daemon_addr_b); + LOG_PRINT_GREEN("Using wallets: " << ENDL << "Source: " << w1.get_account().get_public_address_str() << ENDL << "Path: " << working_folder + "/" + path_source_wallet << ENDL << "Target: " << w2.get_account().get_public_address_str() << ENDL << "Path: " << working_folder + "/" + path_terget_wallet, LOG_LEVEL_1); @@ -125,7 +127,6 @@ bool transactions_flow_test(std::string& working_folder, bool r = net_utils::invoke_http_json_remote_command2(daemon_addr_a + "/stop_mine", daemon1_req, daemon1_rsp, http_client, 10000); CHECK_AND_ASSERT_MES(r, false, "failed to stop mining"); - COMMAND_RPC_START_MINING::request daemon_req = AUTO_VAL_INIT(daemon_req); COMMAND_RPC_START_MINING::response daemon_rsp = AUTO_VAL_INIT(daemon_rsp); daemon_req.miner_address = w1.get_account().get_public_address_str(); @@ -135,11 +136,11 @@ bool transactions_flow_test(std::string& working_folder, CHECK_AND_ASSERT_MES(daemon_rsp.status == CORE_RPC_STATUS_OK, false, "failed to getrandom_outs.bin"); //wait for money, until balance will have enough money - w1.refresh(blocks_fetched, fd); + w1.refresh(blocks_fetched, received_money, ok); while(w1.unlocked_balance() < amount_to_transfer) { misc_utils::sleep_no_w(1000); - w1.refresh(blocks_fetched, fd); + w1.refresh(blocks_fetched, received_money, ok); } //lets make a lot of small outs to ourselves @@ -166,7 +167,7 @@ bool transactions_flow_test(std::string& working_folder, }else { misc_utils::sleep_no_w(1000); - w1.refresh(fd); + w1.refresh(blocks_fetched, received_money, ok); } } //do actual transfer @@ -188,7 +189,7 @@ bool transactions_flow_test(std::string& working_folder, { misc_utils::sleep_no_w(1000); LOG_PRINT_L0("not enough money, waiting for cashback or mining"); - w1.refresh(blocks_fetched, fd); + w1.refresh(blocks_fetched, received_money, ok); } transaction tx; @@ -203,7 +204,7 @@ bool transactions_flow_test(std::string& working_folder, if(!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx)) { LOG_PRINT_L0("failed to transfer money, tx: " << get_transaction_hash(tx) << ", refresh and try again" ); - w1.refresh(blocks_fetched, fd); + w1.refresh(blocks_fetched, received_money, ok); if(!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx)) { LOG_PRINT_L0( "failed to transfer money, second chance. tx: " << get_transaction_hash(tx) << ", exit" ); @@ -228,7 +229,7 @@ bool transactions_flow_test(std::string& working_folder, misc_utils::sleep_no_w(DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN*20*1000);//wait two blocks before sync on another wallet on another daemon LOG_PRINT_L0( "refreshing..."); bool recvd_money = false; - while(w2.refresh(blocks_fetched, recvd_money, fd) && ( (blocks_fetched && recvd_money) || !blocks_fetched ) ) + while(w2.refresh(blocks_fetched, recvd_money, ok) && ( (blocks_fetched && recvd_money) || !blocks_fetched ) ) { misc_utils::sleep_no_w(DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN*1000);//wait two blocks before sync on another wallet on another daemon } diff --git a/tests/net_load_tests/clt.cpp b/tests/net_load_tests/clt.cpp index 3bb29a7db..9c153a299 100644 --- a/tests/net_load_tests/clt.cpp +++ b/tests/net_load_tests/clt.cpp @@ -35,7 +35,7 @@ namespace if (predicate()) return true; //std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms)); - epee::misc_utils::sleep_no_w(sleep_ms); + epee::misc_utils::sleep_no_w(static_cast<long>(sleep_ms)); } return false; } diff --git a/tests/performance_tests/multi_tx_test_base.h b/tests/performance_tests/multi_tx_test_base.h index a65062362..c919bdf2d 100644 --- a/tests/performance_tests/multi_tx_test_base.h +++ b/tests/performance_tests/multi_tx_test_base.h @@ -29,8 +29,7 @@ public: { m_miners[i].generate(); - std::vector<size_t> block_sizes; - if (!construct_miner_tx(0, 0, m_miners[i].get_keys().m_account_address, m_miner_txs[i], 0, block_sizes, 2)) + if (!construct_miner_tx(0, 0, 0, 2, 0, m_miners[i].get_keys().m_account_address, m_miner_txs[i])) return false; txout_to_key tx_out = boost::get<txout_to_key>(m_miner_txs[i].vout[0].target); diff --git a/tests/performance_tests/single_tx_test_base.h b/tests/performance_tests/single_tx_test_base.h index a622fce89..1c1fe655b 100644 --- a/tests/performance_tests/single_tx_test_base.h +++ b/tests/performance_tests/single_tx_test_base.h @@ -17,8 +17,7 @@ public: m_bob.generate(); - std::vector<size_t> block_sizes; - if (!construct_miner_tx(0, 0, m_bob.get_keys().m_account_address, m_tx, 0, block_sizes, 2)) + if (!construct_miner_tx(0, 0, 0, 2, 0, m_bob.get_keys().m_account_address, m_tx)) return false; m_tx_pub_key = get_tx_pub_key_from_extra(m_tx); diff --git a/tests/unit_tests/block_reward.cpp b/tests/unit_tests/block_reward.cpp index cfa12a7c8..2381d3686 100644 --- a/tests/unit_tests/block_reward.cpp +++ b/tests/unit_tests/block_reward.cpp @@ -16,14 +16,13 @@ namespace protected: static const size_t current_block_size = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE / 2; - std::vector<size_t> m_last_block_sizes; - bool m_block_too_big; + bool m_block_not_too_big; uint64_t m_block_reward; }; - #define TEST_ALREADY_GENERATED_COINS(already_generated_coins, expected_reward) \ - m_block_reward = get_block_reward(m_last_block_sizes, current_block_size, m_block_too_big, already_generated_coins); \ - ASSERT_FALSE(m_block_too_big); \ + #define TEST_ALREADY_GENERATED_COINS(already_generated_coins, expected_reward) \ + m_block_not_too_big = get_block_reward(0, current_block_size, already_generated_coins, m_block_reward); \ + ASSERT_TRUE(m_block_not_too_big); \ ASSERT_EQ(m_block_reward, UINT64_C(expected_reward)); TEST_F(block_reward_and_already_generated_coins, handles_first_values) @@ -53,72 +52,70 @@ namespace protected: virtual void SetUp() { - m_standard_block_reward = get_block_reward(m_last_block_sizes, 0, m_block_too_big, already_generated_coins); - ASSERT_FALSE(m_block_too_big); + m_block_not_too_big = get_block_reward(0, 0, already_generated_coins, m_standard_block_reward); + ASSERT_TRUE(m_block_not_too_big); ASSERT_LT(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE, m_standard_block_reward); } - void do_test(size_t current_block_size) + void do_test(size_t median_block_size, size_t current_block_size) { - m_block_reward = get_block_reward(m_last_block_sizes, current_block_size, m_block_too_big, already_generated_coins); + m_block_not_too_big = get_block_reward(median_block_size, current_block_size, already_generated_coins, m_block_reward); } static const uint64_t already_generated_coins = 0; - std::vector<size_t> m_last_block_sizes; - bool m_block_too_big; + bool m_block_not_too_big; uint64_t m_block_reward; uint64_t m_standard_block_reward; }; TEST_F(block_reward_and_current_block_size, handles_block_size_less_relevance_level) { - do_test(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1); - ASSERT_FALSE(m_block_too_big); + do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(m_block_reward, m_standard_block_reward); } TEST_F(block_reward_and_current_block_size, handles_block_size_eq_relevance_level) { - do_test(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE); - ASSERT_FALSE(m_block_too_big); + do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(m_block_reward, m_standard_block_reward); } TEST_F(block_reward_and_current_block_size, handles_block_size_gt_relevance_level) { - do_test(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1); - ASSERT_FALSE(m_block_too_big); + do_test(0, CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1); + ASSERT_TRUE(m_block_not_too_big); ASSERT_LT(m_block_reward, m_standard_block_reward); } TEST_F(block_reward_and_current_block_size, handles_block_size_less_2_relevance_level) { - do_test(2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1); - ASSERT_FALSE(m_block_too_big); + do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE - 1); + ASSERT_TRUE(m_block_not_too_big); ASSERT_LT(m_block_reward, m_standard_block_reward); ASSERT_LT(0, m_block_reward); } TEST_F(block_reward_and_current_block_size, handles_block_size_eq_2_relevance_level) { - do_test(2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE); - ASSERT_FALSE(m_block_too_big); + do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(0, m_block_reward); } TEST_F(block_reward_and_current_block_size, handles_block_size_gt_2_relevance_level) { - do_test(2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1); - ASSERT_TRUE(m_block_too_big); + do_test(0, 2 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE + 1); + ASSERT_FALSE(m_block_not_too_big); } TEST_F(block_reward_and_current_block_size, fails_on_huge_median_size) { #if !defined(NDEBUG) size_t huge_size = std::numeric_limits<uint32_t>::max() + UINT64_C(2); - m_last_block_sizes.push_back(huge_size); - ASSERT_DEATH(do_test(huge_size + 1), ""); + ASSERT_DEATH(do_test(huge_size, huge_size + 1), ""); #endif } @@ -126,8 +123,7 @@ namespace { #if !defined(NDEBUG) size_t huge_size = std::numeric_limits<uint32_t>::max() + UINT64_C(2); - m_last_block_sizes.push_back(huge_size - 2); - ASSERT_DEATH(do_test(huge_size), ""); + ASSERT_DEATH(do_test(huge_size - 2, huge_size), ""); #endif } @@ -145,21 +141,21 @@ namespace m_last_block_sizes_median = 7 * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE; - m_standard_block_reward = get_block_reward(m_last_block_sizes, 0, m_block_too_big, already_generated_coins); - ASSERT_FALSE(m_block_too_big); + m_block_not_too_big = get_block_reward(epee::misc_utils::median(m_last_block_sizes), 0, already_generated_coins, m_standard_block_reward); + ASSERT_TRUE(m_block_not_too_big); ASSERT_LT(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE, m_standard_block_reward); } void do_test(size_t current_block_size) { - m_block_reward = get_block_reward(m_last_block_sizes, current_block_size, m_block_too_big, already_generated_coins); + m_block_not_too_big = get_block_reward(epee::misc_utils::median(m_last_block_sizes), current_block_size, already_generated_coins, m_block_reward); } static const uint64_t already_generated_coins = 0; std::vector<size_t> m_last_block_sizes; uint64_t m_last_block_sizes_median; - bool m_block_too_big; + bool m_block_not_too_big; uint64_t m_block_reward; uint64_t m_standard_block_reward; }; @@ -167,28 +163,28 @@ namespace TEST_F(block_reward_and_last_block_sizes, handles_block_size_less_median) { do_test(m_last_block_sizes_median - 1); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(m_block_reward, m_standard_block_reward); } TEST_F(block_reward_and_last_block_sizes, handles_block_size_eq_median) { do_test(m_last_block_sizes_median); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(m_block_reward, m_standard_block_reward); } TEST_F(block_reward_and_last_block_sizes, handles_block_size_gt_median) { do_test(m_last_block_sizes_median + 1); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_LT(m_block_reward, m_standard_block_reward); } TEST_F(block_reward_and_last_block_sizes, handles_block_size_less_2_medians) { do_test(2 * m_last_block_sizes_median - 1); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_LT(m_block_reward, m_standard_block_reward); ASSERT_LT(0, m_block_reward); } @@ -196,14 +192,14 @@ namespace TEST_F(block_reward_and_last_block_sizes, handles_block_size_eq_2_medians) { do_test(2 * m_last_block_sizes_median); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(0, m_block_reward); } TEST_F(block_reward_and_last_block_sizes, handles_block_size_gt_2_medians) { do_test(2 * m_last_block_sizes_median + 1); - ASSERT_TRUE(m_block_too_big); + ASSERT_FALSE(m_block_not_too_big); } TEST_F(block_reward_and_last_block_sizes, calculates_correctly) @@ -211,16 +207,16 @@ namespace ASSERT_EQ(0, m_last_block_sizes_median % 8); do_test(m_last_block_sizes_median * 9 / 8); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(m_block_reward, m_standard_block_reward * 63 / 64); // 3/2 = 12/8 do_test(m_last_block_sizes_median * 3 / 2); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(m_block_reward, m_standard_block_reward * 3 / 4); do_test(m_last_block_sizes_median * 15 / 8); - ASSERT_FALSE(m_block_too_big); + ASSERT_TRUE(m_block_not_too_big); ASSERT_EQ(m_block_reward, m_standard_block_reward * 15 / 64); } } diff --git a/tests/unit_tests/get_xtype_from_string.cpp b/tests/unit_tests/get_xtype_from_string.cpp index 0ba80fb3d..dd4d2e419 100644 --- a/tests/unit_tests/get_xtype_from_string.cpp +++ b/tests/unit_tests/get_xtype_from_string.cpp @@ -45,6 +45,7 @@ TEST_pos(uint16_t, 0, "0"); TEST_pos(uint16_t, 1, "1"); TEST_pos(uint16_t, 65535, "65535"); +TEST_neg(uint16_t, ""); TEST_neg(uint16_t, "+0"); TEST_neg(uint16_t, "+1"); TEST_neg(uint16_t, "+65535"); @@ -77,6 +78,7 @@ TEST_pos(uint32_t, 0, "0"); TEST_pos(uint32_t, 1, "1"); TEST_pos(uint32_t, 4294967295, "4294967295"); +TEST_neg(uint32_t, ""); TEST_neg(uint32_t, "+0"); TEST_neg(uint32_t, "+1"); TEST_neg(uint32_t, "+4294967295"); @@ -107,6 +109,7 @@ TEST_pos(uint64_t, 0, "0"); TEST_pos(uint64_t, 1, "1"); TEST_pos(uint64_t, 18446744073709551615ULL, "18446744073709551615"); +TEST_neg(uint64_t, ""); TEST_neg(uint64_t, "+0"); TEST_neg(uint64_t, "+1"); TEST_neg(uint64_t, "+18446744073709551615"); diff --git a/tests/unit_tests/parse_amount.cpp b/tests/unit_tests/parse_amount.cpp new file mode 100644 index 000000000..7d0b64088 --- /dev/null +++ b/tests/unit_tests/parse_amount.cpp @@ -0,0 +1,124 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "gtest/gtest.h" + +#include "cryptonote_core/cryptonote_format_utils.h" + +using namespace cryptonote; + +namespace +{ + void do_pos_test(uint64_t expected, const std::string& str) + { + uint64_t val; + std::string number_str = str; + std::replace(number_str.begin(), number_str.end(), '_', '.'); + number_str.erase(std::remove(number_str.begin(), number_str.end(), '~'), number_str.end()); + ASSERT_TRUE(parse_amount(val, number_str)); + ASSERT_EQ(expected, val); + } + + void do_neg_test(const std::string& str) + { + uint64_t val; + std::string number_str = str; + std::replace(number_str.begin(), number_str.end(), '_', '.'); + number_str.erase(std::remove(number_str.begin(), number_str.end(), '~'), number_str.end()); + ASSERT_FALSE(parse_amount(val, number_str)); + } +} + +#define TEST_pos(expected, str) \ + TEST(parse_amount, handles_pos_ ## str) \ + { \ + do_pos_test(UINT64_C(expected), #str); \ + } + +#define TEST_neg(str) \ + TEST(parse_amount, handles_neg_ ## str) \ + { \ + do_neg_test(#str); \ + } + +#define TEST_neg_n(str, name) \ + TEST(parse_amount, handles_neg_ ## name) \ + { \ + do_neg_test(#str); \ + } + + +TEST_pos(0, 0); +TEST_pos(0, 00); +TEST_pos(0, 00000000); +TEST_pos(0, 000000000); +TEST_pos(0, 00000000000000000000000000000000); + +TEST_pos(0, _0); +TEST_pos(0, _00); +TEST_pos(0, _00000000); +TEST_pos(0, _000000000); +TEST_pos(0, _00000000000000000000000000000000); + +TEST_pos(0, 00000000_); +TEST_pos(0, 000000000_); +TEST_pos(0, 00000000000000000000000000000000_); + +TEST_pos(0, 0_); +TEST_pos(0, 0_0); +TEST_pos(0, 0_00); +TEST_pos(0, 0_00000000); +TEST_pos(0, 0_000000000); +TEST_pos(0, 0_00000000000000000000000000000000); + +TEST_pos(0, 00_); +TEST_pos(0, 00_0); +TEST_pos(0, 00_00); +TEST_pos(0, 00_00000000); +TEST_pos(0, 00_000000000); +TEST_pos(0, 00_00000000000000000000000000000000); + +TEST_pos(1, 0_00000001); +TEST_pos(1, 0_000000010); +TEST_pos(1, 0_000000010000000000000000000000000); +TEST_pos(9, 0_00000009); +TEST_pos(9, 0_000000090); +TEST_pos(9, 0_000000090000000000000000000000000); + +TEST_pos( 100000000, 1); +TEST_pos( 6553500000000, 65535); +TEST_pos( 429496729500000000, 4294967295); +TEST_pos(18446744073700000000, 184467440737_); +TEST_pos(18446744073700000000, 184467440737_0); +TEST_pos(18446744073700000000, 184467440737_00000000); +TEST_pos(18446744073700000000, 184467440737_000000000); +TEST_pos(18446744073700000000, 184467440737_0000000000000000000); +TEST_pos(18446744073709551615, 184467440737_09551615); + +// Invalid numbers +TEST_neg_n(~, empty_string); +TEST_neg_n(-0, minus_0); +TEST_neg_n(+0, plus_0); +TEST_neg_n(-1, minus_1); +TEST_neg_n(+1, plus_1); +TEST_neg_n(_, only_point); + +// A lot of fraction digits +TEST_neg(0_000000001); +TEST_neg(0_000000009); +TEST_neg(184467440737_000000001); + +// Overflow +TEST_neg(184467440737_09551616); +TEST_neg(184467440738); +TEST_neg(18446744073709551616); + +// Two or more points +TEST_neg(__); +TEST_neg(0__); +TEST_neg(__0); +TEST_neg(0__0); +TEST_neg(0_0_); +TEST_neg(_0_0); +TEST_neg(0_0_0); diff --git a/tests/unit_tests/test_format_utils.cpp b/tests/unit_tests/test_format_utils.cpp index 22f56628e..dfc05f55a 100644 --- a/tests/unit_tests/test_format_utils.cpp +++ b/tests/unit_tests/test_format_utils.cpp @@ -7,30 +7,28 @@ #include "common/util.h" #include "cryptonote_core/cryptonote_format_utils.h" -TEST(parse_and_validate_tx_extra, is_correct_parse_and_validate_tx_extra) +TEST(parse_and_validate_tx_extra, is_correct_parse_and_validate_tx_extra) { cryptonote::transaction tx = AUTO_VAL_INIT(tx); cryptonote::account_base acc; acc.generate(); - std::vector<size_t> bss; cryptonote::blobdata b = "dsdsdfsdfsf"; - bool r = cryptonote::construct_miner_tx(0, 10000000000000, acc.get_keys().m_account_address, tx, DEFAULT_FEE, bss, 1000, b, 1); + bool r = cryptonote::construct_miner_tx(0, 0, 10000000000000, 1000, DEFAULT_FEE, acc.get_keys().m_account_address, tx, b, 1); ASSERT_TRUE(r); crypto::public_key tx_pub_key; r = cryptonote::parse_and_validate_tx_extra(tx, tx_pub_key); ASSERT_TRUE(r); } -TEST(parse_and_validate_tx_extra, is_correct_extranonce_too_big) +TEST(parse_and_validate_tx_extra, is_correct_extranonce_too_big) { cryptonote::transaction tx = AUTO_VAL_INIT(tx); cryptonote::account_base acc; acc.generate(); - std::vector<size_t> bss; cryptonote::blobdata b(260, 0); - bool r = cryptonote::construct_miner_tx(0, 10000000000000, acc.get_keys().m_account_address, tx, DEFAULT_FEE, bss, 1000, b, 1); + bool r = cryptonote::construct_miner_tx(0, 0, 10000000000000, 1000, DEFAULT_FEE, acc.get_keys().m_account_address, tx, b, 1); ASSERT_FALSE(r); } -TEST(parse_and_validate_tx_extra, is_correct_wrong_extra_couner_too_big) +TEST(parse_and_validate_tx_extra, is_correct_wrong_extra_couner_too_big) { cryptonote::transaction tx = AUTO_VAL_INIT(tx); tx.extra.resize(20, 0); @@ -75,7 +73,7 @@ TEST(validate_parse_amount_case, validate_parse_amount) r = cryptonote::parse_amount(res, " 100.0001 "); ASSERT_TRUE(r); ASSERT_EQ(res, 10000010000); - + r = cryptonote::parse_amount(res, " 100.0000 "); ASSERT_TRUE(r); ASSERT_EQ(res, 10000000000); @@ -88,10 +86,10 @@ TEST(validate_parse_amount_case, validate_parse_amount) r = cryptonote::parse_amount(res, "100 . 0000"); ASSERT_FALSE(r); - + r = cryptonote::parse_amount(res, "100.00 00"); ASSERT_FALSE(r); - + r = cryptonote::parse_amount(res, "1 00.00 00"); ASSERT_FALSE(r); } |