This commit is contained in:
tezc 2021-02-08 23:34:02 +03:00
parent 3a211cce08
commit 851a748323
4 changed files with 79 additions and 6 deletions

View File

@ -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")

View File

@ -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;
}

View File

@ -88,6 +88,9 @@ void test1(void)
fclose(fp);
sc_log_term();
sc_log_init();
sc_log_term();
}
#ifdef SC_HAVE_WRAP

View File

@ -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