This source file includes following definitions.
- calloc_fails
- getpwnam_r_fails
- 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 calloc_fails(void **state)
22 {
23 uid_t uid;
24 gid_t gid;
25
26 pcmk__mock_calloc = true;
27
28 expect_value(__wrap_calloc, nmemb, 1);
29 expect_value(__wrap_calloc, size, PCMK__PW_BUFFER_LEN);
30 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -ENOMEM);
31
32 pcmk__mock_calloc = false;
33 }
34
35 static void
36 getpwnam_r_fails(void **state)
37 {
38 uid_t uid;
39 gid_t gid;
40
41
42 pcmk__mock_getpwnam_r = true;
43
44 expect_string(__wrap_getpwnam_r, name, "hauser");
45 expect_any(__wrap_getpwnam_r, pwd);
46 expect_any(__wrap_getpwnam_r, buf);
47 expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
48 expect_any(__wrap_getpwnam_r, result);
49 will_return(__wrap_getpwnam_r, EIO);
50 will_return(__wrap_getpwnam_r, NULL);
51
52 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EIO);
53
54 pcmk__mock_getpwnam_r = false;
55 }
56
57 static void
58 no_matching_pwent(void **state)
59 {
60 uid_t uid;
61 gid_t gid;
62
63
64 pcmk__mock_getpwnam_r = true;
65
66 expect_string(__wrap_getpwnam_r, name, "hauser");
67 expect_any(__wrap_getpwnam_r, pwd);
68 expect_any(__wrap_getpwnam_r, buf);
69 expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
70 expect_any(__wrap_getpwnam_r, result);
71 will_return(__wrap_getpwnam_r, 0);
72 will_return(__wrap_getpwnam_r, NULL);
73
74 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EINVAL);
75
76 pcmk__mock_getpwnam_r = false;
77 }
78
79 static void
80 entry_found(void **state)
81 {
82 uid_t uid;
83 gid_t gid;
84
85
86
87
88 struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
89
90
91
92
93 pcmk__mock_getpwnam_r = true;
94
95 expect_string(__wrap_getpwnam_r, name, "hauser");
96 expect_any(__wrap_getpwnam_r, pwd);
97 expect_any(__wrap_getpwnam_r, buf);
98 expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
99 expect_any(__wrap_getpwnam_r, result);
100 will_return(__wrap_getpwnam_r, 0);
101 will_return(__wrap_getpwnam_r, &returned_ent);
102
103 assert_int_equal(crm_user_lookup("hauser", NULL, NULL), 0);
104
105
106
107
108 expect_string(__wrap_getpwnam_r, name, "hauser");
109 expect_any(__wrap_getpwnam_r, pwd);
110 expect_any(__wrap_getpwnam_r, buf);
111 expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
112 expect_any(__wrap_getpwnam_r, result);
113 will_return(__wrap_getpwnam_r, 0);
114 will_return(__wrap_getpwnam_r, &returned_ent);
115
116 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), 0);
117 assert_int_equal(uid, 1000);
118 assert_int_equal(gid, 1000);
119
120 pcmk__mock_getpwnam_r = false;
121 }
122
123 PCMK__UNIT_TEST(NULL, NULL,
124 cmocka_unit_test(calloc_fails),
125 cmocka_unit_test(getpwnam_r_fails),
126 cmocka_unit_test(no_matching_pwent),
127 cmocka_unit_test(entry_found))