1 | /* Basic test of passwd database handling. |
2 | Copyright (C) 2017-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
19 | #include <nss.h> |
20 | #include <pwd.h> |
21 | #include <stdio.h> |
22 | #include <stdlib.h> |
23 | #include <string.h> |
24 | |
25 | #include <support/support.h> |
26 | |
27 | #include "nss_test.h" |
28 | |
29 | static int hook_called = 0; |
30 | |
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[] = { |
35 | PWD (100), |
36 | PWD (30), |
37 | PWD (200), |
38 | PWD (60), |
39 | PWD (20000), |
40 | PWD_LAST () |
41 | }; |
42 | |
43 | void |
44 | _nss_test1_init_hook(test_tables *t) |
45 | { |
46 | hook_called = 1; |
47 | t->pwd_table = pwd_table; |
48 | } |
49 | |
50 | static int |
51 | do_test (void) |
52 | { |
53 | int retval = 0; |
54 | |
55 | __nss_configure_lookup (dbname: "passwd" , string: "test1" ); |
56 | |
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])) |
60 | |
61 | setpwent (); |
62 | |
63 | const unsigned int *np = pwdids; |
64 | for (struct passwd *p = getpwent (); p != NULL; ++np, p = getpwent ()) |
65 | { |
66 | retval += compare_passwds (i: np-pwdids, p, e: & pwd_table[np-pwdids]); |
67 | |
68 | if (p->pw_uid != *np || strncmp (p->pw_name, "name" , 4) != 0 |
69 | || atol (nptr: p->pw_name + 4) != *np) |
70 | { |
71 | printf (format: "FAIL: passwd entry %td wrong (%s, %u)\n" , |
72 | np - pwdids, p->pw_name, p->pw_uid); |
73 | retval = 1; |
74 | break; |
75 | } |
76 | } |
77 | |
78 | endpwent (); |
79 | |
80 | for (int i = npwdids - 1; i >= 0; --i) |
81 | { |
82 | char buf[30]; |
83 | snprintf (s: buf, maxlen: sizeof (buf), format: "name%u" , pwdids[i]); |
84 | |
85 | struct passwd *p = getpwnam (name: buf); |
86 | if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0) |
87 | { |
88 | printf (format: "FAIL: passwd entry \"%s\" wrong\n" , buf); |
89 | retval = 1; |
90 | } |
91 | |
92 | p = getpwuid (uid: pwdids[i]); |
93 | if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0) |
94 | { |
95 | printf (format: "FAIL: passwd entry %u wrong\n" , pwdids[i]); |
96 | retval = 1; |
97 | } |
98 | |
99 | snprintf (s: buf, maxlen: sizeof (buf), format: "name%u" , pwdids[i] + 1); |
100 | |
101 | p = getpwnam (name: buf); |
102 | if (p != NULL) |
103 | { |
104 | printf (format: "FAIL: passwd entry \"%s\" wrong\n" , buf); |
105 | retval = 1; |
106 | } |
107 | |
108 | p = getpwuid (uid: pwdids[i] + 1); |
109 | if (p != NULL) |
110 | { |
111 | printf (format: "FAIL: passwd entry %u wrong\n" , pwdids[i] + 1); |
112 | retval = 1; |
113 | } |
114 | } |
115 | |
116 | if (!hook_called) |
117 | { |
118 | retval = 1; |
119 | printf(format: "FAIL: init hook never called\n" ); |
120 | } |
121 | |
122 | return retval; |
123 | } |
124 | |
125 | #include <support/test-driver.c> |
126 | |