peerplays_migrated/tests/tests/serialization_tests.cpp
Michael Vandeberg 8df58439b1 Opens up cryptographic methods in their own API with serialized wrappers for public and private keys.
Tests for serialization of the wrappers added in serialization_tests.
2016-01-07 17:17:27 -05:00

122 lines
4.4 KiB
C++

/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Any modified source or binaries are used only with the BitShares network.
*
* 2. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <boost/test/unit_test.hpp>
#include <graphene/chain/database.hpp>
#include <fc/crypto/digest.hpp>
#include <fc/crypto/elliptic.hpp>
#include <fc/reflect/variant.hpp>
#include "../common/database_fixture.hpp"
using namespace graphene::chain;
BOOST_FIXTURE_TEST_SUITE( operation_unit_tests, database_fixture )
BOOST_AUTO_TEST_CASE( serialization_raw_test )
{
try {
make_account();
transfer_operation op;
op.from = account_id_type(1);
op.to = account_id_type(2);
op.amount = asset(100);
trx.operations.push_back( op );
auto packed = fc::raw::pack( trx );
signed_transaction unpacked = fc::raw::unpack<signed_transaction>(packed);
unpacked.validate();
BOOST_CHECK( digest(trx) == digest(unpacked) );
} catch (fc::exception& e) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_CASE( serialization_json_test )
{
try {
make_account();
transfer_operation op;
op.from = account_id_type(1);
op.to = account_id_type(2);
op.amount = asset(100);
trx.operations.push_back( op );
fc::variant packed(trx);
signed_transaction unpacked = packed.as<signed_transaction>();
unpacked.validate();
BOOST_CHECK( digest(trx) == digest(unpacked) );
} catch (fc::exception& e) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_CASE( json_tests )
{
try {
auto var = fc::json::variants_from_string( "10.6 " );
var = fc::json::variants_from_string( "10.5" );
} catch ( const fc::exception& e )
{
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_CASE( extended_private_key_type_test )
{
try
{
fc::ecc::extended_private_key key = fc::ecc::extended_private_key( fc::ecc::private_key::generate(),
fc::sha256(),
0, 0, 0 );
extended_private_key_type type = extended_private_key_type( key );
std::string packed = std::string( type );
extended_private_key_type unpacked = extended_private_key_type( packed );
BOOST_CHECK( type == unpacked );
} catch ( const fc::exception& e )
{
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_CASE( extended_public_key_type_test )
{
try
{
fc::ecc::extended_public_key key = fc::ecc::extended_public_key( fc::ecc::private_key::generate().get_public_key(),
fc::sha256(),
0, 0, 0 );
extended_public_key_type type = extended_public_key_type( key );
std::string packed = std::string( type );
extended_public_key_type unpacked = extended_public_key_type( packed );
BOOST_CHECK( type == unpacked );
} catch ( const fc::exception& e )
{
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_SUITE_END()