1 /* Copyright (C) 1999-2022 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 <https://www.gnu.org/licenses/>. */ 17 18 #include <stdio.h> 19 #include <pwd.h> 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; 33 34 static void check(uid_t uid)35check (uid_t uid) 36 { 37 int ret; 38 char buf[1024]; 39 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 } 75 } 76 77 static int do_test(void)78do_test (void) 79 { 80 int ret; 81 uid_t uid; 82 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"); 106 107 if (!seen_miss) 108 printf ("FAIL: Did not find even one invalid uid.\n"); 109 110 return errors; 111 } 112 113 #define TEST_FUNCTION do_test () 114 #include "../test-skeleton.c" 115