Skip to content

Commit

Permalink
test (#53)
Browse files Browse the repository at this point in the history
* test
  • Loading branch information
tezc authored Feb 19, 2021
1 parent 2944a7c commit ab8abe4
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 124 deletions.
33 changes: 32 additions & 1 deletion buffer/buf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ size_t __wrap_strlen(const char *s)
}

bool fail_vsnprintf;
int fail_vsnprintf_value = -1;
int fail_vsnprintf_at = -1;
extern int __real_vsnprintf(char *str, size_t size, const char *format,
va_list ap);
Expand All @@ -289,7 +290,7 @@ int __wrap_vsnprintf(char *str, size_t size, const char *format, va_list ap)
return __real_vsnprintf(str, size, format, ap);
}

return -1;
return fail_vsnprintf_value;
}

void fail_test()
Expand Down Expand Up @@ -456,6 +457,36 @@ void fail_test()
fail_vsnprintf_at = -1;
fail_realloc = false;

sc_buf_init(&buf, 3);
fail_vsnprintf_at = 2;
fail_vsnprintf_value = 1000000;
sc_buf_put_text(&buf, "test");
assert(sc_buf_valid(&buf) == false);
sc_buf_term(&buf);

sc_buf_init(&buf, 3);
fail_vsnprintf_at = 2;
fail_vsnprintf_value = -1;
sc_buf_put_text(&buf, "test");
assert(sc_buf_valid(&buf) == false);
sc_buf_term(&buf);

sc_buf_init(&buf, 3);
fail_vsnprintf_at = 2;
fail_vsnprintf_value = 1000000;
sc_buf_put_fmt(&buf, "%s", "test");
assert(sc_buf_valid(&buf) == false);
sc_buf_term(&buf);

fail_vsnprintf_value = -1;
fail_vsnprintf_at = -1;
fail_realloc = false;

sc_buf_init(&buf, 3);
sc_buf_put_text(&buf, "test");
assert(sc_buf_valid(&buf) == true);
sc_buf_term(&buf);

}
#else
void fail_test()
Expand Down
2 changes: 1 addition & 1 deletion buffer/sc_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ void sc_buf_put_text(struct sc_buf *buf, const char *fmt, ...)
rc = vsnprintf((char *) sc_buf_wbuf(buf) - offset, quota, fmt, args);
va_end(args);

if (rc < 0 || (uint32_t) rc >= quota) {
if (rc < 0 || (uint32_t) rc >= sc_buf_quota(buf)) {
sc_buf_set_wpos(buf, 0);
buf->error = SC_BUF_OOM;
return;
Expand Down
2 changes: 1 addition & 1 deletion logger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if(SC_BUILD_TEST)
target_link_options(${PROJECT_NAME}_test PRIVATE
-Wl,--wrap=fprintf,--wrap=vfprintf,--wrap=fopen,--wrap=localtime_r
-Wl,--wrap=pthread_mutexattr_init,--wrap=pthread_mutex_init
-Wl,--wrap=fclose)
-Wl,--wrap=fclose,--wrap=ftell)
endif ()
endif ()

Expand Down
15 changes: 15 additions & 0 deletions logger/log_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ FILE *__wrap_fopen(const char *filename, const char *mode)
return NULL;
}

bool mock_ftell = false;
extern long int __real_ftell (FILE *stream);
extern long int __wrap_ftell (FILE *stream)
{
if (mock_ftell) {
return -1;
}

return __real_ftell(stream);
}

bool mock_fclose = false;
extern int __real_fclose (FILE *__stream);
int __wrap_fclose (FILE *__stream)
Expand Down Expand Up @@ -277,6 +288,10 @@ void fail_test(void)
mock_fopen = true;
assert(sc_log_set_file("prev.txt", "current.txt") == -1);
mock_fopen = false;
mock_ftell = true;
assert(sc_log_set_file("prev.txt", "current.txt") == -1);
mock_ftell = false;

assert(sc_log_set_file("prev.txt", "current.txt") == 0);
mock_localtime_r= true;
assert(sc_log_error("test") == -1);
Expand Down
144 changes: 74 additions & 70 deletions signal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

add_library(
sc_signal SHARED
sc_signal.c
sc_signal.h)
sc_signal SHARED
sc_signal.c
sc_signal.h)

target_include_directories(sc_signal PUBLIC ${CMAKE_CURRENT_LIST_DIR})

Expand All @@ -20,18 +20,18 @@ endif ()
# --------------------------------------------------------------------------- #
# --------------------- Test Configuration Start ---------------------------- #
# --------------------------------------------------------------------------- #
if(SC_BUILD_TEST)
if (SC_BUILD_TEST)

include(CTest)
include(CheckCCompilerFlag)
include(CTest)
include(CheckCCompilerFlag)

enable_testing()
enable_testing()

add_executable(${PROJECT_NAME}_test signal_test.c sc_signal.c)
add_executable(${PROJECT_NAME}_test signal_test.c sc_signal.c)

target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=1400000ul)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=1400000ul)

check_c_source_compiles("
check_c_source_compiles("
#include <execinfo.h>
#include <unistd.h>
Expand All @@ -42,80 +42,84 @@ if(SC_BUILD_TEST)
return 0;
}" HAVE_BACKTRACE)

FIND_LIBRARY(EXECINFO_LIBRARY NAMES execinfo)
IF (EXECINFO_LIBRARY)
SET(CMAKE_REQUIRED_LIBRARIES "${EXECINFO_LIBRARY}")
ENDIF(EXECINFO_LIBRARY)
FIND_LIBRARY(EXECINFO_LIBRARY NAMES execinfo)
IF (EXECINFO_LIBRARY)
SET(CMAKE_REQUIRED_LIBRARIES "${EXECINFO_LIBRARY}")
ENDIF (EXECINFO_LIBRARY)

if (${HAVE_BACKTRACE})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_BACKTRACE")
endif ()
if (${HAVE_BACKTRACE})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_BACKTRACE")
endif ()

if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")

target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-omit-frame-pointer)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP -DSC_SIGNAL_TEST)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin)
endif ()
endif ()
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-omit-frame-pointer)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP -DSC_SIGNAL_TEST)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin)
target_link_options(${PROJECT_NAME}_test PRIVATE -Wl,--wrap=signal)

if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
endif ()
endif ()

target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-omit-frame-pointer)
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")

if (SANITIZER)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER})
target_link_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER})
endif ()
endif ()
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-omit-frame-pointer)

if (SANITIZER)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER})
target_link_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER})
endif ()
endif ()

add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test)

SET(MEMORYCHECK_COMMAND_OPTIONS
"-q --log-fd=2 --trace-children=yes --track-origins=yes \
add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test)

SET(MEMORYCHECK_COMMAND_OPTIONS
"-q --log-fd=2 --trace-children=yes --track-origins=yes \
--leak-check=full --show-leak-kinds=all \
--error-exitcode=255")

add_custom_target(valgrind_${PROJECT_NAME} ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG>
--overwrite MemoryCheckCommandOptions=${MEMORYCHECK_COMMAND_OPTIONS}
--verbose -T memcheck WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_custom_target(check_${PROJECT_NAME} ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG> --verbose
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

# ----------------------- - Code Coverage Start ----------------------------- #

if (${CMAKE_BUILD_TYPE} MATCHES "Coverage")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(${PROJECT_NAME}_test PRIVATE --coverage)
target_link_libraries(${PROJECT_NAME}_test gcov)
else ()
message(FATAL_ERROR "Only GCC is supported for coverage")
endif ()
endif ()

add_custom_target(coverage_${PROJECT_NAME})
add_custom_command(
TARGET coverage_${PROJECT_NAME}
COMMAND lcov --capture --directory .
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --remove coverage.info '/usr/*' '*example*' '*test*'
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --list coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
)

add_dependencies(coverage_${PROJECT_NAME} check_${PROJECT_NAME})
add_custom_target(valgrind_${PROJECT_NAME} ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG>
--overwrite MemoryCheckCommandOptions=${MEMORYCHECK_COMMAND_OPTIONS}
--verbose -T memcheck WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_custom_target(check_${PROJECT_NAME} ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG> --verbose
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

# ------------------------- Code Coverage Start ----------------------------- #

if (${CMAKE_BUILD_TYPE} MATCHES "Coverage")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(${PROJECT_NAME}_test PRIVATE --coverage)
target_link_libraries(${PROJECT_NAME}_test gcov)
else ()
message(FATAL_ERROR "Only GCC is supported for coverage")
endif ()
endif ()

add_custom_target(coverage_${PROJECT_NAME})
add_custom_command(
TARGET coverage_${PROJECT_NAME}
COMMAND lcov --capture --directory .
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --remove coverage.info '/usr/*' '*example*' '*test*'
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --list coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
)

add_dependencies(coverage_${PROJECT_NAME} check_${PROJECT_NAME})

# -------------------------- Code Coverage End ------------------------------ #

endif ()

# ----------------------- Test Configuration End ---------------------------- #

Loading

0 comments on commit ab8abe4

Please sign in to comment.