xref: /freebsd/lib/libc/tests/nss/getgr_test.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 /*-
2  * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <arpa/inet.h>
29 #include <errno.h>
30 #include <grp.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stringlist.h>
35 #include <unistd.h>
36 
37 #include <atf-c.h>
38 
39 #include "testutil.h"
40 
41 enum test_methods {
42 	TEST_GETGRENT,
43 	TEST_GETGRNAM,
44 	TEST_GETGRGID,
45 	TEST_GETGRENT_2PASS,
46 	TEST_GETGRENT_INTERLEAVED_GETGRNAM,
47 	TEST_GETGRENT_INTERLEAVED_GETGRGID,
48 	TEST_BUILD_SNAPSHOT,
49 };
50 
51 DECLARE_TEST_DATA(group)
52 DECLARE_TEST_FILE_SNAPSHOT(group)
53 DECLARE_1PASS_TEST(group)
54 DECLARE_2PASS_TEST(group)
55 
56 static void clone_group(struct group *, struct group const *);
57 static int compare_group(struct group *, struct group *, void *);
58 static void dump_group(struct group *);
59 static void free_group(struct group *);
60 
61 static void sdump_group(struct group *, char *, size_t);
62 static int group_read_snapshot_func(struct group *, char *);
63 
64 static int group_check_ambiguity(struct group_test_data *, struct group *);
65 static int group_fill_test_data(struct group_test_data *,
66     int (*cb)(struct group *, void *));
67 static int group_test_correctness(struct group *, void *);
68 static int group_test_getgrnam(struct group *, void *);
69 static int group_test_getgrgid(struct group *, void *);
70 static int group_test_getgrent(struct group *, void *);
71 
72 IMPLEMENT_TEST_DATA(group)
73 IMPLEMENT_TEST_FILE_SNAPSHOT(group)
74 IMPLEMENT_1PASS_TEST(group)
75 IMPLEMENT_2PASS_TEST(group)
76 
77 static void
78 clone_group(struct group *dest, struct group const *src)
79 {
80 	ATF_REQUIRE(dest != NULL);
81 	ATF_REQUIRE(src != NULL);
82 
83 	char **cp;
84 	int members_num;
85 
86 	memset(dest, 0, sizeof(struct group));
87 
88 	if (src->gr_name != NULL) {
89 		dest->gr_name = strdup(src->gr_name);
90 		ATF_REQUIRE(dest->gr_name != NULL);
91 	}
92 
93 	if (src->gr_passwd != NULL) {
94 		dest->gr_passwd = strdup(src->gr_passwd);
95 		ATF_REQUIRE(dest->gr_passwd != NULL);
96 	}
97 	dest->gr_gid = src->gr_gid;
98 
99 	if (src->gr_mem != NULL) {
100 		members_num = 0;
101 		for (cp = src->gr_mem; *cp; ++cp)
102 			++members_num;
103 
104 		dest->gr_mem = calloc(members_num + 1, sizeof(char *));
105 		ATF_REQUIRE(dest->gr_mem != NULL);
106 
107 		for (cp = src->gr_mem; *cp; ++cp) {
108 			dest->gr_mem[cp - src->gr_mem] = strdup(*cp);
109 			ATF_REQUIRE(dest->gr_mem[cp - src->gr_mem] != NULL);
110 		}
111 	}
112 }
113 
114 static void
115 free_group(struct group *grp)
116 {
117 	char **cp;
118 
119 	ATF_REQUIRE(grp != NULL);
120 
121 	free(grp->gr_name);
122 	free(grp->gr_passwd);
123 
124 	for (cp = grp->gr_mem; *cp; ++cp)
125 		free(*cp);
126 	free(grp->gr_mem);
127 }
128 
129 static  int
130 compare_group(struct group *grp1, struct group *grp2, void *mdata)
131 {
132 	char **c1, **c2;
133 
134 	if (grp1 == grp2)
135 		return (0);
136 
137 	if (grp1 == NULL || grp2 == NULL)
138 		goto errfin;
139 
140 	if (strcmp(grp1->gr_name, grp2->gr_name) != 0 ||
141 	    strcmp(grp1->gr_passwd, grp2->gr_passwd) != 0 ||
142 	    grp1->gr_gid != grp2->gr_gid)
143 			goto errfin;
144 
145 	c1 = grp1->gr_mem;
146 	c2 = grp2->gr_mem;
147 
148 	if (grp1->gr_mem == NULL || grp2->gr_mem == NULL)
149 		goto errfin;
150 
151 	for (; *c1 && *c2; ++c1, ++c2)
152 		if (strcmp(*c1, *c2) != 0)
153 			goto errfin;
154 
155 	if (*c1 != NULL || *c2 != NULL)
156 		goto errfin;
157 
158 	return 0;
159 
160 errfin:
161 	if (mdata == NULL) {
162 		printf("following structures are not equal:\n");
163 		dump_group(grp1);
164 		dump_group(grp2);
165 	}
166 
167 	return (-1);
168 }
169 
170 static void
171 sdump_group(struct group *grp, char *buffer, size_t buflen)
172 {
173 	char **cp;
174 	int written;
175 
176 	written = snprintf(buffer, buflen, "%s:%s:%d:",
177 	    grp->gr_name, grp->gr_passwd, grp->gr_gid);
178 	buffer += written;
179 	if (written > (int)buflen)
180 		return;
181 	buflen -= written;
182 
183 	if (grp->gr_mem != NULL) {
184 		if (*(grp->gr_mem) != NULL) {
185 			for (cp = grp->gr_mem; *cp; ++cp) {
186 				written = snprintf(buffer, buflen, "%s%s",
187 				    cp == grp->gr_mem ? "" : ",", *cp);
188 				buffer += written;
189 				if (written > (int)buflen)
190 					return;
191 				buflen -= written;
192 
193 				if (buflen == 0)
194 					return;
195 			}
196 		} else
197 			snprintf(buffer, buflen, "nomem");
198 	} else
199 		snprintf(buffer, buflen, "(null)");
200 }
201 
202 static int
203 group_read_snapshot_func(struct group *grp, char *line)
204 {
205 	StringList *sl;
206 	char *s, *ps, *ts;
207 	const char *sep;
208 	int i;
209 
210 	printf("1 line read from snapshot:\n%s\n", line);
211 
212 	i = 0;
213 	sl = NULL;
214 	ps = line;
215 	sep = ":";
216 	memset(grp, 0, sizeof(struct group));
217 	while ((s = strsep(&ps, sep)) != NULL) {
218 		switch (i) {
219 		case 0:
220 			grp->gr_name = strdup(s);
221 			ATF_REQUIRE(grp->gr_name != NULL);
222 			break;
223 
224 		case 1:
225 			grp->gr_passwd = strdup(s);
226 			ATF_REQUIRE(grp->gr_passwd != NULL);
227 			break;
228 
229 		case 2:
230 			grp->gr_gid = (gid_t)strtol(s, &ts, 10);
231 			if (*ts != '\0') {
232 				free(grp->gr_name);
233 				free(grp->gr_passwd);
234 				grp->gr_name = NULL;
235 				grp->gr_passwd = NULL;
236 				return (-1);
237 			}
238 			/* Change to parsing groups. */
239 			sep = ",";
240 			break;
241 
242 		default:
243 			if (sl == NULL) {
244 				if (strcmp(s, "(null)") == 0)
245 					return (0);
246 
247 				sl = sl_init();
248 				ATF_REQUIRE(sl != NULL);
249 
250 				if (strcmp(s, "nomem") != 0) {
251 					ts = strdup(s);
252 					ATF_REQUIRE(ts != NULL);
253 					sl_add(sl, ts);
254 				}
255 			} else {
256 				ts = strdup(s);
257 				ATF_REQUIRE(ts != NULL);
258 				sl_add(sl, ts);
259 			}
260 			break;
261 		}
262 		++i;
263 	}
264 
265 	if (i < 3) {
266 		free(grp->gr_name);
267 		free(grp->gr_passwd);
268 		memset(grp, 0, sizeof(struct group));
269 		return (-1);
270 	}
271 
272 	sl_add(sl, NULL);
273 	grp->gr_mem = sl->sl_str;
274 
275 	/* NOTE: is it a dirty hack or not? */
276 	free(sl);
277 	return (0);
278 }
279 
280 static void
281 dump_group(struct group *result)
282 {
283 	if (result != NULL) {
284 		char buffer[1024];
285 		sdump_group(result, buffer, sizeof(buffer));
286 		printf("%s\n", buffer);
287 	} else
288 		printf("(null)\n");
289 }
290 
291 static int
292 group_fill_test_data(struct group_test_data *td,
293     int (*cb)(struct group *, void *))
294 {
295 	struct group *grp;
296 
297 	setgroupent(1);
298 	while ((grp = getgrent()) != NULL) {
299 		if (group_test_correctness(grp, NULL) == 0) {
300 			TEST_DATA_APPEND(group, td, grp);
301 			if (cb != NULL && cb(grp, td) != 0)
302 				return (-1);
303 		} else {
304 			return (-1);
305 		}
306 	}
307 	endgrent();
308 
309 	return (0);
310 }
311 
312 static int
313 group_test_correctness(struct group *grp, void *mdata __unused)
314 {
315 	printf("testing correctness with the following data:\n");
316 	dump_group(grp);
317 
318 	if (grp == NULL)
319 		goto errfin;
320 
321 	if (grp->gr_name == NULL)
322 		goto errfin;
323 
324 	if (grp->gr_passwd == NULL)
325 		goto errfin;
326 
327 	if (grp->gr_mem == NULL)
328 		goto errfin;
329 
330 	printf("correct\n");
331 
332 	return (0);
333 errfin:
334 	printf("incorrect\n");
335 
336 	return (-1);
337 }
338 
339 /* group_check_ambiguity() is needed here because when doing the getgrent()
340  * calls sequence, records from different nsswitch sources can be different,
341  * though having the same pw_name/pw_uid */
342 static int
343 group_check_ambiguity(struct group_test_data *td, struct group *pwd)
344 {
345 
346 	return (TEST_DATA_FIND(group, td, pwd, compare_group, NULL) !=
347 	    NULL ? 0 : -1);
348 }
349 
350 static int
351 group_test_getgrnam(struct group *grp_model, void *mdata)
352 {
353 	struct group *grp;
354 
355 	printf("testing getgrnam() with the following data:\n");
356 	dump_group(grp_model);
357 
358 	grp = getgrnam(grp_model->gr_name);
359 	if (group_test_correctness(grp, NULL) != 0)
360 		goto errfin;
361 
362 	if (compare_group(grp, grp_model, NULL) != 0 &&
363 	    group_check_ambiguity((struct group_test_data *)mdata, grp) != 0)
364 	    goto errfin;
365 
366 	return (0);
367 
368 errfin:
369 	return (-1);
370 }
371 
372 static int
373 group_test_getgrgid(struct group *grp_model, void *mdata)
374 {
375 	struct group *grp;
376 
377 	printf("testing getgrgid() with the following data...\n");
378 	dump_group(grp_model);
379 
380 	grp = getgrgid(grp_model->gr_gid);
381 	if (group_test_correctness(grp, NULL) != 0 ||
382 	    (compare_group(grp, grp_model, NULL) != 0 &&
383 	     group_check_ambiguity((struct group_test_data *)mdata, grp) != 0))
384 		return (-1);
385 	else
386 		return (0);
387 }
388 
389 static int
390 group_test_getgrent(struct group *grp, void *mdata __unused)
391 {
392 	/*
393 	 * Only correctness can be checked when doing 1-pass test for
394 	 * getgrent().
395 	 */
396 	return (group_test_correctness(grp, NULL));
397 }
398 
399 static int
400 run_tests(const char *snapshot_file, enum test_methods method)
401 {
402 	struct group_test_data td, td_snap, td_2pass, td_interleaved;
403 	int rv;
404 
405 	TEST_DATA_INIT(group, &td, clone_group, free_group);
406 	TEST_DATA_INIT(group, &td_snap, clone_group, free_group);
407 	if (snapshot_file != NULL) {
408 		if (access(snapshot_file, W_OK | R_OK) != 0) {
409 			if (errno == ENOENT)
410 				method = TEST_BUILD_SNAPSHOT;
411 			else {
412 				printf("can't access the file %s\n",
413 				    snapshot_file);
414 
415 				rv = -1;
416 				goto fin;
417 			}
418 		} else {
419 			if (method == TEST_BUILD_SNAPSHOT) {
420 				rv = 0;
421 				goto fin;
422 			}
423 
424 			TEST_SNAPSHOT_FILE_READ(group, snapshot_file,
425 				&td_snap, group_read_snapshot_func);
426 		}
427 	}
428 
429 	rv = group_fill_test_data(&td, NULL);
430 	if (rv == -1)
431 		return (-1);
432 	switch (method) {
433 	case TEST_GETGRNAM:
434 		if (snapshot_file == NULL)
435 			rv = DO_1PASS_TEST(group, &td,
436 				group_test_getgrnam, (void *)&td);
437 		else
438 			rv = DO_1PASS_TEST(group, &td_snap,
439 				group_test_getgrnam, (void *)&td_snap);
440 		break;
441 	case TEST_GETGRGID:
442 		if (snapshot_file == NULL)
443 			rv = DO_1PASS_TEST(group, &td,
444 				group_test_getgrgid, (void *)&td);
445 		else
446 			rv = DO_1PASS_TEST(group, &td_snap,
447 				group_test_getgrgid, (void *)&td_snap);
448 		break;
449 	case TEST_GETGRENT:
450 		if (snapshot_file == NULL)
451 			rv = DO_1PASS_TEST(group, &td, group_test_getgrent,
452 				(void *)&td);
453 		else
454 			rv = DO_2PASS_TEST(group, &td, &td_snap,
455 				compare_group, NULL);
456 		break;
457 	case TEST_GETGRENT_2PASS:
458 		TEST_DATA_INIT(group, &td_2pass, clone_group, free_group);
459 		rv = group_fill_test_data(&td_2pass, NULL);
460 		if (rv != -1)
461 			rv = DO_2PASS_TEST(group, &td, &td_2pass,
462 				compare_group, NULL);
463 		TEST_DATA_DESTROY(group, &td_2pass);
464 		break;
465 	case TEST_GETGRENT_INTERLEAVED_GETGRNAM:
466 		TEST_DATA_INIT(group, &td_interleaved, clone_group, free_group);
467 		rv = group_fill_test_data(&td_interleaved, group_test_getgrnam);
468 		if (rv != -1)
469 			rv = DO_2PASS_TEST(group, &td, &td_interleaved,
470 			    compare_group, NULL);
471 		TEST_DATA_DESTROY(group, &td_interleaved);
472 		break;
473 	case TEST_GETGRENT_INTERLEAVED_GETGRGID:
474 		TEST_DATA_INIT(group, &td_interleaved, clone_group, free_group);
475 		rv = group_fill_test_data(&td_interleaved, group_test_getgrgid);
476 		if (rv != -1)
477 			rv = DO_2PASS_TEST(group, &td, &td_interleaved,
478 			    compare_group, NULL);
479 		TEST_DATA_DESTROY(group, &td_interleaved);
480 		break;
481 	case TEST_BUILD_SNAPSHOT:
482 		if (snapshot_file != NULL)
483 			rv = TEST_SNAPSHOT_FILE_WRITE(group, snapshot_file, &td,
484 			    sdump_group);
485 		break;
486 	default:
487 		rv = 0;
488 		break;
489 	}
490 
491 fin:
492 	TEST_DATA_DESTROY(group, &td_snap);
493 	TEST_DATA_DESTROY(group, &td);
494 
495 	return (rv);
496 }
497 
498 #define	SNAPSHOT_FILE	"snapshot_grp"
499 
500 ATF_TC_WITHOUT_HEAD(getgrent);
501 ATF_TC_BODY(getgrent, tc)
502 {
503 	ATF_REQUIRE(run_tests(NULL, TEST_GETGRENT) == 0);
504 }
505 
506 ATF_TC_WITHOUT_HEAD(getgrent_with_snapshot);
507 ATF_TC_BODY(getgrent_with_snapshot, tc)
508 {
509 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
510 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0);
511 }
512 
513 ATF_TC_WITHOUT_HEAD(getgrent_with_two_pass);
514 ATF_TC_BODY(getgrent_with_two_pass, tc)
515 {
516 	ATF_REQUIRE(run_tests(NULL, TEST_GETGRENT_2PASS) == 0);
517 }
518 
519 ATF_TC_WITHOUT_HEAD(getgrgid);
520 ATF_TC_BODY(getgrgid, tc)
521 {
522 	ATF_REQUIRE(run_tests(NULL, TEST_GETGRGID) == 0);
523 }
524 
525 ATF_TC_WITHOUT_HEAD(getgrgid_with_snapshot);
526 ATF_TC_BODY(getgrgid_with_snapshot, tc)
527 {
528 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
529 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0);
530 }
531 
532 ATF_TC_WITHOUT_HEAD(getgrnam);
533 ATF_TC_BODY(getgrnam, tc)
534 {
535 	ATF_REQUIRE(run_tests(NULL, TEST_GETGRNAM) == 0);
536 }
537 
538 ATF_TC_WITHOUT_HEAD(getgrnam_with_snapshot);
539 ATF_TC_BODY(getgrnam_with_snapshot, tc)
540 {
541 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
542 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0);
543 }
544 
545 ATF_TC_WITHOUT_HEAD(getgrent_interleaved_getgrnam);
546 ATF_TC_BODY(getgrent_interleaved_getgrnam, tc)
547 {
548 	ATF_REQUIRE(run_tests(NULL, TEST_GETGRENT_INTERLEAVED_GETGRNAM) == 0);
549 }
550 
551 ATF_TC_WITHOUT_HEAD(getgrent_interleaved_getgrgid);
552 ATF_TC_BODY(getgrent_interleaved_getgrgid, tc)
553 {
554 	ATF_REQUIRE(run_tests(NULL, TEST_GETGRENT_INTERLEAVED_GETGRGID) == 0);
555 }
556 
557 ATF_TP_ADD_TCS(tp)
558 {
559 	ATF_TP_ADD_TC(tp, getgrent);
560 	ATF_TP_ADD_TC(tp, getgrent_with_snapshot);
561 	ATF_TP_ADD_TC(tp, getgrent_with_two_pass);
562 	ATF_TP_ADD_TC(tp, getgrgid);
563 	ATF_TP_ADD_TC(tp, getgrgid_with_snapshot);
564 	ATF_TP_ADD_TC(tp, getgrnam);
565 	ATF_TP_ADD_TC(tp, getgrnam_with_snapshot);
566 	ATF_TP_ADD_TC(tp, getgrent_interleaved_getgrnam);
567 	ATF_TP_ADD_TC(tp, getgrent_interleaved_getgrgid);
568 
569 	return (atf_no_error());
570 }
571