]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/tst-nss-test4.c
731e0ed10a32d8024f859d67d6f5b9be4d645d61
[thirdparty/glibc.git] / nss / tst-nss-test4.c
1 /* Test group merging.
2 Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. */
18
19 #include <nss.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/signal.h>
24
25 #include "nss_test.h"
26
27 /* The name choices here are arbitrary, aside from the merge_1 list
28 needing to be an expected merge of group_1 and group_2. */
29
30 static const char *group_1[] = {
31 "foo", "bar", NULL
32 };
33
34 static const char *group_2[] = {
35 "foo", "dick", "harry", NULL
36 };
37
38 /* Note that deduplication is NOT supposed to happen. */
39 static const char *merge_1[] = {
40 "foo", "bar", "foo", "dick", "harry", NULL
41 };
42
43 static const char *group_4[] = {
44 "fred", "wilma", NULL
45 };
46
47 /* This is the data we're giving the service. */
48 static struct group group_table_data1[] = {
49 GRP_N(1, "name1", group_1),
50 GRP(2),
51 GRP_LAST ()
52 };
53
54 /* This is the data we're giving the service. */
55 static struct group group_table_data2[] = {
56 GRP_N(1, "name1", group_2),
57 GRP(4),
58 GRP_LAST ()
59 };
60
61 /* This is the data we compare against. */
62 static struct group group_table[] = {
63 GRP_N(1, "name1", merge_1),
64 GRP(2),
65 GRP(4),
66 GRP_LAST ()
67 };
68
69 void
70 _nss_test1_init_hook(test_tables *t)
71 {
72 t->grp_table = group_table_data1;
73 }
74
75 void
76 _nss_test2_init_hook(test_tables *t)
77 {
78 t->grp_table = group_table_data2;
79 }
80
81 static int
82 do_test (void)
83 {
84 int retval = 0;
85 int i;
86 struct group *g = NULL;
87 uintptr_t align_mask;
88
89 __nss_configure_lookup ("group", "test1 [SUCCESS=merge] test2");
90
91 align_mask = __alignof__ (struct group *) - 1;
92
93 setgrent ();
94
95 for (i = 0; group_table[i].gr_gid; ++i)
96 {
97 g = getgrgid (group_table[i].gr_gid);
98 if (g)
99 {
100 retval += compare_groups (i, g, & group_table[i]);
101 if ((uintptr_t)g & align_mask)
102 {
103 printf("FAIL: [%d] unaligned group %p\n", i, g);
104 ++retval;
105 }
106 if ((uintptr_t)(g->gr_mem) & align_mask)
107 {
108 printf("FAIL: [%d] unaligned member list %p\n", i, g->gr_mem);
109 ++retval;
110 }
111 }
112 else
113 {
114 printf ("FAIL: [%d] group %u.%s not found\n", i,
115 group_table[i].gr_gid, group_table[i].gr_name);
116 ++retval;
117 }
118 }
119
120 endgrent ();
121
122 #define EXPECTED 0
123 if (retval == EXPECTED)
124 {
125 if (retval > 0)
126 printf ("PASS: Found %d expected errors\n", retval);
127 return 0;
128 }
129 else
130 {
131 printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
132 return 1;
133 }
134 }
135
136 #define TEST_FUNCTION do_test ()
137 #include "../test-skeleton.c"