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