even more unit tests

This commit is contained in:
Crypto City 2019-08-01 01:22:52 +00:00
parent 9ba3a6e427
commit b8df9f7adb

View File

@ -370,7 +370,7 @@ TEST(cc_command, transfer_mixed)
ASSERT_EQ(cc_out, 1500);
}
static void test_commands(bool good, uint32_t cc_account, const cryptonote::cc_command_t &cmd)
static void test_commands(bool good, uint32_t cc_account, const std::vector<std::pair<uint32_t, cryptonote::cc_command_t>> &setup, const cryptonote::cc_command_t &cmd)
{
std::unique_ptr<cryptonote::Blockchain> bc;
cryptonote::tx_memory_pool txpool(*bc);
@ -389,10 +389,21 @@ static void test_commands(bool good, uint32_t cc_account, const cryptonote::cc_c
bool r = blockchain->init(db, cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL);
ASSERT_TRUE(r);
for (const auto &s: setup)
{
uint64_t balance, cost = cryptonote::get_cc_command_cost(s.second);
r = db->get_cc_account_balance(s.first, balance)
&& cost <= balance
&& cryptonote::check_cc_command(db, s.first, s.second);
ASSERT_TRUE(r);
r = cryptonote::execute_cc_command(db, s.first, s.second);
ASSERT_TRUE(r);
}
uint64_t balance, cost = cryptonote::get_cc_command_cost(cmd);
r = db->get_cc_account_balance(cc_account, balance)
&& cost <= balance
&& cryptonote::check_cc_command(db, cc_account, cmd);
&& cost <= balance
&& cryptonote::check_cc_command(db, cc_account, cmd);
ASSERT_EQ(r, good);
if (good)
@ -409,8 +420,7 @@ static void test_commands(bool good, uint32_t cc_account, const cryptonote::cc_c
TEST(cc_command, execute_none)
{
test_commands(false, 0, cryptonote::cc_command_none_t());
test_commands(true, 1, cryptonote::cc_command_none_t());
test_commands(true, 1, {}, cryptonote::cc_command_none_t());
}
TEST(cc_command, execute_create_account)
@ -422,20 +432,123 @@ TEST(cc_command, execute_create_account)
// good
create_account.public_key = keys.pub;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE;
test_commands(true, 1, create_account);
test_commands(true, 1, {}, create_account);
// bad key
create_account.public_key = crypto::null_pkey;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE;
test_commands(false, 1, create_account);
test_commands(false, 1, {}, create_account);
// low amount
create_account.public_key = keys.pub;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE - 1;
test_commands(false, 1, create_account);
test_commands(false, 1, {}, create_account);
// good, plus funding
create_account.public_key = keys.pub;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE + 42;
test_commands(true, 1, create_account);
test_commands(true, 1, {}, create_account);
}
TEST(cc_command, execute_transfer_deposit)
{
cryptonote::cc_command_transfer_t transfer;
std::vector<std::pair<uint32_t, cryptonote::cc_command_t>> setup;
cryptonote::keypair keys = cryptonote::keypair::generate(hw::get_device("default"));
// setup
cryptonote::cc_command_create_account_t create_account;
create_account.public_key = keys.pub;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE + 1000;
setup.push_back(std::make_pair(1, create_account));
// deposit
transfer.in_amount = 0;
transfer.public_key = keys.pub;
transfer.out_amount = 5000;
test_commands(true, 4, setup, transfer);
// overflow
transfer.in_amount = 0;
transfer.public_key = keys.pub;
transfer.out_amount = std::numeric_limits<uint64_t>::max();
test_commands(false, 4, setup, transfer);
}
TEST(cc_command, execute_transfer_withdrawal)
{
cryptonote::cc_command_transfer_t transfer;
std::vector<std::pair<uint32_t, cryptonote::cc_command_t>> setup;
cryptonote::keypair keys = cryptonote::keypair::generate(hw::get_device("default"));
// setup
cryptonote::cc_command_create_account_t create_account;
create_account.public_key = keys.pub;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE + 1000;
setup.push_back(std::make_pair(1, create_account));
// withdraw
transfer.in_amount = 1000;
transfer.public_key = crypto::null_pkey;
transfer.out_amount = 0;
test_commands(true, 4, setup, transfer);
// more than balance
transfer.in_amount = 1001;
transfer.public_key = crypto::null_pkey;
transfer.out_amount = 0;
test_commands(false, 4, setup, transfer);
// more than balance
transfer.in_amount = std::numeric_limits<uint64_t>::max();
transfer.public_key = crypto::null_pkey;
transfer.out_amount = 0;
test_commands(false, 4, setup, transfer);
}
TEST(cc_command, execute_transfer_mixed)
{
cryptonote::cc_command_transfer_t transfer;
std::vector<std::pair<uint32_t, cryptonote::cc_command_t>> setup;
cryptonote::keypair keys1 = cryptonote::keypair::generate(hw::get_device("default"));
cryptonote::keypair keys2 = cryptonote::keypair::generate(hw::get_device("default"));
// setup
cryptonote::cc_command_create_account_t create_account;
create_account.public_key = keys1.pub;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE + 1000;
setup.push_back(std::make_pair(1, create_account));
create_account.public_key = keys2.pub;
create_account.amount = CRYPTONOTE_CC_NEW_ACCOUNT_FEE + 2000;
setup.push_back(std::make_pair(1, create_account));
// transfer to another
transfer.in_amount = 500;
transfer.public_key = keys2.pub;
transfer.out_amount = 500;
test_commands(true, 4, setup, transfer);
// more than balance
transfer.in_amount = 1001;
transfer.public_key = keys2.pub;
transfer.out_amount = 1001;
test_commands(false, 4, setup, transfer);
// those checks are made at the tx layer, since they're OK for non bare txes
#if 0
// receive more than sent
transfer.in_amount = 500;
transfer.public_key = keys2.pub;
transfer.out_amount = 501;
test_commands(false, 4, setup, transfer);
// receive less than sent
transfer.in_amount = 500;
transfer.public_key = keys2.pub;
transfer.out_amount = 499;
test_commands(false, 4, setup, transfer);
#endif
}