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