Skip to content

Commit 25022c9

Browse files
authored
Merge pull request #273 from marcransome/test-config-alloc-failure
2 parents 29a498a + 6c691cb commit 25022c9

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

src/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <stdio.h>
2525

2626
#ifdef UNIT_TESTING
27-
#include "testing.h"
27+
#include "../test/testing.h"
2828
#endif
2929

3030
static const char *

src/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <unistd.h>
3333

3434
#ifdef UNIT_TESTING
35-
#include "testing.h"
35+
#include "../test/testing.h"
3636
#endif
3737

3838
const int subsystem_len = 257;

src/flog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "config.h"
3030

3131
#ifdef UNIT_TESTING
32-
#include "testing.h"
32+
#include "../test/testing.h"
3333
#endif
3434

3535
#define OS_LOG_FORMAT_PUBLIC "%{public}s"

test/test_config.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <setjmp.h>
2626
#include <cmocka.h>
2727
#include <string.h>
28+
#include <stdbool.h>
2829
#include <sys/syslimits.h>
2930
#include "config.h"
3031
#include "common.h"
@@ -78,6 +79,22 @@
7879

7980
#define UNUSED(x) (void)(x)
8081

82+
extern bool fail_calloc;
83+
84+
static int
85+
enable_calloc_failure(void **state) {
86+
UNUSED(state);
87+
fail_calloc = true;
88+
return 0;
89+
}
90+
91+
static int
92+
disable_calloc_failure(void **state) {
93+
UNUSED(state);
94+
fail_calloc = false;
95+
return 0;
96+
}
97+
8198
static void
8299
flog_config_new_with_null_arg_values_fails(void **state) {
83100
UNUSED(state);
@@ -99,6 +116,22 @@ test_new_config_with_no_error_ptr_calls_assert(void **state) {
99116
expect_assert_failure(flog_config_new(mock_argc, mock_argv, NULL));
100117
}
101118

119+
static void
120+
flog_config_new_alloc_fails(void **state) {
121+
UNUSED(state);
122+
123+
FlogError error = TEST_ERROR;
124+
MOCK_ARGS(
125+
TEST_PROGRAM_NAME,
126+
TEST_MESSAGE
127+
)
128+
129+
FlogConfig *config = flog_config_new(mock_argc, mock_argv, &error);
130+
131+
assert_null(config);
132+
assert_int_equal(error, FLOG_ERROR_ALLOC);
133+
}
134+
102135
static void
103136
flog_config_new_with_short_invalid_opt_fails(void **state) {
104137
UNUSED(state);
@@ -1348,6 +1381,7 @@ int main(void) {
13481381
cmocka_unit_test(flog_config_new_with_null_arg_values_fails),
13491382

13501383
// flog_config_new() failure tests
1384+
cmocka_unit_test_setup_teardown(flog_config_new_alloc_fails, enable_calloc_failure, disable_calloc_failure),
13511385
cmocka_unit_test(flog_config_new_with_short_invalid_opt_fails),
13521386
cmocka_unit_test(flog_config_new_with_long_invalid_opt_fails),
13531387
cmocka_unit_test(flog_config_new_with_short_category_opt_and_no_subsystem_opt_fails),

src/testing.h renamed to test/testing.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#ifndef TESTING_H
2+
#define TESTING_H
3+
14
#include <stdlib.h>
5+
#include <stdbool.h>
26

37
extern void mock_assert(const int result, const char * const expression,
48
const char * const file, const int line);
@@ -15,5 +19,18 @@ extern void _test_free(void * const ptr, const char *file, const int line);
1519

1620
#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
1721
#define realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__)
18-
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
22+
#define calloc(num, size) flog_test_calloc(num, size, __FILE__, __LINE__)
1923
#define free(ptr) _test_free(ptr, __FILE__, __LINE__)
24+
25+
bool fail_calloc = false;
26+
27+
void *
28+
flog_test_calloc(size_t count, size_t size, char *file, int line) {
29+
if (fail_calloc) {
30+
return NULL;
31+
} else {
32+
return _test_calloc(count, size, file, line);
33+
}
34+
}
35+
36+
#endif // TESTING_H

0 commit comments

Comments
 (0)