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. __wrap_calloc
  2. __wrap_getpwnam_r
  3. calloc_fails
  4. getpwnam_r_fails
  5. no_matching_pwent
  6. entry_found
  7. 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 void *
  22 __wrap_calloc(size_t nmemb, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  33 int
  34 __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen,
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  44 static void
  45 calloc_fails(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  58 static void
  59 getpwnam_r_fails(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  72 static void
  73 no_matching_pwent(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
  86 static void
  87 entry_found(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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 
 116 int main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 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 }

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