]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 1999-2021 Free Software Foundation, Inc. |
c11f1209 UD |
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 | |
41bdb6e2 AJ |
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. | |
c11f1209 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
c11f1209 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
c11f1209 | 17 | |
0897c551 | 18 | #include <stdio.h> |
c11f1209 | 19 | #include <pwd.h> |
0897c551 CD |
20 | #include <errno.h> |
21 | #include <stdbool.h> | |
22 | ||
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; | |
30 | ||
31 | /* How many errors we've had while running the test. */ | |
32 | int errors; | |
c11f1209 UD |
33 | |
34 | static void | |
35 | check (uid_t uid) | |
36 | { | |
0897c551 | 37 | int ret; |
c11f1209 UD |
38 | char buf[1024]; |
39 | ||
0897c551 CD |
40 | ret = getpw (uid, buf); |
41 | ||
42 | /* Successfully read a password line. */ | |
43 | if (ret == 0 && !seen_hit) | |
44 | { | |
45 | printf ("PASS: Read a password line given a uid.\n"); | |
46 | seen_hit = true; | |
47 | } | |
48 | ||
49 | /* Failed to read a password line. Why? */ | |
50 | if (ret == -1) | |
51 | { | |
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) | |
57 | { | |
58 | printf ("PASS: Found an invalid uid.\n"); | |
59 | seen_miss = true; | |
60 | return; | |
61 | } | |
62 | ||
63 | /* Out of memory? */ | |
64 | if (errno == ENOMEM && !seen_oom) | |
65 | { | |
66 | printf ("FAIL: Failed with ENOMEM.\n"); | |
67 | seen_oom = true; | |
68 | errors++; | |
69 | } | |
70 | ||
71 | /* We don't expect any other values for errno. */ | |
72 | if (errno != ENOMEM && errno != 0) | |
73 | errors++; | |
74 | } | |
c11f1209 UD |
75 | } |
76 | ||
29955b5d AS |
77 | static int |
78 | do_test (void) | |
c11f1209 | 79 | { |
0897c551 | 80 | int ret; |
c11f1209 UD |
81 | uid_t uid; |
82 | ||
0897c551 CD |
83 | /* Should return -1 and set errnot to EINVAL. */ |
84 | ret = getpw (0, NULL); | |
85 | if (ret == -1 && errno == EINVAL) | |
86 | { | |
87 | printf ("PASS: NULL buffer returns -1 and sets errno to EINVAL.\n"); | |
88 | } | |
89 | else | |
90 | { | |
91 | printf ("FAIL: NULL buffer did not return -1 or set errno to EINVAL.\n"); | |
92 | errors++; | |
93 | } | |
94 | ||
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) | |
98 | { | |
99 | check (uid); | |
100 | if (seen_miss && seen_hit) | |
101 | break; | |
102 | } | |
103 | ||
104 | if (!seen_hit) | |
105 | printf ("FAIL: Did not read even one password line given a uid.\n"); | |
c11f1209 | 106 | |
0897c551 CD |
107 | if (!seen_miss) |
108 | printf ("FAIL: Did not find even one invalid uid.\n"); | |
c11f1209 | 109 | |
0897c551 | 110 | return errors; |
c11f1209 | 111 | } |
29955b5d AS |
112 | |
113 | #define TEST_FUNCTION do_test () | |
114 | #include "../test-skeleton.c" |