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