Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "../aot/aot_runtime.h"
#endif

static void
WASM_RUNTIME_API_INTER void
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
{
if (error_buf != NULL) {
Expand All @@ -40,7 +40,7 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
}
}

static void
WASM_RUNTIME_API_INTER void
set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
{
va_list args;
Expand Down
9 changes: 9 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ extern "C" {

#define EXCEPTION_BUF_LEN 128

/* Test visibility macro for internal functions */
#ifndef WASM_RUNTIME_API_INTER
#ifdef WAMR_BUILD_TEST
#define WASM_RUNTIME_API_INTER
#else
#define WASM_RUNTIME_API_INTER static
#endif
#endif

typedef struct WASMModuleInstance WASMModuleInstance;
typedef struct WASMFunctionInstance WASMFunctionInstance;
typedef struct WASMMemoryInstance WASMMemoryInstance;
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ endif()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# Fetch CMocka for C unit tests
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24")
FetchContent_Declare(
cmocka
URL https://git.cryptomilk.org/projects/cmocka.git/snapshot/cmocka-2.0.1.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
)
else()
FetchContent_Declare(
cmocka
URL https://git.cryptomilk.org/projects/cmocka.git/snapshot/cmocka-2.0.1.tar.gz
)
endif()
FetchContent_MakeAvailable(cmocka)

include(GoogleTest)
enable_testing()

Expand All @@ -65,6 +80,7 @@ add_subdirectory(gc)
add_subdirectory(tid-allocator)
add_subdirectory(unsupported-features)
add_subdirectory(smart-tests)
add_subdirectory(wasm-runtime)

if (NOT WAMR_BUILD_TARGET STREQUAL "X86_32")
add_subdirectory(aot-stack-frame)
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/wasm-runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.14)

project(test-wasm-runtime)

# Enable test build flag
add_definitions(-DWAMR_BUILD_TEST=1)

# Test-specific feature configuration
set(WAMR_BUILD_AOT 0)
set(WAMR_BUILD_FAST_INTERP 0)
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_JIT 0)
set(WAMR_BUILD_LIBC_WASI 0)
set(WAMR_BUILD_APP_FRAMEWORK 0)

# Test both multi-module enabled and disabled
if(NOT DEFINED WAMR_BUILD_MULTI_MODULE)
set(WAMR_BUILD_MULTI_MODULE 0)
endif()

include(../unit_common.cmake)

# Test source files
set(TEST_SOURCES
test_runner.c
mocks/wasm_loader_mock.c
${WAMR_RUNTIME_LIB_SOURCE}
)

# Create test executable
add_executable(wasm-runtime-test ${TEST_SOURCES})

target_include_directories(wasm-runtime-test PRIVATE
${CMAKE_CURRENT_LIST_DIR}/mocks
)

# Link dependencies
target_link_libraries(wasm-runtime-test cmocka::cmocka m)
target_link_options(wasm-runtime-test PRIVATE -Wl,--wrap=wasm_loader_load)

# Add to ctest
add_test(NAME wasm-runtime-test COMMAND wasm-runtime-test)
set_tests_properties(wasm-runtime-test PROPERTIES TIMEOUT 30)
39 changes: 39 additions & 0 deletions tests/unit/wasm-runtime/mocks/wasm_loader_mock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
#include "wasm_loader_mock.h"
#include "wasm_export.h"
#include "bh_platform.h"

WASMModule *
__wrap_wasm_loader_load(uint8 *buf, uint32 size,
#if WASM_ENABLE_MULTI_MODULE != 0
bool main_module,
#endif
const LoadArgs *args, char *error_buf, uint32 error_buf_size)
{
/* Check expected parameters */
check_expected_ptr(buf);
check_expected_uint(size);
#if WASM_ENABLE_MULTI_MODULE != 0
check_expected_uint(main_module);
#endif
check_expected_ptr(args);

/* Mock error buffer writing if provided */
bool populate_error = mock_type(bool);
if (populate_error && error_buf) {
const char *error_msg = mock_ptr_type(const char *);
if (error_msg && error_buf_size > 0) {
strncpy(error_buf, error_msg, error_buf_size);
if (error_buf_size > 0) {
error_buf[error_buf_size - 1] = '\0';
}
}
}

/* Return mocked module pointer */
return mock_ptr_type(WASMModule *);
}
32 changes: 32 additions & 0 deletions tests/unit/wasm-runtime/mocks/wasm_loader_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef WASM_LOADER_MOCK_H
#define WASM_LOADER_MOCK_H

#include <stdint.h>
#include <stdbool.h>
#include "wasm_loader.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Mock function declarations - only compiled when WAMR_BUILD_TEST is defined */
#ifdef WAMR_BUILD_TEST

WASMModule *
wasm_loader_load(uint8 *buf, uint32 size,
#if WASM_ENABLE_MULTI_MODULE != 0
bool main_module,
#endif
const LoadArgs *args, char *error_buf, uint32 error_buf_size);

WASMModule *
wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
uint32 error_buf_size);

#endif /* WAMR_BUILD_TEST */

#ifdef __cplusplus
}
#endif

#endif /* WASM_LOADER_MOCK_H */
30 changes: 30 additions & 0 deletions tests/unit/wasm-runtime/test_runner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>

/* Include test implementations */
#include "wasm_runtime_test.c"

int
main(void)
{
int result = 0;

/* Run all test groups */
// result |= cmocka_run_group_tests(set_error_buf_tests, NULL, NULL);
// result |= cmocka_run_group_tests(set_error_buf_v_tests, NULL, NULL);

/* Run tests with multi-module enabled */
result |= cmocka_run_group_tests(wasm_load_tests_multi_module_enabled, NULL,
NULL);

/* Run tests with multi-module disabled (if compiled) */
#if WASM_ENABLE_MULTI_MODULE == 0
result |= cmocka_run_group_tests(wasm_load_tests_multi_module_disabled,
NULL, NULL);
#endif

return result;
}
Loading
Loading