root/lib/common/tests/utils/pcmk_daemon_user_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __wrap_getpwnam_r
  2. no_matching_pwent
  3. entry_found
  4. main

   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 
  21 int
  22 __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen,
     /* [previous][next][first][last][top][bottom][index][help] */
  23                   struct passwd **result)
  24 {
  25     *result = mock_ptr_type(struct passwd *);
  26     return 0;
  27 }
  28 
  29 static void
  30 no_matching_pwent(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  40 static void
  41 entry_found(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  64 int main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 }

/* [previous][next][first][last][top][bottom][index][help] */