Skip to content

Commit

Permalink
GH-1614 Fix tests for removal of subjective max_nonprivileged_inline_…
Browse files Browse the repository at this point in the history
…action_size limit and add new test for 512k limit
  • Loading branch information
heifner committed Sep 15, 2023
1 parent 4057c4b commit 509c11f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 46 deletions.
46 changes: 10 additions & 36 deletions unittests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,10 +1435,16 @@ BOOST_FIXTURE_TEST_CASE(transaction_tests, validating_tester) { try {
// test send_action_empty
CALL_TEST_FUNCTION(*this, "test_transaction", "send_action_empty", {});

// test send_action_large
BOOST_CHECK_EXCEPTION(CALL_TEST_FUNCTION(*this, "test_transaction", "send_action_large", {}), inline_action_too_big_nonprivileged,
// test send_action_large (512k)
CALL_TEST_FUNCTION( *this, "test_transaction", "send_action_512k", {});

// test send_many_actions_512k (512k)
CALL_TEST_FUNCTION( *this, "test_transaction", "send_many_actions_512k", {});

// test send_action_large (512k + 1)
BOOST_CHECK_EXCEPTION(CALL_TEST_FUNCTION(*this, "test_transaction", "send_action_large", {}), inline_action_too_big,
[](const fc::exception& e) {
return expect_assert_message(e, "inline action too big for nonprivileged account");
return expect_assert_message(e, "inline action too big");
}
);

Expand Down Expand Up @@ -1611,38 +1617,6 @@ BOOST_AUTO_TEST_CASE(inline_action_objective_limit) { try {

} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE(deferred_inline_action_subjective_limit_failure) { try {
const uint32_t _4k = 4 * 1024;
tester chain(setup_policy::full, db_read_mode::HEAD, {_4k + 100}, {_4k});
chain.produce_blocks(2);
chain.create_accounts( {"testapi"_n, "testapi2"_n, "alice"_n} );
chain.set_code( "testapi"_n, test_contracts::test_api_wasm() );
chain.set_code( "testapi2"_n, test_contracts::test_api_wasm() );
chain.produce_block();

transaction_trace_ptr trace;
auto c = chain.control->applied_transaction.connect([&](std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&> x) {
auto& t = std::get<0>(x);
if (t->scheduled) { trace = t; }
} );
CALL_TEST_FUNCTION(chain, "test_transaction", "send_deferred_transaction_4k_action", {} );
BOOST_CHECK(!trace);
BOOST_CHECK_EXCEPTION(chain.produce_block( fc::seconds(2) ), fc::exception,
[](const fc::exception& e) {
return expect_assert_message(e, "inline action too big for nonprivileged account");
}
);

//check that it populates exception fields
BOOST_REQUIRE(trace);
BOOST_REQUIRE(trace->except);
BOOST_REQUIRE(trace->error_code);

BOOST_REQUIRE_EQUAL(1, trace->action_traces.size());
c.disconnect();

} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE(deferred_inline_action_subjective_limit) { try {
const uint32_t _4k = 4 * 1024;
tester chain(setup_policy::full, db_read_mode::HEAD, {_4k + 100}, {_4k + 1});
Expand Down Expand Up @@ -1674,7 +1648,7 @@ BOOST_AUTO_TEST_CASE(deferred_inline_action_subjective_limit) { try {

//confirm printed message
BOOST_TEST(!trace->action_traces.empty());
BOOST_TEST(trace->action_traces.back().console == "exec 8");
BOOST_TEST(trace->action_traces.back().console == "action size: 4096");
c.disconnect();

for (int n=0; n < 10; ++n) {
Expand Down
6 changes: 6 additions & 0 deletions unittests/test-contracts/test_api/test_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ void test_action::test_dummy_action() {
}
}

void test_action::read_action() {
print("action size: " + std::to_string(action_data_size()));
void* p = malloc(action_data_size());
read_action_data(p, action_data_size());
}

void test_action::read_action_to_0() {
read_action_data( (void *)0, action_data_size() );
}
Expand Down
3 changes: 3 additions & 0 deletions unittests/test-contracts/test_api/test_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extern "C" {

//test_action
WASM_TEST_HANDLER ( test_action, read_action_normal );
WASM_TEST_HANDLER ( test_action, read_action );
WASM_TEST_HANDLER ( test_action, read_action_to_0 );
WASM_TEST_HANDLER ( test_action, read_action_to_64k );
WASM_TEST_HANDLER_EX( test_action, require_notice );
Expand Down Expand Up @@ -118,6 +119,8 @@ extern "C" {
WASM_TEST_HANDLER ( test_transaction, send_action_empty );
WASM_TEST_HANDLER ( test_transaction, send_action_large );
WASM_TEST_HANDLER ( test_transaction, send_action_4k );
WASM_TEST_HANDLER ( test_transaction, send_action_512k );
WASM_TEST_HANDLER ( test_transaction, send_many_actions_512k );
WASM_TEST_HANDLER ( test_transaction, send_action_recurse );
WASM_TEST_HANDLER ( test_transaction, test_read_transaction );
WASM_TEST_HANDLER ( test_transaction, test_transaction_size );
Expand Down
3 changes: 3 additions & 0 deletions unittests/test-contracts/test_api/test_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ struct test_print {

struct test_action {
static void read_action_normal();
static void read_action();
static void read_action_to_0();
static void read_action_to_64k();
static void test_dummy_action();
Expand Down Expand Up @@ -202,6 +203,8 @@ struct test_transaction {
static void send_action_max();
static void send_action_large();
static void send_action_4k();
static void send_action_512k();
static void send_many_actions_512k();
static void send_action_recurse();
static void send_action_inline_fail();
static void test_read_transaction();
Expand Down
Binary file modified unittests/test-contracts/test_api/test_api.wasm
Binary file not shown.
61 changes: 51 additions & 10 deletions unittests/test-contracts/test_api/test_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,15 @@ void test_transaction::send_action_empty() {
}

/**
* cause failure due to a large action payload
* cause failure due to a large action payload, larger than max_inline_action_size of 512K
*/
void test_transaction::send_action_large() {
using namespace eosio;
static char large_message[8 * 1024];
test_action_action<"testapi"_n.value, WASM_TEST_ACTION( "test_action", "read_action_normal" )> test_action;
copy_data( large_message, 8*1024, test_action.data );
test_action_action<"testapi"_n.value, WASM_TEST_ACTION( "test_action", "read_action" )> test_action;
test_action.data.resize(512*1024+1);

std::vector<permission_level> permissions = { {"testapi"_n, "active"_n} };
action act( permissions, name{"testapi"}, name{WASM_TEST_ACTION("test_action", "read_action_normal")}, test_action );

action act( permissions, name{"testapi"}, name{WASM_TEST_ACTION("test_action", "read_action")}, test_action );
act.send();
eosio_assert( false, "send_message_large() should've thrown an error" );
}
Expand All @@ -104,16 +102,59 @@ void test_transaction::send_action_large() {
*/
void test_transaction::send_action_4k() {
using namespace eosio;
static char large_message[4 * 1024];
test_action_action<"testapi"_n.value, WASM_TEST_ACTION( "test_action", "test_action_ordinal4" )> test_action;
copy_data( large_message, 4*1024, test_action.data );
test_action_action<"testapi"_n.value, WASM_TEST_ACTION( "test_action", "read_action" )> test_action;
test_action.data.resize(4*1024);

std::vector<permission_level> permissions = { {"testapi"_n, "active"_n} };
action act( permissions, name{"testapi"}, name{WASM_TEST_ACTION("test_action", "read_action")}, test_action );

act.send();
}

/**
* send an inline action that is 512K (limit is < 512K)
* the limit includes the size of the action
*/
void test_transaction::send_action_512k() {
using namespace eosio;
test_action_action<"testapi"_n.value, WASM_TEST_ACTION( "test_action", "read_action" )> test_action;

test_action.data.resize(1);
std::vector<permission_level> permissions = { {"testapi"_n, "active"_n} };
action act( permissions, name{"testapi"}, name{WASM_TEST_ACTION("test_action", "test_action_ordinal4")}, test_action );
action temp_act( permissions, name{"testapi"}, name{WASM_TEST_ACTION("test_action", "read_action")}, test_action );

size_t action_size = pack_size(temp_act);
test_action.data.resize(512*1024-action_size-2); // check is < 512K

// send at limit (512K - 1)
action act( permissions, name{"testapi"}, name{WASM_TEST_ACTION("test_action", "read_action")}, test_action );

if (pack_size(act) != 512*1024-1) {
std::string err = "send_action_512k action size is: " + std::to_string(action_size) + " not 512K-1";
eosio_assert(false, err.c_str());
}

act.send();
}

/**
* send many inline actions that are 512K (limit is < 512K)
* the limit includes the size of the action
*/
void test_transaction::send_many_actions_512k() {
using namespace eosio;
test_action_action<"testapi"_n.value, WASM_TEST_ACTION( "test_transaction", "send_action_512k" )> test_action;

test_action.data.resize(1);
std::vector<permission_level> permissions = { {"testapi"_n, "active"_n} };
action act( permissions, name{"testapi"}, name{WASM_TEST_ACTION("test_transaction", "send_action_512k")}, test_action );

// 65 * 512K > wasm memory limit, which is ok because each gets their own wasm instantiation
for (size_t i = 0; i < 65; ++i) {
act.send();
}
}

/**
* cause failure due recursive loop
*/
Expand Down

0 comments on commit 509c11f

Please sign in to comment.