Remove special nss_wrapper code
[Samba.git] / lib / nss_wrapper / testsuite.c
blob6fae1244f08767e3a6081f74b40c2e6fe0218af6
1 /*
2 Unix SMB/CIFS implementation.
4 local testing of the nss wrapper
6 Copyright (C) Guenther Deschner 2009-2010
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/>.
22 #include "includes.h"
24 #ifndef NSS_WRAPPER
25 #define NSS_WRAPPER
26 #endif
28 #include "torture/torture.h"
29 #include "torture/local/proto.h"
30 #include "lib/replace/system/passwd.h"
32 static bool copy_passwd(struct torture_context *tctx,
33 const struct passwd *pwd,
34 struct passwd *p)
36 p->pw_name = talloc_strdup(tctx, pwd->pw_name);
37 p->pw_passwd = talloc_strdup(tctx, pwd->pw_passwd);
38 p->pw_uid = pwd->pw_uid;
39 p->pw_gid = pwd->pw_gid;
40 p->pw_gecos = talloc_strdup(tctx, pwd->pw_gecos);
41 p->pw_dir = talloc_strdup(tctx, pwd->pw_dir);
42 p->pw_shell = talloc_strdup(tctx, pwd->pw_shell);
44 return true;
47 static void print_passwd(struct passwd *pwd)
49 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
50 pwd->pw_name,
51 pwd->pw_passwd,
52 (unsigned long)pwd->pw_uid,
53 (unsigned long)pwd->pw_gid,
54 pwd->pw_gecos,
55 pwd->pw_dir,
56 pwd->pw_shell);
60 static bool test_nwrap_getpwnam(struct torture_context *tctx,
61 const char *name,
62 struct passwd *pwd_p)
64 struct passwd *pwd;
66 torture_comment(tctx, "Testing getpwnam: %s\n", name);
68 pwd = getpwnam(name);
69 if (pwd) {
70 print_passwd(pwd);
73 if (pwd_p) {
74 copy_passwd(tctx, pwd, pwd_p);
77 return pwd ? true : false;
80 static bool test_nwrap_getpwnam_r(struct torture_context *tctx,
81 const char *name,
82 struct passwd *pwd_p)
84 struct passwd pwd, *pwdp;
85 char buffer[4096];
86 int ret;
88 torture_comment(tctx, "Testing getpwnam_r: %s\n", name);
90 ret = getpwnam_r(name, &pwd, buffer, sizeof(buffer), &pwdp);
91 if (ret != 0) {
92 if (ret != ENOENT) {
93 torture_comment(tctx, "got %d return code\n", ret);
95 return false;
98 print_passwd(&pwd);
100 if (pwd_p) {
101 copy_passwd(tctx, &pwd, pwd_p);
104 return true;
107 static bool test_nwrap_getpwuid(struct torture_context *tctx,
108 uid_t uid,
109 struct passwd *pwd_p)
111 struct passwd *pwd;
113 torture_comment(tctx, "Testing getpwuid: %lu\n", (unsigned long)uid);
115 pwd = getpwuid(uid);
116 if (pwd) {
117 print_passwd(pwd);
120 if (pwd_p) {
121 copy_passwd(tctx, pwd, pwd_p);
124 return pwd ? true : false;
127 static bool test_nwrap_getpwuid_r(struct torture_context *tctx,
128 uid_t uid,
129 struct passwd *pwd_p)
131 struct passwd pwd, *pwdp;
132 char buffer[4096];
133 int ret;
135 torture_comment(tctx, "Testing getpwuid_r: %lu\n", (unsigned long)uid);
137 ret = getpwuid_r(uid, &pwd, buffer, sizeof(buffer), &pwdp);
138 if (ret != 0) {
139 if (ret != ENOENT) {
140 torture_comment(tctx, "got %d return code\n", ret);
142 return false;
145 print_passwd(&pwd);
147 if (pwd_p) {
148 copy_passwd(tctx, &pwd, pwd_p);
151 return true;
155 static bool copy_group(struct torture_context *tctx,
156 const struct group *grp,
157 struct group *g)
159 int i;
161 g->gr_name = talloc_strdup(tctx, grp->gr_name);
162 g->gr_passwd = talloc_strdup(tctx, grp->gr_passwd);
163 g->gr_gid = grp->gr_gid;
164 g->gr_mem = NULL;
166 for (i=0; grp->gr_mem && grp->gr_mem[i]; i++) {
167 g->gr_mem = talloc_realloc(tctx, g->gr_mem, char *, i + 2);
168 g->gr_mem[i] = talloc_strdup(g->gr_mem, grp->gr_mem[i]);
169 g->gr_mem[i+1] = NULL;
172 return true;
175 static void print_group(struct group *grp)
177 int i;
178 printf("%s:%s:%lu:",
179 grp->gr_name,
180 grp->gr_passwd,
181 (unsigned long)grp->gr_gid);
183 if ((grp->gr_mem == NULL) || !grp->gr_mem[0]) {
184 printf("\n");
185 return;
188 for (i=0; grp->gr_mem[i+1]; i++) {
189 printf("%s,", grp->gr_mem[i]);
191 printf("%s\n", grp->gr_mem[i]);
194 static bool test_nwrap_getgrnam(struct torture_context *tctx,
195 const char *name,
196 struct group *grp_p)
198 struct group *grp;
200 torture_comment(tctx, "Testing getgrnam: %s\n", name);
202 grp = getgrnam(name);
203 if (grp) {
204 print_group(grp);
207 if (grp_p) {
208 copy_group(tctx, grp, grp_p);
211 return grp ? true : false;
214 static bool test_nwrap_getgrnam_r(struct torture_context *tctx,
215 const char *name,
216 struct group *grp_p)
218 struct group grp, *grpp;
219 char buffer[4096];
220 int ret;
222 torture_comment(tctx, "Testing getgrnam_r: %s\n", name);
224 ret = getgrnam_r(name, &grp, buffer, sizeof(buffer), &grpp);
225 if (ret != 0) {
226 if (ret != ENOENT) {
227 torture_comment(tctx, "got %d return code\n", ret);
229 return false;
232 print_group(&grp);
234 if (grp_p) {
235 copy_group(tctx, &grp, grp_p);
238 return true;
242 static bool test_nwrap_getgrgid(struct torture_context *tctx,
243 gid_t gid,
244 struct group *grp_p)
246 struct group *grp;
248 torture_comment(tctx, "Testing getgrgid: %lu\n", (unsigned long)gid);
250 grp = getgrgid(gid);
251 if (grp) {
252 print_group(grp);
255 if (grp_p) {
256 copy_group(tctx, grp, grp_p);
259 return grp ? true : false;
262 static bool test_nwrap_getgrgid_r(struct torture_context *tctx,
263 gid_t gid,
264 struct group *grp_p)
266 struct group grp, *grpp;
267 char buffer[4096];
268 int ret;
270 torture_comment(tctx, "Testing getgrgid_r: %lu\n", (unsigned long)gid);
272 ret = getgrgid_r(gid, &grp, buffer, sizeof(buffer), &grpp);
273 if (ret != 0) {
274 if (ret != ENOENT) {
275 torture_comment(tctx, "got %d return code\n", ret);
277 return false;
280 print_group(&grp);
282 if (grp_p) {
283 copy_group(tctx, &grp, grp_p);
286 return true;
289 static bool test_nwrap_enum_passwd(struct torture_context *tctx,
290 struct passwd **pwd_array_p,
291 size_t *num_pwd_p)
293 struct passwd *pwd;
294 struct passwd *pwd_array = NULL;
295 size_t num_pwd = 0;
297 torture_comment(tctx, "Testing setpwent\n");
298 setpwent();
300 while ((pwd = getpwent()) != NULL) {
301 torture_comment(tctx, "Testing getpwent\n");
303 print_passwd(pwd);
304 if (pwd_array_p && num_pwd_p) {
305 pwd_array = talloc_realloc(tctx, pwd_array, struct passwd, num_pwd+1);
306 torture_assert(tctx, pwd_array, "out of memory");
307 copy_passwd(tctx, pwd, &pwd_array[num_pwd]);
308 num_pwd++;
312 torture_comment(tctx, "Testing endpwent\n");
313 endpwent();
315 if (pwd_array_p) {
316 *pwd_array_p = pwd_array;
318 if (num_pwd_p) {
319 *num_pwd_p = num_pwd;
322 return true;
325 static bool test_nwrap_enum_r_passwd(struct torture_context *tctx,
326 struct passwd **pwd_array_p,
327 size_t *num_pwd_p)
329 struct passwd pwd, *pwdp;
330 struct passwd *pwd_array = NULL;
331 size_t num_pwd = 0;
332 char buffer[4096];
333 int ret;
335 torture_comment(tctx, "Testing setpwent\n");
336 setpwent();
338 while (1) {
339 torture_comment(tctx, "Testing getpwent_r\n");
341 ret = getpwent_r(&pwd, buffer, sizeof(buffer), &pwdp);
342 if (ret != 0) {
343 if (ret != ENOENT) {
344 torture_comment(tctx, "got %d return code\n", ret);
346 break;
348 print_passwd(&pwd);
349 if (pwd_array_p && num_pwd_p) {
350 pwd_array = talloc_realloc(tctx, pwd_array, struct passwd, num_pwd+1);
351 torture_assert(tctx, pwd_array, "out of memory");
352 copy_passwd(tctx, &pwd, &pwd_array[num_pwd]);
353 num_pwd++;
357 torture_comment(tctx, "Testing endpwent\n");
358 endpwent();
360 if (pwd_array_p) {
361 *pwd_array_p = pwd_array;
363 if (num_pwd_p) {
364 *num_pwd_p = num_pwd;
367 return true;
370 static bool torture_assert_passwd_equal(struct torture_context *tctx,
371 const struct passwd *p1,
372 const struct passwd *p2,
373 const char *comment)
375 torture_assert_str_equal(tctx, p1->pw_name, p2->pw_name, comment);
376 torture_assert_str_equal(tctx, p1->pw_passwd, p2->pw_passwd, comment);
377 torture_assert_int_equal(tctx, p1->pw_uid, p2->pw_uid, comment);
378 torture_assert_int_equal(tctx, p1->pw_gid, p2->pw_gid, comment);
379 torture_assert_str_equal(tctx, p1->pw_gecos, p2->pw_gecos, comment);
380 torture_assert_str_equal(tctx, p1->pw_dir, p2->pw_dir, comment);
381 torture_assert_str_equal(tctx, p1->pw_shell, p2->pw_shell, comment);
383 return true;
386 static bool test_nwrap_passwd(struct torture_context *tctx)
388 int i;
389 struct passwd *pwd, pwd1, pwd2;
390 size_t num_pwd;
392 torture_assert(tctx, test_nwrap_enum_passwd(tctx, &pwd, &num_pwd),
393 "failed to enumerate passwd");
395 for (i=0; i < num_pwd; i++) {
396 torture_assert(tctx, test_nwrap_getpwnam(tctx, pwd[i].pw_name, &pwd1),
397 "failed to call getpwnam for enumerated user");
398 torture_assert_passwd_equal(tctx, &pwd[i], &pwd1,
399 "getpwent and getpwnam gave different results");
400 torture_assert(tctx, test_nwrap_getpwuid(tctx, pwd[i].pw_uid, &pwd2),
401 "failed to call getpwuid for enumerated user");
402 torture_assert_passwd_equal(tctx, &pwd[i], &pwd2,
403 "getpwent and getpwuid gave different results");
404 torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
405 "getpwnam and getpwuid gave different results");
408 return true;
411 static bool test_nwrap_passwd_r(struct torture_context *tctx)
413 int i;
414 struct passwd *pwd, pwd1, pwd2;
415 size_t num_pwd;
417 torture_assert(tctx, test_nwrap_enum_r_passwd(tctx, &pwd, &num_pwd),
418 "failed to enumerate passwd");
420 for (i=0; i < num_pwd; i++) {
421 torture_assert(tctx, test_nwrap_getpwnam_r(tctx, pwd[i].pw_name, &pwd1),
422 "failed to call getpwnam_r for enumerated user");
423 torture_assert_passwd_equal(tctx, &pwd[i], &pwd1,
424 "getpwent_r and getpwnam_r gave different results");
425 torture_assert(tctx, test_nwrap_getpwuid_r(tctx, pwd[i].pw_uid, &pwd2),
426 "failed to call getpwuid_r for enumerated user");
427 torture_assert_passwd_equal(tctx, &pwd[i], &pwd2,
428 "getpwent_r and getpwuid_r gave different results");
429 torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
430 "getpwnam_r and getpwuid_r gave different results");
433 return true;
436 static bool test_nwrap_passwd_r_cross(struct torture_context *tctx)
438 int i;
439 struct passwd *pwd, pwd1, pwd2, pwd3, pwd4;
440 size_t num_pwd;
442 torture_assert(tctx, test_nwrap_enum_r_passwd(tctx, &pwd, &num_pwd),
443 "failed to enumerate passwd");
445 for (i=0; i < num_pwd; i++) {
446 torture_assert(tctx, test_nwrap_getpwnam_r(tctx, pwd[i].pw_name, &pwd1),
447 "failed to call getpwnam_r for enumerated user");
448 torture_assert_passwd_equal(tctx, &pwd[i], &pwd1,
449 "getpwent_r and getpwnam_r gave different results");
450 torture_assert(tctx, test_nwrap_getpwuid_r(tctx, pwd[i].pw_uid, &pwd2),
451 "failed to call getpwuid_r for enumerated user");
452 torture_assert_passwd_equal(tctx, &pwd[i], &pwd2,
453 "getpwent_r and getpwuid_r gave different results");
454 torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
455 "getpwnam_r and getpwuid_r gave different results");
456 torture_assert(tctx, test_nwrap_getpwnam(tctx, pwd[i].pw_name, &pwd3),
457 "failed to call getpwnam for enumerated user");
458 torture_assert_passwd_equal(tctx, &pwd[i], &pwd3,
459 "getpwent_r and getpwnam gave different results");
460 torture_assert(tctx, test_nwrap_getpwuid(tctx, pwd[i].pw_uid, &pwd4),
461 "failed to call getpwuid for enumerated user");
462 torture_assert_passwd_equal(tctx, &pwd[i], &pwd4,
463 "getpwent_r and getpwuid gave different results");
464 torture_assert_passwd_equal(tctx, &pwd3, &pwd4,
465 "getpwnam and getpwuid gave different results");
468 return true;
471 static bool test_nwrap_enum_group(struct torture_context *tctx,
472 struct group **grp_array_p,
473 size_t *num_grp_p)
475 struct group *grp;
476 struct group *grp_array = NULL;
477 size_t num_grp = 0;
479 torture_comment(tctx, "Testing setgrent\n");
480 setgrent();
482 while ((grp = getgrent()) != NULL) {
483 torture_comment(tctx, "Testing getgrent\n");
485 print_group(grp);
486 if (grp_array_p && num_grp_p) {
487 grp_array = talloc_realloc(tctx, grp_array, struct group, num_grp+1);
488 torture_assert(tctx, grp_array, "out of memory");
489 copy_group(tctx, grp, &grp_array[num_grp]);
490 num_grp++;
494 torture_comment(tctx, "Testing endgrent\n");
495 endgrent();
497 if (grp_array_p) {
498 *grp_array_p = grp_array;
500 if (num_grp_p) {
501 *num_grp_p = num_grp;
504 return true;
507 static bool test_nwrap_enum_r_group(struct torture_context *tctx,
508 struct group **grp_array_p,
509 size_t *num_grp_p)
511 struct group grp, *grpp;
512 struct group *grp_array = NULL;
513 size_t num_grp = 0;
514 char buffer[4096];
515 int ret;
517 torture_comment(tctx, "Testing setgrent\n");
518 setgrent();
520 while (1) {
521 torture_comment(tctx, "Testing getgrent_r\n");
523 ret = getgrent_r(&grp, buffer, sizeof(buffer), &grpp);
524 if (ret != 0) {
525 if (ret != ENOENT) {
526 torture_comment(tctx, "got %d return code\n", ret);
528 break;
530 print_group(&grp);
531 if (grp_array_p && num_grp_p) {
532 grp_array = talloc_realloc(tctx, grp_array, struct group, num_grp+1);
533 torture_assert(tctx, grp_array, "out of memory");
534 copy_group(tctx, &grp, &grp_array[num_grp]);
535 num_grp++;
539 torture_comment(tctx, "Testing endgrent\n");
540 endgrent();
542 if (grp_array_p) {
543 *grp_array_p = grp_array;
545 if (num_grp_p) {
546 *num_grp_p = num_grp;
549 return true;
552 static bool torture_assert_group_equal(struct torture_context *tctx,
553 const struct group *g1,
554 const struct group *g2,
555 const char *comment)
557 int i;
558 torture_assert_str_equal(tctx, g1->gr_name, g2->gr_name, comment);
559 torture_assert_str_equal(tctx, g1->gr_passwd, g2->gr_passwd, comment);
560 torture_assert_int_equal(tctx, g1->gr_gid, g2->gr_gid, comment);
561 if (g1->gr_mem && !g2->gr_mem) {
562 return false;
564 if (!g1->gr_mem && g2->gr_mem) {
565 return false;
567 if (!g1->gr_mem && !g2->gr_mem) {
568 return true;
570 for (i=0; g1->gr_mem[i] && g2->gr_mem[i]; i++) {
571 torture_assert_str_equal(tctx, g1->gr_mem[i], g2->gr_mem[i], comment);
574 return true;
577 static bool test_nwrap_group(struct torture_context *tctx)
579 int i;
580 struct group *grp, grp1, grp2;
581 size_t num_grp;
583 torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
584 "failed to enumerate group");
586 for (i=0; i < num_grp; i++) {
587 torture_assert(tctx, test_nwrap_getgrnam(tctx, grp[i].gr_name, &grp1),
588 "failed to call getgrnam for enumerated user");
589 torture_assert_group_equal(tctx, &grp[i], &grp1,
590 "getgrent and getgrnam gave different results");
591 torture_assert(tctx, test_nwrap_getgrgid(tctx, grp[i].gr_gid, &grp2),
592 "failed to call getgrgid for enumerated user");
593 torture_assert_group_equal(tctx, &grp[i], &grp2,
594 "getgrent and getgrgid gave different results");
595 torture_assert_group_equal(tctx, &grp1, &grp2,
596 "getgrnam and getgrgid gave different results");
599 return true;
602 static bool test_nwrap_group_r(struct torture_context *tctx)
604 int i;
605 struct group *grp, grp1, grp2;
606 size_t num_grp;
608 torture_assert(tctx, test_nwrap_enum_r_group(tctx, &grp, &num_grp),
609 "failed to enumerate group");
611 for (i=0; i < num_grp; i++) {
612 torture_assert(tctx, test_nwrap_getgrnam_r(tctx, grp[i].gr_name, &grp1),
613 "failed to call getgrnam_r for enumerated user");
614 torture_assert_group_equal(tctx, &grp[i], &grp1,
615 "getgrent_r and getgrnam_r gave different results");
616 torture_assert(tctx, test_nwrap_getgrgid_r(tctx, grp[i].gr_gid, &grp2),
617 "failed to call getgrgid_r for enumerated user");
618 torture_assert_group_equal(tctx, &grp[i], &grp2,
619 "getgrent_r and getgrgid_r gave different results");
620 torture_assert_group_equal(tctx, &grp1, &grp2,
621 "getgrnam_r and getgrgid_r gave different results");
624 return true;
627 static bool test_nwrap_group_r_cross(struct torture_context *tctx)
629 int i;
630 struct group *grp, grp1, grp2, grp3, grp4;
631 size_t num_grp;
633 torture_assert(tctx, test_nwrap_enum_r_group(tctx, &grp, &num_grp),
634 "failed to enumerate group");
636 for (i=0; i < num_grp; i++) {
637 torture_assert(tctx, test_nwrap_getgrnam_r(tctx, grp[i].gr_name, &grp1),
638 "failed to call getgrnam_r for enumerated user");
639 torture_assert_group_equal(tctx, &grp[i], &grp1,
640 "getgrent_r and getgrnam_r gave different results");
641 torture_assert(tctx, test_nwrap_getgrgid_r(tctx, grp[i].gr_gid, &grp2),
642 "failed to call getgrgid_r for enumerated user");
643 torture_assert_group_equal(tctx, &grp[i], &grp2,
644 "getgrent_r and getgrgid_r gave different results");
645 torture_assert_group_equal(tctx, &grp1, &grp2,
646 "getgrnam_r and getgrgid_r gave different results");
647 torture_assert(tctx, test_nwrap_getgrnam(tctx, grp[i].gr_name, &grp3),
648 "failed to call getgrnam for enumerated user");
649 torture_assert_group_equal(tctx, &grp[i], &grp3,
650 "getgrent_r and getgrnam gave different results");
651 torture_assert(tctx, test_nwrap_getgrgid(tctx, grp[i].gr_gid, &grp4),
652 "failed to call getgrgid for enumerated user");
653 torture_assert_group_equal(tctx, &grp[i], &grp4,
654 "getgrent_r and getgrgid gave different results");
655 torture_assert_group_equal(tctx, &grp3, &grp4,
656 "getgrnam and getgrgid gave different results");
659 return true;
662 static bool test_nwrap_getgrouplist(struct torture_context *tctx,
663 const char *user,
664 gid_t gid,
665 gid_t **gids_p,
666 int *num_gids_p)
668 int ret;
669 int num_groups = 0;
670 gid_t *groups = NULL;
672 torture_comment(tctx, "Testing getgrouplist: %s\n", user);
674 ret = getgrouplist(user, gid, NULL, &num_groups);
675 if (ret == -1 || num_groups != 0) {
677 groups = talloc_array(tctx, gid_t, num_groups);
678 torture_assert(tctx, groups, "out of memory\n");
680 ret = getgrouplist(user, gid, groups, &num_groups);
683 torture_assert(tctx, (ret != -1), "failed to call getgrouplist");
685 torture_comment(tctx, "%s is member in %d groups\n", user, num_groups);
687 if (gids_p) {
688 *gids_p = groups;
690 if (num_gids_p) {
691 *num_gids_p = num_groups;
694 return true;
697 static bool test_nwrap_user_in_group(struct torture_context *tctx,
698 const struct passwd *pwd,
699 const struct group *grp)
701 int i;
703 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
704 if (strequal(grp->gr_mem[i], pwd->pw_name)) {
705 return true;
709 return false;
712 static bool test_nwrap_membership_user(struct torture_context *tctx,
713 const struct passwd *pwd,
714 struct group *grp_array,
715 size_t num_grp)
717 int num_user_groups = 0;
718 int num_user_groups_from_enum = 0;
719 gid_t *user_groups = NULL;
720 int g, i;
721 bool primary_group_had_user_member = false;
723 torture_assert(tctx, test_nwrap_getgrouplist(tctx,
724 pwd->pw_name,
725 pwd->pw_gid,
726 &user_groups,
727 &num_user_groups),
728 "failed to test getgrouplist");
730 for (g=0; g < num_user_groups; g++) {
731 torture_assert(tctx, test_nwrap_getgrgid(tctx, user_groups[g], NULL),
732 "failed to find the group the user is a member of");
736 for (i=0; i < num_grp; i++) {
738 struct group grp = grp_array[i];
740 if (test_nwrap_user_in_group(tctx, pwd, &grp)) {
742 struct group current_grp;
743 num_user_groups_from_enum++;
745 torture_assert(tctx, test_nwrap_getgrnam(tctx, grp.gr_name, &current_grp),
746 "failed to find the group the user is a member of");
748 if (current_grp.gr_gid == pwd->pw_gid) {
749 torture_comment(tctx, "primary group %s of user %s lists user as member\n",
750 current_grp.gr_name,
751 pwd->pw_name);
752 primary_group_had_user_member = true;
755 continue;
759 if (!primary_group_had_user_member) {
760 num_user_groups_from_enum++;
763 torture_assert_int_equal(tctx, num_user_groups, num_user_groups_from_enum,
764 "getgrouplist and real inspection of grouplist gave different results\n");
766 return true;
769 static bool test_nwrap_membership(struct torture_context *tctx)
771 const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
772 const char *old_group = getenv("NSS_WRAPPER_GROUP");
773 struct passwd *pwd;
774 size_t num_pwd;
775 struct group *grp;
776 size_t num_grp;
777 int i;
779 if (!old_pwd || !old_group) {
780 torture_comment(tctx, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
781 torture_skip(tctx, "nothing to test\n");
784 torture_assert(tctx, test_nwrap_enum_passwd(tctx, &pwd, &num_pwd),
785 "failed to enumerate passwd");
786 torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
787 "failed to enumerate group");
789 for (i=0; i < num_pwd; i++) {
791 torture_assert(tctx, test_nwrap_membership_user(tctx, &pwd[i], grp, num_grp),
792 "failed to test membership for user");
796 return true;
799 static bool test_nwrap_enumeration(struct torture_context *tctx)
801 const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
802 const char *old_group = getenv("NSS_WRAPPER_GROUP");
804 if (!old_pwd || !old_group) {
805 torture_comment(tctx, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
806 torture_skip(tctx, "nothing to test\n");
809 torture_assert(tctx, test_nwrap_passwd(tctx),
810 "failed to test users");
811 torture_assert(tctx, test_nwrap_group(tctx),
812 "failed to test groups");
814 return true;
817 static bool test_nwrap_reentrant_enumeration(struct torture_context *tctx)
819 const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
820 const char *old_group = getenv("NSS_WRAPPER_GROUP");
822 if (!old_pwd || !old_group) {
823 torture_comment(tctx, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
824 torture_skip(tctx, "nothing to test\n");
827 torture_comment(tctx, "Testing re-entrant calls\n");
829 torture_assert(tctx, test_nwrap_passwd_r(tctx),
830 "failed to test users");
831 torture_assert(tctx, test_nwrap_group_r(tctx),
832 "failed to test groups");
834 return true;
837 static bool test_nwrap_reentrant_enumeration_crosschecks(struct torture_context *tctx)
839 const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
840 const char *old_group = getenv("NSS_WRAPPER_GROUP");
842 if (!old_pwd || !old_group) {
843 torture_comment(tctx, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
844 torture_skip(tctx, "nothing to test\n");
847 torture_comment(tctx, "Testing re-entrant calls with cross checks\n");
849 torture_assert(tctx, test_nwrap_passwd_r_cross(tctx),
850 "failed to test users");
851 torture_assert(tctx, test_nwrap_group_r_cross(tctx),
852 "failed to test groups");
854 return true;
857 static bool test_nwrap_passwd_duplicates(struct torture_context *tctx)
859 int i, d;
860 struct passwd *pwd;
861 size_t num_pwd;
862 int duplicates = 0;
864 torture_assert(tctx, test_nwrap_enum_passwd(tctx, &pwd, &num_pwd),
865 "failed to enumerate passwd");
867 for (i=0; i < num_pwd; i++) {
868 const char *current_name = pwd[i].pw_name;
869 for (d=0; d < num_pwd; d++) {
870 const char *dup_name = pwd[d].pw_name;
871 if (d == i) {
872 continue;
874 if (!strequal(current_name, dup_name)) {
875 continue;
878 torture_warning(tctx, "found duplicate names:");
879 print_passwd(&pwd[d]);
880 print_passwd(&pwd[i]);
881 duplicates++;
885 if (duplicates) {
886 torture_fail(tctx, talloc_asprintf(tctx, "found %d duplicate names", duplicates));
889 return true;
892 static bool test_nwrap_group_duplicates(struct torture_context *tctx)
894 int i, d;
895 struct group *grp;
896 size_t num_grp;
897 int duplicates = 0;
899 torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
900 "failed to enumerate group");
902 for (i=0; i < num_grp; i++) {
903 const char *current_name = grp[i].gr_name;
904 for (d=0; d < num_grp; d++) {
905 const char *dup_name = grp[d].gr_name;
906 if (d == i) {
907 continue;
909 if (!strequal(current_name, dup_name)) {
910 continue;
913 torture_warning(tctx, "found duplicate names:");
914 print_group(&grp[d]);
915 print_group(&grp[i]);
916 duplicates++;
920 if (duplicates) {
921 torture_fail(tctx, talloc_asprintf(tctx, "found %d duplicate names", duplicates));
924 return true;
928 static bool test_nwrap_duplicates(struct torture_context *tctx)
930 const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
931 const char *old_group = getenv("NSS_WRAPPER_GROUP");
933 if (!old_pwd || !old_group) {
934 torture_comment(tctx, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
935 torture_skip(tctx, "nothing to test\n");
938 torture_assert(tctx, test_nwrap_passwd_duplicates(tctx),
939 "failed to test users");
940 torture_assert(tctx, test_nwrap_group_duplicates(tctx),
941 "failed to test groups");
943 return true;
947 struct torture_suite *torture_local_nss_wrapper(TALLOC_CTX *mem_ctx)
949 struct torture_suite *suite = torture_suite_create(mem_ctx, "nss-wrapper");
951 torture_suite_add_simple_test(suite, "enumeration", test_nwrap_enumeration);
952 torture_suite_add_simple_test(suite, "reentrant enumeration", test_nwrap_reentrant_enumeration);
953 torture_suite_add_simple_test(suite, "reentrant enumeration crosschecks", test_nwrap_reentrant_enumeration_crosschecks);
954 torture_suite_add_simple_test(suite, "membership", test_nwrap_membership);
955 torture_suite_add_simple_test(suite, "duplicates", test_nwrap_duplicates);
957 return suite;