Update copyright dates with scripts/update-copyrights.
[platform/upstream/glibc.git] / nss / tst-nss-getpwent.c
1 /* Copyright (C) 2015-2018 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <pwd.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <support/support.h>
25
26 int
27 do_test (void)
28 {
29   /* Count the number of entries in the password database, and fetch
30      data from the first and last entries.  */
31   size_t count = 0;
32   struct passwd * pw;
33   char *first_name = NULL;
34   uid_t first_uid = 0;
35   char *last_name = NULL;
36   uid_t last_uid = 0;
37   setpwent ();
38   while ((pw  = getpwent ()) != NULL)
39     {
40       if (first_name == NULL)
41         {
42           first_name = xstrdup (pw->pw_name);
43           first_uid = pw->pw_uid;
44         }
45
46       free (last_name);
47       last_name = xstrdup (pw->pw_name);
48       last_uid = pw->pw_uid;
49       ++count;
50     }
51   endpwent ();
52
53   if (count == 0)
54     {
55       printf ("No entries in the password database.\n");
56       return 0;
57     }
58
59   /* Try again, this time interleaving with name-based and UID-based
60      lookup operations.  The counts do not match if the interleaved
61      lookups affected the enumeration.  */
62   size_t new_count = 0;
63   setpwent ();
64   while ((pw  = getpwent ()) != NULL)
65     {
66       if (new_count == count)
67         {
68           printf ("Additional entry in the password database.\n");
69           return 1;
70         }
71       ++new_count;
72       struct passwd *pw2 = getpwnam (first_name);
73       if (pw2 == NULL)
74         {
75           printf ("getpwnam (%s) failed: %m\n", first_name);
76           return 1;
77         }
78       pw2 = getpwnam (last_name);
79       if (pw2 == NULL)
80         {
81           printf ("getpwnam (%s) failed: %m\n", last_name);
82           return 1;
83         }
84       pw2 = getpwuid (first_uid);
85       if (pw2 == NULL)
86         {
87           printf ("getpwuid (%llu) failed: %m\n",
88                   (unsigned long long) first_uid);
89           return 1;
90         }
91       pw2 = getpwuid (last_uid);
92       if (pw2 == NULL)
93         {
94           printf ("getpwuid (%llu) failed: %m\n",
95                   (unsigned long long) last_uid);
96           return 1;
97         }
98     }
99   endpwent ();
100   if (new_count < count)
101     {
102       printf ("Missing entry in the password database.\n");
103       return 1;
104     }
105
106   return 0;
107 }
108
109 #define TIMEOUT 300
110 #include <support/test-driver.c>