00db10
commit ae5c498d93d049d9574d3f8f18e62cac64cbdf5c
00db10
Author: DJ Delorie <dj@delorie.com>
00db10
Date:   Mon Jul 17 15:50:43 2017 -0400
00db10
00db10
    Extend NSS test suite
00db10
    
00db10
    * nss/nss_test.h: New.
00db10
    * nss/nss_test1.h: Rewrite to use test-provided data.  Add group
00db10
    tests.  Parameterize to allow multiple instances.
00db10
    * nss/nss_test2.h: New.  Second instance.
00db10
    * nss/nss_test.ver: New.
00db10
    * nss/nss_test1.c: Update to use new framework.
00db10
    * nss/nss_test2.c: New.
00db10
    * nss/nss_test3.c: New.
00db10
    * nss/nss_test4.c: New.
00db10
    * nss/nss_test5.c: New.
00db10
    * nss/Makefile: Build new tests.
00db10
    * shlib-versions: Add libnss_test2.
00db10
00db10
Moved nss/Makefile to separate patch to resolve conflicts. 
00db10
00db10
diff --git a/nss/nss_test.h b/nss/nss_test.h
00db10
new file mode 100644
00db10
index 0000000000..0a0e00b4e6
00db10
--- /dev/null
00db10
+++ b/nss/nss_test.h
00db10
@@ -0,0 +1,308 @@
00db10
+/* Common code for NSS test cases.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+
00db10
+/* There are two (or more) NSS test modules named nss_test1,
00db10
+   nss_test2, etc.  Each one will call a function IN THE TEST CASE
00db10
+   called _nss_test1_init_hook(test_tables *) (or _nss_test2_*, etc).
00db10
+
00db10
+   In your copy of the hook function, you may change the *_table
00db10
+   pointers in the passed struct to point to static tables in your
00db10
+   test case, and the test modules will use that table instead.
00db10
+
00db10
+   Your tables MUST end with an entry that has a *_LAST() macro.
00db10
+   Use the *_ISLAST() macro to test for end of list.
00db10
+
00db10
+   Use __nss_configure_lookup("passwd", "test1 test2") (for example) to
00db10
+   configure NSS to use the test modules.  */
00db10
+
00db10
+#include <pwd.h>
00db10
+#include <grp.h>
00db10
+
00db10
+typedef struct test_tables {
00db10
+  struct passwd *pwd_table;
00db10
+  struct group *grp_table;
00db10
+} test_tables;
00db10
+
00db10
+extern void _nss_test1_init_hook (test_tables *) __attribute__((weak));
00db10
+extern void _nss_test2_init_hook (test_tables *) __attribute__((weak));
00db10
+
00db10
+#define PWD_LAST()    { .pw_name = NULL, .pw_uid = 0 }
00db10
+#define GRP_LAST()    { .gr_name = NULL, .gr_gid = 0 }
00db10
+
00db10
+#define PWD_ISLAST(p)    ((p)->pw_name == NULL && (p)->pw_uid == 0)
00db10
+#define GRP_ISLAST(g)    ((g)->gr_name == NULL && (g)->gr_gid == 0)
00db10
+
00db10
+/* Macros to fill in the tables easily.  */
00db10
+
00db10
+/* Note that the "unparameterized" fields are not magic; they're just
00db10
+   arbitrary values.  Tests which need to verify those fields should
00db10
+   fill them in explicitly.  */
00db10
+
00db10
+#define PWD(u) \
00db10
+    { .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u,  \
00db10
+      .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*",	      \
00db10
+      .pw_shell = (char *) "*" }
00db10
+
00db10
+#define PWD_N(u,n)								\
00db10
+    { .pw_name = (char *) n, .pw_passwd = (char *) "*", .pw_uid = u,  \
00db10
+      .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*",	      \
00db10
+      .pw_shell = (char *) "*" }
00db10
+
00db10
+#define GRP(u) \
00db10
+    { .gr_name = (char *) "name" #u, .gr_passwd = (char *) "*", .gr_gid = u, \
00db10
+      .gr_mem = (char **) group_##u }
00db10
+
00db10
+#define GRP_N(u,n,m)						     \
00db10
+    { .gr_name = (char *) n, .gr_passwd = (char *) "*", .gr_gid = u, \
00db10
+      .gr_mem = (char **) m }
00db10
+
00db10
+/*------------------------------------------------------------*/
00db10
+
00db10
+/* Helper functions for testing passwd entries.  Call
00db10
+   compare_passwds() passing a test index, the passwd entry you got,
00db10
+   and the expected passwd entry.  The function will return the number
00db10
+   of mismatches, or zero of the two records are the same.  */
00db10
+
00db10
+static void __attribute__((used))
00db10
+print_passwd (struct passwd *p)
00db10
+{
00db10
+  printf ("    passwd %u.%s (%s) :", p->pw_uid, p->pw_name, p->pw_passwd);
00db10
+  printf (" %u, %s, %s, %s\n", p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
00db10
+  printf ("\n");
00db10
+}
00db10
+
00db10
+static int  __attribute__((used))
00db10
+compare_passwd_field (int i, struct passwd *p, const char *got,
00db10
+		      const char *exp, const char *name)
00db10
+{
00db10
+  /* Does the entry have a value?  */
00db10
+  if (got == NULL)
00db10
+    {
00db10
+      printf ("[%d] passwd %s for %u.%s was (null)\n",
00db10
+	      i, name,
00db10
+	      p->pw_uid, p->pw_name);
00db10
+      return 1;
00db10
+    }
00db10
+  /* Does the entry have an unexpected name?  */
00db10
+  else if (exp == NULL)
00db10
+    {
00db10
+      printf ("[%d] passwd %s for %u.(null) was %s\n",
00db10
+	      i, name,
00db10
+	      p->pw_uid, got);
00db10
+      return 1;
00db10
+    }
00db10
+  /* And is it correct?  */
00db10
+  else if (got && strcmp (got, exp) != 0)
00db10
+    {
00db10
+      printf("[%d] passwd entry %u.%s had %s \"%s\" (expected \"%s\") \n",
00db10
+	     i,
00db10
+	     p->pw_uid, p->pw_name, name,
00db10
+	     got, exp);
00db10
+      return 1;
00db10
+    }
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+#define COMPARE_PWD_FIELD(f) \
00db10
+  retval += compare_passwd_field (i, e, p->f, e->f, #f)
00db10
+
00db10
+/* Compare passwd to expected passwd, return number of "problems".
00db10
+   "I" is the index into the testcase data.  */
00db10
+static int  __attribute__((used))
00db10
+compare_passwds (int i, struct passwd *p, struct passwd *e)
00db10
+{
00db10
+  int retval = 0;
00db10
+
00db10
+  /* Did we get the expected uid?  */
00db10
+  if (p->pw_uid != e->pw_uid)
00db10
+    {
00db10
+      printf("[%d] passwd entry %u.%s had uid %u\n", i,
00db10
+	     e->pw_uid, e->pw_name,
00db10
+	     p->pw_uid);
00db10
+      ++retval;
00db10
+    }
00db10
+
00db10
+  /* Did we get the expected gid?  */
00db10
+  if (p->pw_gid != e->pw_gid)
00db10
+    {
00db10
+      printf("[%d] passwd entry %u.%s had gid %u (expected %u)\n", i,
00db10
+	     e->pw_uid, e->pw_name,
00db10
+	     p->pw_gid, e->pw_gid);
00db10
+      ++retval;
00db10
+    }
00db10
+
00db10
+  COMPARE_PWD_FIELD (pw_name);
00db10
+  COMPARE_PWD_FIELD (pw_passwd);
00db10
+  COMPARE_PWD_FIELD (pw_gecos);
00db10
+  COMPARE_PWD_FIELD (pw_dir);
00db10
+  COMPARE_PWD_FIELD (pw_shell);
00db10
+
00db10
+  if (retval > 0)
00db10
+    {
00db10
+      /* Left in for debugging later, if needed.  */
00db10
+      print_passwd (p);
00db10
+      print_passwd (e);
00db10
+    }
00db10
+
00db10
+  return retval;
00db10
+}
00db10
+
00db10
+/*------------------------------------------------------------*/
00db10
+
00db10
+/* Helpers for checking group entries.  See passwd helper comment
00db10
+   above for details.  */
00db10
+
00db10
+static void __attribute__((used))
00db10
+print_group (struct group *g)
00db10
+{
00db10
+  int j;
00db10
+
00db10
+  printf ("    group %u.%s (%s) :", g->gr_gid, g->gr_name, g->gr_passwd);
00db10
+  if (g->gr_mem)
00db10
+    for (j=0; g->gr_mem[j]; j++)
00db10
+      printf ("%s%s", j==0 ? " " : ", ", g->gr_mem[j]);
00db10
+  printf ("\n");
00db10
+}
00db10
+
00db10
+/* Compare group to expected group, return number of "problems".  "I"
00db10
+   is the index into the testcase data.  */
00db10
+static int  __attribute__((used))
00db10
+compare_groups (int i, struct group *g, struct group *e)
00db10
+{
00db10
+  int j;
00db10
+  int retval = 0;
00db10
+
00db10
+  /* Did we get the expected gid?  */
00db10
+  if (g->gr_gid != e->gr_gid)
00db10
+    {
00db10
+      printf("[%d] group entry %u.%s had gid %u\n", i,
00db10
+	     e->gr_gid, e->gr_name,
00db10
+	     g->gr_gid);
00db10
+      ++retval;
00db10
+    }
00db10
+
00db10
+  /* Does the entry have a name?  */
00db10
+  if (g->gr_name == NULL)
00db10
+    {
00db10
+      printf ("[%d] group name for %u.%s was (null)\n", i,
00db10
+	      e->gr_gid, e->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  /* Does the entry have an unexpected name?  */
00db10
+  else if (e->gr_name == NULL)
00db10
+    {
00db10
+      printf ("[%d] group name for %u.(null) was %s\n", i,
00db10
+	      e->gr_gid, g->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  /* And is it correct?  */
00db10
+  else if (strcmp (g->gr_name, e->gr_name) != 0)
00db10
+    {
00db10
+      printf("[%d] group entry %u.%s had name \"%s\"\n", i,
00db10
+	     e->gr_gid, e->gr_name,
00db10
+	     g->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+
00db10
+  /* Does the entry have a password?  */
00db10
+  if (g->gr_passwd == NULL && e->gr_passwd != NULL)
00db10
+    {
00db10
+      printf ("[%d] group password for %u.%s was NULL\n", i,
00db10
+	      e->gr_gid, e->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  else if (g->gr_passwd != NULL && e->gr_passwd == NULL)
00db10
+    {
00db10
+      printf ("[%d] group password for %u.%s was not NULL\n", i,
00db10
+	      e->gr_gid, e->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  /* And is it correct?  */
00db10
+  else if (g->gr_passwd && strcmp (g->gr_passwd, e->gr_passwd) != 0)
00db10
+    {
00db10
+      printf("[%d] group entry %u.%s had password \"%s\" (not \"%s\")\n", i,
00db10
+	     e->gr_gid, e->gr_name,
00db10
+	     g->gr_passwd, e->gr_passwd);
00db10
+      ++retval;
00db10
+    }
00db10
+
00db10
+  /* Now compare group members... */
00db10
+
00db10
+  if (e->gr_mem != NULL && g->gr_mem == NULL)
00db10
+    {
00db10
+      printf("[%d] group entry %u.%s missing member list\n", i,
00db10
+	     e->gr_gid, e->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  else if (e->gr_mem == NULL && g->gr_mem != NULL)
00db10
+    {
00db10
+      printf("[%d] group entry %u.%s has unexpected member list\n", i,
00db10
+	     e->gr_gid, e->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  else if (e->gr_mem == NULL && g->gr_mem == NULL)
00db10
+    {
00db10
+      /* This case is OK.  */
00db10
+    }
00db10
+  else
00db10
+    {
00db10
+      /* Compare two existing lists.  */
00db10
+      j = 0;
00db10
+      for (;;)
00db10
+	{
00db10
+	  if (g->gr_mem[j] == NULL && e->gr_mem[j] == NULL)
00db10
+	    {
00db10
+	      /* Matching end-of-lists.  */
00db10
+	      break;
00db10
+	    }
00db10
+	  if (g->gr_mem[j] == NULL)
00db10
+	    {
00db10
+	      printf ("[%d] group member list for %u.%s is too short.\n", i,
00db10
+		      e->gr_gid, e->gr_name);
00db10
+	      ++retval;
00db10
+	      break;
00db10
+	    }
00db10
+	  if (e->gr_mem[j] == NULL)
00db10
+	    {
00db10
+	      printf ("[%d] group member list for %u.%s is too long.\n", i,
00db10
+		      e->gr_gid, e->gr_name);
00db10
+	      ++retval;
00db10
+	      break;
00db10
+	    }
00db10
+	  if (strcmp (g->gr_mem[j], e->gr_mem[j]) != 0)
00db10
+	    {
00db10
+	      printf ("[%d] group member list for %u.%s differs: %s vs %s.\n", i,
00db10
+		      e->gr_gid, e->gr_name,
00db10
+		      e->gr_mem[j], g->gr_mem[j]);
00db10
+	      ++retval;
00db10
+	    }
00db10
+
00db10
+	  j++;
00db10
+	}
00db10
+    }
00db10
+
00db10
+  if (retval > 0)
00db10
+    {
00db10
+      /* Left in for debugging later, if needed.  */
00db10
+      print_group (g);
00db10
+      print_group (e);
00db10
+    }
00db10
+
00db10
+  return retval;
00db10
+}
00db10
diff --git a/nss/nss_test.ver b/nss/nss_test.ver
00db10
new file mode 100644
00db10
index 0000000000..2e21176b40
00db10
--- /dev/null
00db10
+++ b/nss/nss_test.ver
00db10
@@ -0,0 +1,4 @@
00db10
+{
00db10
+  _nss_test1_init_hook;
00db10
+  _nss_test2_init_hook;
00db10
+};
00db10
diff --git a/nss/nss_test1.c b/nss/nss_test1.c
00db10
index 3beb488fcf..b728e418a3 100644
00db10
--- a/nss/nss_test1.c
00db10
+++ b/nss/nss_test1.c
00db10
@@ -1,84 +1,168 @@
00db10
+/* Template generic NSS service provider.  See nss_test.h for usage.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
 #include <errno.h>
00db10
 #include <nss.h>
00db10
 #include <pthread.h>
00db10
 #include <string.h>
00db10
+#include <stdio.h>
00db10
+#include <alloc_buffer.h>
00db10
 
00db10
 
00db10
-#define COPY_IF_ROOM(s) \
00db10
-  ({ size_t len_ = strlen (s) + 1;		\
00db10
-     char *start_ = cp;				\
00db10
-     buflen - (cp - buffer) < len_		\
00db10
-     ? NULL					\
00db10
-     : (cp = mempcpy (cp, s, len_), start_); })
00db10
+/* We need to be able to handle NULLs "properly" within the testsuite,
00db10
+   to test known bad data.  */
00db10
+#define alloc_buffer_maybe_copy_string(b,s) s ? alloc_buffer_copy_string (b, s) : NULL;
00db10
 
00db10
+/* This file is the master template.  Other instances of this test
00db10
+   module should define NAME(x) to have their name instead of "test1",
00db10
+   then include this file.
00db10
+*/
00db10
+#define NAME_(x,n) _nss_##n##_##x
00db10
+#ifndef NAME
00db10
+#define NAME(x) NAME_(x,test1)
00db10
+#endif
00db10
+#define NAMESTR__(x) #x
00db10
+#define NAMESTR_(x) NAMESTR__(x)
00db10
+#define NAMESTR(x) NAMESTR_(NAME(x))
00db10
 
00db10
-/* Password handling.  */
00db10
-#include <pwd.h>
00db10
+#include "nss_test.h"
00db10
+
00db10
+/* -------------------------------------------------- */
00db10
+/* Default Data.  */
00db10
 
00db10
-static struct passwd pwd_data[] =
00db10
+static struct passwd default_pwd_data[] =
00db10
   {
00db10
 #define PWD(u) \
00db10
     { .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u,  \
00db10
       .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*",	      \
00db10
       .pw_shell = (char *) "*" }
00db10
-    PWD (100),
00db10
     PWD (30),
00db10
+    PWD (100),
00db10
     PWD (200),
00db10
     PWD (60),
00db10
     PWD (20000)
00db10
   };
00db10
-#define npwd_data (sizeof (pwd_data) / sizeof (pwd_data[0]))
00db10
+#define default_npwd_data (sizeof (pwd_data) / sizeof (pwd_data[0]))
00db10
+
00db10
+static struct passwd *pwd_data = default_pwd_data;
00db10
+static int npwd_data = default_npwd_data;
00db10
+
00db10
+static struct group *grp_data = NULL;
00db10
+static int ngrp_data = 0;
00db10
+
00db10
+/* This function will get called, and once per session, look back into
00db10
+   the test case's executable for an init hook function, and call
00db10
+   it.  */
00db10
+
00db10
+static int initted = 0;
00db10
+static void
00db10
+init(void)
00db10
+{
00db10
+  test_tables t;
00db10
+  int i;
00db10
+
00db10
+  if (initted)
00db10
+    return;
00db10
+  if (NAME(init_hook))
00db10
+    {
00db10
+      memset (&t, 0, sizeof(t));
00db10
+      NAME(init_hook)(&t);
00db10
+
00db10
+      if (t.pwd_table)
00db10
+	{
00db10
+	  pwd_data = t.pwd_table;
00db10
+	  for (i=0; ! PWD_ISLAST(& pwd_data[i]); i++)
00db10
+	    ;
00db10
+	  npwd_data = i;
00db10
+	}
00db10
+
00db10
+      if (t.grp_table)
00db10
+	{
00db10
+	  grp_data = t.grp_table;
00db10
+	  for (i=0; ! GRP_ISLAST(& grp_data[i]); i++)
00db10
+	    ;
00db10
+	  ngrp_data = i;
00db10
+	}
00db10
+    }
00db10
+  initted = 1;
00db10
+}
00db10
+
00db10
+/* -------------------------------------------------- */
00db10
+/* Password handling.  */
00db10
 
00db10
 static size_t pwd_iter;
00db10
 #define CURPWD pwd_data[pwd_iter]
00db10
 
00db10
 static pthread_mutex_t pwd_lock = PTHREAD_MUTEX_INITIALIZER;
00db10
 
00db10
-
00db10
 enum nss_status
00db10
-_nss_test1_setpwent (int stayopen)
00db10
+NAME(setpwent) (int stayopen)
00db10
 {
00db10
+  init();
00db10
   pwd_iter = 0;
00db10
   return NSS_STATUS_SUCCESS;
00db10
 }
00db10
 
00db10
 
00db10
 enum nss_status
00db10
-_nss_test1_endpwent (void)
00db10
+NAME(endpwent) (void)
00db10
 {
00db10
+  init();
00db10
   return NSS_STATUS_SUCCESS;
00db10
 }
00db10
 
00db10
+static enum nss_status
00db10
+copy_passwd (struct passwd *result, struct passwd *local,
00db10
+	    char *buffer, size_t buflen, int *errnop)
00db10
+{
00db10
+  struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
00db10
+
00db10
+  result->pw_name = alloc_buffer_maybe_copy_string (&buf, local->pw_name);
00db10
+  result->pw_passwd = alloc_buffer_maybe_copy_string (&buf, local->pw_passwd);
00db10
+  result->pw_uid = local->pw_uid;
00db10
+  result->pw_gid = local->pw_gid;
00db10
+  result->pw_gecos = alloc_buffer_maybe_copy_string (&buf, local->pw_gecos);
00db10
+  result->pw_dir = alloc_buffer_maybe_copy_string (&buf, local->pw_dir);
00db10
+  result->pw_shell = alloc_buffer_maybe_copy_string (&buf, local->pw_shell);
00db10
+
00db10
+  if (alloc_buffer_has_failed (&buf))
00db10
+    {
00db10
+      *errnop = ERANGE;
00db10
+      return NSS_STATUS_TRYAGAIN;
00db10
+    }
00db10
+
00db10
+  return NSS_STATUS_SUCCESS;
00db10
+}
00db10
 
00db10
 enum nss_status
00db10
-_nss_test1_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
00db10
+NAME(getpwent_r) (struct passwd *result, char *buffer, size_t buflen,
00db10
 		       int *errnop)
00db10
 {
00db10
-  char *cp = buffer;
00db10
   int res = NSS_STATUS_SUCCESS;
00db10
 
00db10
+  init();
00db10
   pthread_mutex_lock (&pwd_lock);
00db10
 
00db10
   if (pwd_iter >= npwd_data)
00db10
     res = NSS_STATUS_NOTFOUND;
00db10
   else
00db10
     {
00db10
-      result->pw_name = COPY_IF_ROOM (CURPWD.pw_name);
00db10
-      result->pw_passwd = COPY_IF_ROOM (CURPWD.pw_passwd);
00db10
-      result->pw_uid = CURPWD.pw_uid;
00db10
-      result->pw_gid = CURPWD.pw_gid;
00db10
-      result->pw_gecos = COPY_IF_ROOM (CURPWD.pw_gecos);
00db10
-      result->pw_dir = COPY_IF_ROOM (CURPWD.pw_dir);
00db10
-      result->pw_shell = COPY_IF_ROOM (CURPWD.pw_shell);
00db10
-
00db10
-      if (result->pw_name == NULL || result->pw_passwd == NULL
00db10
-	  || result->pw_gecos == NULL || result->pw_dir == NULL
00db10
-	  || result->pw_shell == NULL)
00db10
-	{
00db10
-	  *errnop = ERANGE;
00db10
-	  res = NSS_STATUS_TRYAGAIN;
00db10
-	}
00db10
-
00db10
+      res = copy_passwd (result, &CURPWD, buffer, buflen, errnop);
00db10
       ++pwd_iter;
00db10
     }
00db10
 
00db10
@@ -89,65 +173,140 @@ _nss_test1_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
00db10
 
00db10
 
00db10
 enum nss_status
00db10
-_nss_test1_getpwuid_r (uid_t uid, struct passwd *result, char *buffer,
00db10
+NAME(getpwuid_r) (uid_t uid, struct passwd *result, char *buffer,
00db10
 		       size_t buflen, int *errnop)
00db10
 {
00db10
+  init();
00db10
   for (size_t idx = 0; idx < npwd_data; ++idx)
00db10
     if (pwd_data[idx].pw_uid == uid)
00db10
-      {
00db10
-	char *cp = buffer;
00db10
-	int res = NSS_STATUS_SUCCESS;
00db10
-
00db10
-	result->pw_name = COPY_IF_ROOM (pwd_data[idx].pw_name);
00db10
-	result->pw_passwd = COPY_IF_ROOM (pwd_data[idx].pw_passwd);
00db10
-	result->pw_uid = pwd_data[idx].pw_uid;
00db10
-	result->pw_gid = pwd_data[idx].pw_gid;
00db10
-	result->pw_gecos = COPY_IF_ROOM (pwd_data[idx].pw_gecos);
00db10
-	result->pw_dir = COPY_IF_ROOM (pwd_data[idx].pw_dir);
00db10
-	result->pw_shell = COPY_IF_ROOM (pwd_data[idx].pw_shell);
00db10
-
00db10
-	if (result->pw_name == NULL || result->pw_passwd == NULL
00db10
-	    || result->pw_gecos == NULL || result->pw_dir == NULL
00db10
-	    || result->pw_shell == NULL)
00db10
-	  {
00db10
-	    *errnop = ERANGE;
00db10
-	    res = NSS_STATUS_TRYAGAIN;
00db10
-	  }
00db10
-
00db10
-	return res;
00db10
-      }
00db10
+      return copy_passwd (result, &pwd_data[idx], buffer, buflen, errnop);
00db10
 
00db10
   return NSS_STATUS_NOTFOUND;
00db10
 }
00db10
 
00db10
 
00db10
 enum nss_status
00db10
-_nss_test1_getpwnam_r (const char *name, struct passwd *result, char *buffer,
00db10
+NAME(getpwnam_r) (const char *name, struct passwd *result, char *buffer,
00db10
 		       size_t buflen, int *errnop)
00db10
 {
00db10
+  init();
00db10
   for (size_t idx = 0; idx < npwd_data; ++idx)
00db10
+    if (strcmp (pwd_data[idx].pw_name, name) == 0)
00db10
+      return copy_passwd (result, &pwd_data[idx], buffer, buflen, errnop);
00db10
+
00db10
+  return NSS_STATUS_NOTFOUND;
00db10
+}
00db10
+
00db10
+/* -------------------------------------------------- */
00db10
+/* Group handling.  */
00db10
+
00db10
+static size_t grp_iter;
00db10
+#define CURGRP grp_data[grp_iter]
00db10
+
00db10
+static pthread_mutex_t grp_lock = PTHREAD_MUTEX_INITIALIZER;
00db10
+
00db10
+enum nss_status
00db10
+NAME(setgrent) (int stayopen)
00db10
+{
00db10
+  init();
00db10
+  grp_iter = 0;
00db10
+  return NSS_STATUS_SUCCESS;
00db10
+}
00db10
+
00db10
+
00db10
+enum nss_status
00db10
+NAME(endgrent) (void)
00db10
+{
00db10
+  init();
00db10
+  return NSS_STATUS_SUCCESS;
00db10
+}
00db10
+
00db10
+static enum nss_status
00db10
+copy_group (struct group *result, struct group *local,
00db10
+	    char *buffer, size_t buflen, int *errnop)
00db10
+{
00db10
+  struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
00db10
+  char **memlist;
00db10
+  int i;
00db10
+
00db10
+  if (local->gr_mem)
00db10
+    {
00db10
+      i = 0;
00db10
+      while (local->gr_mem[i])
00db10
+	++i;
00db10
+
00db10
+      memlist = alloc_buffer_alloc_array (&buf, char *, i + 1);
00db10
+
00db10
+      if (memlist) {
00db10
+	for (i = 0; local->gr_mem[i]; ++i)
00db10
+	  memlist[i] = alloc_buffer_maybe_copy_string (&buf, local->gr_mem[i]);
00db10
+	memlist[i] = NULL;
00db10
+      }
00db10
+
00db10
+      result->gr_mem = memlist;
00db10
+    }
00db10
+  else
00db10
+    result->gr_mem = NULL;
00db10
+
00db10
+  result->gr_name = alloc_buffer_maybe_copy_string (&buf, local->gr_name);
00db10
+  result->gr_passwd = alloc_buffer_maybe_copy_string (&buf, local->gr_passwd);
00db10
+  result->gr_gid = local->gr_gid;
00db10
+
00db10
+  if (alloc_buffer_has_failed (&buf))
00db10
+    {
00db10
+      *errnop = ERANGE;
00db10
+      return NSS_STATUS_TRYAGAIN;
00db10
+    }
00db10
+
00db10
+  return NSS_STATUS_SUCCESS;
00db10
+}
00db10
+
00db10
+
00db10
+enum nss_status
00db10
+NAME(getgrent_r) (struct group *result, char *buffer, size_t buflen,
00db10
+		       int *errnop)
00db10
+{
00db10
+  int res = NSS_STATUS_SUCCESS;
00db10
+
00db10
+  init();
00db10
+  pthread_mutex_lock (&grp_lock);
00db10
+
00db10
+  if (grp_iter >= ngrp_data)
00db10
+    res = NSS_STATUS_NOTFOUND;
00db10
+  else
00db10
+    {
00db10
+      res = copy_group (result, &CURGRP, buffer, buflen, errnop);
00db10
+      ++grp_iter;
00db10
+    }
00db10
+
00db10
+  pthread_mutex_unlock (&pwd_lock);
00db10
+
00db10
+  return res;
00db10
+}
00db10
+
00db10
+
00db10
+enum nss_status
00db10
+NAME(getgrgid_r) (gid_t gid, struct group *result, char *buffer,
00db10
+		  size_t buflen, int *errnop)
00db10
+{
00db10
+  init();
00db10
+  for (size_t idx = 0; idx < ngrp_data; ++idx)
00db10
+    if (grp_data[idx].gr_gid == gid)
00db10
+      return copy_group (result, &grp_data[idx], buffer, buflen, errnop);
00db10
+
00db10
+  return NSS_STATUS_NOTFOUND;
00db10
+}
00db10
+
00db10
+
00db10
+enum nss_status
00db10
+NAME(getgrnam_r) (const char *name, struct group *result, char *buffer,
00db10
+		       size_t buflen, int *errnop)
00db10
+{
00db10
+  init();
00db10
+  for (size_t idx = 0; idx < ngrp_data; ++idx)
00db10
     if (strcmp (pwd_data[idx].pw_name, name) == 0)
00db10
       {
00db10
-	char *cp = buffer;
00db10
-	int res = NSS_STATUS_SUCCESS;
00db10
-
00db10
-	result->pw_name = COPY_IF_ROOM (pwd_data[idx].pw_name);
00db10
-	result->pw_passwd = COPY_IF_ROOM (pwd_data[idx].pw_passwd);
00db10
-	result->pw_uid = pwd_data[idx].pw_uid;
00db10
-	result->pw_gid = pwd_data[idx].pw_gid;
00db10
-	result->pw_gecos = COPY_IF_ROOM (pwd_data[idx].pw_gecos);
00db10
-	result->pw_dir = COPY_IF_ROOM (pwd_data[idx].pw_dir);
00db10
-	result->pw_shell = COPY_IF_ROOM (pwd_data[idx].pw_shell);
00db10
-
00db10
-	if (result->pw_name == NULL || result->pw_passwd == NULL
00db10
-	    || result->pw_gecos == NULL || result->pw_dir == NULL
00db10
-	    || result->pw_shell == NULL)
00db10
-	  {
00db10
-	    *errnop = ERANGE;
00db10
-	    res = NSS_STATUS_TRYAGAIN;
00db10
-	  }
00db10
-
00db10
-	return res;
00db10
+	return copy_group (result, &grp_data[idx], buffer, buflen, errnop);
00db10
       }
00db10
 
00db10
   return NSS_STATUS_NOTFOUND;
00db10
diff --git a/nss/nss_test2.c b/nss/nss_test2.c
00db10
new file mode 100644
00db10
index 0000000000..0169344e5f
00db10
--- /dev/null
00db10
+++ b/nss/nss_test2.c
00db10
@@ -0,0 +1,20 @@
00db10
+/* Instance of a generic NSS service provider.  See nss_test.h for usage.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define NAME(x) NAME_(x,test2)
00db10
+#include "nss_test1.c"
00db10
diff --git a/nss/tst-nss-test2.c b/nss/tst-nss-test2.c
00db10
new file mode 100644
00db10
index 0000000000..11c2edf118
00db10
--- /dev/null
00db10
+++ b/nss/tst-nss-test2.c
00db10
@@ -0,0 +1,136 @@
00db10
+/* Basic test for two passwd databases.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <nss.h>
00db10
+#include <pwd.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+
00db10
+#include "nss_test.h"
00db10
+
00db10
+/* The data in these tables is arbitrary, but the merged data based on
00db10
+   the first two tables will be compared against the expected data in
00db10
+   the pwd_expected table, and the tests[] array.  */
00db10
+
00db10
+static struct passwd pwd_table_1[] = {
00db10
+    PWD (100),
00db10
+    PWD (30),
00db10
+    PWD (200),
00db10
+    PWD (60),
00db10
+    PWD (20000),
00db10
+    PWD_LAST ()
00db10
+  };
00db10
+
00db10
+static struct passwd pwd_table_2[] = {
00db10
+    PWD (5),
00db10
+    PWD_N(200, "name30"),
00db10
+    PWD (16),
00db10
+    PWD_LAST ()
00db10
+  };
00db10
+
00db10
+void
00db10
+_nss_test1_init_hook(test_tables *t)
00db10
+{
00db10
+  t->pwd_table = pwd_table_1;
00db10
+}
00db10
+
00db10
+void
00db10
+_nss_test2_init_hook(test_tables *t)
00db10
+{
00db10
+  t->pwd_table = pwd_table_2;
00db10
+}
00db10
+
00db10
+static struct passwd pwd_expected[] = {
00db10
+  PWD(100),
00db10
+  PWD(30),
00db10
+  PWD(200),
00db10
+  PWD(60),
00db10
+  PWD(20000),
00db10
+  PWD(5),
00db10
+  PWD_N(200, "name30"),
00db10
+  PWD(16),
00db10
+  PWD_LAST ()
00db10
+};
00db10
+
00db10
+static struct {
00db10
+  uid_t uid;
00db10
+  const char *name;
00db10
+} tests[] = {
00db10
+  { 100, "name100" }, /* control, first db */
00db10
+  {  16, "name16"  }, /* second db */
00db10
+  {  30, "name30"  }, /* test overlaps in name */
00db10
+  { 200, "name200" }, /* test overlaps uid */
00db10
+  { 0, NULL }
00db10
+};
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  int retval = 0;
00db10
+  int i;
00db10
+
00db10
+  __nss_configure_lookup ("passwd", "test1 test2");
00db10
+
00db10
+  setpwent ();
00db10
+
00db10
+  i = 0;
00db10
+  for (struct passwd *p = getpwent (); p != NULL; ++i, p = getpwent ())
00db10
+    {
00db10
+      retval += compare_passwds (i, & pwd_expected[i], p);
00db10
+
00db10
+      if (p->pw_uid != pwd_expected[i].pw_uid || strcmp (p->pw_name, pwd_expected[i].pw_name) != 0)
00db10
+      {
00db10
+	printf ("FAIL: getpwent for %u.%s returned %u.%s\n",
00db10
+		pwd_expected[i].pw_uid, pwd_expected[i].pw_name,
00db10
+		p->pw_uid, p->pw_name);
00db10
+	retval = 1;
00db10
+	break;
00db10
+      }
00db10
+    }
00db10
+
00db10
+  endpwent ();
00db10
+
00db10
+  for (i=0; tests[i].name; i++)
00db10
+    {
00db10
+      struct passwd *p = getpwnam (tests[i].name);
00db10
+      if (strcmp (p->pw_name, tests[i].name) != 0
00db10
+	  || p->pw_uid != tests[i].uid)
00db10
+	{
00db10
+	  printf("FAIL: getpwnam for %u.%s returned %u.%s\n",
00db10
+		 tests[i].uid, tests[i].name,
00db10
+		 p->pw_uid, p->pw_name);
00db10
+	  retval = 1;
00db10
+	}
00db10
+
00db10
+      p = getpwuid (tests[i].uid);
00db10
+      if (strcmp (p->pw_name, tests[i].name) != 0
00db10
+	  || p->pw_uid != tests[i].uid)
00db10
+	{
00db10
+	  printf("FAIL: getpwuid for %u.%s returned %u.%s\n",
00db10
+		 tests[i].uid, tests[i].name,
00db10
+		 p->pw_uid, p->pw_name);
00db10
+	  retval = 1;
00db10
+	}
00db10
+    }
00db10
+
00db10
+  return retval;
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
diff --git a/nss/tst-nss-test3.c b/nss/tst-nss-test3.c
00db10
new file mode 100644
00db10
index 0000000000..308708f387
00db10
--- /dev/null
00db10
+++ b/nss/tst-nss-test3.c
00db10
@@ -0,0 +1,150 @@
00db10
+/* Test error checking for group entries.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <nss.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+#include <sys/signal.h>
00db10
+
00db10
+#include "nss_test.h"
00db10
+
00db10
+/* The names here are arbitrary, but the *lengths* of the arrays is
00db10
+   not, and groups 6 and 7 test for partial matches.  */
00db10
+
00db10
+static const char *group_2[] = {
00db10
+  "foo", "bar", NULL
00db10
+};
00db10
+
00db10
+static const char *group_3[] = {
00db10
+  "tom", "dick", "harry", NULL
00db10
+};
00db10
+
00db10
+static const char *group_4[] = {
00db10
+  "alpha", "beta", "gamma", "fred", NULL
00db10
+};
00db10
+
00db10
+static const char *group_6[] = {
00db10
+  "larry", "curly", "moe", NULL
00db10
+};
00db10
+
00db10
+static const char *group_7[] = {
00db10
+  "larry", "curly", "darryl", NULL
00db10
+};
00db10
+
00db10
+static const char *group_14[] = {
00db10
+  "huey", "dewey", "louis", NULL
00db10
+};
00db10
+
00db10
+/* Note that we're intentionally causing mis-matches here; the purpose
00db10
+   of this test case is to test each error check and make sure they
00db10
+   detect the errors they check for, and to ensure that the harness
00db10
+   can process all the error cases properly (i.e. a NULL gr_name
00db10
+   field).  We check for the correct number of mismatches at the
00db10
+   end.  */
00db10
+
00db10
+/* This is the data we're giving the service.  */
00db10
+static struct group group_table_data[] = {
00db10
+  GRP(4), /* match */
00db10
+  GRP_N(8, "name6", group_6), /* wrong gid */
00db10
+  GRP_N(14, NULL, group_14), /* missing name */
00db10
+  GRP(14), /* unexpected name */
00db10
+  GRP_N(7, "name7_wrong", group_7), /* wrong name */
00db10
+  { .gr_name =  (char *)"name5", .gr_passwd =  (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* unexpected passwd */
00db10
+  { .gr_name =  (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL }, /* missing passwd */
00db10
+  { .gr_name =  (char *)"name5", .gr_passwd = (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* wrong passwd */
00db10
+  GRP_N(3, "name3a", NULL),   /* missing member list */
00db10
+  GRP_N(3, "name3b", group_3), /* unexpected member list */
00db10
+  GRP_N(3, "name3c", group_3), /* wrong/short member list */
00db10
+  GRP_N(3, "name3d", group_4), /* wrong/long member list */
00db10
+  GRP_LAST ()
00db10
+};
00db10
+
00db10
+/* This is the data we compare against.  */
00db10
+static struct group group_table[] = {
00db10
+  GRP(4),
00db10
+  GRP(6),
00db10
+  GRP(14),
00db10
+  GRP_N(14, NULL, group_14),
00db10
+  GRP(7),
00db10
+  { .gr_name =  (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL },
00db10
+  { .gr_name =  (char *)"name5", .gr_passwd =  (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
00db10
+  { .gr_name =  (char *)"name5", .gr_passwd =  (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
00db10
+  GRP_N(3, "name3a", group_3),
00db10
+  GRP_N(3, "name3b", NULL),
00db10
+  GRP_N(3, "name3c", group_4),
00db10
+  GRP_N(3, "name3d", group_3),
00db10
+  GRP(2),
00db10
+  GRP_LAST ()
00db10
+};
00db10
+
00db10
+void
00db10
+_nss_test1_init_hook(test_tables *t)
00db10
+{
00db10
+  t->grp_table = group_table_data;
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  int retval = 0;
00db10
+  int i;
00db10
+  struct group *g = NULL;
00db10
+
00db10
+  __nss_configure_lookup ("group", "test1");
00db10
+
00db10
+  setgrent ();
00db10
+
00db10
+  i = 0;
00db10
+  for (g = getgrent () ;
00db10
+       g != NULL && ! GRP_ISLAST(&group_table[i]);
00db10
+       ++i, g = getgrent ())
00db10
+    {
00db10
+      retval += compare_groups (i, g, & group_table[i]);
00db10
+    }
00db10
+
00db10
+  endgrent ();
00db10
+
00db10
+  if (g)
00db10
+    {
00db10
+      printf ("FAIL: [?] group entry %u.%s unexpected\n", g->gr_gid, g->gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  if (group_table[i].gr_name || group_table[i].gr_gid)
00db10
+    {
00db10
+      printf ("FAIL: [%d] group entry %u.%s missing\n", i,
00db10
+	      group_table[i].gr_gid, group_table[i].gr_name);
00db10
+      ++retval;
00db10
+    }
00db10
+
00db10
+#define EXPECTED 18
00db10
+  if (retval == EXPECTED)
00db10
+    {
00db10
+      if (retval > 0)
00db10
+	printf ("PASS: Found %d expected errors\n", retval);
00db10
+      return 0;
00db10
+    }
00db10
+  else
00db10
+    {
00db10
+      printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
00db10
+      return 1;
00db10
+    }
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
diff --git a/nss/tst-nss-test4.c b/nss/tst-nss-test4.c
00db10
new file mode 100644
00db10
index 0000000000..731e0ed10a
00db10
--- /dev/null
00db10
+++ b/nss/tst-nss-test4.c
00db10
@@ -0,0 +1,137 @@
00db10
+/* Test group merging.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <nss.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+#include <sys/signal.h>
00db10
+
00db10
+#include "nss_test.h"
00db10
+
00db10
+/* The name choices here are arbitrary, aside from the merge_1 list
00db10
+   needing to be an expected merge of group_1 and group_2.  */
00db10
+
00db10
+static const char *group_1[] = {
00db10
+  "foo", "bar", NULL
00db10
+};
00db10
+
00db10
+static const char *group_2[] = {
00db10
+  "foo", "dick", "harry", NULL
00db10
+};
00db10
+
00db10
+/* Note that deduplication is NOT supposed to happen.  */
00db10
+static const char *merge_1[] = {
00db10
+  "foo", "bar", "foo", "dick", "harry", NULL
00db10
+};
00db10
+
00db10
+static const char *group_4[] = {
00db10
+  "fred", "wilma", NULL
00db10
+};
00db10
+
00db10
+/* This is the data we're giving the service.  */
00db10
+static struct group group_table_data1[] = {
00db10
+  GRP_N(1, "name1", group_1),
00db10
+  GRP(2),
00db10
+  GRP_LAST ()
00db10
+};
00db10
+
00db10
+/* This is the data we're giving the service.  */
00db10
+static struct group group_table_data2[] = {
00db10
+  GRP_N(1, "name1", group_2),
00db10
+  GRP(4),
00db10
+  GRP_LAST ()
00db10
+};
00db10
+
00db10
+/* This is the data we compare against.  */
00db10
+static struct group group_table[] = {
00db10
+  GRP_N(1, "name1", merge_1),
00db10
+  GRP(2),
00db10
+  GRP(4),
00db10
+  GRP_LAST ()
00db10
+};
00db10
+
00db10
+void
00db10
+_nss_test1_init_hook(test_tables *t)
00db10
+{
00db10
+  t->grp_table = group_table_data1;
00db10
+}
00db10
+
00db10
+void
00db10
+_nss_test2_init_hook(test_tables *t)
00db10
+{
00db10
+  t->grp_table = group_table_data2;
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  int retval = 0;
00db10
+  int i;
00db10
+  struct group *g = NULL;
00db10
+  uintptr_t align_mask;
00db10
+
00db10
+  __nss_configure_lookup ("group", "test1 [SUCCESS=merge] test2");
00db10
+
00db10
+  align_mask = __alignof__ (struct group *) - 1;
00db10
+
00db10
+  setgrent ();
00db10
+
00db10
+  for (i = 0; group_table[i].gr_gid; ++i)
00db10
+    {
00db10
+      g = getgrgid (group_table[i].gr_gid);
00db10
+      if (g)
00db10
+	{
00db10
+	  retval += compare_groups (i, g, & group_table[i]);
00db10
+	  if ((uintptr_t)g & align_mask)
00db10
+	    {
00db10
+	      printf("FAIL: [%d] unaligned group %p\n", i, g);
00db10
+	      ++retval;
00db10
+	    }
00db10
+	  if ((uintptr_t)(g->gr_mem) & align_mask)
00db10
+	    {
00db10
+	      printf("FAIL: [%d] unaligned member list %p\n", i, g->gr_mem);
00db10
+	      ++retval;
00db10
+	    }
00db10
+	}
00db10
+      else
00db10
+	{
00db10
+	  printf ("FAIL: [%d] group %u.%s not found\n", i,
00db10
+	      group_table[i].gr_gid, group_table[i].gr_name);
00db10
+	  ++retval;
00db10
+	}
00db10
+    }
00db10
+
00db10
+  endgrent ();
00db10
+
00db10
+#define EXPECTED 0
00db10
+  if (retval == EXPECTED)
00db10
+    {
00db10
+      if (retval > 0)
00db10
+	printf ("PASS: Found %d expected errors\n", retval);
00db10
+      return 0;
00db10
+    }
00db10
+  else
00db10
+    {
00db10
+      printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
00db10
+      return 1;
00db10
+    }
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
diff --git a/nss/tst-nss-test5.c b/nss/tst-nss-test5.c
00db10
new file mode 100644
00db10
index 0000000000..b70f21e8e5
00db10
--- /dev/null
00db10
+++ b/nss/tst-nss-test5.c
00db10
@@ -0,0 +1,108 @@
00db10
+/* Test error checking for passwd entries.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <nss.h>
00db10
+#include <pwd.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+
00db10
+#include "nss_test.h"
00db10
+
00db10
+/* The specific values and names used here are arbitrary, other than
00db10
+   correspondence (with suitable differences according to the tests as
00db10
+   commented) between the given and expected entries.  */
00db10
+
00db10
+static struct passwd pwd_table[] = {
00db10
+  PWD (100),  /* baseline, matches */
00db10
+  PWD (300),  /* wrong name and uid */
00db10
+  PWD_N (200, NULL), /* missing name */
00db10
+  PWD (60), /* unexpected name */
00db10
+  { .pw_name = (char *)"name20000",  .pw_passwd = (char *) "*", .pw_uid = 20000,  \
00db10
+    .pw_gid = 200, .pw_gecos = (char *) "*", .pw_dir = (char *) "*",	\
00db10
+    .pw_shell = (char *) "*" }, /* wrong gid */
00db10
+  { .pw_name = (char *)"name2",  .pw_passwd = (char *) "x", .pw_uid = 2,  \
00db10
+    .pw_gid = 2, .pw_gecos = (char *) "y", .pw_dir = (char *) "z",	\
00db10
+    .pw_shell = (char *) "*" }, /* spot check other text fields */
00db10
+  PWD_LAST ()
00db10
+};
00db10
+
00db10
+static struct passwd exp_table[] = {
00db10
+  PWD (100),
00db10
+  PWD (30),
00db10
+  PWD (200),
00db10
+  PWD_N (60, NULL),
00db10
+  PWD (20000),
00db10
+  PWD (2),
00db10
+  PWD_LAST ()
00db10
+};
00db10
+
00db10
+void
00db10
+_nss_test1_init_hook(test_tables *t)
00db10
+{
00db10
+  t->pwd_table = pwd_table;
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  int retval = 0;
00db10
+  int i;
00db10
+  struct passwd *p;
00db10
+
00db10
+  __nss_configure_lookup ("passwd", "test1 test2");
00db10
+
00db10
+  setpwent ();
00db10
+
00db10
+  i = 0;
00db10
+  for (p = getpwent ();
00db10
+       p != NULL && ! PWD_ISLAST (& exp_table[i]);
00db10
+       ++i, p = getpwent ())
00db10
+    retval += compare_passwds (i, p, & exp_table[i]);
00db10
+
00db10
+  endpwent ();
00db10
+
00db10
+
00db10
+  if (p)
00db10
+    {
00db10
+      printf ("FAIL: [?] passwd entry %u.%s unexpected\n", p->pw_uid, p->pw_name);
00db10
+      ++retval;
00db10
+    }
00db10
+  if (! PWD_ISLAST (& exp_table[i]))
00db10
+    {
00db10
+      printf ("FAIL: [%d] passwd entry %u.%s missing\n", i,
00db10
+	      exp_table[i].pw_uid, exp_table[i].pw_name);
00db10
+      ++retval;
00db10
+    }
00db10
+
00db10
+#define EXPECTED 9
00db10
+  if (retval == EXPECTED)
00db10
+    {
00db10
+      if (retval > 0)
00db10
+	printf ("PASS: Found %d expected errors\n", retval);
00db10
+      return 0;
00db10
+    }
00db10
+  else
00db10
+    {
00db10
+      printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
00db10
+      return 1;
00db10
+    }
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"