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

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

DEFINITIONS

This source file includes following definitions.
  1. calloc_fails
  2. getpwnam_r_fails
  3. no_matching_pwent
  4. 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 General Public License version 2
   7  * or later (GPLv2+) 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 calloc_fails(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  22 {
  23     uid_t uid;
  24     gid_t gid;
  25 
  26     pcmk__mock_calloc = true;   // calloc() will return NULL
  27 
  28     expect_value(__wrap_calloc, nmemb, 1);
  29     expect_value(__wrap_calloc, size, PCMK__PW_BUFFER_LEN);
  30     assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -ENOMEM);
  31 
  32     pcmk__mock_calloc = false;  // Use real calloc()
  33 }
  34 
  35 static void
  36 getpwnam_r_fails(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38     uid_t uid;
  39     gid_t gid;
  40 
  41     // Set getpwnam_r() return value and result parameter
  42     pcmk__mock_getpwnam_r = true;
  43 
  44     expect_string(__wrap_getpwnam_r, name, "hauser");
  45     expect_any(__wrap_getpwnam_r, pwd);
  46     expect_any(__wrap_getpwnam_r, buf);
  47     expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
  48     expect_any(__wrap_getpwnam_r, result);
  49     will_return(__wrap_getpwnam_r, EIO);
  50     will_return(__wrap_getpwnam_r, NULL);
  51 
  52     assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EIO);
  53 
  54     pcmk__mock_getpwnam_r = false;
  55 }
  56 
  57 static void
  58 no_matching_pwent(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60     uid_t uid;
  61     gid_t gid;
  62 
  63     // Set getpwnam_r() return value and result parameter
  64     pcmk__mock_getpwnam_r = true;
  65 
  66     expect_string(__wrap_getpwnam_r, name, "hauser");
  67     expect_any(__wrap_getpwnam_r, pwd);
  68     expect_any(__wrap_getpwnam_r, buf);
  69     expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
  70     expect_any(__wrap_getpwnam_r, result);
  71     will_return(__wrap_getpwnam_r, 0);
  72     will_return(__wrap_getpwnam_r, NULL);
  73 
  74     assert_int_equal(crm_user_lookup("hauser", &uid, &gid), -EINVAL);
  75 
  76     pcmk__mock_getpwnam_r = false;
  77 }
  78 
  79 static void
  80 entry_found(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82     uid_t uid;
  83     gid_t gid;
  84 
  85     /* We don't care about any of the other fields of the password entry, so just
  86      * leave them blank.
  87      */
  88     struct passwd returned_ent = { .pw_uid = 1000, .pw_gid = 1000 };
  89 
  90     /* Test getpwnam_r returning a valid passwd entry, but we don't pass uid or gid. */
  91 
  92     // Set getpwnam_r() return value and result parameter
  93     pcmk__mock_getpwnam_r = true;
  94 
  95     expect_string(__wrap_getpwnam_r, name, "hauser");
  96     expect_any(__wrap_getpwnam_r, pwd);
  97     expect_any(__wrap_getpwnam_r, buf);
  98     expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
  99     expect_any(__wrap_getpwnam_r, result);
 100     will_return(__wrap_getpwnam_r, 0);
 101     will_return(__wrap_getpwnam_r, &returned_ent);
 102 
 103     assert_int_equal(crm_user_lookup("hauser", NULL, NULL), 0);
 104 
 105     /* Test getpwnam_r returning a valid passwd entry, and we do pass uid and gid. */
 106 
 107     // Set getpwnam_r() return value and result parameter
 108     expect_string(__wrap_getpwnam_r, name, "hauser");
 109     expect_any(__wrap_getpwnam_r, pwd);
 110     expect_any(__wrap_getpwnam_r, buf);
 111     expect_value(__wrap_getpwnam_r, buflen, PCMK__PW_BUFFER_LEN);
 112     expect_any(__wrap_getpwnam_r, result);
 113     will_return(__wrap_getpwnam_r, 0);
 114     will_return(__wrap_getpwnam_r, &returned_ent);
 115 
 116     assert_int_equal(crm_user_lookup("hauser", &uid, &gid), 0);
 117     assert_int_equal(uid, 1000);
 118     assert_int_equal(gid, 1000);
 119 
 120     pcmk__mock_getpwnam_r = false;
 121 }
 122 
 123 PCMK__UNIT_TEST(NULL, NULL,
 124                 cmocka_unit_test(calloc_fails),
 125                 cmocka_unit_test(getpwnam_r_fails),
 126                 cmocka_unit_test(no_matching_pwent),
 127                 cmocka_unit_test(entry_found))

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