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. no_matching_pwent
  2. entry_found

   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 
  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 no_matching_pwent(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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))

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