Blame nss/tst-nss-test1.c

Packit 6c4009
/* Basic test of passwd database handling.
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
static int hook_called = 0;
Packit 6c4009
Packit 6c4009
/* Note: the values chosen here are arbitrary; they need only be
Packit 6c4009
   unique within the table.  However, they do need to match the
Packit 6c4009
   "pwdids" array further down.  */
Packit 6c4009
static struct passwd pwd_table[] = {
Packit 6c4009
    PWD (100),
Packit 6c4009
    PWD (30),
Packit 6c4009
    PWD (200),
Packit 6c4009
    PWD (60),
Packit 6c4009
    PWD (20000),
Packit 6c4009
    PWD_LAST ()
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_nss_test1_init_hook(test_tables *t)
Packit 6c4009
{
Packit 6c4009
  hook_called = 1;
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
Packit 6c4009
  __nss_configure_lookup ("passwd", "test1");
Packit 6c4009
Packit 6c4009
  /* This must match the pwd_table above.  */
Packit 6c4009
  static const unsigned int pwdids[] = { 100, 30, 200, 60, 20000 };
Packit 6c4009
#define npwdids (sizeof (pwdids) / sizeof (pwdids[0]))
Packit 6c4009
Packit 6c4009
  setpwent ();
Packit 6c4009
Packit 6c4009
  const unsigned int *np = pwdids;
Packit 6c4009
  for (struct passwd *p = getpwent (); p != NULL; ++np, p = getpwent ())
Packit 6c4009
    {
Packit 6c4009
      retval += compare_passwds (np-pwdids, p, & pwd_table[np-pwdids]);
Packit 6c4009
Packit 6c4009
      if (p->pw_uid != *np || strncmp (p->pw_name, "name", 4) != 0
Packit 6c4009
	  || atol (p->pw_name + 4) != *np)
Packit 6c4009
	{
Packit 6c4009
	  printf ("FAIL: passwd entry %td wrong (%s, %u)\n",
Packit 6c4009
		  np - pwdids, p->pw_name, p->pw_uid);
Packit 6c4009
	  retval = 1;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endpwent ();
Packit 6c4009
Packit 6c4009
  for (int i = npwdids - 1; i >= 0; --i)
Packit 6c4009
    {
Packit 6c4009
      char buf[30];
Packit 6c4009
      snprintf (buf, sizeof (buf), "name%u", pwdids[i]);
Packit 6c4009
Packit 6c4009
      struct passwd *p = getpwnam (buf);
Packit 6c4009
      if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
Packit 6c4009
	  retval = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      p = getpwuid (pwdids[i]);
Packit 6c4009
      if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("FAIL: passwd entry %u wrong\n", pwdids[i]);
Packit 6c4009
	  retval = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      snprintf (buf, sizeof (buf), "name%u", pwdids[i] + 1);
Packit 6c4009
Packit 6c4009
      p = getpwnam (buf);
Packit 6c4009
      if (p != NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
Packit 6c4009
	  retval = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      p = getpwuid (pwdids[i] + 1);
Packit 6c4009
      if (p != NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("FAIL: passwd entry %u wrong\n", pwdids[i] + 1);
Packit 6c4009
	  retval = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (!hook_called)
Packit 6c4009
    {
Packit 6c4009
      retval = 1;
Packit 6c4009
      printf("FAIL: init hook never called\n");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return retval;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>