Add memory NULL tests.

This commit is contained in:
Andreas Schneider 2008-07-28 11:49:56 +02:00
parent bccadc7d67
commit 7143f20f95
5 changed files with 90 additions and 2 deletions

View file

@ -37,6 +37,10 @@ configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
macro_copy_file(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake)
if (MEM_NULL_TESTS)
add_definitions(-DCSYNC_MEM_NULL_TESTS)
endif (MEM_NULL_TESTS)
add_subdirectory(src)
add_subdirectory(modules)
add_subdirectory(client)
@ -44,6 +48,7 @@ add_subdirectory(config)
add_subdirectory(doc)
if (CHECK_FOUND AND UNIT_TESTING)
include(MacroAddCheckTest)
add_subdirectory(tests)
endif (CHECK_FOUND AND UNIT_TESTING)

View file

@ -1,2 +1,3 @@
option(WITH_LOG4C "Build csync without log4c" ON)
option(UNIT_TESTING "Build with unit tests" OFF)
option(MEM_NULL_TESTS "Enable NULL memory testing" OFF)

View file

@ -87,6 +87,9 @@ while test -n "$1"; do
*-verbose)
DOVERBOSE="1"
;;
*-memtest)
OPTIONS="${OPTIONS} -DMEM_NULL_TESTS=ON"
;;
*-libsuffix)
OPTIONS="${OPTIONS} -DLIB_SUFFIX=${ARG}"
shift

View file

@ -29,6 +29,13 @@ void *c_calloc(size_t count, size_t size) {
if (size == 0 || count == 0) {
return NULL;
}
#ifdef CSYNC_MEM_NULL_TESTS
if (getenv("CSYNC_NOMEMORY")) {
return NULL;
}
#endif /* CSYNC_MEM_NULL_TESTS */
#undef calloc
return calloc(count, size);
#define calloc(x,y) DO_NOT_CALL_CALLOC__USE_XCALLOC_INSTEAD
@ -44,6 +51,13 @@ void *c_malloc(size_t size) {
}
void *c_realloc(void *ptr, size_t size) {
#ifdef CSYNC_MEM_NULL_TESTS
if (getenv("CSYNC_NOMEMORY") == NULL) {
return NULL;
}
#endif /* CSYNC_MEM_NULL_TESTS */
#undef realloc
return realloc(ptr, size);
#define realloc(x,y) DO_NOT_CALL_REALLOC__USE_XREALLOC_INSTEAD

View file

@ -1,5 +1,6 @@
#include "support.h"
#include <stdlib.h>
#include <string.h>
#include "std/c_alloc.h"
@ -8,6 +9,16 @@ struct test_s {
int answer;
};
#ifdef CSYNC_MEM_NULL_TESTS
static void setup(void) {
setenv("CSYNC_NOMEMORY", "1", 1);
}
static void teardown(void) {
unsetenv("CSYNC_NOMEMORY");
}
#endif /* CSYNC_MEM_NULL_TESTS */
START_TEST (check_c_malloc)
{
struct test_s *p = NULL;
@ -30,21 +41,69 @@ END_TEST
START_TEST (check_c_strdup)
{
char *str = (char *) "test";
char *tdup;
const char *str = "test";
char *tdup = NULL;
tdup = c_strdup(str);
fail_unless(strcmp(tdup, str) == 0, NULL);
free(tdup);
}
END_TEST
START_TEST (check_c_strndup)
{
const char *str = "test";
char *tdup = NULL;
tdup = c_strndup(str, 3);
fail_unless(strncmp(tdup, "tes", 3) == 0, NULL);
free(tdup);
}
END_TEST
#ifdef CSYNC_MEM_NULL_TESTS
START_TEST (check_c_malloc_nomem)
{
struct test_s *p = NULL;
p = c_malloc(sizeof(struct test_s));
fail_unless(p == NULL, NULL);
}
END_TEST
START_TEST (check_c_strdup_nomem)
{
const char *str = "test";
char *tdup = NULL;
tdup = c_strdup(str);
fail_unless(tdup == NULL, NULL);
}
END_TEST
START_TEST (check_c_strndup_nomem)
{
const char *str = "test";
char *tdup = NULL;
tdup = c_strndup(str, 3);
fail_unless(tdup == NULL, NULL);
}
END_TEST
#endif /* CSYNC_MEM_NULL_TESTS */
static Suite *make_c_malloc_suite(void) {
Suite *s = suite_create("std:alloc:malloc");
create_case(s, "check_c_malloc", check_c_malloc);
create_case(s, "check_c_malloc_zero", check_c_malloc_zero);
#ifdef CSYNC_MEM_NULL_TESTS
create_case_fixture(s, "check_c_malloc_nomem", check_c_malloc_nomem, setup, teardown);
#endif
return s;
}
@ -52,6 +111,12 @@ static Suite *make_c_strdup_suite(void) {
Suite *s = suite_create("std:alloc:strdup");
create_case(s, "check_c_strdup", check_c_strdup);
create_case(s, "check_c_strndup", check_c_strndup);
#ifdef CSYNC_MEM_NULL_TESTS
create_case_fixture(s, "check_c_strdup_nomem", check_c_strdup_nomem, setup, teardown);
create_case_fixture(s, "check_c_strndup_nomem", check_c_strndup_nomem, setup, teardown);
#endif
return s;
}