pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pcmk_daemon_user_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
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  // Set getpwnam_r() return value and result parameter
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  /* We don't care about any of the other fields of the password entry, so just
49  * leave them blank.
50  */
51  struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
52 
53  /* Test getpwnam_r returning a valid passwd entry, but we don't pass uid or gid. */
54 
55  // Set getpwnam_r() return value and result parameter
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  /* Test getpwnam_r returning a valid passwd entry, and we do pass uid and gid. */
69 
70  /* We don't need to call will_return() again because pcmk_daemon_user()
71  * will have cached the uid/gid from the previous call and won't make
72  * another call to getpwnam_r().
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))
bool pcmk__mock_getpwnam_r
Definition: mock.c:329
const char * name
Definition: cib.c:26
int __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result)
Definition: mock.c:332
int pcmk_daemon_user(uid_t *uid, gid_t *gid)
Get user and group IDs of pacemaker daemon user.
Definition: utils.c:126
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(bad_input), cmocka_unit_test(not_found), cmocka_unit_test(find_attrB), cmocka_unit_test(find_attrA_matching))
pcmk__action_result_t result
Definition: pcmk_fence.c:35
#define PCMK__PW_BUFFER_LEN