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/>.
24 #include "torture/torture.h"
25 #include "torture/local/proto.h"
26 #include "lib/replace/system/passwd.h"
28 static bool copy_passwd(struct torture_context
*tctx
,
29 const struct passwd
*pwd
,
32 p
->pw_name
= talloc_strdup(tctx
, pwd
->pw_name
);
33 torture_assert(tctx
, (p
->pw_name
!= NULL
|| pwd
->pw_name
== NULL
), __location__
);
34 p
->pw_passwd
= talloc_strdup(tctx
, pwd
->pw_passwd
);
35 torture_assert(tctx
, (p
->pw_passwd
!= NULL
|| pwd
->pw_passwd
== NULL
), __location__
);
36 p
->pw_uid
= pwd
->pw_uid
;
37 p
->pw_gid
= pwd
->pw_gid
;
38 p
->pw_gecos
= talloc_strdup(tctx
, pwd
->pw_gecos
);
39 torture_assert(tctx
, (p
->pw_gecos
!= NULL
|| pwd
->pw_gecos
== NULL
), __location__
);
40 p
->pw_dir
= talloc_strdup(tctx
, pwd
->pw_dir
);
41 torture_assert(tctx
, (p
->pw_dir
!= NULL
|| pwd
->pw_dir
== NULL
), __location__
);
42 p
->pw_shell
= talloc_strdup(tctx
, pwd
->pw_shell
);
43 torture_assert(tctx
, (p
->pw_shell
!= NULL
|| pwd
->pw_shell
== NULL
), __location__
);
48 static void print_passwd(struct passwd
*pwd
)
50 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
53 (unsigned long)pwd
->pw_uid
,
54 (unsigned long)pwd
->pw_gid
,
61 static bool test_getpwnam(struct torture_context
*tctx
,
68 torture_comment(tctx
, "Testing getpwnam: %s\n", name
);
73 torture_assert(tctx
, (pwd
!= NULL
), talloc_asprintf(tctx
,
74 "getpwnam(%s) failed - %d - %s",
75 name
, ret
, strerror(ret
)));
78 torture_assert(tctx
, copy_passwd(tctx
, pwd
, pwd_p
), __location__
);
84 static bool test_getpwnam_r(struct torture_context
*tctx
,
88 struct passwd pwd
, *pwdp
;
92 torture_comment(tctx
, "Testing getpwnam_r: %s\n", name
);
94 ret
= getpwnam_r(name
, &pwd
, buffer
, sizeof(buffer
), &pwdp
);
95 torture_assert(tctx
, ret
== 0, talloc_asprintf(tctx
,
96 "getpwnam_r(%s) failed - %d - %s",
97 name
, ret
, strerror(ret
)));
102 torture_assert(tctx
, copy_passwd(tctx
, &pwd
, pwd_p
), __location__
);
108 static bool test_getpwuid(struct torture_context
*tctx
,
110 struct passwd
*pwd_p
)
115 torture_comment(tctx
, "Testing getpwuid: %lu\n", (unsigned long)uid
);
120 torture_assert(tctx
, (pwd
!= NULL
), talloc_asprintf(tctx
,
121 "getpwuid(%lu) failed - %d - %s",
122 (unsigned long)uid
, ret
, strerror(ret
)));
127 torture_assert(tctx
, copy_passwd(tctx
, pwd
, pwd_p
), __location__
);
133 static bool test_getpwuid_r(struct torture_context
*tctx
,
135 struct passwd
*pwd_p
)
137 struct passwd pwd
, *pwdp
;
141 torture_comment(tctx
, "Testing getpwuid_r: %lu\n", (unsigned long)uid
);
143 ret
= getpwuid_r(uid
, &pwd
, buffer
, sizeof(buffer
), &pwdp
);
144 torture_assert(tctx
, ret
== 0, talloc_asprintf(tctx
,
145 "getpwuid_r(%lu) failed - %d - %s",
146 (unsigned long)uid
, ret
, strerror(ret
)));
151 torture_assert(tctx
, copy_passwd(tctx
, &pwd
, pwd_p
), __location__
);
158 static bool copy_group(struct torture_context
*tctx
,
159 const struct group
*grp
,
164 g
->gr_name
= talloc_strdup(tctx
, grp
->gr_name
);
165 torture_assert(tctx
, (g
->gr_name
!= NULL
|| grp
->gr_name
== NULL
), __location__
);
166 g
->gr_passwd
= talloc_strdup(tctx
, grp
->gr_passwd
);
167 torture_assert(tctx
, (g
->gr_passwd
!= NULL
|| grp
->gr_passwd
== NULL
), __location__
);
168 g
->gr_gid
= grp
->gr_gid
;
171 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
]; i
++) {
172 g
->gr_mem
= talloc_realloc(tctx
, g
->gr_mem
, char *, i
+ 2);
173 torture_assert(tctx
, (g
->gr_mem
!= NULL
), __location__
);
174 g
->gr_mem
[i
] = talloc_strdup(g
->gr_mem
, grp
->gr_mem
[i
]);
175 torture_assert(tctx
, (g
->gr_mem
[i
] != NULL
), __location__
);
176 g
->gr_mem
[i
+1] = NULL
;
182 static void print_group(struct group
*grp
)
188 (unsigned long)grp
->gr_gid
);
190 if ((grp
->gr_mem
== NULL
) || !grp
->gr_mem
[0]) {
195 for (i
=0; grp
->gr_mem
[i
+1]; i
++) {
196 printf("%s,", grp
->gr_mem
[i
]);
198 printf("%s\n", grp
->gr_mem
[i
]);
201 static bool test_getgrnam(struct torture_context
*tctx
,
208 torture_comment(tctx
, "Testing getgrnam: %s\n", name
);
211 grp
= getgrnam(name
);
213 torture_assert(tctx
, (grp
!= NULL
), talloc_asprintf(tctx
,
214 "getgrnam(%s) failed - %d - %s",
215 name
, ret
, strerror(ret
)));
220 torture_assert(tctx
, copy_group(tctx
, grp
, grp_p
), __location__
);
226 static bool test_getgrnam_r(struct torture_context
*tctx
,
230 struct group grp
, *grpp
;
234 torture_comment(tctx
, "Testing getgrnam_r: %s\n", name
);
236 ret
= getgrnam_r(name
, &grp
, buffer
, sizeof(buffer
), &grpp
);
237 torture_assert(tctx
, ret
== 0, talloc_asprintf(tctx
,
238 "getgrnam_r(%s) failed - %d - %s",
239 name
, ret
, strerror(ret
)));
244 torture_assert(tctx
, copy_group(tctx
, &grp
, grp_p
), __location__
);
251 static bool test_getgrgid(struct torture_context
*tctx
,
258 torture_comment(tctx
, "Testing getgrgid: %lu\n", (unsigned long)gid
);
263 torture_assert(tctx
, (grp
!= NULL
), talloc_asprintf(tctx
,
264 "getgrgid(%lu) failed - %d - %s",
265 (unsigned long)gid
, ret
, strerror(ret
)));
270 torture_assert(tctx
, copy_group(tctx
, grp
, grp_p
), __location__
);
276 static bool test_getgrgid_r(struct torture_context
*tctx
,
280 struct group grp
, *grpp
;
284 torture_comment(tctx
, "Testing getgrgid_r: %lu\n", (unsigned long)gid
);
286 ret
= getgrgid_r(gid
, &grp
, buffer
, sizeof(buffer
), &grpp
);
287 torture_assert(tctx
, ret
== 0, talloc_asprintf(tctx
,
288 "getgrgid_r(%lu) failed - %d - %s",
289 (unsigned long)gid
, ret
, strerror(ret
)));
294 torture_assert(tctx
, copy_group(tctx
, &grp
, grp_p
), __location__
);
300 static bool test_enum_passwd(struct torture_context
*tctx
,
301 struct passwd
**pwd_array_p
,
305 struct passwd
*pwd_array
= NULL
;
308 torture_comment(tctx
, "Testing setpwent\n");
311 while ((pwd
= getpwent()) != NULL
) {
312 torture_comment(tctx
, "Testing getpwent\n");
315 if (pwd_array_p
&& num_pwd_p
) {
316 pwd_array
= talloc_realloc(tctx
, pwd_array
, struct passwd
, num_pwd
+1);
317 torture_assert(tctx
, pwd_array
, "out of memory");
318 copy_passwd(tctx
, pwd
, &pwd_array
[num_pwd
]);
323 torture_comment(tctx
, "Testing endpwent\n");
327 *pwd_array_p
= pwd_array
;
330 *num_pwd_p
= num_pwd
;
336 static bool test_enum_r_passwd(struct torture_context
*tctx
,
337 struct passwd
**pwd_array_p
,
340 struct passwd pwd
, *pwdp
;
341 struct passwd
*pwd_array
= NULL
;
346 torture_comment(tctx
, "Testing setpwent\n");
350 torture_comment(tctx
, "Testing getpwent_r\n");
352 #ifdef SOLARIS_GETPWENT_R
353 ret
= getpwent_r(&pwd
, buffer
, sizeof(buffer
));
354 #else /* SOLARIS_GETPWENT_R */
355 ret
= getpwent_r(&pwd
, buffer
, sizeof(buffer
), &pwdp
);
356 #endif /* SOLARIS_GETPWENT_R */
359 torture_comment(tctx
, "got %d return code\n", ret
);
364 if (pwd_array_p
&& num_pwd_p
) {
365 pwd_array
= talloc_realloc(tctx
, pwd_array
, struct passwd
, num_pwd
+1);
366 torture_assert(tctx
, pwd_array
, "out of memory");
367 copy_passwd(tctx
, &pwd
, &pwd_array
[num_pwd
]);
372 torture_comment(tctx
, "Testing endpwent\n");
376 *pwd_array_p
= pwd_array
;
379 *num_pwd_p
= num_pwd
;
385 static bool torture_assert_passwd_equal(struct torture_context
*tctx
,
386 const struct passwd
*p1
,
387 const struct passwd
*p2
,
390 torture_assert_str_equal(tctx
, p1
->pw_name
, p2
->pw_name
, comment
);
391 torture_assert_str_equal(tctx
, p1
->pw_passwd
, p2
->pw_passwd
, comment
);
392 torture_assert_int_equal(tctx
, p1
->pw_uid
, p2
->pw_uid
, comment
);
393 torture_assert_int_equal(tctx
, p1
->pw_gid
, p2
->pw_gid
, comment
);
394 torture_assert_str_equal(tctx
, p1
->pw_gecos
, p2
->pw_gecos
, comment
);
395 torture_assert_str_equal(tctx
, p1
->pw_dir
, p2
->pw_dir
, comment
);
396 torture_assert_str_equal(tctx
, p1
->pw_shell
, p2
->pw_shell
, comment
);
401 static bool test_passwd(struct torture_context
*tctx
)
404 struct passwd
*pwd
, pwd1
, pwd2
;
407 torture_assert(tctx
, test_enum_passwd(tctx
, &pwd
, &num_pwd
),
408 "failed to enumerate passwd");
410 for (i
=0; i
< num_pwd
; i
++) {
411 torture_assert(tctx
, test_getpwnam(tctx
, pwd
[i
].pw_name
, &pwd1
),
412 "failed to call getpwnam for enumerated user");
413 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd1
,
414 "getpwent and getpwnam gave different results"),
416 torture_assert(tctx
, test_getpwuid(tctx
, pwd
[i
].pw_uid
, &pwd2
),
417 "failed to call getpwuid for enumerated user");
418 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd2
,
419 "getpwent and getpwuid gave different results"),
421 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd1
, &pwd2
,
422 "getpwnam and getpwuid gave different results"),
429 static bool test_passwd_r(struct torture_context
*tctx
)
432 struct passwd
*pwd
, pwd1
, pwd2
;
435 torture_assert(tctx
, test_enum_r_passwd(tctx
, &pwd
, &num_pwd
),
436 "failed to enumerate passwd");
438 for (i
=0; i
< num_pwd
; i
++) {
439 torture_assert(tctx
, test_getpwnam_r(tctx
, pwd
[i
].pw_name
, &pwd1
),
440 "failed to call getpwnam_r for enumerated user");
441 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd1
,
442 "getpwent_r and getpwnam_r gave different results"),
444 torture_assert(tctx
, test_getpwuid_r(tctx
, pwd
[i
].pw_uid
, &pwd2
),
445 "failed to call getpwuid_r for enumerated user");
446 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd2
,
447 "getpwent_r and getpwuid_r gave different results"),
449 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd1
, &pwd2
,
450 "getpwnam_r and getpwuid_r gave different results"),
457 static bool test_passwd_r_cross(struct torture_context
*tctx
)
460 struct passwd
*pwd
, pwd1
, pwd2
, pwd3
, pwd4
;
463 torture_assert(tctx
, test_enum_r_passwd(tctx
, &pwd
, &num_pwd
),
464 "failed to enumerate passwd");
466 for (i
=0; i
< num_pwd
; i
++) {
467 torture_assert(tctx
, test_getpwnam_r(tctx
, pwd
[i
].pw_name
, &pwd1
),
468 "failed to call getpwnam_r for enumerated user");
469 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd1
,
470 "getpwent_r and getpwnam_r gave different results"),
472 torture_assert(tctx
, test_getpwuid_r(tctx
, pwd
[i
].pw_uid
, &pwd2
),
473 "failed to call getpwuid_r for enumerated user");
474 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd2
,
475 "getpwent_r and getpwuid_r gave different results"),
477 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd1
, &pwd2
,
478 "getpwnam_r and getpwuid_r gave different results"),
480 torture_assert(tctx
, test_getpwnam(tctx
, pwd
[i
].pw_name
, &pwd3
),
481 "failed to call getpwnam for enumerated user");
482 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd3
,
483 "getpwent_r and getpwnam gave different results"),
485 torture_assert(tctx
, test_getpwuid(tctx
, pwd
[i
].pw_uid
, &pwd4
),
486 "failed to call getpwuid for enumerated user");
487 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd
[i
], &pwd4
,
488 "getpwent_r and getpwuid gave different results"),
490 torture_assert(tctx
, torture_assert_passwd_equal(tctx
, &pwd3
, &pwd4
,
491 "getpwnam and getpwuid gave different results"),
498 static bool test_enum_group(struct torture_context
*tctx
,
499 struct group
**grp_array_p
,
503 struct group
*grp_array
= NULL
;
506 torture_comment(tctx
, "Testing setgrent\n");
509 while ((grp
= getgrent()) != NULL
) {
510 torture_comment(tctx
, "Testing getgrent\n");
513 if (grp_array_p
&& num_grp_p
) {
514 grp_array
= talloc_realloc(tctx
, grp_array
, struct group
, num_grp
+1);
515 torture_assert(tctx
, grp_array
, "out of memory");
516 copy_group(tctx
, grp
, &grp_array
[num_grp
]);
521 torture_comment(tctx
, "Testing endgrent\n");
525 *grp_array_p
= grp_array
;
528 *num_grp_p
= num_grp
;
534 static bool test_enum_r_group(struct torture_context
*tctx
,
535 struct group
**grp_array_p
,
538 struct group grp
, *grpp
;
539 struct group
*grp_array
= NULL
;
544 torture_comment(tctx
, "Testing setgrent\n");
548 torture_comment(tctx
, "Testing getgrent_r\n");
550 #ifdef SOLARIS_GETGRENT_R
551 ret
= getgrent_r(&grp
, buffer
, sizeof(buffer
));
552 #else /* SOLARIS_GETGRENT_R */
553 ret
= getgrent_r(&grp
, buffer
, sizeof(buffer
), &grpp
);
554 #endif /* SOLARIS_GETGRENT_R */
557 torture_comment(tctx
, "got %d return code\n", ret
);
562 if (grp_array_p
&& num_grp_p
) {
563 grp_array
= talloc_realloc(tctx
, grp_array
, struct group
, num_grp
+1);
564 torture_assert(tctx
, grp_array
, "out of memory");
565 copy_group(tctx
, &grp
, &grp_array
[num_grp
]);
570 torture_comment(tctx
, "Testing endgrent\n");
574 *grp_array_p
= grp_array
;
577 *num_grp_p
= num_grp
;
583 static bool torture_assert_group_equal(struct torture_context
*tctx
,
584 const struct group
*g1
,
585 const struct group
*g2
,
589 torture_assert_str_equal(tctx
, g1
->gr_name
, g2
->gr_name
, comment
);
590 torture_assert_str_equal(tctx
, g1
->gr_passwd
, g2
->gr_passwd
, comment
);
591 torture_assert_int_equal(tctx
, g1
->gr_gid
, g2
->gr_gid
, comment
);
592 torture_assert(tctx
, !(g1
->gr_mem
&& !g2
->gr_mem
), __location__
);
593 torture_assert(tctx
, !(!g1
->gr_mem
&& g2
->gr_mem
), __location__
);
594 if (!g1
->gr_mem
&& !g2
->gr_mem
) {
597 for (i
=0; g1
->gr_mem
[i
] && g2
->gr_mem
[i
]; i
++) {
598 torture_assert_str_equal(tctx
, g1
->gr_mem
[i
], g2
->gr_mem
[i
], comment
);
604 static bool test_group(struct torture_context
*tctx
)
607 struct group
*grp
, grp1
, grp2
;
610 torture_assert(tctx
, test_enum_group(tctx
, &grp
, &num_grp
),
611 "failed to enumerate group");
613 for (i
=0; i
< num_grp
; i
++) {
614 torture_assert(tctx
, test_getgrnam(tctx
, grp
[i
].gr_name
, &grp1
),
615 "failed to call getgrnam for enumerated user");
616 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp1
,
617 "getgrent and getgrnam gave different results"),
619 torture_assert(tctx
, test_getgrgid(tctx
, grp
[i
].gr_gid
, &grp2
),
620 "failed to call getgrgid for enumerated user");
621 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp2
,
622 "getgrent and getgrgid gave different results"),
624 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp1
, &grp2
,
625 "getgrnam and getgrgid gave different results"),
632 static bool test_group_r(struct torture_context
*tctx
)
635 struct group
*grp
, grp1
, grp2
;
638 torture_assert(tctx
, test_enum_r_group(tctx
, &grp
, &num_grp
),
639 "failed to enumerate group");
641 for (i
=0; i
< num_grp
; i
++) {
642 torture_assert(tctx
, test_getgrnam_r(tctx
, grp
[i
].gr_name
, &grp1
),
643 "failed to call getgrnam_r for enumerated user");
644 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp1
,
645 "getgrent_r and getgrnam_r gave different results"),
647 torture_assert(tctx
, test_getgrgid_r(tctx
, grp
[i
].gr_gid
, &grp2
),
648 "failed to call getgrgid_r for enumerated user");
649 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp2
,
650 "getgrent_r and getgrgid_r gave different results"),
652 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp1
, &grp2
,
653 "getgrnam_r and getgrgid_r gave different results"),
660 static bool test_group_r_cross(struct torture_context
*tctx
)
663 struct group
*grp
, grp1
, grp2
, grp3
, grp4
;
666 torture_assert(tctx
, test_enum_r_group(tctx
, &grp
, &num_grp
),
667 "failed to enumerate group");
669 for (i
=0; i
< num_grp
; i
++) {
670 torture_assert(tctx
, test_getgrnam_r(tctx
, grp
[i
].gr_name
, &grp1
),
671 "failed to call getgrnam_r for enumerated user");
672 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp1
,
673 "getgrent_r and getgrnam_r gave different results"),
675 torture_assert(tctx
, test_getgrgid_r(tctx
, grp
[i
].gr_gid
, &grp2
),
676 "failed to call getgrgid_r for enumerated user");
677 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp2
,
678 "getgrent_r and getgrgid_r gave different results"),
680 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp1
, &grp2
,
681 "getgrnam_r and getgrgid_r gave different results"),
683 torture_assert(tctx
, test_getgrnam(tctx
, grp
[i
].gr_name
, &grp3
),
684 "failed to call getgrnam for enumerated user");
685 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp3
,
686 "getgrent_r and getgrnam gave different results"),
688 torture_assert(tctx
, test_getgrgid(tctx
, grp
[i
].gr_gid
, &grp4
),
689 "failed to call getgrgid for enumerated user");
690 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp
[i
], &grp4
,
691 "getgrent_r and getgrgid gave different results"),
693 torture_assert(tctx
, torture_assert_group_equal(tctx
, &grp3
, &grp4
,
694 "getgrnam and getgrgid gave different results"),
701 #ifdef HAVE_GETGROUPLIST
702 static bool test_getgrouplist(struct torture_context
*tctx
,
710 gid_t
*groups
= NULL
;
712 torture_comment(tctx
, "Testing getgrouplist: %s\n", user
);
714 ret
= getgrouplist(user
, gid
, NULL
, &num_groups
);
715 if (ret
== -1 || num_groups
!= 0) {
717 groups
= talloc_array(tctx
, gid_t
, num_groups
);
718 torture_assert(tctx
, groups
, "out of memory\n");
720 ret
= getgrouplist(user
, gid
, groups
, &num_groups
);
723 torture_assert(tctx
, (ret
!= -1), "failed to call getgrouplist");
725 torture_comment(tctx
, "%s is member in %d groups\n", user
, num_groups
);
731 *num_gids_p
= num_groups
;
736 #endif /* HAVE_GETGROUPLIST */
738 static bool test_user_in_group(struct torture_context
*tctx
,
739 const struct passwd
*pwd
,
740 const struct group
*grp
)
744 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
745 if (strequal(grp
->gr_mem
[i
], pwd
->pw_name
)) {
753 static bool test_membership_user(struct torture_context
*tctx
,
754 const struct passwd
*pwd
,
755 struct group
*grp_array
,
758 int num_user_groups
= 0;
759 int num_user_groups_from_enum
= 0;
760 gid_t
*user_groups
= NULL
;
762 bool primary_group_had_user_member
= false;
764 #ifdef HAVE_GETGROUPLIST
765 torture_assert(tctx
, test_getgrouplist(tctx
,
770 "failed to test getgrouplist");
771 #endif /* HAVE_GETGROUPLIST */
773 for (g
=0; g
< num_user_groups
; g
++) {
774 torture_assert(tctx
, test_getgrgid(tctx
, user_groups
[g
], NULL
),
775 "failed to find the group the user is a member of");
779 for (i
=0; i
< num_grp
; i
++) {
781 struct group grp
= grp_array
[i
];
783 if (test_user_in_group(tctx
, pwd
, &grp
)) {
785 struct group current_grp
;
786 num_user_groups_from_enum
++;
788 torture_assert(tctx
, test_getgrnam(tctx
, grp
.gr_name
, ¤t_grp
),
789 "failed to find the group the user is a member of");
791 if (current_grp
.gr_gid
== pwd
->pw_gid
) {
792 torture_comment(tctx
, "primary group %s of user %s lists user as member\n",
795 primary_group_had_user_member
= true;
802 if (!primary_group_had_user_member
) {
803 num_user_groups_from_enum
++;
806 torture_assert_int_equal(tctx
, num_user_groups
, num_user_groups_from_enum
,
807 "getgrouplist and real inspection of grouplist gave different results\n");
812 static bool test_membership(struct torture_context
*tctx
)
814 const char *old_pwd
= getenv("NSS_WRAPPER_PASSWD");
815 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_assert(tctx
, test_enum_passwd(tctx
, &pwd
, &num_pwd
),
828 "failed to enumerate passwd");
829 torture_assert(tctx
, test_enum_group(tctx
, &grp
, &num_grp
),
830 "failed to enumerate group");
832 for (i
=0; i
< num_pwd
; i
++) {
834 torture_assert(tctx
, test_membership_user(tctx
, &pwd
[i
], grp
, num_grp
),
835 "failed to test membership for user");
842 static bool test_enumeration(struct torture_context
*tctx
)
844 const char *old_pwd
= getenv("NSS_WRAPPER_PASSWD");
845 const char *old_group
= getenv("NSS_WRAPPER_GROUP");
847 if (!old_pwd
|| !old_group
) {
848 torture_comment(tctx
, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
849 torture_skip(tctx
, "nothing to test\n");
852 torture_assert(tctx
, test_passwd(tctx
),
853 "failed to test users");
854 torture_assert(tctx
, test_group(tctx
),
855 "failed to test groups");
860 static bool test_reentrant_enumeration(struct torture_context
*tctx
)
862 const char *old_pwd
= getenv("NSS_WRAPPER_PASSWD");
863 const char *old_group
= getenv("NSS_WRAPPER_GROUP");
865 if (!old_pwd
|| !old_group
) {
866 torture_comment(tctx
, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
867 torture_skip(tctx
, "nothing to test\n");
870 torture_comment(tctx
, "Testing re-entrant calls\n");
872 torture_assert(tctx
, test_passwd_r(tctx
),
873 "failed to test users");
874 torture_assert(tctx
, test_group_r(tctx
),
875 "failed to test groups");
880 static bool test_reentrant_enumeration_crosschecks(struct torture_context
*tctx
)
882 const char *old_pwd
= getenv("NSS_WRAPPER_PASSWD");
883 const char *old_group
= getenv("NSS_WRAPPER_GROUP");
885 if (!old_pwd
|| !old_group
) {
886 torture_comment(tctx
, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
887 torture_skip(tctx
, "nothing to test\n");
890 torture_comment(tctx
, "Testing re-entrant calls with cross checks\n");
892 torture_assert(tctx
, test_passwd_r_cross(tctx
),
893 "failed to test users");
894 torture_assert(tctx
, test_group_r_cross(tctx
),
895 "failed to test groups");
900 static bool test_passwd_duplicates(struct torture_context
*tctx
)
907 torture_assert(tctx
, test_enum_passwd(tctx
, &pwd
, &num_pwd
),
908 "failed to enumerate passwd");
910 for (i
=0; i
< num_pwd
; i
++) {
911 const char *current_name
= pwd
[i
].pw_name
;
912 for (d
=0; d
< num_pwd
; d
++) {
913 const char *dup_name
= pwd
[d
].pw_name
;
917 if (!strequal(current_name
, dup_name
)) {
921 torture_warning(tctx
, "found duplicate names:");
922 print_passwd(&pwd
[d
]);
923 print_passwd(&pwd
[i
]);
929 torture_fail(tctx
, talloc_asprintf(tctx
, "found %d duplicate names", duplicates
));
935 static bool test_group_duplicates(struct torture_context
*tctx
)
942 torture_assert(tctx
, test_enum_group(tctx
, &grp
, &num_grp
),
943 "failed to enumerate group");
945 for (i
=0; i
< num_grp
; i
++) {
946 const char *current_name
= grp
[i
].gr_name
;
947 for (d
=0; d
< num_grp
; d
++) {
948 const char *dup_name
= grp
[d
].gr_name
;
952 if (!strequal(current_name
, dup_name
)) {
956 torture_warning(tctx
, "found duplicate names:");
957 print_group(&grp
[d
]);
958 print_group(&grp
[i
]);
964 torture_fail(tctx
, talloc_asprintf(tctx
, "found %d duplicate names", duplicates
));
971 static bool test_duplicates(struct torture_context
*tctx
)
973 const char *old_pwd
= getenv("NSS_WRAPPER_PASSWD");
974 const char *old_group
= getenv("NSS_WRAPPER_GROUP");
976 if (!old_pwd
|| !old_group
) {
977 torture_comment(tctx
, "ENV NSS_WRAPPER_PASSWD or NSS_WRAPPER_GROUP not set\n");
978 torture_skip(tctx
, "nothing to test\n");
981 torture_assert(tctx
, test_passwd_duplicates(tctx
),
982 "failed to test users");
983 torture_assert(tctx
, test_group_duplicates(tctx
),
984 "failed to test groups");
990 struct torture_suite
*torture_local_nss(TALLOC_CTX
*mem_ctx
)
992 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "nss");
994 torture_suite_add_simple_test(suite
, "enumeration", test_enumeration
);
995 torture_suite_add_simple_test(suite
, "reentrant enumeration", test_reentrant_enumeration
);
996 torture_suite_add_simple_test(suite
, "reentrant enumeration crosschecks", test_reentrant_enumeration_crosschecks
);
997 torture_suite_add_simple_test(suite
, "membership", test_membership
);
998 torture_suite_add_simple_test(suite
, "duplicates", test_duplicates
);