lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <nss.h> |
| 2 | #include <pwd.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | |
| 8 | static int |
| 9 | do_test (void) |
| 10 | { |
| 11 | int retval = 0; |
| 12 | |
| 13 | __nss_configure_lookup ("passwd", "test1"); |
| 14 | |
| 15 | static const unsigned int pwdids[] = { 100, 30, 200, 60, 20000 }; |
| 16 | #define npwdids (sizeof (pwdids) / sizeof (pwdids[0])) |
| 17 | setpwent (); |
| 18 | |
| 19 | const unsigned int *np = pwdids; |
| 20 | for (struct passwd *p = getpwent (); p != NULL; ++np, p = getpwent ()) |
| 21 | if (p->pw_uid != *np || strncmp (p->pw_name, "name", 4) != 0 |
| 22 | || atol (p->pw_name + 4) != *np) |
| 23 | { |
| 24 | printf ("passwd entry %td wrong (%s, %u)\n", |
| 25 | np - pwdids, p->pw_name, p->pw_uid); |
| 26 | retval = 1; |
| 27 | break; |
| 28 | } |
| 29 | |
| 30 | endpwent (); |
| 31 | |
| 32 | for (int i = npwdids - 1; i >= 0; --i) |
| 33 | { |
| 34 | char buf[30]; |
| 35 | snprintf (buf, sizeof (buf), "name%u", pwdids[i]); |
| 36 | |
| 37 | struct passwd *p = getpwnam (buf); |
| 38 | if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0) |
| 39 | { |
| 40 | printf ("passwd entry \"%s\" wrong\n", buf); |
| 41 | retval = 1; |
| 42 | } |
| 43 | |
| 44 | p = getpwuid (pwdids[i]); |
| 45 | if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0) |
| 46 | { |
| 47 | printf ("passwd entry %u wrong\n", pwdids[i]); |
| 48 | retval = 1; |
| 49 | } |
| 50 | |
| 51 | snprintf (buf, sizeof (buf), "name%u", pwdids[i] + 1); |
| 52 | |
| 53 | p = getpwnam (buf); |
| 54 | if (p != NULL) |
| 55 | { |
| 56 | printf ("passwd entry \"%s\" wrong\n", buf); |
| 57 | retval = 1; |
| 58 | } |
| 59 | |
| 60 | p = getpwuid (pwdids[i] + 1); |
| 61 | if (p != NULL) |
| 62 | { |
| 63 | printf ("passwd entry %u wrong\n", pwdids[i] + 1); |
| 64 | retval = 1; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return retval; |
| 69 | } |
| 70 | |
| 71 | #define TEST_FUNCTION do_test () |
| 72 | #include "../test-skeleton.c" |