pacemaker 2.1.4-dc6eb4362e
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
22static bool use_mocked = false;
23
24char *
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
39static void
40getenv_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
48 assert_string_equal(result, "/tmp");
49
50 will_return(__wrap_getenv, ""); // getenv("TMPDIR") return value
52 assert_string_equal(result, "/tmp");
53
54 will_return(__wrap_getenv, "subpath"); // getenv("TMPDIR") return value
56 assert_string_equal(result, "/tmp");
57
58 use_mocked = false;
59}
60
61static void
62getenv_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
70 assert_string_equal(result, "/var/tmp");
71
72 will_return(__wrap_getenv, "/"); // getenv("TMPDIR") return value
74 assert_string_equal(result, "/");
75
76 will_return(__wrap_getenv, "/tmp/abcd.1234"); // getenv("TMPDIR") return value
78 assert_string_equal(result, "/tmp/abcd.1234");
79
80 use_mocked = false;
81}
82
83int
84main(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}
const char * name
Definition: cib.c:24
const char * pcmk__get_tmpdir(void)
Definition: io.c:540
char * __real_getenv(const char *name)
int main(int argc, char **argv)
char * __wrap_getenv(const char *name)
pcmk__action_result_t result
Definition: pcmk_fence.c:34