pacemaker  2.1.2-ada5c3b36
Scalable High-Availability cluster resource manager
pcmk__get_tmpdir_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2021 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 Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 #include "mock_private.h"
12 
13 #include <stdarg.h>
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <setjmp.h>
20 #include <cmocka.h>
21 
22 static bool use_mocked = false;
23 
24 char *
25 __wrap_getenv(const char *name)
26 {
27  /* If coverage is enabled, something in "make check" will want to call
28  * the real getenv(), which will of course fail if the mocked version gets
29  * used. This bool needs to be set and unset around every unit test so
30  * the mocked version is only called in that time.
31  */
32  if (use_mocked) {
33  return mock_ptr_type(char *);
34  } else {
35  return __real_getenv(name);
36  }
37 }
38 
39 static void
40 getenv_returns_invalid(void **state)
41 {
42  const char *result;
43 
44  use_mocked = true;
45 
46  will_return(__wrap_getenv, NULL); // getenv("TMPDIR") return value
47  result = pcmk__get_tmpdir();
48  assert_string_equal(result, "/tmp");
49 
50  will_return(__wrap_getenv, ""); // getenv("TMPDIR") return value
51  result = pcmk__get_tmpdir();
52  assert_string_equal(result, "/tmp");
53 
54  will_return(__wrap_getenv, "subpath"); // getenv("TMPDIR") return value
55  result = pcmk__get_tmpdir();
56  assert_string_equal(result, "/tmp");
57 
58  use_mocked = false;
59 }
60 
61 static void
62 getenv_returns_valid(void **state)
63 {
64  const char *result;
65 
66  use_mocked = true;
67 
68  will_return(__wrap_getenv, "/var/tmp"); // getenv("TMPDIR") return value
69  result = pcmk__get_tmpdir();
70  assert_string_equal(result, "/var/tmp");
71 
72  will_return(__wrap_getenv, "/"); // getenv("TMPDIR") return value
73  result = pcmk__get_tmpdir();
74  assert_string_equal(result, "/");
75 
76  will_return(__wrap_getenv, "/tmp/abcd.1234"); // getenv("TMPDIR") return value
77  result = pcmk__get_tmpdir();
78  assert_string_equal(result, "/tmp/abcd.1234");
79 
80  use_mocked = false;
81 }
82 
83 int
84 main(int argc, char **argv)
85 {
86  const struct CMUnitTest tests[] = {
87  cmocka_unit_test(getenv_returns_invalid),
88  cmocka_unit_test(getenv_returns_valid),
89  };
90 
91  cmocka_set_message_output(CM_OUTPUT_TAP);
92  return cmocka_run_group_tests(tests, NULL, NULL);
93 }
char * __wrap_getenv(const char *name)
char * __real_getenv(const char *name)
const char * pcmk__get_tmpdir(void)
Definition: io.c:540
char * name
Definition: pcmk_fence.c:31
int main(int argc, char **argv)