danicoin/tests/unit_tests/test_format_utils.cpp

200 lines
7.6 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2014-08-13 10:38:35 +00:00
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
2014-03-03 22:07:58 +00:00
#include "gtest/gtest.h"
2014-04-29 16:26:45 +00:00
#include <vector>
2015-05-27 12:08:46 +00:00
#include "Common/util.h"
2014-08-13 10:51:37 +00:00
#include "cryptonote_core/account.h"
2014-03-03 22:07:58 +00:00
#include "cryptonote_core/cryptonote_format_utils.h"
2014-08-13 10:51:37 +00:00
#include "cryptonote_core/Currency.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
#include <Logging/LoggerGroup.h>
2014-04-29 16:26:45 +00:00
2015-07-15 12:23:00 +00:00
#define AUTO_VAL_INIT(n) boost::value_initialized<decltype(n)>()
2014-04-29 16:26:45 +00:00
TEST(parse_tx_extra, handles_empty_extra)
{
std::vector<uint8_t> extra;;
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
ASSERT_TRUE(tx_extra_fields.empty());
}
TEST(parse_tx_extra, handles_padding_only_size_1)
{
const uint8_t extra_arr[] = {0};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
ASSERT_EQ(1, tx_extra_fields.size());
2015-05-27 12:08:46 +00:00
ASSERT_EQ(typeid(CryptoNote::tx_extra_padding), tx_extra_fields[0].type());
ASSERT_EQ(1, boost::get<CryptoNote::tx_extra_padding>(tx_extra_fields[0]).size);
2014-04-29 16:26:45 +00:00
}
TEST(parse_tx_extra, handles_padding_only_size_2)
{
const uint8_t extra_arr[] = {0, 0};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
ASSERT_EQ(1, tx_extra_fields.size());
2015-05-27 12:08:46 +00:00
ASSERT_EQ(typeid(CryptoNote::tx_extra_padding), tx_extra_fields[0].type());
ASSERT_EQ(2, boost::get<CryptoNote::tx_extra_padding>(tx_extra_fields[0]).size);
2014-04-29 16:26:45 +00:00
}
TEST(parse_tx_extra, handles_padding_only_max_size)
{
std::vector<uint8_t> extra(TX_EXTRA_NONCE_MAX_COUNT, 0);
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
ASSERT_EQ(1, tx_extra_fields.size());
2015-05-27 12:08:46 +00:00
ASSERT_EQ(typeid(CryptoNote::tx_extra_padding), tx_extra_fields[0].type());
ASSERT_EQ(TX_EXTRA_NONCE_MAX_COUNT, boost::get<CryptoNote::tx_extra_padding>(tx_extra_fields[0]).size);
2014-04-29 16:26:45 +00:00
}
TEST(parse_tx_extra, handles_padding_only_exceed_max_size)
{
std::vector<uint8_t> extra(TX_EXTRA_NONCE_MAX_COUNT + 1, 0);
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_FALSE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
}
TEST(parse_tx_extra, handles_invalid_padding_only)
{
std::vector<uint8_t> extra(2, 0);
extra[1] = 42;
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_FALSE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
}
TEST(parse_tx_extra, handles_pub_key_only)
{
const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
ASSERT_EQ(1, tx_extra_fields.size());
2015-05-27 12:08:46 +00:00
ASSERT_EQ(typeid(CryptoNote::tx_extra_pub_key), tx_extra_fields[0].type());
2014-04-29 16:26:45 +00:00
}
TEST(parse_tx_extra, handles_extra_nonce_only)
{
const uint8_t extra_arr[] = {2, 1, 42};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
ASSERT_EQ(1, tx_extra_fields.size());
2015-05-27 12:08:46 +00:00
ASSERT_EQ(typeid(CryptoNote::tx_extra_nonce), tx_extra_fields[0].type());
CryptoNote::tx_extra_nonce extra_nonce = boost::get<CryptoNote::tx_extra_nonce>(tx_extra_fields[0]);
2014-04-29 16:26:45 +00:00
ASSERT_EQ(1, extra_nonce.nonce.size());
ASSERT_EQ(42, extra_nonce.nonce[0]);
}
TEST(parse_tx_extra, handles_pub_key_and_padding)
{
const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(CryptoNote::parse_tx_extra(extra, tx_extra_fields));
2014-04-29 16:26:45 +00:00
ASSERT_EQ(2, tx_extra_fields.size());
2015-05-27 12:08:46 +00:00
ASSERT_EQ(typeid(CryptoNote::tx_extra_pub_key), tx_extra_fields[0].type());
ASSERT_EQ(typeid(CryptoNote::tx_extra_padding), tx_extra_fields[1].type());
2014-04-29 16:26:45 +00:00
}
TEST(parse_and_validate_tx_extra, is_valid_tx_extra_parsed)
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
Logging::LoggerGroup logger;
CryptoNote::Currency currency = CryptoNote::CurrencyBuilder(logger).currency();
CryptoNote::Transaction tx = AUTO_VAL_INIT(tx);
CryptoNote::account_base acc;
2014-03-03 22:07:58 +00:00
acc.generate();
2015-05-27 12:08:46 +00:00
CryptoNote::blobdata b = "dsdsdfsdfsf";
2014-08-13 10:51:37 +00:00
ASSERT_TRUE(currency.constructMinerTx(0, 0, 10000000000000, 1000, currency.minimumFee(), acc.get_keys().m_account_address, tx, b, 1));
2015-05-27 12:08:46 +00:00
crypto::public_key tx_pub_key = CryptoNote::get_tx_pub_key_from_extra(tx);
ASSERT_NE(tx_pub_key, CryptoNote::null_pkey);
2014-03-03 22:07:58 +00:00
}
2014-04-29 16:26:45 +00:00
TEST(parse_and_validate_tx_extra, fails_on_big_extra_nonce)
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
Logging::LoggerGroup logger;
CryptoNote::Currency currency = CryptoNote::CurrencyBuilder(logger).currency();
CryptoNote::Transaction tx = AUTO_VAL_INIT(tx);
CryptoNote::account_base acc;
2014-03-03 22:07:58 +00:00
acc.generate();
2015-05-27 12:08:46 +00:00
CryptoNote::blobdata b(TX_EXTRA_NONCE_MAX_COUNT + 1, 0);
2014-08-13 10:51:37 +00:00
ASSERT_FALSE(currency.constructMinerTx(0, 0, 10000000000000, 1000, currency.minimumFee(), acc.get_keys().m_account_address, tx, b, 1));
2014-03-03 22:07:58 +00:00
}
2014-04-29 16:26:45 +00:00
TEST(parse_and_validate_tx_extra, fails_on_wrong_size_in_extra_nonce)
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
CryptoNote::Transaction tx = AUTO_VAL_INIT(tx);
2014-03-03 22:07:58 +00:00
tx.extra.resize(20, 0);
tx.extra[0] = TX_EXTRA_NONCE;
tx.extra[1] = 255;
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_extra_field> tx_extra_fields;
ASSERT_FALSE(CryptoNote::parse_tx_extra(tx.extra, tx_extra_fields));
2014-03-03 22:07:58 +00:00
}
TEST(validate_parse_amount_case, validate_parse_amount)
{
2015-05-27 12:08:46 +00:00
Logging::LoggerGroup logger;
CryptoNote::Currency currency = CryptoNote::CurrencyBuilder(logger).numberOfDecimalPlaces(8).currency();
2014-03-03 22:07:58 +00:00
uint64_t res = 0;
2014-08-13 10:51:37 +00:00
bool r = currency.parseAmount("0.0001", res);
2014-03-03 22:07:58 +00:00
ASSERT_TRUE(r);
ASSERT_EQ(res, 10000);
2014-08-13 10:51:37 +00:00
r = currency.parseAmount("100.0001", res);
2014-03-03 22:07:58 +00:00
ASSERT_TRUE(r);
ASSERT_EQ(res, 10000010000);
2014-08-13 10:51:37 +00:00
r = currency.parseAmount("000.0000", res);
2014-03-03 22:07:58 +00:00
ASSERT_TRUE(r);
ASSERT_EQ(res, 0);
2014-08-13 10:51:37 +00:00
r = currency.parseAmount("0", res);
2014-03-03 22:07:58 +00:00
ASSERT_TRUE(r);
ASSERT_EQ(res, 0);
2014-08-13 10:51:37 +00:00
r = currency.parseAmount(" 100.0001 ", res);
2014-03-03 22:07:58 +00:00
ASSERT_TRUE(r);
ASSERT_EQ(res, 10000010000);
2014-04-02 16:00:17 +00:00
2014-08-13 10:51:37 +00:00
r = currency.parseAmount(" 100.0000 ", res);
2014-03-03 22:07:58 +00:00
ASSERT_TRUE(r);
ASSERT_EQ(res, 10000000000);
2014-08-13 10:51:37 +00:00
r = currency.parseAmount(" 100. 0000 ", res);
2014-03-03 22:07:58 +00:00
ASSERT_FALSE(r);
2014-08-13 10:51:37 +00:00
r = currency.parseAmount("100. 0000", res);
2014-03-03 22:07:58 +00:00
ASSERT_FALSE(r);
2014-08-13 10:51:37 +00:00
r = currency.parseAmount("100 . 0000", res);
2014-03-03 22:07:58 +00:00
ASSERT_FALSE(r);
2014-04-02 16:00:17 +00:00
2014-08-13 10:51:37 +00:00
r = currency.parseAmount("100.00 00", res);
2014-03-03 22:07:58 +00:00
ASSERT_FALSE(r);
2014-04-02 16:00:17 +00:00
2014-08-13 10:51:37 +00:00
r = currency.parseAmount("1 00.00 00", res);
2014-03-03 22:07:58 +00:00
ASSERT_FALSE(r);
}