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