Skip to content

Commit

Permalink
Merge pull request ethereum#204 from ethereum/loader-tests
Browse files Browse the repository at this point in the history
Loader unit tests
  • Loading branch information
chfast authored Mar 14, 2019
2 parents 2e69b5a + f4034fd commit 75732e9
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 265 deletions.
2 changes: 0 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ jobs:
- run:
name: "Run codespell"
command: |
sudo pip3 install --upgrade pip setuptools
sudo pip3 install codespell
codespell --quiet-level=4 --ignore-words=./.codespell-whitelist
build: &build
Expand Down
4 changes: 2 additions & 2 deletions cmake/HunterConfig.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# EVMC: Ethereum Client-VM Connector API.
# Copyright 2018 The EVMC Authors.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.
# Copyright 2019 The EVMC Authors.
# Licensed under the Apache License, Version 2.0.

# Setup Hunter.
# Hunter is going to be initialized only if building with tests,
Expand Down
16 changes: 10 additions & 6 deletions lib/loader/loader.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
* Copyright 2019 The EVMC Authors.
* Licensed under the Apache License, Version 2.0.
*/

#include <evmc/loader.h>
Expand All @@ -11,25 +11,29 @@
#include <stdint.h>
#include <string.h>

#if _WIN32
#if defined(EVMC_LOADER_MOCK)
#include "../../test/unittests/loader_mock.h"
#elif _WIN32
#include <Windows.h>
#define DLL_HANDLE HMODULE
#define DLL_OPEN(filename) LoadLibrary(filename)
#define DLL_CLOSE(handle) FreeLibrary(handle)
#define DLL_GET_CREATE_FN(handle, name) (evmc_create_fn)(uintptr_t) GetProcAddress(handle, name)
#define HAVE_STRCPY_S 1
#else
#include <dlfcn.h>
#define DLL_HANDLE void*
#define DLL_OPEN(filename) dlopen(filename, RTLD_LAZY)
#define DLL_CLOSE(handle) dlclose(handle)
#define DLL_GET_CREATE_FN(handle, name) (evmc_create_fn)(uintptr_t) dlsym(handle, name)
#define HAVE_STRCPY_S 0
#endif

#define PATH_MAX_LENGTH 4096

#if !HAVE_STRCPY_S
#if !_WIN32
/*
* Provide strcpy_s() for GNU libc.
* The availability check might need to adjusted for other C standard library implementations.
*/
static void strcpy_s(char* dest, size_t destsz, const char* src)
{
size_t len = strlen(src);
Expand Down
45 changes: 7 additions & 38 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,19 @@
# EVMC: Ethereum Client-VM Connector API.
# Copyright 2018 The EVMC Authors.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.
# Copyright 2019 The EVMC Authors.
# Licensed under the Apache License, Version 2.0.

add_library(vm-mock SHARED vm_mock.c)
target_link_libraries(vm-mock PRIVATE evmc)

add_library(vm-mock-default SHARED vm_mock_default.c)
target_link_libraries(vm-mock-default PRIVATE evmc)


if(UNIX)
set(cmd create_symlink)
else()
set(cmd copy)
endif()

add_custom_command(
TARGET vm-mock POST_BUILD

COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> libaaa.so
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> double_prefix_aaa.evm
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> double-prefix-aaa.evm
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> eee-bbb.dll
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> libeee1.so
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> eee2.so
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> libeee3.x
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> eee4
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> _
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> lib_.so
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> ../aaa.evm
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> failure.vm
COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock> abi42.vm

COMMAND ${CMAKE_COMMAND} -E ${cmd} $<TARGET_FILE:vm-mock-default> default.evmc

COMMAND ${CMAKE_COMMAND} -E touch empty.file
)
add_library(loader-mocked STATIC ${PROJECT_SOURCE_DIR}/lib/loader/loader.c)
target_link_libraries(loader-mocked PRIVATE evmc)
target_compile_definitions(loader-mocked PRIVATE EVMC_LOADER_MOCK=1)

add_executable(
evmc-test
loader_mock.h
test_cpp.cpp
test_helpers.cpp
test_instructions.cpp
test_loader.cpp
)

target_link_libraries(evmc-test PRIVATE evmc-example-vm instructions loader GTest::gtest GTest::main)
target_link_libraries(evmc-test PRIVATE loader-mocked evmc-example-vm instructions GTest::gtest GTest::main)
set_target_properties(evmc-test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ..)
add_dependencies(evmc-test vm-mock)
42 changes: 42 additions & 0 deletions test/unittests/loader_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2019 The EVMC Authors.
* Licensed under the Apache License, Version 2.0.
*/

/**
* @file
* The loader OS mock for opening DLLs. To be inserted in loader.c for unit tests.
*/

static const int magic_handle = 0xE7AC;

const char* evmc_test_library_path = NULL;
const char* evmc_test_library_symbol = NULL;
evmc_create_fn evmc_test_create_fn = NULL;

static int evmc_test_load_library(const char* filename)
{
if (filename && evmc_test_library_path && strcmp(filename, evmc_test_library_path) == 0)
return magic_handle;
return 0;
}

static void evmc_test_free_library(int handle)
{
(void)handle;
}

static evmc_create_fn evmc_test_get_symbol_address(int handle, const char* symbol)
{
if (handle != magic_handle)
return NULL;

if (evmc_test_library_symbol && strcmp(symbol, evmc_test_library_symbol) == 0)
return evmc_test_create_fn;
return NULL;
}

#define DLL_HANDLE int
#define DLL_OPEN(filename) evmc_test_load_library(filename)
#define DLL_CLOSE(handle) evmc_test_free_library(handle)
#define DLL_GET_CREATE_FN(handle, name) evmc_test_get_symbol_address(handle, name)
Loading

0 comments on commit 75732e9

Please sign in to comment.