ctdb-daemon: Schedule running of callback if there are no event scripts
[Samba.git] / source4 / torture / local / nss_tests.c
blob2cd61225ab8bc5d6ba9b37b06a608df22699f8cd
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 #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,
30 struct passwd *p)
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__);
45 return true;
48 static void print_passwd(struct passwd *pwd)
50 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
51 pwd->pw_name,
52 pwd->pw_passwd,
53 (unsigned long)pwd->pw_uid,
54 (unsigned long)pwd->pw_gid,
55 pwd->pw_gecos,
56 pwd->pw_dir,
57 pwd->pw_shell);
61 static bool test_getpwnam(struct torture_context *tctx,
62 const char *name,
63 struct passwd *pwd_p)
65 struct passwd *pwd;
66 int ret;
68 torture_comment(tctx, "Testing getpwnam: %s\n", name);
70 errno = 0;
71 pwd = getpwnam(name);
72 ret = errno;
73 torture_assert(tctx, (pwd != NULL), talloc_asprintf(tctx,
74 "getpwnam(%s) failed - %d - %s",
75 name, ret, strerror(ret)));
77 if (pwd_p != NULL) {
78 torture_assert(tctx, copy_passwd(tctx, pwd, pwd_p), __location__);
81 return true;
84 static bool test_getpwnam_r(struct torture_context *tctx,
85 const char *name,
86 struct passwd *pwd_p)
88 struct passwd pwd, *pwdp;
89 char buffer[4096];
90 int ret;
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)));
99 print_passwd(&pwd);
101 if (pwd_p != NULL) {
102 torture_assert(tctx, copy_passwd(tctx, &pwd, pwd_p), __location__);
105 return true;
108 static bool test_getpwuid(struct torture_context *tctx,
109 uid_t uid,
110 struct passwd *pwd_p)
112 struct passwd *pwd;
113 int ret;
115 torture_comment(tctx, "Testing getpwuid: %lu\n", (unsigned long)uid);
117 errno = 0;
118 pwd = getpwuid(uid);
119 ret = errno;
120 torture_assert(tctx, (pwd != NULL), talloc_asprintf(tctx,
121 "getpwuid(%lu) failed - %d - %s",
122 (unsigned long)uid, ret, strerror(ret)));
124 print_passwd(pwd);
126 if (pwd_p != NULL) {
127 torture_assert(tctx, copy_passwd(tctx, pwd, pwd_p), __location__);
130 return true;
133 static bool test_getpwuid_r(struct torture_context *tctx,
134 uid_t uid,
135 struct passwd *pwd_p)
137 struct passwd pwd, *pwdp;
138 char buffer[4096];
139 int ret;
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)));
148 print_passwd(&pwd);
150 if (pwd_p != NULL) {
151 torture_assert(tctx, copy_passwd(tctx, &pwd, pwd_p), __location__);
154 return true;
158 static bool copy_group(struct torture_context *tctx,
159 const struct group *grp,
160 struct group *g)
162 int i;
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;
169 g->gr_mem = NULL;
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;
179 return true;
182 static void print_group(struct group *grp)
184 int i;
185 printf("%s:%s:%lu:",
186 grp->gr_name,
187 grp->gr_passwd,
188 (unsigned long)grp->gr_gid);
190 if ((grp->gr_mem == NULL) || !grp->gr_mem[0]) {
191 printf("\n");
192 return;
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,
202 const char *name,
203 struct group *grp_p)
205 struct group *grp;
206 int ret;
208 torture_comment(tctx, "Testing getgrnam: %s\n", name);
210 errno = 0;
211 grp = getgrnam(name);
212 ret = errno;
213 torture_assert(tctx, (grp != NULL), talloc_asprintf(tctx,
214 "getgrnam(%s) failed - %d - %s",
215 name, ret, strerror(ret)));
217 print_group(grp);
219 if (grp_p != NULL) {
220 torture_assert(tctx, copy_group(tctx, grp, grp_p), __location__);
223 return true;
226 static bool test_getgrnam_r(struct torture_context *tctx,
227 const char *name,
228 struct group *grp_p)
230 struct group grp, *grpp;
231 char buffer[4096];
232 int ret;
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)));
241 print_group(&grp);
243 if (grp_p != NULL) {
244 torture_assert(tctx, copy_group(tctx, &grp, grp_p), __location__);
247 return true;
251 static bool test_getgrgid(struct torture_context *tctx,
252 gid_t gid,
253 struct group *grp_p)
255 struct group *grp;
256 int ret;
258 torture_comment(tctx, "Testing getgrgid: %lu\n", (unsigned long)gid);
260 errno = 0;
261 grp = getgrgid(gid);
262 ret = errno;
263 torture_assert(tctx, (grp != NULL), talloc_asprintf(tctx,
264 "getgrgid(%lu) failed - %d - %s",
265 (unsigned long)gid, ret, strerror(ret)));
267 print_group(grp);
269 if (grp_p != NULL) {
270 torture_assert(tctx, copy_group(tctx, grp, grp_p), __location__);
273 return true;
276 static bool test_getgrgid_r(struct torture_context *tctx,
277 gid_t gid,
278 struct group *grp_p)
280 struct group grp, *grpp;
281 char buffer[4096];
282 int ret;
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)));
291 print_group(&grp);
293 if (grp_p != NULL) {
294 torture_assert(tctx, copy_group(tctx, &grp, grp_p), __location__);
297 return true;
300 static bool test_enum_passwd(struct torture_context *tctx,
301 struct passwd **pwd_array_p,
302 size_t *num_pwd_p)
304 struct passwd *pwd;
305 struct passwd *pwd_array = NULL;
306 size_t num_pwd = 0;
308 torture_comment(tctx, "Testing setpwent\n");
309 setpwent();
311 while ((pwd = getpwent()) != NULL) {
312 torture_comment(tctx, "Testing getpwent\n");
314 print_passwd(pwd);
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]);
319 num_pwd++;
323 torture_comment(tctx, "Testing endpwent\n");
324 endpwent();
326 if (pwd_array_p) {
327 *pwd_array_p = pwd_array;
329 if (num_pwd_p) {
330 *num_pwd_p = num_pwd;
333 return true;
336 static bool test_enum_r_passwd(struct torture_context *tctx,
337 struct passwd **pwd_array_p,
338 size_t *num_pwd_p)
340 struct passwd pwd, *pwdp;
341 struct passwd *pwd_array = NULL;
342 size_t num_pwd = 0;
343 char buffer[4096];
344 int ret;
346 torture_comment(tctx, "Testing setpwent\n");
347 setpwent();
349 while (1) {
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 */
357 if (ret != 0) {
358 if (ret != ENOENT) {
359 torture_comment(tctx, "got %d return code\n", ret);
361 break;
363 print_passwd(&pwd);
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]);
368 num_pwd++;
372 torture_comment(tctx, "Testing endpwent\n");
373 endpwent();
375 if (pwd_array_p) {
376 *pwd_array_p = pwd_array;
378 if (num_pwd_p) {
379 *num_pwd_p = num_pwd;
382 return true;
385 static bool torture_assert_passwd_equal(struct torture_context *tctx,
386 const struct passwd *p1,
387 const struct passwd *p2,
388 const char *comment)
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);
398 return true;
401 static bool test_passwd(struct torture_context *tctx)
403 int i;
404 struct passwd *pwd, pwd1, pwd2;
405 size_t num_pwd;
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"),
415 __location__);
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"),
420 __location__);
421 torture_assert(tctx, torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
422 "getpwnam and getpwuid gave different results"),
423 __location__);
426 return true;
429 static bool test_passwd_r(struct torture_context *tctx)
431 int i;
432 struct passwd *pwd, pwd1, pwd2;
433 size_t num_pwd;
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"),
443 __location__);
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"),
448 __location__);
449 torture_assert(tctx, torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
450 "getpwnam_r and getpwuid_r gave different results"),
451 __location__);
454 return true;
457 static bool test_passwd_r_cross(struct torture_context *tctx)
459 int i;
460 struct passwd *pwd, pwd1, pwd2, pwd3, pwd4;
461 size_t num_pwd;
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"),
471 __location__);
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"),
476 __location__);
477 torture_assert(tctx, torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
478 "getpwnam_r and getpwuid_r gave different results"),
479 __location__);
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"),
484 __location__);
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"),
489 __location__);
490 torture_assert(tctx, torture_assert_passwd_equal(tctx, &pwd3, &pwd4,
491 "getpwnam and getpwuid gave different results"),
492 __location__);
495 return true;
498 static bool test_enum_group(struct torture_context *tctx,
499 struct group **grp_array_p,
500 size_t *num_grp_p)
502 struct group *grp;
503 struct group *grp_array = NULL;
504 size_t num_grp = 0;
506 torture_comment(tctx, "Testing setgrent\n");
507 setgrent();
509 while ((grp = getgrent()) != NULL) {
510 torture_comment(tctx, "Testing getgrent\n");
512 print_group(grp);
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]);
517 num_grp++;
521 torture_comment(tctx, "Testing endgrent\n");
522 endgrent();
524 if (grp_array_p) {
525 *grp_array_p = grp_array;
527 if (num_grp_p) {
528 *num_grp_p = num_grp;
531 return true;
534 static bool test_enum_r_group(struct torture_context *tctx,
535 struct group **grp_array_p,
536 size_t *num_grp_p)
538 struct group grp, *grpp;
539 struct group *grp_array = NULL;
540 size_t num_grp = 0;
541 char buffer[4096];
542 int ret;
544 torture_comment(tctx, "Testing setgrent\n");
545 setgrent();
547 while (1) {
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 */
555 if (ret != 0) {
556 if (ret != ENOENT) {
557 torture_comment(tctx, "got %d return code\n", ret);
559 break;
561 print_group(&grp);
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]);
566 num_grp++;
570 torture_comment(tctx, "Testing endgrent\n");
571 endgrent();
573 if (grp_array_p) {
574 *grp_array_p = grp_array;
576 if (num_grp_p) {
577 *num_grp_p = num_grp;
580 return true;
583 static bool torture_assert_group_equal(struct torture_context *tctx,
584 const struct group *g1,
585 const struct group *g2,
586 const char *comment)
588 int i;
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) {
595 return true;
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);
601 return true;
604 static bool test_group(struct torture_context *tctx)
606 int i;
607 struct group *grp, grp1, grp2;
608 size_t num_grp;
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"),
618 __location__);
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"),
623 __location__);
624 torture_assert(tctx, torture_assert_group_equal(tctx, &grp1, &grp2,
625 "getgrnam and getgrgid gave different results"),
626 __location__);
629 return true;
632 static bool test_group_r(struct torture_context *tctx)
634 int i;
635 struct group *grp, grp1, grp2;
636 size_t num_grp;
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"),
646 __location__);
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"),
651 __location__);
652 torture_assert(tctx, torture_assert_group_equal(tctx, &grp1, &grp2,
653 "getgrnam_r and getgrgid_r gave different results"),
654 __location__);
657 return true;
660 static bool test_group_r_cross(struct torture_context *tctx)
662 int i;
663 struct group *grp, grp1, grp2, grp3, grp4;
664 size_t num_grp;
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"),
674 __location__);
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"),
679 __location__);
680 torture_assert(tctx, torture_assert_group_equal(tctx, &grp1, &grp2,
681 "getgrnam_r and getgrgid_r gave different results"),
682 __location__);
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"),
687 __location__);
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"),
692 __location__);
693 torture_assert(tctx, torture_assert_group_equal(tctx, &grp3, &grp4,
694 "getgrnam and getgrgid gave different results"),
695 __location__);
698 return true;
701 #ifdef HAVE_GETGROUPLIST
702 static bool test_getgrouplist(struct torture_context *tctx,
703 const char *user,
704 gid_t gid,
705 gid_t **gids_p,
706 int *num_gids_p)
708 int ret;
709 int num_groups = 0;
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);
727 if (gids_p) {
728 *gids_p = groups;
730 if (num_gids_p) {
731 *num_gids_p = num_groups;
734 return true;
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)
742 int i;
744 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
745 if (strequal(grp->gr_mem[i], pwd->pw_name)) {
746 return true;
750 return false;
753 static bool test_membership_user(struct torture_context *tctx,
754 const struct passwd *pwd,
755 struct group *grp_array,
756 size_t num_grp)
758 int num_user_groups = 0;
759 int num_user_groups_from_enum = 0;
760 gid_t *user_groups = NULL;
761 int g, i;
762 bool primary_group_had_user_member = false;
764 #ifdef HAVE_GETGROUPLIST
765 torture_assert(tctx, test_getgrouplist(tctx,
766 pwd->pw_name,
767 pwd->pw_gid,
768 &user_groups,
769 &num_user_groups),
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, &current_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",
793 current_grp.gr_name,
794 pwd->pw_name);
795 primary_group_had_user_member = true;
798 continue;
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");
809 return true;
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");
816 struct passwd *pwd;
817 size_t num_pwd;
818 struct group *grp;
819 size_t num_grp;
820 int i;
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");
839 return true;
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");
857 return true;
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");
877 return true;
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");
897 return true;
900 static bool test_passwd_duplicates(struct torture_context *tctx)
902 int i, d;
903 struct passwd *pwd;
904 size_t num_pwd;
905 int duplicates = 0;
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;
914 if (d == i) {
915 continue;
917 if (!strequal(current_name, dup_name)) {
918 continue;
921 torture_warning(tctx, "found duplicate names:");
922 print_passwd(&pwd[d]);
923 print_passwd(&pwd[i]);
924 duplicates++;
928 if (duplicates) {
929 torture_fail(tctx, talloc_asprintf(tctx, "found %d duplicate names", duplicates));
932 return true;
935 static bool test_group_duplicates(struct torture_context *tctx)
937 int i, d;
938 struct group *grp;
939 size_t num_grp;
940 int duplicates = 0;
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;
949 if (d == i) {
950 continue;
952 if (!strequal(current_name, dup_name)) {
953 continue;
956 torture_warning(tctx, "found duplicate names:");
957 print_group(&grp[d]);
958 print_group(&grp[i]);
959 duplicates++;
963 if (duplicates) {
964 torture_fail(tctx, talloc_asprintf(tctx, "found %d duplicate names", duplicates));
967 return true;
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");
986 return true;
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);
1000 return suite;