Skip to content

Commit

Permalink
Add mocked tests for serializer and de-serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktooth committed Jul 13, 2022
1 parent 4c477b0 commit f1f3f6a
Show file tree
Hide file tree
Showing 12 changed files with 806 additions and 45 deletions.
2 changes: 1 addition & 1 deletion native-schema-registry/c/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
execute_process(COMMAND sed -ie "s/<graal_isolate.h>/\"graal_isolate.h\"/" ${LIB_NATIVE_SCHEMA_REGISTRY_PATH}/libnativeschemaregistry.h)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftest-coverage -fprofile-arcs -ggdb3 -O2 -Wall")
include(cmake/FetchAwsCommon.cmake)

include_directories("include")
include(cmake/FetchAwsCommon.cmake)

add_library(${AWS_COMMON_MEMALLOC} SHARED memory_allocator.c)
target_link_libraries(${AWS_COMMON_MEMALLOC} ${AWS_C_COMMON})
Expand Down
34 changes: 16 additions & 18 deletions native-schema-registry/c/src/glue_schema_registry_deserializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,26 @@ void delete_glue_schema_registry_deserializer(glue_schema_registry_deserializer
aws_common_free(deserializer);
}

mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer * deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
static bool validate(
glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
if (deserializer == NULL || deserializer->instance_context == NULL) {
throw_error(p_err, "Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
return NULL;
return false;
}

if (array == NULL || array->len == 0) {
throw_error(p_err, "Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
return false;
}
return true;
}

mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer * deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
if (!validate(deserializer, array, p_err)) {
return NULL;
}

Expand All @@ -54,13 +64,7 @@ mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registr
glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer * deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
if (deserializer == NULL || deserializer->instance_context == NULL) {
throw_error(p_err, "Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
return NULL;
}

if (array == NULL || array->len == 0) {
throw_error(p_err, "Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
if (!validate(deserializer, array, p_err)) {
return NULL;
}

Expand All @@ -71,13 +75,7 @@ glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(glu
bool glue_schema_registry_deserializer_can_decode(glue_schema_registry_deserializer * deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
if (deserializer == NULL || deserializer->instance_context == NULL) {
throw_error(p_err, "Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
return NULL;
}

if (array == NULL || array->len == 0) {
throw_error(p_err, "Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
if (!validate(deserializer, array, p_err)) {
return NULL;
}

Expand Down
10 changes: 5 additions & 5 deletions native-schema-registry/c/src/glue_schema_registry_error.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "../include/memory_allocator.h"
#include "../include/glue_schema_registry_error.h"

static int validate(const char *err_msg) {
static bool validate(const char *err_msg) {
if (err_msg == NULL) {
return 1;
return false;
}

return 0;
return true;
}

glue_schema_registry_error *new_glue_schema_registry_error(
const char *err_msg,
int err_code) {
if (validate(err_msg) != 0) {
if (!validate(err_msg)) {
log_warn("Error message cannot be null", ERR_CODE_NULL_PARAMETERS);
return NULL;
}
Expand All @@ -28,7 +29,6 @@ glue_schema_registry_error *new_glue_schema_registry_error(

void delete_glue_schema_registry_error(glue_schema_registry_error *error) {
if (error == NULL) {
log_warn("Error pointer passed is NULL", ERR_CODE_NULL_PARAMETERS);
return;
}

Expand Down
32 changes: 28 additions & 4 deletions native-schema-registry/c/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
list(APPEND sanitizer_checks "-fsanitize=address,undefined -fsanitize-recover=address -fno-stack-protector")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${sanitizer_checks} -fprofile-arcs -ftest-coverage -ggdb3 -O2 -Wall")
include_directories("include")
include(cmake/FetchCMocka.cmake)

set(TEST_HELPERS test_helpers)
add_library(${TEST_HELPERS} glue_schema_registry_test_helper.c)
target_link_libraries(${TEST_HELPERS} cmocka)

##GraalVM Mock
add_library(${NATIVE_SCHEMA_REGISTRY_MODULE_NAME} libnativeschemaregistry_mock.c)
target_link_libraries(${NATIVE_SCHEMA_REGISTRY_MODULE_NAME} cmocka)

list(
APPEND
tests
Expand All @@ -12,22 +21,37 @@ list(
glue_schema_registry_error_test
)

foreach (test ${tests})
#TODO: These tests don't work on OSX due to CMake not linking the mock library.
#We have to investigate and fix these.
if (NOT APPLE)
list(APPEND
tests
glue_schema_registry_serializer_test
glue_schema_registry_deserializer_test
)
ENDIF ()

foreach (test ${tests})
add_executable(
"${test}"
"${test}.c"
)
)
target_link_libraries(
"${test}"
"${DATA_TYPES_MODULE_NAME}"
"${SERDE_MODULE_NAME}"
"${NATIVE_SCHEMA_REGISTRY_MODULE_NAME}"
"${AWS_COMMON_MEMALLOC}"
"${TEST_HELPERS}"
"cmocka"
)

add_test(NAME "${test}" COMMAND "${test}" ${CMAKE_CURRENT_BINARY_DIR})
endforeach ()

add_custom_target(copy-libs-for-tests ALL
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${DATA_TYPES_MODULE_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:cmocka> ${CMAKE_CURRENT_BINARY_DIR}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${DATA_TYPES_MODULE_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${SERDE_MODULE_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:cmocka> ${CMAKE_CURRENT_BINARY_DIR}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${NATIVE_SCHEMA_REGISTRY_MODULE_NAME}> ${CMAKE_CURRENT_BINARY_DIR}/
)
Loading

0 comments on commit f1f3f6a

Please sign in to comment.