This source file includes following definitions.
- __wrap_calloc
- __wrap_getpwnam_r
- calloc_fails
- getpwnam_r_fails
- no_matching_pwent
- entry_found
- main
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11 #include "mock_private.h"
12
13 #include <pwd.h>
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <setjmp.h>
18 #include <cmocka.h>
19 #include <sys/types.h>
20
21 void *
22 __wrap_calloc(size_t nmemb, size_t size)
23 {
24 int fail = mock_type(int);
25
26 if (fail) {
27 return mock_ptr_type(void *);
28 } else {
29 return __real_calloc(nmemb, size);
30 }
31 }
32
33 int
34 __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen,
35 struct passwd **result)
36 {
37 int retval = mock_type(int);
38
39 *result = mock_ptr_type(struct passwd *);
40
41 return retval;
42 }
43
44 static void
45 calloc_fails(void **state)
46 {
47 uid_t uid;
48 gid_t gid;
49
50
51
52 will_return(__wrap_calloc, 1);
53 will_return(__wrap_calloc, NULL);
54
55 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -ENOMEM);
56 }
57
58 static void
59 getpwnam_r_fails(void **state)
60 {
61 uid_t uid;
62 gid_t gid;
63
64 will_return_always(__wrap_calloc, 0);
65
66 will_return(__wrap_getpwnam_r, EIO);
67 will_return(__wrap_getpwnam_r, NULL);
68
69 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EIO);
70 }
71
72 static void
73 no_matching_pwent(void **state)
74 {
75 uid_t uid;
76 gid_t gid;
77
78 will_return_always(__wrap_calloc, 0);
79
80 will_return(__wrap_getpwnam_r, 0);
81 will_return(__wrap_getpwnam_r, NULL);
82
83 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EINVAL);
84 }
85
86 static void
87 entry_found(void **state)
88 {
89 uid_t uid;
90 gid_t gid;
91
92
93
94
95 struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
96
97 will_return_always(__wrap_calloc, 0);
98
99
100
101 will_return(__wrap_getpwnam_r, 0);
102 will_return(__wrap_getpwnam_r, &returned_ent);
103
104 assert_int_equal(crm_user_lookup("hauser", NULL, NULL), 0);
105
106
107
108 will_return(__wrap_getpwnam_r, 0);
109 will_return(__wrap_getpwnam_r, &returned_ent);
110
111 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), 0);
112 assert_int_equal(uid, 1000);
113 assert_int_equal(gid, 1000);
114 }
115
116 int main(int argc, char **argv)
117 {
118 const struct CMUnitTest tests[] = {
119 cmocka_unit_test(calloc_fails),
120 cmocka_unit_test(getpwnam_r_fails),
121 cmocka_unit_test(no_matching_pwent),
122 cmocka_unit_test(entry_found),
123 };
124
125 cmocka_set_message_output(CM_OUTPUT_TAP);
126 return cmocka_run_group_tests(tests, NULL, NULL);
127 }