Blame nss/tst-nss-test4.c

Packit 6c4009
/* Test group merging.
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 <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
Packit 6c4009
#include "nss_test.h"
Packit 6c4009
Packit 6c4009
/* The name choices here are arbitrary, aside from the merge_1 list
Packit 6c4009
   needing to be an expected merge of group_1 and group_2.  */
Packit 6c4009
Packit 6c4009
static const char *group_1[] = {
Packit 6c4009
  "foo", "bar", NULL
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const char *group_2[] = {
Packit 6c4009
  "foo", "dick", "harry", NULL
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Note that deduplication is NOT supposed to happen.  */
Packit 6c4009
static const char *merge_1[] = {
Packit 6c4009
  "foo", "bar", "foo", "dick", "harry", NULL
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const char *group_4[] = {
Packit 6c4009
  "fred", "wilma", NULL
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* This is the data we're giving the service.  */
Packit 6c4009
static struct group group_table_data1[] = {
Packit 6c4009
  GRP_N(1, "name1", group_1),
Packit 6c4009
  GRP(2),
Packit 6c4009
  GRP_LAST ()
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* This is the data we're giving the service.  */
Packit 6c4009
static struct group group_table_data2[] = {
Packit 6c4009
  GRP_N(1, "name1", group_2),
Packit 6c4009
  GRP(4),
Packit 6c4009
  GRP_LAST ()
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* This is the data we compare against.  */
Packit 6c4009
static struct group group_table[] = {
Packit 6c4009
  GRP_N(1, "name1", merge_1),
Packit 6c4009
  GRP(2),
Packit 6c4009
  GRP(4),
Packit 6c4009
  GRP_LAST ()
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_nss_test1_init_hook(test_tables *t)
Packit 6c4009
{
Packit 6c4009
  t->grp_table = group_table_data1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_nss_test2_init_hook(test_tables *t)
Packit 6c4009
{
Packit 6c4009
  t->grp_table = group_table_data2;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int retval = 0;
Packit 6c4009
  int i;
Packit 6c4009
  struct group *g = NULL;
Packit 6c4009
  uintptr_t align_mask;
Packit 6c4009
Packit 6c4009
  __nss_configure_lookup ("group", "test1 [SUCCESS=merge] test2");
Packit 6c4009
Packit 6c4009
  align_mask = __alignof__ (struct group *) - 1;
Packit 6c4009
Packit 6c4009
  setgrent ();
Packit 6c4009
Packit 6c4009
  for (i = 0; group_table[i].gr_gid; ++i)
Packit 6c4009
    {
Packit 6c4009
      g = getgrgid (group_table[i].gr_gid);
Packit 6c4009
      if (g)
Packit 6c4009
	{
Packit 6c4009
	  retval += compare_groups (i, g, & group_table[i]);
Packit 6c4009
	  if ((uintptr_t)g & align_mask)
Packit 6c4009
	    {
Packit 6c4009
	      printf("FAIL: [%d] unaligned group %p\n", i, g);
Packit 6c4009
	      ++retval;
Packit 6c4009
	    }
Packit 6c4009
	  if ((uintptr_t)(g->gr_mem) & align_mask)
Packit 6c4009
	    {
Packit 6c4009
	      printf("FAIL: [%d] unaligned member list %p\n", i, g->gr_mem);
Packit 6c4009
	      ++retval;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  printf ("FAIL: [%d] group %u.%s not found\n", i,
Packit 6c4009
	      group_table[i].gr_gid, group_table[i].gr_name);
Packit 6c4009
	  ++retval;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endgrent ();
Packit 6c4009
Packit 6c4009
#define EXPECTED 0
Packit 6c4009
  if (retval == EXPECTED)
Packit 6c4009
    {
Packit 6c4009
      if (retval > 0)
Packit 6c4009
	printf ("PASS: Found %d expected errors\n", retval);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>