2 Unix SMB/CIFS implementation.
4 local testing of the nss wrapper
6 Copyright (C) Guenther Deschner 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "lib/replace/system/passwd.h"
25 #include "lib/nss_wrapper/nss_wrapper.h"
27 static void print_passwd(struct passwd
*pwd
)
29 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
32 (unsigned long)pwd
->pw_uid
,
33 (unsigned long)pwd
->pw_gid
,
40 static bool test_nwrap_getpwnam(struct torture_context
*tctx
,
45 torture_comment(tctx
, "Testing getpwnam: %s\n", name
);
52 return pwd
? true : false;
55 static bool test_nwrap_getpwuid(struct torture_context
*tctx
,
60 torture_comment(tctx
, "Testing getpwuid: %lu\n", (unsigned long)uid
);
67 return pwd
? true : false;
70 static void print_group(struct group
*grp
)
76 (unsigned long)grp
->gr_gid
);
78 if (!grp
->gr_mem
[0]) {
83 for (i
=0; grp
->gr_mem
[i
+1]; i
++) {
84 printf("%s,", grp
->gr_mem
[i
]);
86 printf("%s\n", grp
->gr_mem
[i
]);
89 static bool test_nwrap_getgrnam(struct torture_context
*tctx
,
94 torture_comment(tctx
, "Testing getgrnam: %s\n", name
);
101 return grp
? true : false;
104 static bool test_nwrap_getgrgid(struct torture_context
*tctx
,
109 torture_comment(tctx
, "Testing getgrgid: %lu\n", (unsigned long)gid
);
116 return grp
? true : false;
119 static bool test_nwrap_enum_passwd(struct torture_context
*tctx
,
120 struct passwd
**pwd_array_p
,
124 struct passwd
*pwd_array
= NULL
;
127 torture_comment(tctx
, "Testing setpwent\n");
130 while ((pwd
= getpwent()) != NULL
) {
131 torture_comment(tctx
, "Testing getpwent\n");
134 if (pwd_array_p
&& num_pwd_p
) {
135 pwd_array
= talloc_realloc(tctx
, pwd_array
, struct passwd
, num_pwd
+1);
136 torture_assert(tctx
, pwd_array
, "out of memory");
137 pwd_array
[num_pwd
].pw_name
= talloc_strdup(tctx
, pwd
->pw_name
);
138 pwd_array
[num_pwd
].pw_uid
= pwd
->pw_uid
;
139 pwd_array
[num_pwd
].pw_gid
= pwd
->pw_gid
;
144 torture_comment(tctx
, "Testing endpwent\n");
148 *pwd_array_p
= pwd_array
;
151 *num_pwd_p
= num_pwd
;
157 static bool test_nwrap_passwd(struct torture_context
*tctx
)
163 torture_assert(tctx
, test_nwrap_enum_passwd(tctx
, &pwd
, &num_pwd
),
164 "failed to enumerate passwd");
166 for (i
=0; i
< num_pwd
; i
++) {
167 torture_assert(tctx
, test_nwrap_getpwnam(tctx
, pwd
[i
].pw_name
),
168 "failed to call getpwnam for enumerated user");
169 torture_assert(tctx
, test_nwrap_getpwuid(tctx
, pwd
[i
].pw_uid
),
170 "failed to call getpwuid for enumerated user");
176 static bool test_nwrap_enum_group(struct torture_context
*tctx
,
177 struct group
**grp_array_p
,
181 struct group
*grp_array
= NULL
;
184 torture_comment(tctx
, "Testing setgrent\n");
187 while ((grp
= getgrent()) != NULL
) {
188 torture_comment(tctx
, "Testing getgrent\n");
191 if (grp_array_p
&& num_grp_p
) {
192 grp_array
= talloc_realloc(tctx
, grp_array
, struct group
, num_grp
+1);
193 torture_assert(tctx
, grp_array
, "out of memory");
194 grp_array
[num_grp
].gr_name
= talloc_strdup(tctx
, grp
->gr_name
);
195 grp_array
[num_grp
].gr_gid
= grp
->gr_gid
;
200 torture_comment(tctx
, "Testing endgrent\n");
204 *grp_array_p
= grp_array
;
207 *num_grp_p
= num_grp
;
214 static bool test_nwrap_group(struct torture_context
*tctx
)
220 torture_assert(tctx
, test_nwrap_enum_group(tctx
, &grp
, &num_grp
),
221 "failed to enumerate group");
223 for (i
=0; i
< num_grp
; i
++) {
224 torture_assert(tctx
, test_nwrap_getgrnam(tctx
, grp
[i
].gr_name
),
225 "failed to call getgrnam for enumerated user");
226 torture_assert(tctx
, test_nwrap_getgrgid(tctx
, grp
[i
].gr_gid
),
227 "failed to call getgrgid for enumerated user");
233 static bool test_nwrap_env(struct torture_context
*tctx
)
235 const char *old_pwd
= getenv("NSS_WRAPPER_PASSWD");
236 const char *old_group
= getenv("NSS_WRAPPER_GROUP");
238 if (!old_pwd
|| !old_group
) {
239 torture_skip(tctx
, "nothing to test\n");
243 torture_assert(tctx
, test_nwrap_passwd(tctx
),
244 "failed to test users");
245 torture_assert(tctx
, test_nwrap_group(tctx
),
246 "failed to test groups");
251 struct torture_suite
*torture_local_nss_wrapper(TALLOC_CTX
*mem_ctx
)
253 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "NSS-WRAPPER");
255 torture_suite_add_simple_test(suite
, "env", test_nwrap_env
);