Blame nss/tst-nss-test5.c

Packit 6c4009
/* Test error checking for passwd entries.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <nss.h>
Packit 6c4009
#include <pwd.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
Packit 6c4009
#include "nss_test.h"
Packit 6c4009
Packit 6c4009
/* The specific values and names used here are arbitrary, other than
Packit 6c4009
   correspondence (with suitable differences according to the tests as
Packit 6c4009
   commented) between the given and expected entries.  */
Packit 6c4009
Packit 6c4009
static struct passwd pwd_table[] = {
Packit 6c4009
  PWD (100),  /* baseline, matches */
Packit 6c4009
  PWD (300),  /* wrong name and uid */
Packit 6c4009
  PWD_N (200, NULL), /* missing name */
Packit 6c4009
  PWD (60), /* unexpected name */
Packit 6c4009
  { .pw_name = (char *)"name20000",  .pw_passwd = (char *) "*", .pw_uid = 20000,  \
Packit 6c4009
    .pw_gid = 200, .pw_gecos = (char *) "*", .pw_dir = (char *) "*",	\
Packit 6c4009
    .pw_shell = (char *) "*" }, /* wrong gid */
Packit 6c4009
  { .pw_name = (char *)"name2",  .pw_passwd = (char *) "x", .pw_uid = 2,  \
Packit 6c4009
    .pw_gid = 2, .pw_gecos = (char *) "y", .pw_dir = (char *) "z",	\
Packit 6c4009
    .pw_shell = (char *) "*" }, /* spot check other text fields */
Packit 6c4009
  PWD_LAST ()
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static struct passwd exp_table[] = {
Packit 6c4009
  PWD (100),
Packit 6c4009
  PWD (30),
Packit 6c4009
  PWD (200),
Packit 6c4009
  PWD_N (60, NULL),
Packit 6c4009
  PWD (20000),
Packit 6c4009
  PWD (2),
Packit 6c4009
  PWD_LAST ()
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_nss_test1_init_hook(test_tables *t)
Packit 6c4009
{
Packit 6c4009
  t->pwd_table = pwd_table;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int retval = 0;
Packit 6c4009
  int i;
Packit 6c4009
  struct passwd *p;
Packit 6c4009
Packit 6c4009
  __nss_configure_lookup ("passwd", "test1");
Packit 6c4009
Packit 6c4009
  setpwent ();
Packit 6c4009
Packit 6c4009
  i = 0;
Packit 6c4009
  for (p = getpwent ();
Packit 6c4009
       p != NULL && ! PWD_ISLAST (& exp_table[i]);
Packit 6c4009
       ++i, p = getpwent ())
Packit 6c4009
    retval += compare_passwds (i, p, & exp_table[i]);
Packit 6c4009
Packit 6c4009
  endpwent ();
Packit 6c4009
Packit 6c4009
Packit 6c4009
  if (p)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: [?] passwd entry %u.%s unexpected\n", p->pw_uid, p->pw_name);
Packit 6c4009
      ++retval;
Packit 6c4009
    }
Packit 6c4009
  if (! PWD_ISLAST (& exp_table[i]))
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: [%d] passwd entry %u.%s missing\n", i,
Packit 6c4009
	      exp_table[i].pw_uid, exp_table[i].pw_name);
Packit 6c4009
      ++retval;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#define EXPECTED 9
Packit 6c4009
  if (retval == EXPECTED)
Packit 6c4009
    {
Packit 6c4009
      if (retval > 0)
Packit 6c4009
	printf ("PASS: Found %d expected errors\n", retval);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>