Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10224 from EOSIO/keke_epe70
Browse files Browse the repository at this point in the history
Add test case for decompress zlib funciton of transaction, see EPE70
  • Loading branch information
softprofe authored Apr 5, 2021
2 parents 1a8daae + 70665a3 commit bac5789
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions unittests/chain_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/permission_object.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/transaction.hpp>
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>

#include "fork_test_utilities.hpp"
#include "test_cfd_transaction.hpp"

using namespace eosio;
using namespace eosio::chain;
Expand Down Expand Up @@ -74,4 +76,95 @@ BOOST_AUTO_TEST_CASE( replace_account_keys ) try {

} FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_CASE( decompressed_size_over_limit ) try {
tester chain;
const name usr = config::system_account_name;
const name active_permission = config::active_name;

// build a transaction, add cf data, sign
cf_action cfa;
eosio::chain::signed_transaction trx;
eosio::chain::action act({}, cfa);
trx.context_free_actions.push_back(act);
// this is a over limit size (4+4)*129*1024 = 1032*1024 > 1M
for(int i = 0; i < 129*1024; ++i){
trx.context_free_data.emplace_back(fc::raw::pack<uint32_t>(100));
trx.context_free_data.emplace_back(fc::raw::pack<uint32_t>(200));
}
// add a normal action along with cfa
dummy_action da = {DUMMY_ACTION_DEFAULT_A, DUMMY_ACTION_DEFAULT_B, DUMMY_ACTION_DEFAULT_C};
eosio::chain::action act1(
std::vector<eosio::chain::permission_level>{{"testapi"_n, eosio::chain::config::active_name}}, da);
trx.actions.push_back(act1);
chain.set_transaction_headers(trx);
auto sig = trx.sign(chain.get_private_key("testapi"_n, "active"), chain.control->get_chain_id());


// pack
packed_transaction_v0 ptv0(trx, packed_transaction_v0::compression_type::zlib);
// try unpack and throw
bytes packed_txn = ptv0.get_packed_transaction();
bytes pcfd = ptv0.get_packed_context_free_data();
vector<signature_type> sigs;
sigs.push_back(sig);
bool throwed = false;
bool throw_expect_string = false;
try {
// will call into decompress zlib
packed_transaction_v0 copy( packed_txn, sigs, pcfd, packed_transaction_v0::compression_type::zlib );
} catch(fc::exception& er){
throwed = true;
if ( er.to_detail_string().find("Exceeded maximum decompressed transaction size") != std::string::npos){
throw_expect_string = true;
}
}
BOOST_REQUIRE(throwed);
BOOST_REQUIRE(throw_expect_string);
} FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_CASE( decompressed_size_under_limit ) try {
tester chain;

// build a transaction, add cf data, sign
cf_action cfa;
eosio::chain::signed_transaction trx;
eosio::chain::action act({}, cfa);
trx.context_free_actions.push_back(act);
// this is a under limit size (4+4)*128*1024 = 1024*1024
for(int i = 0; i < 100*1024; ++i){
trx.context_free_data.emplace_back(fc::raw::pack<uint32_t>(100));
trx.context_free_data.emplace_back(fc::raw::pack<uint32_t>(200));
}
// add a normal action along with cfa
dummy_action da = {DUMMY_ACTION_DEFAULT_A, DUMMY_ACTION_DEFAULT_B, DUMMY_ACTION_DEFAULT_C};
eosio::chain::action act1(
std::vector<eosio::chain::permission_level>{{"testapi"_n, eosio::chain::config::active_name}}, da);
trx.actions.push_back(act1);
chain.set_transaction_headers(trx);
auto sig = trx.sign(chain.get_private_key("testapi"_n, "active"), chain.control->get_chain_id());

// pack
packed_transaction_v0 ptv0(trx, packed_transaction_v0::compression_type::zlib);
// try unpack and throw
bytes packed_txn = ptv0.get_packed_transaction();
bytes pcfd = ptv0.get_packed_context_free_data();
vector<signature_type> sigs;
sigs.push_back(sig);
bool throwed = false;
bool throw_expect_string = false;
try {
// will call into decompress zlib
packed_transaction_v0 copy( packed_txn, sigs, pcfd, packed_transaction_v0::compression_type::zlib );
} catch(fc::exception& er){
throwed = true;
if ( er.to_detail_string().find("Exceeded maximum decompressed transaction size") != std::string::npos){
throw_expect_string = true;
}
}
BOOST_REQUIRE(throwed == false);
BOOST_REQUIRE(throw_expect_string == false);
} FC_LOG_AND_RETHROW()



BOOST_AUTO_TEST_SUITE_END()

0 comments on commit bac5789

Please sign in to comment.