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 DECLARE_TEST_DATA(group)
53 DECLARE_TEST_FILE_SNAPSHOT(group)
54 DECLARE_1PASS_TEST(group)
55 DECLARE_2PASS_TEST(group)
57 static void clone_group(struct group *, struct group const *);
58 static int compare_group(struct group *, struct group *, void *);
59 static void dump_group(struct group *);
60 static void free_group(struct group *);
62 static void sdump_group(struct group *, char *, size_t);
63 static int group_read_snapshot_func(struct group *, char *);
65 static int group_check_ambiguity(struct group_test_data *,
67 static int group_fill_test_data(struct group_test_data *);
68 static int group_test_correctness(struct group *, void *);
69 static int group_test_getgrnam(struct group *, void *);
70 static int group_test_getgrgid(struct group *, void *);
71 static int group_test_getgrent(struct group *, void *);
73 IMPLEMENT_TEST_DATA(group)
74 IMPLEMENT_TEST_FILE_SNAPSHOT(group)
75 IMPLEMENT_1PASS_TEST(group)
76 IMPLEMENT_2PASS_TEST(group)
79 clone_group(struct group *dest, struct group const *src)
81 ATF_REQUIRE(dest != NULL);
82 ATF_REQUIRE(src != NULL);
87 memset(dest, 0, sizeof(struct group));
89 if (src->gr_name != NULL) {
90 dest->gr_name = strdup(src->gr_name);
91 ATF_REQUIRE(dest->gr_name != NULL);
94 if (src->gr_passwd != NULL) {
95 dest->gr_passwd = strdup(src->gr_passwd);
96 ATF_REQUIRE(dest->gr_passwd != NULL);
98 dest->gr_gid = src->gr_gid;
100 if (src->gr_mem != NULL) {
102 for (cp = src->gr_mem; *cp; ++cp)
105 dest->gr_mem = calloc(members_num + 1, sizeof(char *));
106 ATF_REQUIRE(dest->gr_mem != NULL);
108 for (cp = src->gr_mem; *cp; ++cp) {
109 dest->gr_mem[cp - src->gr_mem] = strdup(*cp);
110 ATF_REQUIRE(dest->gr_mem[cp - src->gr_mem] != NULL);
116 free_group(struct group *grp)
120 ATF_REQUIRE(grp != NULL);
123 free(grp->gr_passwd);
125 for (cp = grp->gr_mem; *cp; ++cp)
131 compare_group(struct group *grp1, struct group *grp2, void *mdata)
138 if (grp1 == NULL || grp2 == NULL)
141 if (strcmp(grp1->gr_name, grp2->gr_name) != 0 ||
142 strcmp(grp1->gr_passwd, grp2->gr_passwd) != 0 ||
143 grp1->gr_gid != grp2->gr_gid)
149 if (grp1->gr_mem == NULL || grp2->gr_mem == NULL)
152 for (; *c1 && *c2; ++c1, ++c2)
153 if (strcmp(*c1, *c2) != 0)
156 if (*c1 != '\0' || *c2 != '\0')
163 printf("following structures are not equal:\n");
172 sdump_group(struct group *grp, char *buffer, size_t buflen)
177 written = snprintf(buffer, buflen, "%s %s %d",
178 grp->gr_name, grp->gr_passwd, grp->gr_gid);
180 if (written > (int)buflen)
184 if (grp->gr_mem != NULL) {
185 if (*(grp->gr_mem) != '\0') {
186 for (cp = grp->gr_mem; *cp; ++cp) {
187 written = snprintf(buffer, buflen, " %s",*cp);
189 if (written > (int)buflen)
197 snprintf(buffer, buflen, " nomem");
199 snprintf(buffer, buflen, " (null)");
203 group_read_snapshot_func(struct group *grp, char *line)
209 printf("1 line read from snapshot:\n%s\n", line);
214 memset(grp, 0, sizeof(struct group));
215 while ((s = strsep(&ps, " ")) != NULL) {
218 grp->gr_name = strdup(s);
219 ATF_REQUIRE(grp->gr_name != NULL);
223 grp->gr_passwd = strdup(s);
224 ATF_REQUIRE(grp->gr_passwd != NULL);
228 grp->gr_gid = (gid_t)strtol(s, &ts, 10);
231 free(grp->gr_passwd);
233 grp->gr_passwd = NULL;
240 if (strcmp(s, "(null)") == 0)
244 ATF_REQUIRE(sl != NULL);
246 if (strcmp(s, "nomem") != 0) {
248 ATF_REQUIRE(ts != NULL);
253 ATF_REQUIRE(ts != NULL);
263 free(grp->gr_passwd);
264 memset(grp, 0, sizeof(struct group));
269 grp->gr_mem = sl->sl_str;
271 /* NOTE: is it a dirty hack or not? */
277 dump_group(struct group *result)
279 if (result != NULL) {
281 sdump_group(result, buffer, sizeof(buffer));
282 printf("%s\n", buffer);
288 group_fill_test_data(struct group_test_data *td)
293 while ((grp = getgrent()) != NULL) {
294 if (group_test_correctness(grp, NULL) == 0)
295 TEST_DATA_APPEND(group, td, grp);
305 group_test_correctness(struct group *grp, void *mdata __unused)
307 printf("testing correctness with the following data:\n");
313 if (grp->gr_name == NULL)
316 if (grp->gr_passwd == NULL)
319 if (grp->gr_mem == NULL)
326 printf("incorrect\n");
331 /* group_check_ambiguity() is needed here because when doing the getgrent()
332 * calls sequence, records from different nsswitch sources can be different,
333 * though having the same pw_name/pw_uid */
335 group_check_ambiguity(struct group_test_data *td, struct group *pwd)
338 return (TEST_DATA_FIND(group, td, pwd, compare_group,
339 NULL) != NULL ? 0 : -1);
343 group_test_getgrnam(struct group *grp_model, void *mdata)
347 printf("testing getgrnam() with the following data:\n");
348 dump_group(grp_model);
350 grp = getgrnam(grp_model->gr_name);
351 if (group_test_correctness(grp, NULL) != 0)
354 if (compare_group(grp, grp_model, NULL) != 0 &&
355 group_check_ambiguity((struct group_test_data *)mdata, grp) != 0)
365 group_test_getgrgid(struct group *grp_model, void *mdata)
369 printf("testing getgrgid() with the following data...\n");
370 dump_group(grp_model);
372 grp = getgrgid(grp_model->gr_gid);
373 if (group_test_correctness(grp, NULL) != 0 ||
374 (compare_group(grp, grp_model, NULL) != 0 &&
375 group_check_ambiguity((struct group_test_data *)mdata, grp) != 0)) {
383 group_test_getgrent(struct group *grp, void *mdata __unused)
385 /* Only correctness can be checked when doing 1-pass test for
387 return (group_test_correctness(grp, NULL));
391 run_tests(const char *snapshot_file, enum test_methods method)
393 struct group_test_data td, td_snap, td_2pass;
396 TEST_DATA_INIT(group, &td, clone_group, free_group);
397 TEST_DATA_INIT(group, &td_snap, clone_group, free_group);
398 if (snapshot_file != NULL) {
399 if (access(snapshot_file, W_OK | R_OK) != 0) {
401 method = TEST_BUILD_SNAPSHOT;
403 printf("can't access the file %s\n",
410 if (method == TEST_BUILD_SNAPSHOT) {
415 TEST_SNAPSHOT_FILE_READ(group, snapshot_file,
416 &td_snap, group_read_snapshot_func);
420 rv = group_fill_test_data(&td);
425 if (snapshot_file == NULL)
426 rv = DO_1PASS_TEST(group, &td,
427 group_test_getgrnam, (void *)&td);
429 rv = DO_1PASS_TEST(group, &td_snap,
430 group_test_getgrnam, (void *)&td_snap);
433 if (snapshot_file == NULL)
434 rv = DO_1PASS_TEST(group, &td,
435 group_test_getgrgid, (void *)&td);
437 rv = DO_1PASS_TEST(group, &td_snap,
438 group_test_getgrgid, (void *)&td_snap);
441 if (snapshot_file == NULL)
442 rv = DO_1PASS_TEST(group, &td, group_test_getgrent,
445 rv = DO_2PASS_TEST(group, &td, &td_snap,
446 compare_group, NULL);
448 case TEST_GETGRENT_2PASS:
449 TEST_DATA_INIT(group, &td_2pass, clone_group, free_group);
450 rv = group_fill_test_data(&td_2pass);
452 rv = DO_2PASS_TEST(group, &td, &td_2pass,
453 compare_group, NULL);
454 TEST_DATA_DESTROY(group, &td_2pass);
456 case TEST_BUILD_SNAPSHOT:
457 if (snapshot_file != NULL)
458 rv = TEST_SNAPSHOT_FILE_WRITE(group, snapshot_file, &td,
467 TEST_DATA_DESTROY(group, &td_snap);
468 TEST_DATA_DESTROY(group, &td);
473 #define SNAPSHOT_FILE "snapshot_grp"
475 ATF_TC_WITHOUT_HEAD(getgrent);
476 ATF_TC_BODY(getgrent, tc)
479 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0);
482 ATF_TC_WITHOUT_HEAD(getgrent_with_snapshot);
483 ATF_TC_BODY(getgrent_with_snapshot, tc)
486 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
487 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0);
490 ATF_TC_WITHOUT_HEAD(getgrent_with_two_pass);
491 ATF_TC_BODY(getgrent_with_two_pass, tc)
494 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT_2PASS) == 0);
497 ATF_TC_WITHOUT_HEAD(getgrgid);
498 ATF_TC_BODY(getgrgid, tc)
501 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0);
504 ATF_TC_WITHOUT_HEAD(getgrgid_with_snapshot);
505 ATF_TC_BODY(getgrgid_with_snapshot, tc)
508 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
509 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0);
512 ATF_TC_WITHOUT_HEAD(getgrnam);
513 ATF_TC_BODY(getgrnam, tc)
516 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0);
519 ATF_TC_WITHOUT_HEAD(getgrnam_with_snapshot);
520 ATF_TC_BODY(getgrnam_with_snapshot, tc)
523 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
524 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0);
530 ATF_TP_ADD_TC(tp, getgrent);
531 ATF_TP_ADD_TC(tp, getgrent_with_snapshot);
532 ATF_TP_ADD_TC(tp, getgrent_with_two_pass);
533 ATF_TP_ADD_TC(tp, getgrgid);
534 ATF_TP_ADD_TC(tp, getgrgid_with_snapshot);
535 ATF_TP_ADD_TC(tp, getgrnam);
536 ATF_TP_ADD_TC(tp, getgrnam_with_snapshot);
538 return (atf_no_error());