1/* Test error checking for group entries.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <nss.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <support/support.h>
25
26#include "nss_test.h"
27
28/* The names here are arbitrary, but the *lengths* of the arrays is
29 not, and groups 6 and 7 test for partial matches. */
30
31static const char *group_2[] = {
32 "foo", "bar", NULL
33};
34
35static const char *group_3[] = {
36 "tom", "dick", "harry", NULL
37};
38
39static const char *group_4[] = {
40 "alpha", "beta", "gamma", "fred", NULL
41};
42
43static const char *group_6[] = {
44 "larry", "curly", "moe", NULL
45};
46
47static const char *group_7[] = {
48 "larry", "curly", "darryl", NULL
49};
50
51static const char *group_14[] = {
52 "huey", "dewey", "louis", NULL
53};
54
55/* Note that we're intentionally causing mis-matches here; the purpose
56 of this test case is to test each error check and make sure they
57 detect the errors they check for, and to ensure that the harness
58 can process all the error cases properly (i.e. a NULL gr_name
59 field). We check for the correct number of mismatches at the
60 end. */
61
62/* This is the data we're giving the service. */
63static struct group group_table_data[] = {
64 GRP(4), /* match */
65 GRP_N(8, "name6", group_6), /* wrong gid */
66 GRP_N(14, NULL, group_14), /* missing name */
67 GRP(14), /* unexpected name */
68 GRP_N(7, "name7_wrong", group_7), /* wrong name */
69 { .gr_name = (char *)"name5", .gr_passwd = (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* unexpected passwd */
70 { .gr_name = (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL }, /* missing passwd */
71 { .gr_name = (char *)"name5", .gr_passwd = (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* wrong passwd */
72 GRP_N(3, "name3a", NULL), /* missing member list */
73 GRP_N(3, "name3b", group_3), /* unexpected member list */
74 GRP_N(3, "name3c", group_3), /* wrong/short member list */
75 GRP_N(3, "name3d", group_4), /* wrong/long member list */
76 GRP_LAST ()
77};
78
79/* This is the data we compare against. */
80static struct group group_table[] = {
81 GRP(4),
82 GRP(6),
83 GRP(14),
84 GRP_N(14, NULL, group_14),
85 GRP(7),
86 { .gr_name = (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL },
87 { .gr_name = (char *)"name5", .gr_passwd = (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
88 { .gr_name = (char *)"name5", .gr_passwd = (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
89 GRP_N(3, "name3a", group_3),
90 GRP_N(3, "name3b", NULL),
91 GRP_N(3, "name3c", group_4),
92 GRP_N(3, "name3d", group_3),
93 GRP(2),
94 GRP_LAST ()
95};
96
97void
98_nss_test1_init_hook(test_tables *t)
99{
100 t->grp_table = group_table_data;
101}
102
103static int
104do_test (void)
105{
106 int retval = 0;
107 int i;
108 struct group *g = NULL;
109
110/* Previously we used __nss_configure_lookup to isolate the test
111 from the host environment and to get it to lookup from our new
112 test1 NSS service module, but now this test is run in a different
113 root filesystem via the test-container support and we directly
114 configure the use of the test1 NSS service. */
115
116 setgrent ();
117
118 i = 0;
119 for (g = getgrent () ;
120 g != NULL && ! GRP_ISLAST(&group_table[i]);
121 ++i, g = getgrent ())
122 {
123 retval += compare_groups (i, g, e: & group_table[i]);
124 }
125
126 endgrent ();
127
128 if (g)
129 {
130 printf (format: "FAIL: [?] group entry %u.%s unexpected\n", g->gr_gid, g->gr_name);
131 ++retval;
132 }
133 if (group_table[i].gr_name || group_table[i].gr_gid)
134 {
135 printf (format: "FAIL: [%d] group entry %u.%s missing\n", i,
136 group_table[i].gr_gid, group_table[i].gr_name);
137 ++retval;
138 }
139
140#define EXPECTED 18
141 if (retval == EXPECTED)
142 {
143 if (retval > 0)
144 printf (format: "PASS: Found %d expected errors\n", retval);
145 return 0;
146 }
147 else
148 {
149 printf (format: "FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
150 return 1;
151 }
152}
153
154#include <support/test-driver.c>
155

source code of glibc/nss/tst-nss-test3.c