1 /* Basic test of passwd database handling.
2 Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. */
27 static int hook_called
= 0;
29 /* Note: the values chosen here are arbitrary; they need only be
30 unique within the table. However, they do need to match the
31 "pwdids" array further down. */
32 static struct passwd pwd_table
[] = {
42 _nss_test1_init_hook(test_tables
*t
)
45 t
->pwd_table
= pwd_table
;
53 __nss_configure_lookup ("passwd", "test1");
55 /* This must match the pwd_table above. */
56 static const unsigned int pwdids
[] = { 100, 30, 200, 60, 20000 };
57 #define npwdids (sizeof (pwdids) / sizeof (pwdids[0]))
61 const unsigned int *np
= pwdids
;
62 for (struct passwd
*p
= getpwent (); p
!= NULL
; ++np
, p
= getpwent ())
64 retval
+= compare_passwds (np
-pwdids
, p
, & pwd_table
[np
-pwdids
]);
66 if (p
->pw_uid
!= *np
|| strncmp (p
->pw_name
, "name", 4) != 0
67 || atol (p
->pw_name
+ 4) != *np
)
69 printf ("FAIL: passwd entry %td wrong (%s, %u)\n",
70 np
- pwdids
, p
->pw_name
, p
->pw_uid
);
78 for (int i
= npwdids
- 1; i
>= 0; --i
)
81 snprintf (buf
, sizeof (buf
), "name%u", pwdids
[i
]);
83 struct passwd
*p
= getpwnam (buf
);
84 if (p
== NULL
|| p
->pw_uid
!= pwdids
[i
] || strcmp (buf
, p
->pw_name
) != 0)
86 printf ("FAIL: passwd entry \"%s\" wrong\n", buf
);
90 p
= getpwuid (pwdids
[i
]);
91 if (p
== NULL
|| p
->pw_uid
!= pwdids
[i
] || strcmp (buf
, p
->pw_name
) != 0)
93 printf ("FAIL: passwd entry %u wrong\n", pwdids
[i
]);
97 snprintf (buf
, sizeof (buf
), "name%u", pwdids
[i
] + 1);
102 printf ("FAIL: passwd entry \"%s\" wrong\n", buf
);
106 p
= getpwuid (pwdids
[i
] + 1);
109 printf ("FAIL: passwd entry %u wrong\n", pwdids
[i
] + 1);
117 printf("FAIL: init hook never called\n");
123 #define TEST_FUNCTION do_test ()
124 #include "../test-skeleton.c"