2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <arpa/inet.h>
37 #include <stringlist.h>
48 TEST_GETGRENT_2PASS = 8,
49 TEST_BUILD_SNAPSHOT = 16,
52 static enum test_methods method = TEST_BUILD_SNAPSHOT;
54 DECLARE_TEST_DATA(group)
55 DECLARE_TEST_FILE_SNAPSHOT(group)
56 DECLARE_1PASS_TEST(group)
57 DECLARE_2PASS_TEST(group)
59 static void clone_group(struct group *, struct group const *);
60 static int compare_group(struct group *, struct group *, void *);
61 static void dump_group(struct group *);
62 static void free_group(struct group *);
64 static void sdump_group(struct group *, char *, size_t);
65 static int group_read_snapshot_func(struct group *, char *);
67 static int group_check_ambiguity(struct group_test_data *,
69 static int group_fill_test_data(struct group_test_data *);
70 static int group_test_correctness(struct group *, void *);
71 static int group_test_getgrnam(struct group *, void *);
72 static int group_test_getgrgid(struct group *, void *);
73 static int group_test_getgrent(struct group *, void *);
75 IMPLEMENT_TEST_DATA(group)
76 IMPLEMENT_TEST_FILE_SNAPSHOT(group)
77 IMPLEMENT_1PASS_TEST(group)
78 IMPLEMENT_2PASS_TEST(group)
81 clone_group(struct group *dest, struct group const *src)
83 ATF_REQUIRE(dest != NULL);
84 ATF_REQUIRE(src != NULL);
89 memset(dest, 0, sizeof(struct group));
91 if (src->gr_name != NULL) {
92 dest->gr_name = strdup(src->gr_name);
93 ATF_REQUIRE(dest->gr_name != NULL);
96 if (src->gr_passwd != NULL) {
97 dest->gr_passwd = strdup(src->gr_passwd);
98 ATF_REQUIRE(dest->gr_passwd != NULL);
100 dest->gr_gid = src->gr_gid;
102 if (src->gr_mem != NULL) {
104 for (cp = src->gr_mem; *cp; ++cp)
107 dest->gr_mem = calloc(1, (members_num + 1) * sizeof(char *));
108 ATF_REQUIRE(dest->gr_mem != NULL);
110 for (cp = src->gr_mem; *cp; ++cp) {
111 dest->gr_mem[cp - src->gr_mem] = strdup(*cp);
112 ATF_REQUIRE(dest->gr_mem[cp - src->gr_mem] != NULL);
118 free_group(struct group *grp)
122 ATF_REQUIRE(grp != NULL);
125 free(grp->gr_passwd);
127 for (cp = grp->gr_mem; *cp; ++cp)
133 compare_group(struct group *grp1, struct group *grp2, void *mdata)
140 if (grp1 == NULL || grp2 == NULL)
143 if (strcmp(grp1->gr_name, grp2->gr_name) != 0 ||
144 strcmp(grp1->gr_passwd, grp2->gr_passwd) != 0 ||
145 grp1->gr_gid != grp2->gr_gid)
151 if (grp1->gr_mem == NULL || grp2->gr_mem == NULL)
154 for (; *c1 && *c2; ++c1, ++c2)
155 if (strcmp(*c1, *c2) != 0)
158 if (*c1 != '\0' || *c2 != '\0')
165 printf("following structures are not equal:\n");
174 sdump_group(struct group *grp, char *buffer, size_t buflen)
179 written = snprintf(buffer, buflen, "%s %s %d",
180 grp->gr_name, grp->gr_passwd, grp->gr_gid);
182 if (written > buflen)
186 if (grp->gr_mem != NULL) {
187 if (*(grp->gr_mem) != '\0') {
188 for (cp = grp->gr_mem; *cp; ++cp) {
189 written = snprintf(buffer, buflen, " %s",*cp);
191 if (written > buflen)
199 snprintf(buffer, buflen, " nomem");
201 snprintf(buffer, buflen, " (null)");
205 group_read_snapshot_func(struct group *grp, char *line)
211 printf("1 line read from snapshot:\n%s\n", line);
216 memset(grp, 0, sizeof(struct group));
217 while ((s = strsep(&ps, " ")) != NULL) {
220 grp->gr_name = strdup(s);
221 ATF_REQUIRE(grp->gr_name != NULL);
225 grp->gr_passwd = strdup(s);
226 ATF_REQUIRE(grp->gr_passwd != NULL);
230 grp->gr_gid = (gid_t)strtol(s, &ts, 10);
233 free(grp->gr_passwd);
235 grp->gr_passwd = NULL;
242 if (strcmp(s, "(null)") == 0)
246 ATF_REQUIRE(sl != NULL);
248 if (strcmp(s, "nomem") != 0) {
250 ATF_REQUIRE(ts != NULL);
255 ATF_REQUIRE(ts != NULL);
265 free(grp->gr_passwd);
266 memset(grp, 0, sizeof(struct group));
271 grp->gr_mem = sl->sl_str;
273 /* NOTE: is it a dirty hack or not? */
279 dump_group(struct group *result)
281 if (result != NULL) {
283 sdump_group(result, buffer, sizeof(buffer));
284 printf("%s\n", buffer);
290 group_fill_test_data(struct group_test_data *td)
295 while ((grp = getgrent()) != NULL) {
296 if (group_test_correctness(grp, NULL) == 0)
297 TEST_DATA_APPEND(group, td, grp);
307 group_test_correctness(struct group *grp, void *mdata)
309 printf("testing correctness with the following data:\n");
315 if (grp->gr_name == NULL)
318 if (grp->gr_passwd == NULL)
321 if (grp->gr_mem == NULL)
328 printf("incorrect\n");
333 /* group_check_ambiguity() is needed here because when doing the getgrent()
334 * calls sequence, records from different nsswitch sources can be different,
335 * though having the same pw_name/pw_uid */
337 group_check_ambiguity(struct group_test_data *td, struct group *pwd)
340 return (TEST_DATA_FIND(group, td, pwd, compare_group,
341 NULL) != NULL ? 0 : -1);
345 group_test_getgrnam(struct group *grp_model, void *mdata)
349 printf("testing getgrnam() with the following data:\n");
350 dump_group(grp_model);
352 grp = getgrnam(grp_model->gr_name);
353 if (group_test_correctness(grp, NULL) != 0)
356 if (compare_group(grp, grp_model, NULL) != 0 &&
357 group_check_ambiguity((struct group_test_data *)mdata, grp) != 0)
367 group_test_getgrgid(struct group *grp_model, void *mdata)
371 printf("testing getgrgid() with the following data...\n");
372 dump_group(grp_model);
374 grp = getgrgid(grp_model->gr_gid);
375 if (group_test_correctness(grp, NULL) != 0 ||
376 (compare_group(grp, grp_model, NULL) != 0 &&
377 group_check_ambiguity((struct group_test_data *)mdata, grp) != 0)) {
385 group_test_getgrent(struct group *grp, void *mdata)
387 /* Only correctness can be checked when doing 1-pass test for
389 return (group_test_correctness(grp, NULL));
393 run_tests(const char *snapshot_file, enum test_methods method)
395 struct group_test_data td, td_snap, td_2pass;
398 TEST_DATA_INIT(group, &td, clone_group, free_group);
399 TEST_DATA_INIT(group, &td_snap, clone_group, free_group);
400 if (snapshot_file != NULL) {
401 if (access(snapshot_file, W_OK | R_OK) != 0) {
403 method = TEST_BUILD_SNAPSHOT;
405 printf("can't access the file %s\n",
412 if (method == TEST_BUILD_SNAPSHOT) {
417 TEST_SNAPSHOT_FILE_READ(group, snapshot_file,
418 &td_snap, group_read_snapshot_func);
422 rv = group_fill_test_data(&td);
427 if (snapshot_file == NULL)
428 rv = DO_1PASS_TEST(group, &td,
429 group_test_getgrnam, (void *)&td);
431 rv = DO_1PASS_TEST(group, &td_snap,
432 group_test_getgrnam, (void *)&td_snap);
435 if (snapshot_file == NULL)
436 rv = DO_1PASS_TEST(group, &td,
437 group_test_getgrgid, (void *)&td);
439 rv = DO_1PASS_TEST(group, &td_snap,
440 group_test_getgrgid, (void *)&td_snap);
443 if (snapshot_file == NULL)
444 rv = DO_1PASS_TEST(group, &td, group_test_getgrent,
447 rv = DO_2PASS_TEST(group, &td, &td_snap,
448 compare_group, NULL);
450 case TEST_GETGRENT_2PASS:
451 TEST_DATA_INIT(group, &td_2pass, clone_group, free_group);
452 rv = group_fill_test_data(&td_2pass);
454 rv = DO_2PASS_TEST(group, &td, &td_2pass,
455 compare_group, NULL);
456 TEST_DATA_DESTROY(group, &td_2pass);
458 case TEST_BUILD_SNAPSHOT:
459 if (snapshot_file != NULL)
460 rv = TEST_SNAPSHOT_FILE_WRITE(group, snapshot_file, &td,
469 TEST_DATA_DESTROY(group, &td_snap);
470 TEST_DATA_DESTROY(group, &td);
475 #define SNAPSHOT_FILE "snapshot_grp"
477 ATF_TC_BODY(getgrent, tc)
480 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0);
483 ATF_TC_WITHOUT_HEAD(getgrent_with_snapshot);
484 ATF_TC_BODY(getgrent_with_snapshot, tc)
487 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
488 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0);
491 ATF_TC_WITHOUT_HEAD(getgrent_with_two_pass);
492 ATF_TC_BODY(getgrent_with_two_pass, tc)
495 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT_2PASS) == 0);
498 ATF_TC_WITHOUT_HEAD(getgrgid);
499 ATF_TC_BODY(getgrgid, tc)
502 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0);
505 ATF_TC_WITHOUT_HEAD(getgrgid_with_snapshot);
506 ATF_TC_BODY(getgrgid_with_snapshot, tc)
509 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
510 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0);
513 ATF_TC_WITHOUT_HEAD(getgrnam);
514 ATF_TC_BODY(getgrnam, tc)
517 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0);
520 ATF_TC_WITHOUT_HEAD(getgrnam_with_snapshot);
521 ATF_TC_BODY(getgrnam_with_snapshot, tc)
524 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
525 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0);
528 ATF_TC_WITHOUT_HEAD(getgrent);
532 ATF_TP_ADD_TC(tp, getgrent);
533 ATF_TP_ADD_TC(tp, getgrent_with_snapshot);
534 ATF_TP_ADD_TC(tp, getgrent_with_two_pass);
535 ATF_TP_ADD_TC(tp, getgrgid);
536 ATF_TP_ADD_TC(tp, getgrgid_with_snapshot);
537 ATF_TP_ADD_TC(tp, getgrnam);
538 ATF_TP_ADD_TC(tp, getgrnam_with_snapshot);
540 return (atf_no_error());