diff --git a/ini/CMakeLists.txt b/ini/CMakeLists.txt index 2fb2b44..3864e32 100644 --- a/ini/CMakeLists.txt +++ b/ini/CMakeLists.txt @@ -23,6 +23,15 @@ enable_testing() add_executable(${PROJECT_NAME}_test ini_test.c sc_ini.c) +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 -DSC_HAVE_WRAP) + target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin) + target_link_options(${PROJECT_NAME}_test PRIVATE -Wl,--wrap=ferror) + endif () +endif () + if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") diff --git a/ini/ini_test.c b/ini/ini_test.c index c824478..70e391b 100644 --- a/ini/ini_test.c +++ b/ini/ini_test.c @@ -491,6 +491,56 @@ void example(void) file_example(); } +#ifdef SC_HAVE_WRAP + +int fail_ferror = 0; + +int __real_ferror (FILE *stream); +int __wrap_ferror (FILE *stream) +{ + if (fail_ferror) { + return -1; + } + + return __real_ferror(stream); +} + +int cb_fail(void *arg, int line, const char *section, const char *key, + const char *value) +{ + (void) arg; + (void) line; + + printf("%s %s %s \n", section, key, value); + return 0; +} + +void test_fail() +{ + int rc; + FILE *fp; + static const char *ini = " ;Sample \n" + " [section] \n" + "key = value0 #;comment\n" + " value1 \n" + " value2 "; + + fp = fopen("config.ini", "w+"); + fwrite(ini, 1, strlen(ini), fp); + fclose(fp); + + fail_ferror = true; + rc = sc_ini_parse_file(NULL, cb_fail, "config.ini"); + assert(rc == -1); + fail_ferror = false; +} +#else +void test_fail() +{ + +} +#endif + int main() { example(); @@ -507,6 +557,7 @@ int main() test11(); test12(); test13(); + test_fail(); return 0; } diff --git a/logger/log_test.c b/logger/log_test.c index 20ad2d8..da2bc2a 100644 --- a/logger/log_test.c +++ b/logger/log_test.c @@ -88,6 +88,9 @@ void test1(void) fclose(fp); sc_log_term(); + + sc_log_init(); + sc_log_term(); } #ifdef SC_HAVE_WRAP diff --git a/sc/sc_test.c b/sc/sc_test.c index 730c960..a0368ca 100644 --- a/sc/sc_test.c +++ b/sc/sc_test.c @@ -163,7 +163,7 @@ void test_rand() #ifdef SC_HAVE_WRAP -bool fail_snprintf; +int fail_snprintf; int __wrap_snprintf(char *str, size_t size, const char *format, ...) { @@ -178,7 +178,7 @@ int __wrap_snprintf(char *str, size_t size, const char *format, ...) return rc; } - return -1; + return fail_snprintf; } void fail_test() @@ -186,15 +186,25 @@ void fail_test() char* t; char buf[32]; - fail_snprintf = true; + fail_snprintf = -1; t = sc_bytes_to_size(buf, sizeof(buf), 2 * 1024); assert(t == NULL); - fail_snprintf = false; + fail_snprintf = 0; - fail_snprintf = true; + fail_snprintf = -1; t = sc_bytes_to_size(buf, sizeof(buf), 313); assert(t == NULL); - fail_snprintf = false; + fail_snprintf = 0; + + fail_snprintf = 10000; + t = sc_bytes_to_size(buf, sizeof(buf), 2 * 1024); + assert(t == NULL); + fail_snprintf = 0; + + fail_snprintf = 10000; + t = sc_bytes_to_size(buf, sizeof(buf), 313); + assert(t == NULL); + fail_snprintf = 0; } #else