diff --git a/src/testing.h b/src/testing.h index 06b43ac..c00aff5 100644 --- a/src/testing.h +++ b/src/testing.h @@ -1,4 +1,8 @@ +#ifndef TESTING_H +#define TESTING_H + #include +#include extern void mock_assert(const int result, const char * const expression, const char * const file, const int line); @@ -15,5 +19,18 @@ extern void _test_free(void * const ptr, const char *file, const int line); #define malloc(size) _test_malloc(size, __FILE__, __LINE__) #define realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__) -#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) +#define calloc(num, size) flog_test_calloc(num, size, __FILE__, __LINE__) #define free(ptr) _test_free(ptr, __FILE__, __LINE__) + +bool fail_calloc = false; + +void * +flog_test_calloc(size_t count, size_t size, char *file, int line) { + if (fail_calloc) { + return NULL; + } else { + return _test_calloc(count, size, file, line); + } +} + +#endif // TESTING_H diff --git a/test/test_config.c b/test/test_config.c index d947fb8..0fe91a3 100644 --- a/test/test_config.c +++ b/test/test_config.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "config.h" #include "common.h" @@ -78,6 +79,22 @@ #define UNUSED(x) (void)(x) +extern bool fail_calloc; + +static int +enable_calloc_failure(void **state) { + UNUSED(state); + fail_calloc = true; + return 0; +} + +static int +disable_calloc_failure(void **state) { + UNUSED(state); + fail_calloc = false; + return 0; +} + static void flog_config_new_with_null_arg_values_fails(void **state) { UNUSED(state); @@ -99,6 +116,20 @@ test_new_config_with_no_error_ptr_calls_assert(void **state) { expect_assert_failure(flog_config_new(mock_argc, mock_argv, NULL)); } +static void +flog_config_new_alloc_fails(void **state) { + FlogError error = TEST_ERROR; + MOCK_ARGS( + TEST_PROGRAM_NAME, + TEST_MESSAGE + ) + + FlogConfig *config = flog_config_new(mock_argc, mock_argv, &error); + + assert_null(config); + assert_int_equal(error, FLOG_ERROR_ALLOC); +} + static void flog_config_new_with_short_invalid_opt_fails(void **state) { UNUSED(state); @@ -1348,6 +1379,7 @@ int main(void) { cmocka_unit_test(flog_config_new_with_null_arg_values_fails), // flog_config_new() failure tests + cmocka_unit_test_setup_teardown(flog_config_new_alloc_fails, enable_calloc_failure, disable_calloc_failure), cmocka_unit_test(flog_config_new_with_short_invalid_opt_fails), cmocka_unit_test(flog_config_new_with_long_invalid_opt_fails), cmocka_unit_test(flog_config_new_with_short_category_opt_and_no_subsystem_opt_fails),