This source file includes following definitions.
- no_matching_pwent
- entry_found
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <crm/common/unittest_internal.h>
13
14 #include "crmcommon_private.h"
15 #include "mock_private.h"
16
17 #include <pwd.h>
18 #include <sys/types.h>
19
20 static void
21 no_matching_pwent(void **state)
22 {
23 uid_t uid;
24 gid_t gid;
25
26
27 pcmk__mock_getpwnam_r = true;
28
29 expect_string(__wrap_getpwnam_r, name, "hacluster");
30 expect_any(__wrap_getpwnam_r, pwd);
31 expect_any(__wrap_getpwnam_r, buf);
32 expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
33 expect_any(__wrap_getpwnam_r, result);
34 will_return(__wrap_getpwnam_r, ENOENT);
35 will_return(__wrap_getpwnam_r, NULL);
36
37 assert_int_equal(pcmk_daemon_user(&uid, &gid), -ENOENT);
38
39 pcmk__mock_getpwnam_r = false;
40 }
41
42 static void
43 entry_found(void **state)
44 {
45 uid_t uid;
46 gid_t gid;
47
48
49
50
51 struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
52
53
54
55
56 pcmk__mock_getpwnam_r = true;
57
58 expect_string(__wrap_getpwnam_r, name, "hacluster");
59 expect_any(__wrap_getpwnam_r, pwd);
60 expect_any(__wrap_getpwnam_r, buf);
61 expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
62 expect_any(__wrap_getpwnam_r, result);
63 will_return(__wrap_getpwnam_r, 0);
64 will_return(__wrap_getpwnam_r, &returned_ent);
65
66 assert_int_equal(pcmk_daemon_user(NULL, NULL), 0);
67
68
69
70
71
72
73
74 assert_int_equal(pcmk_daemon_user(&uid, &gid), 0);
75 assert_int_equal(uid, 1000);
76 assert_int_equal(gid, 1000);
77
78 pcmk__mock_getpwnam_r = false;
79 }
80
81 PCMK__UNIT_TEST(NULL, NULL,
82 cmocka_unit_test(no_matching_pwent),
83 cmocka_unit_test(entry_found))