summaryrefslogtreecommitdiff
blob: 308708f387aee6872e42730062230b20082c001a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Test error checking for group entries.
   Copyright (C) 2017 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <nss.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>

#include "nss_test.h"

/* The names here are arbitrary, but the *lengths* of the arrays is
   not, and groups 6 and 7 test for partial matches.  */

static const char *group_2[] = {
  "foo", "bar", NULL
};

static const char *group_3[] = {
  "tom", "dick", "harry", NULL
};

static const char *group_4[] = {
  "alpha", "beta", "gamma", "fred", NULL
};

static const char *group_6[] = {
  "larry", "curly", "moe", NULL
};

static const char *group_7[] = {
  "larry", "curly", "darryl", NULL
};

static const char *group_14[] = {
  "huey", "dewey", "louis", NULL
};

/* Note that we're intentionally causing mis-matches here; the purpose
   of this test case is to test each error check and make sure they
   detect the errors they check for, and to ensure that the harness
   can process all the error cases properly (i.e. a NULL gr_name
   field).  We check for the correct number of mismatches at the
   end.  */

/* This is the data we're giving the service.  */
static struct group group_table_data[] = {
  GRP(4), /* match */
  GRP_N(8, "name6", group_6), /* wrong gid */
  GRP_N(14, NULL, group_14), /* missing name */
  GRP(14), /* unexpected name */
  GRP_N(7, "name7_wrong", group_7), /* wrong name */
  { .gr_name =  (char *)"name5", .gr_passwd =  (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* unexpected passwd */
  { .gr_name =  (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL }, /* missing passwd */
  { .gr_name =  (char *)"name5", .gr_passwd = (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* wrong passwd */
  GRP_N(3, "name3a", NULL),   /* missing member list */
  GRP_N(3, "name3b", group_3), /* unexpected member list */
  GRP_N(3, "name3c", group_3), /* wrong/short member list */
  GRP_N(3, "name3d", group_4), /* wrong/long member list */
  GRP_LAST ()
};

/* This is the data we compare against.  */
static struct group group_table[] = {
  GRP(4),
  GRP(6),
  GRP(14),
  GRP_N(14, NULL, group_14),
  GRP(7),
  { .gr_name =  (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL },
  { .gr_name =  (char *)"name5", .gr_passwd =  (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
  { .gr_name =  (char *)"name5", .gr_passwd =  (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
  GRP_N(3, "name3a", group_3),
  GRP_N(3, "name3b", NULL),
  GRP_N(3, "name3c", group_4),
  GRP_N(3, "name3d", group_3),
  GRP(2),
  GRP_LAST ()
};

void
_nss_test1_init_hook(test_tables *t)
{
  t->grp_table = group_table_data;
}

static int
do_test (void)
{
  int retval = 0;
  int i;
  struct group *g = NULL;

  __nss_configure_lookup ("group", "test1");

  setgrent ();

  i = 0;
  for (g = getgrent () ;
       g != NULL && ! GRP_ISLAST(&group_table[i]);
       ++i, g = getgrent ())
    {
      retval += compare_groups (i, g, & group_table[i]);
    }

  endgrent ();

  if (g)
    {
      printf ("FAIL: [?] group entry %u.%s unexpected\n", g->gr_gid, g->gr_name);
      ++retval;
    }
  if (group_table[i].gr_name || group_table[i].gr_gid)
    {
      printf ("FAIL: [%d] group entry %u.%s missing\n", i,
	      group_table[i].gr_gid, group_table[i].gr_name);
      ++retval;
    }

#define EXPECTED 18
  if (retval == EXPECTED)
    {
      if (retval > 0)
	printf ("PASS: Found %d expected errors\n", retval);
      return 0;
    }
  else
    {
      printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
      return 1;
    }
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"