Skip to content

Commit

Permalink
Merge pull request #273 from marcransome/test-config-alloc-failure
Browse files Browse the repository at this point in the history
  • Loading branch information
marcransome authored Dec 8, 2024
2 parents 29a498a + 6c691cb commit 25022c9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <stdio.h>

#ifdef UNIT_TESTING
#include "testing.h"
#include "../test/testing.h"
#endif

static const char *
Expand Down
2 changes: 1 addition & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <unistd.h>

#ifdef UNIT_TESTING
#include "testing.h"
#include "../test/testing.h"
#endif

const int subsystem_len = 257;
Expand Down
2 changes: 1 addition & 1 deletion src/flog.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "config.h"

#ifdef UNIT_TESTING
#include "testing.h"
#include "../test/testing.h"
#endif

#define OS_LOG_FORMAT_PUBLIC "%{public}s"
Expand Down
34 changes: 34 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,22 @@ 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) {
UNUSED(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 +1381,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
19 changes: 18 additions & 1 deletion src/testing.h → test/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

0 comments on commit 25022c9

Please sign in to comment.