This source file includes following definitions.
- __wrap_getpwnam_r
- 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 int
22 __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen,
23 struct passwd **result)
24 {
25 *result = mock_ptr_type(struct passwd *);
26 return 0;
27 }
28
29 static void
30 no_matching_pwent(void **state)
31 {
32 uid_t uid;
33 gid_t gid;
34
35 will_return(__wrap_getpwnam_r, NULL);
36
37 assert_int_equal(pcmk_daemon_user(&uid, &gid), -EINVAL);
38 }
39
40 static void
41 entry_found(void **state)
42 {
43 uid_t uid;
44 gid_t gid;
45
46
47
48
49 struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
50
51
52
53 will_return_always(__wrap_getpwnam_r, &returned_ent);
54
55 assert_int_equal(pcmk_daemon_user(NULL, NULL), 0);
56
57
58
59 assert_int_equal(pcmk_daemon_user(&uid, &gid), 0);
60 assert_int_equal(uid, 1000);
61 assert_int_equal(gid, 1000);
62 }
63
64 int main(int argc, char **argv)
65 {
66 const struct CMUnitTest tests[] = {
67 cmocka_unit_test(no_matching_pwent),
68 cmocka_unit_test(entry_found),
69 };
70
71 cmocka_set_message_output(CM_OUTPUT_TAP);
72 return cmocka_run_group_tests(tests, NULL, NULL);
73 }