From c8872f1a6fec2d1d650a7d191036f36377a7fbb4 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 15 Mar 2019 11:53:24 +0100 Subject: [PATCH] test(runtime-c-api) Test the new `wasmer_module_(de)?serialize` functions. This test suite compiles a module, then serializes it, deserializes it, and continues by creating an instance and calling a function on it. It allows to test the entire roundtrip. --- lib/runtime-c-api/tests/.gitignore | 1 + lib/runtime-c-api/tests/CMakeLists.txt | 5 ++ .../tests/test-module-serialize.c | 75 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 lib/runtime-c-api/tests/test-module-serialize.c diff --git a/lib/runtime-c-api/tests/.gitignore b/lib/runtime-c-api/tests/.gitignore index 7b8c0645b..b8f6888c2 100644 --- a/lib/runtime-c-api/tests/.gitignore +++ b/lib/runtime-c-api/tests/.gitignore @@ -19,5 +19,6 @@ test-memory test-module test-module-exports test-module-imports +test-module-serialize test-tables test-validate \ No newline at end of file diff --git a/lib/runtime-c-api/tests/CMakeLists.txt b/lib/runtime-c-api/tests/CMakeLists.txt index 165261418..136c0c1e9 100644 --- a/lib/runtime-c-api/tests/CMakeLists.txt +++ b/lib/runtime-c-api/tests/CMakeLists.txt @@ -10,6 +10,7 @@ add_executable(test-memory test-memory.c) add_executable(test-module test-module.c) add_executable(test-module-exports test-module-exports.c) add_executable(test-module-imports test-module-imports.c) +add_executable(test-module-serialize test-module-serialize.c) add_executable(test-tables test-tables.c) add_executable(test-validate test-validate.c) @@ -70,6 +71,10 @@ target_link_libraries(test-module-imports general ${WASMER_LIB}) target_compile_options(test-module-imports PRIVATE ${COMPILER_OPTIONS}) add_test(test-module-imports test-module-imports) +target_link_libraries(test-module-serialize general ${WASMER_LIB}) +target_compile_options(test-module-serialize PRIVATE ${COMPILER_OPTIONS}) +add_test(test-module-serialize test-module-serialize) + target_link_libraries(test-tables general ${WASMER_LIB}) target_compile_options(test-tables PRIVATE ${COMPILER_OPTIONS}) add_test(test-tables test-tables) diff --git a/lib/runtime-c-api/tests/test-module-serialize.c b/lib/runtime-c-api/tests/test-module-serialize.c new file mode 100644 index 000000000..7c1ac092e --- /dev/null +++ b/lib/runtime-c-api/tests/test-module-serialize.c @@ -0,0 +1,75 @@ +#include +#include "../wasmer.h" +#include +#include + +int main() +{ + // Read the wasm file bytes + FILE *file = fopen("sum.wasm", "r"); + fseek(file, 0, SEEK_END); + long len = ftell(file); + uint8_t *bytes = malloc(len); + fseek(file, 0, SEEK_SET); + fread(bytes, 1, len, file); + fclose(file); + + wasmer_module_t *module_one = NULL; + wasmer_result_t compile_result = wasmer_compile(&module_one, bytes, len); + printf("Compile result: %d\n", compile_result); + assert(compile_result == WASMER_OK); + + wasmer_byte_array *serialized_module = NULL; + wasmer_result_t serialize_result = wasmer_module_serialize(&serialized_module, module_one); + printf("Serialize result: %d\n", serialize_result); + printf("Serialized module pointer: %p\n", serialized_module->bytes); + printf("Serialized module length: %d\n", serialized_module->bytes_len); + assert(serialize_result == WASMER_OK); + assert(serialized_module->bytes != NULL); + assert(serialized_module->bytes_len > 8); + assert(serialized_module->bytes[0] == 'W'); + assert(serialized_module->bytes[1] == 'A'); + assert(serialized_module->bytes[2] == 'S'); + assert(serialized_module->bytes[3] == 'M'); + assert(serialized_module->bytes[4] == 'E'); + assert(serialized_module->bytes[5] == 'R'); + + wasmer_module_t *module_two = NULL; + wasmer_result_t unserialize_result = wasmer_module_deserialize(&module_two, serialized_module->bytes, serialized_module->bytes_len); + assert(unserialize_result == WASMER_OK); + + wasmer_import_t imports[] = {}; + wasmer_instance_t *instance = NULL; + wasmer_result_t instantiate_result = wasmer_module_instantiate(module_two, &instance, imports, 0); + printf("Instantiate result: %d\n", compile_result); + assert(instantiate_result == WASMER_OK); + + wasmer_value_t param_one; + param_one.tag = WASM_I32; + param_one.value.I32 = 7; + wasmer_value_t param_two; + param_two.tag = WASM_I32; + param_two.value.I32 = 8; + wasmer_value_t params[] = {param_one, param_two}; + + wasmer_value_t result_one; + wasmer_value_t results[] = {result_one}; + + wasmer_result_t call_result = wasmer_instance_call(instance, "sum", params, 2, results, 1); + printf("Call result: %d\n", call_result); + printf("Result: %d\n", results[0].value.I32); + assert(results[0].value.I32 == 15); + assert(call_result == WASMER_OK); + + printf("Destroy the serialized module\n"); + free((uint8_t *) serialized_module->bytes); + free(serialized_module); + + printf("Destroy instance\n"); + wasmer_instance_destroy(instance); + + printf("Destroy modules\n"); + wasmer_module_destroy(module_one); + wasmer_module_destroy(module_two); + return 0; +}