This source file includes following definitions.
- __wrap_getenv
- getenv_returns_invalid
- getenv_returns_valid
- main
1
2
3
4
5
6
7
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
28
29
30
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);
47 result = pcmk__get_tmpdir();
48 assert_string_equal(result, "/tmp");
49
50 will_return(__wrap_getenv, "");
51 result = pcmk__get_tmpdir();
52 assert_string_equal(result, "/tmp");
53
54 will_return(__wrap_getenv, "subpath");
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");
69 result = pcmk__get_tmpdir();
70 assert_string_equal(result, "/var/tmp");
71
72 will_return(__wrap_getenv, "/");
73 result = pcmk__get_tmpdir();
74 assert_string_equal(result, "/");
75
76 will_return(__wrap_getenv, "/tmp/abcd.1234");
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 }