pacemaker  2.1.9-49aab99839
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 
64  {
65  pcmk__mock_strdup = true; // strdup() will return NULL
66  expect_string(__wrap_strdup, s, "world");
67  pcmk__str_update(&str, "world");
68  pcmk__mock_strdup = false; // Use the real strdup()
69  }
70  );
71 
72  free(str);
73 }
74 
75 PCMK__UNIT_TEST(NULL, NULL,
76  cmocka_unit_test(update_null),
77  cmocka_unit_test(update_same),
78  cmocka_unit_test(update_different),
79  cmocka_unit_test(strdup_fails))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
External (OS/environmental) problem.
Definition: results.h:272
void pcmk__str_update(char **str, const char *value)
Definition: strings.c:1289
#define pcmk__assert_exits(rc, stmt)
bool pcmk__mock_strdup
Definition: mock.c:450
char * __wrap_strdup(const char *s)
Definition: mock.c:453