Skip to content

Commit

Permalink
Merge pull request #1478 from AntelopeIO/abi_tester_nonnullterm
Browse files Browse the repository at this point in the history
return `string` from test contract's `_abi()` call instead of `vector` to avoid non-null terminated `char*` access
  • Loading branch information
spoonincode authored Aug 4, 2023
2 parents 5b6226b + 6e9d776 commit 47db353
Show file tree
Hide file tree
Showing 29 changed files with 112 additions and 111 deletions.
4 changes: 2 additions & 2 deletions libraries/testing/contracts.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace eosio::testing {
std::vector<std::uint8_t> contracts:: CN ## _wasm() { \
return std::vector<std::uint8_t>(geosio_testing_contract_ ## CN ## _wasm_data, geosio_testing_contract_ ## CN ## _wasm_data + geosio_testing_contract_ ## CN ## _wasm_size); \
} \
std::vector<char> contracts:: CN ## _abi() { \
return std::vector<char>(geosio_testing_contract_ ## CN ## _abi_data, geosio_testing_contract_ ## CN ## _abi_data + geosio_testing_contract_ ## CN ## _abi_size); \
std::string contracts:: CN ## _abi() { \
return geosio_testing_contract_ ## CN ## _abi_data; \
} \
}

Expand Down
3 changes: 2 additions & 1 deletion libraries/testing/contracts.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#include <cstdint>
#include <vector>
#include <string>

//contracts that need to be available by native contract unit testing's libtester need to be embedded
// in to the library as the build directory may not exist after being 'make install'ed.
#define MAKE_EMBD_WASM_ABI(CN) \
static std::vector<std::uint8_t> CN ## _wasm(); \
static std::vector<char> CN ## _abi();
static std::string CN ## _abi();

namespace eosio {
namespace testing {
Expand Down
2 changes: 1 addition & 1 deletion libraries/testing/include/eosio/testing/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ namespace eosio { namespace testing {

void set_code( account_name name, const char* wast, const private_key_type* signer = nullptr );
void set_code( account_name name, const vector<uint8_t> wasm, const private_key_type* signer = nullptr );
void set_abi( account_name name, const char* abi_json, const private_key_type* signer = nullptr );
void set_abi( account_name name, const std::string& abi_json, const private_key_type* signer = nullptr );

bool is_code_cached( account_name name ) const;

Expand Down
8 changes: 4 additions & 4 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ namespace eosio { namespace testing {
} FC_CAPTURE_AND_RETHROW( (account) )


void base_tester::set_abi( account_name account, const char* abi_json, const private_key_type* signer ) {
void base_tester::set_abi( account_name account, const std::string& abi_json, const private_key_type* signer ) {
auto abi = fc::json::from_string(abi_json).template as<abi_def>();
signed_transaction trx;
trx.actions.emplace_back( vector<permission_level>{{account,config::active_name}},
Expand Down Expand Up @@ -1105,17 +1105,17 @@ namespace eosio { namespace testing {

void base_tester::set_before_preactivate_bios_contract() {
set_code(config::system_account_name, contracts::before_preactivate_eosio_bios_wasm());
set_abi(config::system_account_name, contracts::before_preactivate_eosio_bios_abi().data());
set_abi(config::system_account_name, contracts::before_preactivate_eosio_bios_abi());
}

void base_tester::set_before_producer_authority_bios_contract() {
set_code(config::system_account_name, contracts::before_producer_authority_eosio_bios_wasm());
set_abi(config::system_account_name, contracts::before_producer_authority_eosio_bios_abi().data());
set_abi(config::system_account_name, contracts::before_producer_authority_eosio_bios_abi());
}

void base_tester::set_bios_contract() {
set_code(config::system_account_name, contracts::eosio_bios_wasm());
set_abi(config::system_account_name, contracts::eosio_bios_abi().data());
set_abi(config::system_account_name, contracts::eosio_bios_abi());
}


Expand Down
4 changes: 2 additions & 2 deletions tests/chain_plugin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, validating_tester ) try {

// setup contract and abi
set_code( "asserter"_n, test_contracts::asserter_wasm() );
set_abi( "asserter"_n, test_contracts::asserter_abi().data() );
set_abi( "asserter"_n, test_contracts::asserter_abi() );
produce_blocks(1);

auto resolver = [&,this]( const account_name& name ) -> std::optional<abi_serializer> {
Expand Down Expand Up @@ -101,7 +101,7 @@ BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, validating_tester ) try {
BOOST_TEST(block_str.find("011253686f756c64204e6f742041737365727421") != std::string::npos); //action data

// set an invalid abi (int8->xxxx)
std::string abi2 = test_contracts::asserter_abi().data();
std::string abi2 = test_contracts::asserter_abi();
auto pos = abi2.find("int8");
BOOST_TEST(pos != std::string::npos);
abi2.replace(pos, 4, "xxxx");
Expand Down
2 changes: 1 addition & 1 deletion tests/get_table_seckey_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_next_key_test, validating_tester ) try {

// setup contract and abi
set_code( "test"_n, test_contracts::get_table_seckey_test_wasm() );
set_abi( "test"_n, test_contracts::get_table_seckey_test_abi().data() );
set_abi( "test"_n, test_contracts::get_table_seckey_test_abi() );
produce_block();

chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum(), fc::microseconds::maximum(), {});
Expand Down
10 changes: 5 additions & 5 deletions tests/get_table_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BOOST_FIXTURE_TEST_CASE( get_scope_test, validating_tester ) try {
produce_block();

set_code( "eosio.token"_n, test_contracts::eosio_token_wasm() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi().data() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi() );
produce_blocks(1);

// create currency
Expand Down Expand Up @@ -146,7 +146,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_test, validating_tester ) try {
produce_block();

set_code( "eosio.token"_n, test_contracts::eosio_token_wasm() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi().data() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi() );
produce_blocks(1);

// create currency
Expand Down Expand Up @@ -326,7 +326,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_by_seckey_test, validating_tester ) try {
produce_block();

set_code( "eosio.token"_n, test_contracts::eosio_token_wasm() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi().data() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi() );
produce_blocks(1);

// create currency
Expand All @@ -342,7 +342,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_by_seckey_test, validating_tester ) try {
produce_blocks(1);

set_code( config::system_account_name, test_contracts::eosio_system_wasm() );
set_abi( config::system_account_name, test_contracts::eosio_system_abi().data() );
set_abi( config::system_account_name, test_contracts::eosio_system_abi() );

base_tester::push_action(config::system_account_name, "init"_n,
config::system_account_name, mutable_variant_object()
Expand Down Expand Up @@ -456,7 +456,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_next_key_test, validating_tester ) try {

// setup contract and abi
set_code( "test"_n, test_contracts::get_table_test_wasm() );
set_abi( "test"_n, test_contracts::get_table_test_abi().data() );
set_abi( "test"_n, test_contracts::get_table_test_abi() );
produce_block();

// Init some data
Expand Down
4 changes: 2 additions & 2 deletions tests/test_chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class chain_plugin_tester : public validating_tester {
"eosio.bpay"_n, "eosio.vpay"_n, "eosio.saving"_n, "eosio.names"_n, "eosio.rex"_n });

set_code( "eosio.token"_n, test_contracts::eosio_token_wasm() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi().data() );
set_abi( "eosio.token"_n, test_contracts::eosio_token_abi() );

{
const auto& accnt = control->db().get<account_object,by_name>( "eosio.token"_n );
Expand All @@ -207,7 +207,7 @@ class chain_plugin_tester : public validating_tester {
BOOST_CHECK_EQUAL( core_from_string("1000000000.0000"), get_balance( name("eosio") ) );

set_code( config::system_account_name, test_contracts::eosio_system_wasm() );
set_abi( config::system_account_name, test_contracts::eosio_system_abi().data() );
set_abi( config::system_account_name, test_contracts::eosio_system_abi() );

base_tester::push_action(config::system_account_name, "init"_n,
config::system_account_name, mutable_variant_object()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_snapshot_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_snapshot_information, SNAPSHOT_SUITE, snapsho
chain.create_account("snapshot"_n);
chain.produce_blocks(1);
chain.set_code("snapshot"_n, test_contracts::snapshot_test_wasm());
chain.set_abi("snapshot"_n, test_contracts::snapshot_test_abi().data());
chain.set_abi("snapshot"_n, test_contracts::snapshot_test_abi());
chain.produce_blocks(1);

auto block = chain.produce_block();
Expand Down
10 changes: 5 additions & 5 deletions unittests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ BOOST_FIXTURE_TEST_CASE(action_receipt_tests, validating_tester) { try {
set_code( config::system_account_name, contracts::eosio_bios_wasm() );

set_code( "test"_n, contracts::eosio_bios_wasm() );
set_abi( "test"_n, contracts::eosio_bios_abi().data() );
set_abi( "test"_n, contracts::eosio_bios_abi() );
set_code( "test"_n, test_contracts::payloadless_wasm() );

call_doit_and_check( "test"_n, "test"_n, [&]( const transaction_trace_ptr& res ) {
Expand Down Expand Up @@ -1896,7 +1896,7 @@ BOOST_AUTO_TEST_CASE(more_deferred_transaction_tests) { try {

chain.create_accounts( {contract_account, test_account} );
chain.set_code( contract_account, test_contracts::deferred_test_wasm() );
chain.set_abi( contract_account, test_contracts::deferred_test_abi().data() );
chain.set_abi( contract_account, test_contracts::deferred_test_abi() );
chain.produce_block();

BOOST_REQUIRE_EQUAL(0, index.size());
Expand Down Expand Up @@ -2106,9 +2106,9 @@ BOOST_FIXTURE_TEST_CASE(db_tests, validating_tester) { try {
create_account( "testapi2"_n );
produce_blocks(10);
set_code( "testapi"_n, test_contracts::test_api_db_wasm() );
set_abi( "testapi"_n, test_contracts::test_api_db_abi().data() );
set_abi( "testapi"_n, test_contracts::test_api_db_abi() );
set_code( "testapi2"_n, test_contracts::test_api_db_wasm() );
set_abi( "testapi2"_n, test_contracts::test_api_db_abi().data() );
set_abi( "testapi2"_n, test_contracts::test_api_db_abi() );
produce_blocks(1);

push_action( "testapi"_n, "pg"_n, "testapi"_n, mutable_variant_object() ); // primary_i64_general
Expand Down Expand Up @@ -2266,7 +2266,7 @@ BOOST_FIXTURE_TEST_CASE(multi_index_tests, validating_tester) { try {
create_account( "testapi"_n );
produce_blocks(1);
set_code( "testapi"_n, test_contracts::test_api_multi_index_wasm() );
set_abi( "testapi"_n, test_contracts::test_api_multi_index_abi().data() );
set_abi( "testapi"_n, test_contracts::test_api_multi_index_abi() );
produce_blocks(1);

auto check_failure = [this]( action_name a, const char* expected_error_msg ) {
Expand Down
12 changes: 6 additions & 6 deletions unittests/bootseq_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class bootseq_tester : public validating_tester {
public:
void deploy_contract( bool call_init = true ) {
set_code( config::system_account_name, test_contracts::eosio_system_wasm() );
set_abi( config::system_account_name, test_contracts::eosio_system_abi().data() );
set_abi( config::system_account_name, test_contracts::eosio_system_abi() );
if( call_init ) {
base_tester::push_action(config::system_account_name, "init"_n,
config::system_account_name, mutable_variant_object()
Expand Down Expand Up @@ -158,7 +158,7 @@ class bootseq_tester : public validating_tester {
return get_currency_balance("eosio.token"_n, symbol(CORE_SYMBOL), act);
}

void set_code_abi(const account_name& account, const vector<uint8_t>& wasm, const char* abi, const private_key_type* signer = nullptr) {
void set_code_abi(const account_name& account, const vector<uint8_t>& wasm, const std::string& abi, const private_key_type* signer = nullptr) {
wdump((account));
set_code(account, wasm, signer);
set_abi(account, abi, signer);
Expand Down Expand Up @@ -186,15 +186,15 @@ BOOST_FIXTURE_TEST_CASE( bootseq_test, bootseq_tester ) {
// - eosio (code: eosio.bios) (already set by tester constructor)
// - eosio.msig (code: eosio.msig)
// - eosio.token (code: eosio.token)
// set_code_abi("eosio.msig"_n, contracts::eosio_msig_wasm(), contracts::eosio_msig_abi().data());//, &eosio_active_pk);
// set_code_abi("eosio.token"_n, contracts::eosio_token_wasm(), contracts::eosio_token_abi().data()); //, &eosio_active_pk);
// set_code_abi("eosio.msig"_n, contracts::eosio_msig_wasm(), contracts::eosio_msig_abi());//, &eosio_active_pk);
// set_code_abi("eosio.token"_n, contracts::eosio_token_wasm(), contracts::eosio_token_abi()); //, &eosio_active_pk);

set_code_abi("eosio.msig"_n,
test_contracts::eosio_msig_wasm(),
test_contracts::eosio_msig_abi().data());//, &eosio_active_pk);
test_contracts::eosio_msig_abi());//, &eosio_active_pk);
set_code_abi("eosio.token"_n,
test_contracts::eosio_token_wasm(),
test_contracts::eosio_token_abi().data()); //, &eosio_active_pk);
test_contracts::eosio_token_abi()); //, &eosio_active_pk);

// Set privileged for eosio.msig and eosio.token
set_privileged("eosio.msig"_n);
Expand Down
18 changes: 9 additions & 9 deletions unittests/crypto_primitives_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE( alt_bn128_add_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using test_add = std::tuple<std::string, std::string, int32_t, std::string>;
Expand Down Expand Up @@ -160,7 +160,7 @@ BOOST_AUTO_TEST_CASE( alt_bn128_mul_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using test_mul = std::tuple<std::string, std::string, int32_t, std::string>;
Expand Down Expand Up @@ -287,7 +287,7 @@ BOOST_AUTO_TEST_CASE( alt_bn128_pair_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using g1g2_pair = std::vector<std::string>;
Expand Down Expand Up @@ -427,7 +427,7 @@ BOOST_AUTO_TEST_CASE( modexp_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using modexp_test = std::tuple<std::vector<string>, int32_t, std::string>;
Expand Down Expand Up @@ -517,7 +517,7 @@ BOOST_AUTO_TEST_CASE( modexp_subjective_limit_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

auto exponent = h2bin("010001");
Expand Down Expand Up @@ -591,7 +591,7 @@ BOOST_AUTO_TEST_CASE( blake2f_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using compress_test = std::tuple<std::vector<string>, int32_t, std::string>;
Expand Down Expand Up @@ -718,7 +718,7 @@ BOOST_AUTO_TEST_CASE( keccak256_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using test_keccak256 = std::tuple<std::string, std::string>;
Expand Down Expand Up @@ -769,7 +769,7 @@ BOOST_AUTO_TEST_CASE( sha3_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using test_sha3 = std::tuple<std::string, std::string>;
Expand Down Expand Up @@ -820,7 +820,7 @@ BOOST_AUTO_TEST_CASE( k1_recover_test ) { try {
c.produce_block();

c.set_code( tester1_account, test_contracts::crypto_primitives_test_wasm() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi().data() );
c.set_abi( tester1_account, test_contracts::crypto_primitives_test_abi() );
c.produce_block();

using test_k1_recover = std::tuple<std::string, std::string, int32_t, std::string>;
Expand Down
6 changes: 3 additions & 3 deletions unittests/currency_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class currency_tester : public validating_tester {
}

currency_tester()
:validating_tester(),abi_ser(json::from_string(test_contracts::eosio_token_abi().data()).as<abi_def>(), abi_serializer::create_yield_function( abi_serializer_max_time ))
:validating_tester(),abi_ser(json::from_string(test_contracts::eosio_token_abi()).as<abi_def>(), abi_serializer::create_yield_function( abi_serializer_max_time ))
{
create_account( "eosio.token"_n);
set_code( "eosio.token"_n, test_contracts::eosio_token_wasm() );
Expand Down Expand Up @@ -398,7 +398,7 @@ BOOST_FIXTURE_TEST_CASE( test_proxy, currency_tester ) try {
set_code("proxy"_n, test_contracts::proxy_wasm());
produce_blocks(1);

abi_serializer proxy_abi_ser(json::from_string(test_contracts::proxy_abi().data()).as<abi_def>(), abi_serializer::create_yield_function( abi_serializer_max_time ));
abi_serializer proxy_abi_ser(json::from_string(test_contracts::proxy_abi()).as<abi_def>(), abi_serializer::create_yield_function( abi_serializer_max_time ));

// set up proxy owner
{
Expand Down Expand Up @@ -454,7 +454,7 @@ BOOST_FIXTURE_TEST_CASE( test_deferred_failure, currency_tester ) try {
set_code("bob"_n, test_contracts::proxy_wasm());
produce_blocks(1);

abi_serializer proxy_abi_ser(json::from_string(test_contracts::proxy_abi().data()).as<abi_def>(), abi_serializer::create_yield_function( abi_serializer_max_time ));
abi_serializer proxy_abi_ser(json::from_string(test_contracts::proxy_abi()).as<abi_def>(), abi_serializer::create_yield_function( abi_serializer_max_time ));

// set up proxy owner
{
Expand Down
Loading

0 comments on commit 47db353

Please sign in to comment.