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

Commit

Permalink
Added more unit tests for memory.hpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjohnson5972 committed Sep 11, 2017
1 parent 5efb0ca commit 76a0011
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 120 deletions.
45 changes: 44 additions & 1 deletion tests/slow_tests/memory_test/memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern "C" {
const char* char_ptr = (const char*)ptr;
for (uint32_t i = 0; i < size; ++i)
{
assert(static_cast<uint32_t>(static_cast<unsigned char>(char_ptr[i])) == val, "buffer slot not empty");
assert(static_cast<uint32_t>(static_cast<unsigned char>(char_ptr[i])) == val, "buffer slot doesn't match");
}
}

Expand Down Expand Up @@ -132,6 +132,35 @@ extern "C" {
verify(buf1, 0x22, 1);
verify(&buf1[1], 1, 1);
verify(&buf1[2], 0x22, 8);

// verify adjacent non-overlapping buffers
char buf3[50] = {};
memset(&buf3[25], 0xee, 25);
verify(buf3, 0, 25);
memcpy(buf3, &buf3[25], 25);
verify(buf3, 0xee, 50);

memset(buf3, 0, 25);
verify(&buf3[25], 0xee, 25);
memcpy(&buf3[25], buf3, 25);
verify(buf3, 0, 50);
}

void test_memcpy_overlap_start()
{
char buf3[99] = {};
memset(buf3, 0xee, 50);
memset(&buf3[50], 0xff, 49);
memcpy(&buf3[49], buf3, 50);
}


void test_memcpy_overlap_end()
{
char buf3[99] = {};
memset(buf3, 0xee, 50);
memset(&buf3[50], 0xff, 49);
memcpy(buf3, &buf3[49], 50);
}

/// The apply method implements the dispatch of events to this contract
Expand All @@ -158,5 +187,19 @@ extern "C" {
test_memset_memcpy();
}
}
else if( code == N(testolstart) )
{
if( action == N(transfer) )
{
test_memcpy_overlap_start();
}
}
else if( code == N(testolend) )
{
if( action == N(transfer) )
{
test_memcpy_overlap_end();
}
}
}
}
190 changes: 71 additions & 119 deletions tests/slow_tests/slow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,133 +1184,85 @@ BOOST_FIXTURE_TEST_CASE(create_script_w_loop, testing_fixture)
}
} FC_LOG_AND_RETHROW() }

//Test wasm memory
BOOST_FIXTURE_TEST_CASE(test_memory, testing_fixture)
{ try {
Make_Blockchain(chain);
chain.produce_blocks(1);
Make_Account(chain, testmemory);
chain.produce_blocks(1);


types::setcode handler;
handler.account = "testmemory";

auto wasm = assemble_wast( memory_test_wast );
handler.code.resize(wasm.size());
memcpy( handler.code.data(), wasm.data(), wasm.size() );

{
eos::chain::SignedTransaction trx;
trx.scope = {"testmemory"};
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{"testmemory","active"});
transaction_set_message(trx, 0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
transaction_set_reference_block(trx, chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
#define MEMORY_TEST_RUN(account_name) \
Make_Blockchain(chain); \
chain.produce_blocks(1); \
Make_Account(chain, account_name); \
chain.produce_blocks(1); \
\
\
types::setcode handler; \
handler.account = #account_name; \
\
auto wasm = assemble_wast( memory_test_wast ); \
handler.code.resize(wasm.size()); \
memcpy( handler.code.data(), wasm.data(), wasm.size() ); \
\
{ \
eos::chain::SignedTransaction trx; \
trx.scope = {#account_name}; \
trx.messages.resize(1); \
trx.messages[0].code = config::EosContractName; \
trx.messages[0].authorization.emplace_back(types::AccountPermission{#account_name,"active"}); \
transaction_set_message(trx, 0, "setcode", handler); \
trx.expiration = chain.head_block_time() + 100; \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
chain.produce_blocks(1); \
} \
\
\
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names({#account_name,"inita"}); \
transaction_emplace_message(trx, #account_name, \
vector<types::AccountPermission>{}, \
"transfer", types::transfer{#account_name, "inita", 1,""}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
chain.produce_blocks(1); \
}


{
eos::chain::SignedTransaction trx;
trx.scope = sort_names({"testmemory","inita"});
transaction_emplace_message(trx, "testmemory",
vector<types::AccountPermission>{},
"transfer", types::transfer{"testmemory", "inita", 1,""});
trx.expiration = chain.head_block_time() + 100;
transaction_set_reference_block(trx, chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
}
#define MEMORY_TEST_CASE(test_case_name, account_name) \
BOOST_FIXTURE_TEST_CASE(test_case_name, testing_fixture) \
{ try{ \
MEMORY_TEST_RUN(account_name); \
} FC_LOG_AND_RETHROW() }

//Test wasm memory
BOOST_FIXTURE_TEST_CASE(test_memory_bounds, testing_fixture)
{ try {
Make_Blockchain(chain);
chain.produce_blocks(1);
Make_Account(chain, testbounds);
chain.produce_blocks(1);
//Test wasm memory allocation
MEMORY_TEST_CASE(test_memory, testmemory)


types::setcode handler;
handler.account = "testbounds";

auto wasm = assemble_wast( memory_test_wast );
handler.code.resize(wasm.size());
memcpy( handler.code.data(), wasm.data(), wasm.size() );

{
eos::chain::SignedTransaction trx;
trx.scope = {"testbounds"};
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{"testbounds","active"});
transaction_set_message(trx, 0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
transaction_set_reference_block(trx, chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
}


{
eos::chain::SignedTransaction trx;
trx.scope = sort_names({"testbounds","inita"});
transaction_emplace_message(trx, "testbounds",
vector<types::AccountPermission>{},
"transfer", types::transfer{"testbounds", "inita", 1,""});
trx.expiration = chain.head_block_time() + 100;
transaction_set_reference_block(trx, chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
}
} FC_LOG_AND_RETHROW() }
//Test wasm memory allocation at boundaries
MEMORY_TEST_CASE(test_memory_bounds, testbounds)

//Test intrinsic provided memset and memcpy
BOOST_FIXTURE_TEST_CASE(test_memset_memcpy, testing_fixture)
{ try {
Make_Blockchain(chain);
chain.produce_blocks(1);
Make_Account(chain, testmemset);
chain.produce_blocks(1);


types::setcode handler;
handler.account = "testmemset";

auto wasm = assemble_wast( memory_test_wast );
handler.code.resize(wasm.size());
memcpy( handler.code.data(), wasm.data(), wasm.size() );

{
eos::chain::SignedTransaction trx;
trx.scope = {"testmemset"};
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{"testmemset","active"});
transaction_set_message(trx, 0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
transaction_set_reference_block(trx, chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
}
MEMORY_TEST_CASE(test_memset_memcpy, testmemset)

//Test memcpy overlap at start of destination
BOOST_FIXTURE_TEST_CASE(test_memcpy_overlap_start, testing_fixture)
{
try {
MEMORY_TEST_RUN(testolstart);
BOOST_FAIL("memcpy should have thrown assert acception");
}
catch(fc::assert_exception& ex)
{
BOOST_REQUIRE(ex.to_detail_string().find("overlap of memory range is undefined") != std::string::npos);
}
}

{
eos::chain::SignedTransaction trx;
trx.scope = sort_names({"testmemset","inita"});
transaction_emplace_message(trx, "testmemset",
vector<types::AccountPermission>{},
"transfer", types::transfer{"testmemset", "inita", 1,""});
trx.expiration = chain.head_block_time() + 100;
transaction_set_reference_block(trx, chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
}
} FC_LOG_AND_RETHROW() }
//Test memcpy overlap at end of destination
BOOST_FIXTURE_TEST_CASE(test_memcpy_overlap_end, testing_fixture)
{
try {
MEMORY_TEST_RUN(testolend);
BOOST_FAIL("memcpy should have thrown assert acception");
}
catch(fc::assert_exception& ex)
{
BOOST_REQUIRE(ex.to_detail_string().find("overlap of memory range is undefined") != std::string::npos);
}
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 76a0011

Please sign in to comment.