From 4a3bd021f4712d6ea485a7a6d0c0eec5c0ddc48b Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 27 Aug 2018 15:33:16 +0100 Subject: [PATCH 1/2] Add vm test for execute --- test/vmtester/tests.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/vmtester/tests.cpp b/test/vmtester/tests.cpp index d459307..f2b534b 100644 --- a/test/vmtester/tests.cpp +++ b/test/vmtester/tests.cpp @@ -6,6 +6,7 @@ #include +#include #include // Compile time checks: @@ -31,6 +32,26 @@ TEST_F(evmc_vm_test, abi_version_match) ASSERT_EQ(vm->abi_version, EVMC_ABI_VERSION); } +TEST_F(evmc_vm_test, execute) +{ + evmc_context context{}; + evmc_message msg{}; + std::array code = {{0xfe, 0x00}}; + + evmc_result result = + vm->execute(vm, &context, EVMC_LATEST_REVISION, &msg, code.data(), code.size()); + + // Validate some constraints + if (result.status_code != EVMC_SUCCESS && result.status_code != EVMC_REVERT) + EXPECT_EQ(result.gas_left, 0); + + if (result.output_data == NULL) + EXPECT_EQ(result.output_size, 0); + + if (result.release) + result.release(&result); +} + TEST_F(evmc_vm_test, set_option_unknown) { if (vm->set_option) From 81425662e6f8034f68f53c673abe899f3e46859b Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 28 Aug 2018 15:42:30 +0100 Subject: [PATCH 2/2] Add mock context for VM testing --- test/vmtester/CMakeLists.txt | 2 +- test/vmtester/mock_context.cpp | 130 +++++++++++++++++++++++++++++++++ test/vmtester/tests.cpp | 4 +- 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 test/vmtester/mock_context.cpp diff --git a/test/vmtester/CMakeLists.txt b/test/vmtester/CMakeLists.txt index 4a36609..1986b71 100644 --- a/test/vmtester/CMakeLists.txt +++ b/test/vmtester/CMakeLists.txt @@ -7,7 +7,7 @@ include(GNUInstallDirs) hunter_add_package(CLI11) find_package(CLI11) -add_executable(evmc-vmtester vmtester.hpp vmtester.cpp tests.cpp) +add_executable(evmc-vmtester vmtester.hpp vmtester.cpp tests.cpp mock_context.cpp) set_target_properties(evmc-vmtester PROPERTIES RUNTIME_OUTPUT_DIRECTORY ..) target_link_libraries(evmc-vmtester PRIVATE evmc loader GTest::gtest CLI11::CLI11) diff --git a/test/vmtester/mock_context.cpp b/test/vmtester/mock_context.cpp new file mode 100644 index 0000000..bc59548 --- /dev/null +++ b/test/vmtester/mock_context.cpp @@ -0,0 +1,130 @@ +#include +#include + +#include +#include +#include +#include + +evmc_uint256be balance(evmc_context* context, const evmc_address* address) +{ + (void)context; + (void)address; + evmc_uint256be ret = {{1, 2, 3, 4}}; + return ret; +} + +static int account_exists(evmc_context* context, const evmc_address* address) +{ + (void)context; + (void)address; + return 0; +} + +static void get_storage(evmc_uint256be* result, + evmc_context* context, + const evmc_address* address, + const evmc_uint256be* key) +{ + (void)result; + (void)context; + (void)address; + (void)key; +} + +static enum evmc_storage_status set_storage(evmc_context* context, + const evmc_address* address, + const evmc_uint256be* key, + const evmc_uint256be* value) +{ + (void)context; + (void)address; + (void)key; + (void)value; + return EVMC_STORAGE_UNCHANGED; +} + +static void get_balance(evmc_uint256be* result, evmc_context* context, const evmc_address* address) +{ + *result = balance(context, address); +} + +static size_t get_code_size(evmc_context* context, const evmc_address* address) +{ + (void)context; + (void)address; + return 0; +} + +static void get_code_hash(evmc_uint256be* result, + evmc_context* context, + const evmc_address* address) +{ + (void)result; + (void)context; + (void)address; +} + +static size_t copy_code(evmc_context* context, + const evmc_address* address, + size_t code_offset, + uint8_t* buffer_data, + size_t buffer_size) +{ + (void)context; + (void)address; + (void)code_offset; + (void)buffer_data; + (void)buffer_size; + return 0; +} + +static void selfdestruct(evmc_context* context, + const evmc_address* address, + const evmc_address* beneficiary) +{ + (void)context; + (void)address; + (void)beneficiary; +} + +static void call(evmc_result* result, evmc_context* context, const evmc_message* msg) +{ + (void)context; + (void)msg; + result->status_code = EVMC_FAILURE; +} + +static void get_tx_context(evmc_tx_context* result, evmc_context* context) +{ + (void)result; + (void)context; +} + +static void get_block_hash(evmc_uint256be* result, evmc_context* context, int64_t number) +{ + (void)result; + (void)context; + (void)number; +} + +/// EVM log callback. +static void emit_log(evmc_context* context, + const evmc_address* address, + const uint8_t* data, + size_t data_size, + const evmc_uint256be topics[], + size_t topics_count) +{ + (void)context; + (void)address; + (void)data; + (void)data_size; + (void)topics; + (void)topics_count; +} + +extern const evmc_context_fn_table mock_context_fn_table = { + account_exists, get_storage, set_storage, get_balance, get_code_size, get_code_hash, + copy_code, selfdestruct, call, get_tx_context, get_block_hash, emit_log, +}; diff --git a/test/vmtester/tests.cpp b/test/vmtester/tests.cpp index f2b534b..e70d29f 100644 --- a/test/vmtester/tests.cpp +++ b/test/vmtester/tests.cpp @@ -27,6 +27,8 @@ static constexpr size_t optionalDataSize = sizeof(evmc_result) - offsetof(evmc_result, create_address); static_assert(optionalDataSize == sizeof(evmc_result_optional_storage), ""); +extern const struct evmc_context_fn_table mock_context_fn_table; + TEST_F(evmc_vm_test, abi_version_match) { ASSERT_EQ(vm->abi_version, EVMC_ABI_VERSION); @@ -34,7 +36,7 @@ TEST_F(evmc_vm_test, abi_version_match) TEST_F(evmc_vm_test, execute) { - evmc_context context{}; + evmc_context context = {&mock_context_fn_table}; evmc_message msg{}; std::array code = {{0xfe, 0x00}};