Add IPPROTO_L2TP from Linux 6.1 to netinet/in.h
[glibc.git] / pwd / tst-getpw.c
blob30d4740c6c2b93a9ffad5a117fd3f101d7d211e2
1 /* Copyright (C) 1999-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
18 #include <stdio.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdbool.h>
23 /* We want to test getpw by calling it with a uid that does
24 exist and one that doesn't exist. We track if we've met those
25 conditions and exit. We also track if we've failed due to lack
26 of memory. That constitutes all of the standard failure cases. */
27 bool seen_hit;
28 bool seen_miss;
29 bool seen_oom;
31 /* How many errors we've had while running the test. */
32 int errors;
34 static void
35 check (uid_t uid)
37 int ret;
38 char buf[1024];
40 ret = getpw (uid, buf);
42 /* Successfully read a password line. */
43 if (ret == 0 && !seen_hit)
45 printf ("PASS: Read a password line given a uid.\n");
46 seen_hit = true;
49 /* Failed to read a password line. Why? */
50 if (ret == -1)
52 /* No entry? Technically the errno could be any number
53 of values including ESRCH, EBADP or EPERM depending
54 on the quality of the nss module that implements the
55 underlying lookup. It should be 0 for getpw.*/
56 if (errno == 0 && !seen_miss)
58 printf ("PASS: Found an invalid uid.\n");
59 seen_miss = true;
60 return;
63 /* Out of memory? */
64 if (errno == ENOMEM && !seen_oom)
66 printf ("FAIL: Failed with ENOMEM.\n");
67 seen_oom = true;
68 errors++;
71 /* We don't expect any other values for errno. */
72 if (errno != ENOMEM && errno != 0)
73 errors++;
77 static int
78 do_test (void)
80 int ret;
81 uid_t uid;
83 /* Should return -1 and set errnot to EINVAL. */
84 ret = getpw (0, NULL);
85 if (ret == -1 && errno == EINVAL)
87 printf ("PASS: NULL buffer returns -1 and sets errno to EINVAL.\n");
89 else
91 printf ("FAIL: NULL buffer did not return -1 or set errno to EINVAL.\n");
92 errors++;
95 /* Look for one matching uid, one non-found uid and then stop.
96 Set an upper limit at the 16-bit UID mark; no need to go farther. */
97 for (uid = 0; uid < ((uid_t) 65535); ++uid)
99 check (uid);
100 if (seen_miss && seen_hit)
101 break;
104 if (!seen_hit)
105 printf ("FAIL: Did not read even one password line given a uid.\n");
107 if (!seen_miss)
108 printf ("FAIL: Did not find even one invalid uid.\n");
110 return errors;
113 #define TEST_FUNCTION do_test ()
114 #include "../test-skeleton.c"