1 /* Basic test of passwd database handling.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
25 #include <support/support.h>
29 static int hook_called
= 0;
31 /* Note: the values chosen here are arbitrary; they need only be
32 unique within the table. However, they do need to match the
33 "pwdids" array further down. */
34 static struct passwd pwd_table
[] = {
44 _nss_test1_init_hook(test_tables
*t
)
47 t
->pwd_table
= pwd_table
;
55 __nss_configure_lookup ("passwd", "test1");
57 /* This must match the pwd_table above. */
58 static const unsigned int pwdids
[] = { 100, 30, 200, 60, 20000 };
59 #define npwdids (sizeof (pwdids) / sizeof (pwdids[0]))
63 const unsigned int *np
= pwdids
;
64 for (struct passwd
*p
= getpwent (); p
!= NULL
; ++np
, p
= getpwent ())
66 retval
+= compare_passwds (np
-pwdids
, p
, & pwd_table
[np
-pwdids
]);
68 if (p
->pw_uid
!= *np
|| strncmp (p
->pw_name
, "name", 4) != 0
69 || atol (p
->pw_name
+ 4) != *np
)
71 printf ("FAIL: passwd entry %td wrong (%s, %u)\n",
72 np
- pwdids
, p
->pw_name
, p
->pw_uid
);
80 for (int i
= npwdids
- 1; i
>= 0; --i
)
83 snprintf (buf
, sizeof (buf
), "name%u", pwdids
[i
]);
85 struct passwd
*p
= getpwnam (buf
);
86 if (p
== NULL
|| p
->pw_uid
!= pwdids
[i
] || strcmp (buf
, p
->pw_name
) != 0)
88 printf ("FAIL: passwd entry \"%s\" wrong\n", buf
);
92 p
= getpwuid (pwdids
[i
]);
93 if (p
== NULL
|| p
->pw_uid
!= pwdids
[i
] || strcmp (buf
, p
->pw_name
) != 0)
95 printf ("FAIL: passwd entry %u wrong\n", pwdids
[i
]);
99 snprintf (buf
, sizeof (buf
), "name%u", pwdids
[i
] + 1);
104 printf ("FAIL: passwd entry \"%s\" wrong\n", buf
);
108 p
= getpwuid (pwdids
[i
] + 1);
111 printf ("FAIL: passwd entry %u wrong\n", pwdids
[i
] + 1);
119 printf("FAIL: init hook never called\n");
125 #include <support/test-driver.c>