提交 9e22215c 编写于 作者: B Bart Wyatt

Basic tests, more incoming ref EOSIO/eos#175

上级 00c57d7f
......@@ -121,7 +121,7 @@ macro(add_wast_target target SOURCE_FILES INCLUDE_FOLDERS DESTINATION_FOLDER)
add_custom_command(OUTPUT ${DESTINATION_FOLDER}/${target}.wast
DEPENDS ${target}.s
COMMAND ${BINARYEN_BIN}/s2wasm -o ${DESTINATION_FOLDER}/${target}.wast -s 1024 ${target}.s
COMMAND ${BINARYEN_BIN}/s2wasm -o ${DESTINATION_FOLDER}/${target}.wast -s 16384 ${target}.s
COMMENT "Generating WAST ${target}.wast"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VERBATIM
......
#pragma once
#include <eoslib/types.h>
#include <eoslib/system.h>
extern "C" {
/**
......
#pragma once
#include <eoslib/types.h>
#include <eoslib/system.h>
extern "C" {
/**
......@@ -106,22 +106,5 @@ extern "C" {
*/
AccountName currentCode();
/**
* Aborts processing of this message and unwinds all pending changes if the test condition is true
* @brief Aborts processing of this message and unwinds all pending changes
* @param test - 0 to abort, 1 to ignore
* @param cstr - a null terminated message to explain the reason for failure
*/
void assert( uint32_t test, const char* cstr );
/**
* Returns the time in seconds from 1970 of the last accepted block (not the block including this message)
* @brief Get time of the last accepted block
* @return time in seconds from 1970 of the last accepted block
*/
Time now();
///@ } messagecapi
}
#pragma once
#include <eoslib/types.h>
extern "C" {
/**
* @defgroup systemapi System API
* @ingroup contractdev
* @brief Define API for interating with system level intrinsics
*
*/
/**
* @defgroup systemcapi System C API
* @ingroup systemapi
* @brief Define API for interating with system level intrinsics
*
* @{
*/
/**
* Aborts processing of this message and unwinds all pending changes if the test condition is true
* @brief Aborts processing of this message and unwinds all pending changes
* @param test - 0 to abort, 1 to ignore
* @param cstr - a null terminated message to explain the reason for failure
*/
void assert( uint32_t test, const char* cstr );
/**
* Returns the time in seconds from 1970 of the last accepted block (not the block including this message)
* @brief Get time of the last accepted block
* @return time in seconds from 1970 of the last accepted block
*/
Time now();
///@ } systemcapi
}
......@@ -60,6 +60,14 @@ extern "C" {
WASM_TEST_HANDLER(test_crypto, asert_sha256_true);
WASM_TEST_HANDLER(test_crypto, asert_no_data);
//test transaction
WASM_TEST_HANDLER(test_transaction, send_message);
WASM_TEST_HANDLER(test_transaction, send_message_max);
WASM_TEST_HANDLER(test_transaction, send_message_large);
WASM_TEST_HANDLER(test_transaction, send_transaction);
WASM_TEST_HANDLER(test_transaction, send_transaction_max);
WASM_TEST_HANDLER(test_transaction, send_transaction_large);
//unhandled test call
WASM_TEST_ERROR_CODE = WASM_TEST_FAIL;
}
......
......@@ -9,12 +9,6 @@
#define WASM_ASSERT(m, message) if(!(m)) { WASM_TEST_ERROR_MESSAGE=(unsigned int)message; return WASM_TEST_FAIL; }
typedef unsigned long long u64;
#define WASM_TEST_HANDLER(CLASS, METHOD) \
if( u32(action>>32) == DJBH(#CLASS) && u32(action) == DJBH(#METHOD) ) { \
WASM_TEST_ERROR_CODE = CLASS::METHOD(); \
return; \
}
typedef unsigned int u32;
static constexpr u32 DJBH(const char* cp)
{
......@@ -24,6 +18,17 @@ static constexpr u32 DJBH(const char* cp)
return hash;
}
static constexpr u64 WASM_TEST_ACTION(const char* cls, const char* method)
{
return u64(DJBH(cls)) << 32 | u64(DJBH(method));
}
#define WASM_TEST_HANDLER(CLASS, METHOD) \
if( action == WASM_TEST_ACTION(#CLASS, #METHOD) ) { \
WASM_TEST_ERROR_CODE = CLASS::METHOD(); \
return; \
}
#pragma pack(push, 1)
struct dummy_message {
char a; //1
......@@ -99,3 +104,13 @@ struct test_crypto {
static unsigned int asert_sha256_true();
static unsigned int asert_no_data();
};
struct test_transaction {
static unsigned int send_message();
static unsigned int send_message_max();
static unsigned int send_message_large();
static unsigned int send_transaction();
static unsigned int send_transaction_max();
static unsigned int send_transaction_large();
};
#include <eoslib/transaction.hpp>
#include "test_api.hpp"
unsigned int test_transaction::send_message() {
dummy_message payload = {DUMMY_MESSAGE_DEFAULT_A, DUMMY_MESSAGE_DEFAULT_B, DUMMY_MESSAGE_DEFAULT_C};
auto msg = messageCreate(N(testapi), WASM_TEST_ACTION("test_message", "read_message"), &payload, sizeof(dummy_message));
messageSend(msg);
return WASM_TEST_PASS;
}
unsigned int test_transaction::send_transaction() {
dummy_message payload = {DUMMY_MESSAGE_DEFAULT_A, DUMMY_MESSAGE_DEFAULT_B, DUMMY_MESSAGE_DEFAULT_C};
auto msg = messageCreate(N(testapi), WASM_TEST_ACTION("test_message", "read_message"), &payload, sizeof(dummy_message));
auto trx = transactionCreate();
transactionRequireScope(trx, N(testapi));
transactionAddMessage(trx, msg);
transactionSend(trx);
return WASM_TEST_PASS;
}
/**
* cause failure due to too many pending inline messages
*/
unsigned int test_transaction::send_message_max() {
dummy_message payload = {DUMMY_MESSAGE_DEFAULT_A, DUMMY_MESSAGE_DEFAULT_B, DUMMY_MESSAGE_DEFAULT_C};
for (int i = 0; i < 10; i++) {
messageCreate(N(testapi), WASM_TEST_ACTION("test_message", "read_message"), &payload, sizeof(dummy_message));
}
return WASM_TEST_FAIL;
}
/**
* cause failure due to a large message payload
*/
unsigned int test_transaction::send_message_large() {
char large_message[8 * 1024];
messageCreate(N(testapi), WASM_TEST_ACTION("test_message", "read_message"), large_message, sizeof(large_message));
return WASM_TEST_FAIL;
}
/**
* cause failure due to too many pending deferred transactions
*/
unsigned int test_transaction::send_transaction_max() {
for (int i = 0; i < 10; i++) {
transactionCreate();
}
return WASM_TEST_FAIL;
}
/**
* cause failure due to a large transaction size
*/
unsigned int test_transaction::send_transaction_large() {
auto trx = transactionCreate();
transactionRequireScope(trx, N(testapi));
for (int i = 0; i < 32; i ++) {
char large_message[4 * 1024];
auto msg = messageCreate(N(testapi), WASM_TEST_ACTION("test_message", "read_message"), large_message, sizeof(large_message));
transactionAddMessage(trx, msg);
}
transactionSend(trx);
return WASM_TEST_FAIL;
}
......@@ -289,21 +289,22 @@ DEFINE_INTRINSIC_FUNCTION1(env,transactionDrop,transactionDrop,none,i32,handle)
DEFINE_INTRINSIC_FUNCTION4(env,messageCreate,messageCreate,i32,i64,code,i64,type,i32,data,i32,length) {
auto& wasm = wasm_interface::get();
auto mem = wasm.current_memory;
EOS_ASSERT( length > 0, tx_unknown_argument,
"Attempting to push an empty message" );
const char* buffer = nullptr;
Bytes payload;
try {
// memoryArrayPtr checks that the entire array of bytes is valid and
// within the bounds of the memory segment so that transactions cannot pass
// bad values in attempts to read improper memory
buffer = memoryArrayPtr<const char>( mem, data, uint32_t(length) );
const char* buffer = memoryArrayPtr<const char>( mem, uint32_t(data), uint32_t(length) );
payload.insert(payload.end(), buffer, buffer + length);
} catch( const Runtime::Exception& e ) {
FC_THROW_EXCEPTION(tx_unknown_argument, "Message data is not a valid memory range");
}
auto& pmsg = wasm.current_apply_context->create_pending_message(Name(code), Name(type), Bytes(buffer, buffer + length));
auto& pmsg = wasm.current_apply_context->create_pending_message(Name(code), Name(type), payload);
return pmsg.handle;
}
......
......@@ -143,6 +143,8 @@ bool is_access_violation(fc::unhandled_exception const & e) {
bool is_tx_missing_recipient(tx_missing_recipient const & e) { return true;}
bool is_tx_missing_auth(tx_missing_auth const & e) { return true; }
bool is_tx_missing_scope(tx_missing_scope const& e) { return true; }
bool is_tx_resource_exhausted(tx_resource_exhausted const & e) { return true; }
bool is_tx_unknown_argument(tx_unknown_argument const & e) { return true; }
bool is_assert_exception(fc::assert_exception const & e) { return true; }
std::vector<std::string> capture;
......@@ -380,6 +382,19 @@ BOOST_FIXTURE_TEST_CASE(test_all, testing_fixture)
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_crypto", "asert_no_data"), {}, {} ),
fc::assert_exception, is_assert_exception );
//Test transaction
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_transaction", "send_message"), {}, {}) == WASM_TEST_PASS, "test_transaction::send_message()");
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_transaction", "send_message_large"), {}, {} ),
tx_resource_exhausted, is_tx_resource_exhausted );
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_transaction", "send_message_max"), {}, {} ),
tx_resource_exhausted, is_tx_resource_exhausted );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_transaction", "send_transaction"), {}, {}) == WASM_TEST_PASS, "test_transaction::send_message()");
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_transaction", "send_transaction_large"), {}, {} ),
tx_resource_exhausted, is_tx_resource_exhausted );
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_transaction", "send_transaction_max"), {}, {} ),
tx_resource_exhausted, is_tx_resource_exhausted );
} FC_LOG_AND_RETHROW() }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册