pacemaker 2.1.4-dc6eb4362e
Scalable High-Availability cluster resource manager
crm_user_lookup_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
21void *
22__wrap_calloc(size_t nmemb, size_t size)
23{
24 int fail = mock_type(int);
25
26 if (fail) {
27 return mock_ptr_type(void *);
28 } else {
29 return __real_calloc(nmemb, size);
30 }
31}
32
33int
34__wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen,
35 struct passwd **result)
36{
37 int retval = mock_type(int);
38
39 *result = mock_ptr_type(struct passwd *);
40
41 return retval;
42}
43
44static void
45calloc_fails(void **state)
46{
47 uid_t uid;
48 gid_t gid;
49
50 /* Test calloc() returning NULL. */
51
52 will_return(__wrap_calloc, 1); // calloc() should fail
53 will_return(__wrap_calloc, NULL); // calloc() return value
54
55 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -ENOMEM);
56}
57
58static void
59getpwnam_r_fails(void **state)
60{
61 uid_t uid;
62 gid_t gid;
63
64 will_return_always(__wrap_calloc, 0); // calloc() should never fail
65
66 will_return(__wrap_getpwnam_r, EIO); // getpwnam_r() return value
67 will_return(__wrap_getpwnam_r, NULL); // result parameter to getpwnam_r()
68
69 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EIO);
70}
71
72static void
73no_matching_pwent(void **state)
74{
75 uid_t uid;
76 gid_t gid;
77
78 will_return_always(__wrap_calloc, 0); // calloc() should never fail
79
80 will_return(__wrap_getpwnam_r, 0); // getpwnam_r() return value
81 will_return(__wrap_getpwnam_r, NULL); // result parameter to getpwnam_r()
82
83 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EINVAL);
84}
85
86static void
87entry_found(void **state)
88{
89 uid_t uid;
90 gid_t gid;
91
92 /* We don't care about any of the other fields of the password entry, so just
93 * leave them blank.
94 */
95 struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
96
97 will_return_always(__wrap_calloc, 0); // calloc() should never fail
98
99 /* Test getpwnam_r returning a valid passwd entry, but we don't pass uid or gid. */
100
101 will_return(__wrap_getpwnam_r, 0); // getpwnam_r() return value
102 will_return(__wrap_getpwnam_r, &returned_ent); // result parameter to getpwnam_r()
103
104 assert_int_equal(crm_user_lookup("hauser", NULL, NULL), 0);
105
106 /* Test getpwnam_r returning a valid passwd entry, and we do pass uid and gid. */
107
108 will_return(__wrap_getpwnam_r, 0); // getpwnam_r() return value
109 will_return(__wrap_getpwnam_r, &returned_ent); // result parameter to getpwnam_r()
110
111 assert_int_equal(crm_user_lookup("hauser", &uid, &gid), 0);
112 assert_int_equal(uid, 1000);
113 assert_int_equal(gid, 1000);
114}
115
116int main(int argc, char **argv)
117{
118 const struct CMUnitTest tests[] = {
119 cmocka_unit_test(calloc_fails),
120 cmocka_unit_test(getpwnam_r_fails),
121 cmocka_unit_test(no_matching_pwent),
122 cmocka_unit_test(entry_found),
123 };
124
125 cmocka_set_message_output(CM_OUTPUT_TAP);
126 return cmocka_run_group_tests(tests, NULL, NULL);
127}
const char * name
Definition: cib.c:24
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:57
uint32_t size
Definition: cpg.c:4
int main(int argc, char **argv)
void * __wrap_calloc(size_t nmemb, size_t size)
int __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result)
void * __real_calloc(size_t nmemb, size_t size)
pcmk__action_result_t result
Definition: pcmk_fence.c:34