loader: Abstract and mock evmc_last_error_msg()

This commit is contained in:
Paweł Bylica 2019-04-17 17:02:10 +02:00
parent caf6c2a1bd
commit 51fe9f48d4
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
3 changed files with 28 additions and 6 deletions

View File

@ -19,12 +19,14 @@
#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 DLL_GET_ERROR_MSG() NULL
#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 DLL_GET_ERROR_MSG() dlerror()
#endif
#define PATH_MAX_LENGTH 4096
@ -69,11 +71,8 @@ evmc_create_fn evmc_load(const char* filename, enum evmc_loader_error_code* erro
DLL_HANDLE handle = DLL_OPEN(filename);
if (!handle)
{
#if !defined(EVMC_LOADER_MOCK) && !_WIN32
// If available, get the error message from dlerror().
last_error_msg = dlerror();
#endif
// Get error message if available.
last_error_msg = DLL_GET_ERROR_MSG();
ec = EVMC_LOADER_CANNOT_OPEN;
goto exit;
}

View File

@ -14,10 +14,14 @@ const char* evmc_test_library_path = NULL;
const char* evmc_test_library_symbol = NULL;
evmc_create_fn evmc_test_create_fn = NULL;
static const char* evmc_test_last_error_msg = NULL;
static int evmc_test_load_library(const char* filename)
{
evmc_test_last_error_msg = NULL;
if (filename && evmc_test_library_path && strcmp(filename, evmc_test_library_path) == 0)
return magic_handle;
evmc_test_last_error_msg = "cannot load library";
return 0;
}
@ -36,7 +40,16 @@ static evmc_create_fn evmc_test_get_symbol_address(int handle, const char* symbo
return NULL;
}
static const char* evmc_test_get_last_error_msg()
{
// Return the last error message only once.
const char* m = evmc_test_last_error_msg;
evmc_test_last_error_msg = NULL;
return m;
}
#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)
#define DLL_GET_ERROR_MSG() evmc_test_get_last_error_msg()

View File

@ -137,7 +137,17 @@ TEST_F(loader, load_windows_path)
evmc_loader_error_code ec;
evmc_load(path, &ec);
EXPECT_EQ(ec, should_open ? EVMC_LOADER_SUCCESS : EVMC_LOADER_CANNOT_OPEN);
if (should_open)
{
EXPECT_EQ(ec, EVMC_LOADER_SUCCESS);
EXPECT_EQ(evmc_last_error_msg(), nullptr);
}
else
{
EXPECT_EQ(ec, EVMC_LOADER_CANNOT_OPEN);
EXPECT_STREQ(evmc_last_error_msg(), "cannot load library");
EXPECT_EQ(evmc_last_error_msg(), nullptr);
}
}
}