pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk__realloc_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2024 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
13 
14 #include "mock_private.h"
15 
16 static void
17 bad_size(void **state)
18 {
19  char *ptr = NULL;
20 
21  pcmk__assert_asserts(pcmk__realloc(ptr, 0));
22 }
23 
24 static void
25 realloc_fails(void **state)
26 {
27  char *ptr = NULL;
28 
30  {
31  pcmk__mock_realloc = true; // realloc() will return NULL
32  expect_any(__wrap_realloc, ptr);
33  expect_value(__wrap_realloc, size, 1000);
34  pcmk__realloc(ptr, 1000);
35  pcmk__mock_realloc = false; // Use real realloc()
36  }
37  );
38 }
39 
40 static void
41 realloc_succeeds(void **state)
42 {
43  char *ptr = NULL;
44 
45  /* We can't really test that the resulting pointer is the size we asked
46  * for - it might be larger if that's what the memory allocator decides
47  * to do. And anyway, testing realloc isn't really the point. All we
48  * want to do here is make sure the function works when given good input.
49  */
50 
51  /* Allocate new memory */
52  ptr = pcmk__realloc(ptr, 1000);
53  assert_non_null(ptr);
54 
55  /* Grow previously allocated memory */
56  ptr = pcmk__realloc(ptr, 2000);
57  assert_non_null(ptr);
58 
59  /* Shrink previously allocated memory */
60  ptr = pcmk__realloc(ptr, 500);
61  assert_non_null(ptr);
62 
63  free(ptr);
64 }
65 
66 PCMK__UNIT_TEST(NULL, NULL,
67  cmocka_unit_test(bad_size),
68  cmocka_unit_test(realloc_fails),
69  cmocka_unit_test(realloc_succeeds))
#define pcmk__assert_aborts(stmt)
uint32_t size
Definition: cpg.c:52
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
void * __wrap_realloc(void *ptr, size_t size)
Definition: mock.c:142
#define pcmk__assert_asserts(stmt)
bool pcmk__mock_realloc
Definition: mock.c:139