1/* Basic test for two passwd databases.
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 <pwd.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <support/support.h>
26
27#include "nss_test.h"
28
29/* The data in these tables is arbitrary, but the merged data based on
30 the first two tables will be compared against the expected data in
31 the pwd_expected table, and the tests[] array. */
32
33static struct passwd pwd_table_1[] = {
34 PWD (100),
35 PWD (30),
36 PWD (200),
37 PWD (60),
38 PWD (20000),
39 PWD_LAST ()
40 };
41
42static struct passwd pwd_table_2[] = {
43 PWD (5),
44 PWD_N(200, "name30"),
45 PWD (16),
46 PWD_LAST ()
47 };
48
49void
50_nss_test1_init_hook(test_tables *t)
51{
52 t->pwd_table = pwd_table_1;
53}
54
55void
56_nss_test2_init_hook(test_tables *t)
57{
58 t->pwd_table = pwd_table_2;
59}
60
61static struct passwd pwd_expected[] = {
62 PWD(100),
63 PWD(30),
64 PWD(200),
65 PWD(60),
66 PWD(20000),
67 PWD(5),
68 PWD_N(200, "name30"),
69 PWD(16),
70 PWD_LAST ()
71};
72
73static struct {
74 uid_t uid;
75 const char *name;
76} tests[] = {
77 { 100, "name100" }, /* control, first db */
78 { 16, "name16" }, /* second db */
79 { 30, "name30" }, /* test overlaps in name */
80 { 200, "name200" }, /* test overlaps uid */
81 { 0, NULL }
82};
83
84static int
85do_test (void)
86{
87 int retval = 0;
88 int i;
89
90 __nss_configure_lookup (dbname: "passwd", string: "test1 test2");
91
92 setpwent ();
93
94 i = 0;
95 for (struct passwd *p = getpwent (); p != NULL; ++i, p = getpwent ())
96 {
97 retval += compare_passwds (i, p: & pwd_expected[i], e: p);
98
99 if (p->pw_uid != pwd_expected[i].pw_uid || strcmp (p->pw_name, pwd_expected[i].pw_name) != 0)
100 {
101 printf (format: "FAIL: getpwent for %u.%s returned %u.%s\n",
102 pwd_expected[i].pw_uid, pwd_expected[i].pw_name,
103 p->pw_uid, p->pw_name);
104 retval = 1;
105 break;
106 }
107 }
108
109 endpwent ();
110
111 for (i=0; tests[i].name; i++)
112 {
113 struct passwd *p = getpwnam (name: tests[i].name);
114 if (strcmp (p->pw_name, tests[i].name) != 0
115 || p->pw_uid != tests[i].uid)
116 {
117 printf(format: "FAIL: getpwnam for %u.%s returned %u.%s\n",
118 tests[i].uid, tests[i].name,
119 p->pw_uid, p->pw_name);
120 retval = 1;
121 }
122
123 p = getpwuid (uid: tests[i].uid);
124 if (strcmp (p->pw_name, tests[i].name) != 0
125 || p->pw_uid != tests[i].uid)
126 {
127 printf(format: "FAIL: getpwuid for %u.%s returned %u.%s\n",
128 tests[i].uid, tests[i].name,
129 p->pw_uid, p->pw_name);
130 retval = 1;
131 }
132 }
133
134 return retval;
135}
136
137#include <support/test-driver.c>
138

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