Skip to content

Commit

Permalink
Add test for flog_config allocation failure
Browse files Browse the repository at this point in the history
  • Loading branch information
marcransome committed Dec 7, 2024
1 parent 29a498a commit b3191b9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/testing.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#ifndef TESTING_H
#define TESTING_H

#include <stdlib.h>
#include <stdbool.h>

extern void mock_assert(const int result, const char * const expression,
const char * const file, const int line);
Expand All @@ -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
32 changes: 32 additions & 0 deletions test/test_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
#include <stdbool.h>
#include <sys/syslimits.h>
#include "config.h"
#include "common.h"
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit b3191b9

Please sign in to comment.