pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pcmk__str_update_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022 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 update_null(void **state) {
18  char *str = NULL;
19 
20  // These just make sure they don't crash
21  pcmk__str_update(NULL, NULL);
22  pcmk__str_update(NULL, "value");
23 
24  // Update an already NULL string to NULL
25  pcmk__str_update(&str, NULL);
26  assert_null(str);
27 
28  // Update an already allocated string to NULL
29  str = strdup("hello");
30  pcmk__str_update(&str, NULL);
31  assert_null(str);
32 }
33 
34 static void
35 update_same(void **state) {
36  char *str = NULL;
37  char *saved = NULL;
38 
39  str = strdup("hello");
40  saved = str;
41  pcmk__str_update(&str, "hello");
42  assert_ptr_equal(saved, str); // No free and reallocation
43  free(str);
44 }
45 
46 static void
47 update_different(void **state) {
48  char *str = NULL;
49 
50  str = strdup("hello");
51  pcmk__str_update(&str, "world");
52  assert_string_equal(str, "world");
53  free(str);
54 }
55 
56 static void
57 strdup_fails(void **state) {
58  char *str = NULL;
59 
60  str = strdup("hello");
61 
63  {
64  pcmk__mock_strdup = true; // strdup() will return NULL
65  expect_string(__wrap_strdup, s, "world");
66  pcmk__str_update(&str, "world");
67  pcmk__mock_strdup = false; // Use the real strdup()
68  }
69  );
70 
71  free(str);
72 }
73 
74 PCMK__UNIT_TEST(NULL, NULL,
75  cmocka_unit_test(update_null),
76  cmocka_unit_test(update_same),
77  cmocka_unit_test(update_different),
78  cmocka_unit_test(strdup_fails))
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(bad_input), cmocka_unit_test(not_found), cmocka_unit_test(find_attrB), cmocka_unit_test(find_attrA_matching))
void pcmk__str_update(char **str, const char *value)
Definition: strings.c:1193
#define pcmk__assert_asserts(stmt)
bool pcmk__mock_strdup
Definition: mock.c:403
char * __wrap_strdup(const char *s)
Definition: mock.c:406