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 9105fd4
Show file tree
Hide file tree
Showing 23 changed files with 861 additions and 83 deletions.
6 changes: 4 additions & 2 deletions native-schema-registry/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ set(AWS_COMMON_MEMALLOC aws_common_memalloc)
set(NATIVE_SCHEMA_REGISTRY_MODULE_NAME libnativeschemaregistry)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

include_directories("include")
include_directories(${LIB_NATIVE_SCHEMA_REGISTRY_PATH})

add_subdirectory("src")
include (CTest)
enable_testing()
add_subdirectory("test")
include_directories("include")
add_subdirectory("test")
2 changes: 1 addition & 1 deletion native-schema-registry/c/include/mutable_byte_array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MUTABLE_BYTE_ARRAY_H
#define MUTABLE_BYTE_ARRAY_H
#include <stdlib.h>
#include "../include/glue_schema_registry_error.h"
#include "glue_schema_registry_error.h"

//Integer.MAX_VALUE in Java
//This gives ~2.1Gb limit on a record.
Expand Down
2 changes: 1 addition & 1 deletion native-schema-registry/c/include/read_only_byte_array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef READ_ONLY_BYTE_ARRAY_H
#define READ_ONLY_BYTE_ARRAY_H
#include <stdlib.h>
#include "../include/glue_schema_registry_error.h"
#include "glue_schema_registry_error.h"

typedef struct read_only_byte_array {
unsigned char * data;
Expand Down
13 changes: 10 additions & 3 deletions native-schema-registry/c/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#Fix the include path in GraalVM generated header file.
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)
if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin)$")
list(APPEND TEST_COVERAGE "-fsanitize=address,undefined -fsanitize-recover=address")
else()
#TODO: Figure out equivalent options for Windows
list(APPEND TEST_COVERAGE "")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TEST_COVERAGE} -O2 -Wall")

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 Expand Up @@ -88,6 +93,7 @@ swig_link_libraries(
PUBLIC
${DATA_TYPES_MODULE_NAME}
${SERDE_MODULE_NAME}
${AWS_COMMON_MEMALLOC}
${NATIVE_SCHEMA_REGISTRY_MODULE_NAME}
)
##TODO: Fix this during release. We should segregate Debug and Release
Expand All @@ -98,6 +104,7 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${CSHARP_MODULE_NAME}> ${CSHARP_GEN_LIB_PATH}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${DATA_TYPES_MODULE_NAME}> ${CSHARP_GEN_LIB_PATH}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${NATIVE_SCHEMA_REGISTRY_MODULE_NAME}> ${CSHARP_GEN_LIB_PATH}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${AWS_COMMON_MEMALLOC}> ${CSHARP_GEN_LIB_PATH}/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${SERDE_MODULE_NAME}> ${CSHARP_GEN_LIB_PATH}/
)

40 changes: 19 additions & 21 deletions native-schema-registry/c/src/glue_schema_registry_deserializer.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../include/glue_schema_registry_deserializer.h"
#include "../include/memory_allocator.h"
#include "../../target/libnativeschemaregistry.h"
#include "glue_schema_registry_deserializer.h"
#include "memory_allocator.h"
#include "libnativeschemaregistry.h"
#include <stdlib.h>

glue_schema_registry_deserializer * new_glue_schema_registry_deserializer(glue_schema_registry_error **p_err) {
Expand Down 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
16 changes: 8 additions & 8 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 "../include/memory_allocator.h"
#include "../include/glue_schema_registry_error.h"
#include <stdbool.h>
#include "memory_allocator.h"
#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,12 +29,11 @@ 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;
}

if (error->msg != NULL) {
aws_common_free(error->msg);
free(error->msg);
error->msg = NULL;
}
error->code = 0;
Expand Down
12 changes: 6 additions & 6 deletions native-schema-registry/c/src/glue_schema_registry_schema.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../include/glue_schema_registry_schema.h"
#include "../include/glue_schema_registry_error.h"
#include "../include/memory_allocator.h"
#include "glue_schema_registry_schema.h"
#include "glue_schema_registry_error.h"
#include "memory_allocator.h"
#include <string.h>

static int validate(const char *schema_name, const char *schema_def, const char *data_format) {
Expand Down Expand Up @@ -41,13 +41,13 @@ void delete_glue_schema_registry_schema(glue_schema_registry_schema * glueSchema
}

if (glueSchemaRegistrySchema->schema_name != NULL) {
aws_common_free(glueSchemaRegistrySchema->schema_name);
free(glueSchemaRegistrySchema->schema_name);
}
if (glueSchemaRegistrySchema->schema_def != NULL) {
aws_common_free(glueSchemaRegistrySchema->schema_def);
free(glueSchemaRegistrySchema->schema_def);
}
if (glueSchemaRegistrySchema->data_format != NULL) {
aws_common_free(glueSchemaRegistrySchema->data_format);
free(glueSchemaRegistrySchema->data_format);
}
aws_common_free(glueSchemaRegistrySchema);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../include/glue_schema_registry_serializer.h"
#include "../include/memory_allocator.h"
#include "../../target/libnativeschemaregistry.h"
#include "glue_schema_registry_serializer.h"
#include "memory_allocator.h"
#include "libnativeschemaregistry.h"
#include <stdlib.h>

glue_schema_registry_serializer *new_glue_schema_registry_serializer(glue_schema_registry_error **p_err) {
Expand Down
2 changes: 1 addition & 1 deletion native-schema-registry/c/src/memory_allocator.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/memory_allocator.h"
#include "memory_allocator.h"
#include "aws/common/allocator.h"

/*
Expand Down
6 changes: 3 additions & 3 deletions native-schema-registry/c/src/mutable_byte_array.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../include/mutable_byte_array.h"
#include "../include/glue_schema_registry_error.h"
#include "../include/memory_allocator.h"
#include "mutable_byte_array.h"
#include "glue_schema_registry_error.h"
#include "memory_allocator.h"
#include <stdio.h>

/**
Expand Down
4 changes: 2 additions & 2 deletions native-schema-registry/c/src/read_only_byte_array.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../include/read_only_byte_array.h"
#include "../include/memory_allocator.h"
#include "read_only_byte_array.h"
#include "memory_allocator.h"
#include <stdio.h>


Expand Down
41 changes: 35 additions & 6 deletions native-schema-registry/c/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
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")
if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin)$")
list(APPEND SANITIZER_CHECKS "-fsanitize=address,undefined -fsanitize-recover=address -fno-stack-protector")
else()
#TODO: Figure out equivalent options for Windows
list(APPEND SANITIZER_CHECKS "")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_CHECKS} -Wall")
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 +25,38 @@ list(
glue_schema_registry_error_test
)

foreach (test ${tests})
#TODO: These tests don't work on Windows and OSX due to CMake not linking the mock library.
#We have to investigate and fix these.
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
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:${AWS_COMMON_MEMALLOC}> ${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 9105fd4

Please sign in to comment.