c-template
safe_mem_test.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <stddef.h>
4 #include <setjmp.h>
5 #include <cmocka.h>
6 #include <assert.h>
7 #include <stdbool.h>
8 #include "../../include/utils/safe_mem.h"
9 
10 typedef struct test_data {
11  char c[100];
12  bool b[100];
13 } test_data;
14 
15 // -Werror seems to think that assert(....) doesnt use the variable so thats why we ignored unused but set
16 #pragma GCC diagnostic ignored "-Wunused-parameter"
17 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
18 void test_new_memory_object_null(void **state) {
19  memory_object mobj = new_memory_object(NULL);
20  assert_false(mobj.freed);
21  assert_null(mobj.data);
22  // if we call free_memory_object_data within assert() then valgrind detects that the memory isn't actually freed
23  int return_code = free_memory_object_data(&mobj);
24  assert(return_code == 0);
25  return_code = free_memory_object_data(&mobj);
26  assert(return_code == -1);
27  assert(mobj.freed == true);
28  assert(mobj.data == NULL);
29  // this is causing a failure when built without debugging symbols
30  // assert_true(mobj.freed);
31  // assert_null(mobj.data);
32 }
33 
34 // -Werror seems to think that assert(....) doesnt use the variable so thats why we ignored unused but set
35 #pragma GCC diagnostic ignored "-Wunused-parameter"
36 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
38  test_data *td = (test_data *)malloc(sizeof(test_data));
39  td->c[0] = '\n';
41  td = mobj.data;
42  assert_false(mobj.freed);
43  assert_non_null(mobj.data);
44  assert(td->c[0] == '\n');
45  // if we call free_memory_object_data within assert() then valgrind detects that the memory isn't actually freed
46  int return_code = free_memory_object_data(&mobj);
47  assert(return_code == 0);
48  return_code = free_memory_object_data(&mobj);
49  assert(return_code == -1);
50  assert(mobj.freed == true);
51  assert(mobj.data == NULL);
52  // this is causing a failure when built without debugging symbols
53  // assert_true(mobj.freed);
54  // assert_null(mobj.data);
55 }
56 
57 int main(void) {
58  const struct CMUnitTest tests[] = {
59  cmocka_unit_test(test_new_memory_object_null),
60  cmocka_unit_test(test_new_memory_object_test_data),
61  };
62  return cmocka_run_group_tests(tests, NULL, NULL);
63 }
main
int main(void)
Definition: safe_mem_test.c:57
test_new_memory_object_test_data
void test_new_memory_object_test_data(void **state)
Definition: safe_mem_test.c:37
memory_object
Definition: safe_mem.h:9
memory_object::freed
bool freed
Definition: safe_mem.h:11
free_memory_object_data
int free_memory_object_data(memory_object *obj)
Definition: safe_mem.c:11
test_new_memory_object_null
void test_new_memory_object_null(void **state)
Definition: safe_mem_test.c:18
test_data::c
char c[100]
Definition: safe_mem_test.c:11
memory_object::data
void * data
Definition: safe_mem.h:10
new_memory_object
memory_object new_memory_object(void *input)
Definition: safe_mem.c:30
test_data
struct test_data test_data
test_data::b
bool b[100]
Definition: safe_mem_test.c:12
test_data
Definition: safe_mem_test.c:10