pacemaker 2.1.4-dc6eb4362e
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 Lesser General Public License
7 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
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
21int
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
29static void
30no_matching_pwent(void **state)
31{
32 uid_t uid;
33 gid_t gid;
34
35 will_return(__wrap_getpwnam_r, NULL); // result parameter to getpwnam_r()
36
37 assert_int_equal(pcmk_daemon_user(&uid, &gid), -EINVAL);
38}
39
40static void
41entry_found(void **state)
42{
43 uid_t uid;
44 gid_t gid;
45
46 /* We don't care about any of the other fields of the password entry, so just
47 * leave them blank.
48 */
49 struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
50
51 /* Test getpwnam_r returning a valid passwd entry, but we don't pass uid or gid. */
52
53 will_return_always(__wrap_getpwnam_r, &returned_ent); // result parameter to getpwnam_r()
54
55 assert_int_equal(pcmk_daemon_user(NULL, NULL), 0);
56
57 /* Test getpwnam_r returning a valid passwd entry, and we do pass uid and gid. */
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
64int 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}
const char * name
Definition: cib.c:24
int pcmk_daemon_user(uid_t *uid, gid_t *gid)
Get user and group IDs of pacemaker daemon user.
Definition: utils.c:97
int main(int argc, char **argv)
int __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result)
pcmk__action_result_t result
Definition: pcmk_fence.c:34