(root)/
glibc-2.38/
nss/
tst-nss-test5.c
       1  /* Test error checking for passwd entries.
       2     Copyright (C) 2017-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <nss.h>
      20  #include <pwd.h>
      21  #include <stdio.h>
      22  #include <stdlib.h>
      23  #include <string.h>
      24  
      25  #include <support/support.h>
      26  
      27  #include "nss_test.h"
      28  
      29  /* The specific values and names used here are arbitrary, other than
      30     correspondence (with suitable differences according to the tests as
      31     commented) between the given and expected entries.  */
      32  
      33  static struct passwd pwd_table[] = {
      34    PWD (100),  /* baseline, matches */
      35    PWD (300),  /* wrong name and uid */
      36    PWD_N (200, NULL), /* missing name */
      37    PWD (60), /* unexpected name */
      38    { .pw_name = (char *)"name20000",  .pw_passwd = (char *) "*", .pw_uid = 20000,  \
      39      .pw_gid = 200, .pw_gecos = (char *) "*", .pw_dir = (char *) "*",	\
      40      .pw_shell = (char *) "*" }, /* wrong gid */
      41    { .pw_name = (char *)"name2",  .pw_passwd = (char *) "x", .pw_uid = 2,  \
      42      .pw_gid = 2, .pw_gecos = (char *) "y", .pw_dir = (char *) "z",	\
      43      .pw_shell = (char *) "*" }, /* spot check other text fields */
      44    PWD_LAST ()
      45  };
      46  
      47  static struct passwd exp_table[] = {
      48    PWD (100),
      49    PWD (30),
      50    PWD (200),
      51    PWD_N (60, NULL),
      52    PWD (20000),
      53    PWD (2),
      54    PWD_LAST ()
      55  };
      56  
      57  void
      58  _nss_test1_init_hook(test_tables *t)
      59  {
      60    t->pwd_table = pwd_table;
      61  }
      62  
      63  static int
      64  do_test (void)
      65  {
      66    int retval = 0;
      67    int i;
      68    struct passwd *p;
      69  
      70    __nss_configure_lookup ("passwd", "test1");
      71  
      72    setpwent ();
      73  
      74    i = 0;
      75    for (p = getpwent ();
      76         p != NULL && ! PWD_ISLAST (& exp_table[i]);
      77         ++i, p = getpwent ())
      78      retval += compare_passwds (i, p, & exp_table[i]);
      79  
      80    endpwent ();
      81  
      82  
      83    if (p)
      84      {
      85        printf ("FAIL: [?] passwd entry %u.%s unexpected\n", p->pw_uid, p->pw_name);
      86        ++retval;
      87      }
      88    if (! PWD_ISLAST (& exp_table[i]))
      89      {
      90        printf ("FAIL: [%d] passwd entry %u.%s missing\n", i,
      91  	      exp_table[i].pw_uid, exp_table[i].pw_name);
      92        ++retval;
      93      }
      94  
      95  #define EXPECTED 9
      96    if (retval == EXPECTED)
      97      {
      98        if (retval > 0)
      99  	printf ("PASS: Found %d expected errors\n", retval);
     100        return 0;
     101      }
     102    else
     103      {
     104        printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
     105        return 1;
     106      }
     107  }
     108  
     109  #include <support/test-driver.c>