xref: /freebsd/lib/libc/tests/nss/getusershell_test.c (revision 315ee00f)
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 <assert.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include <atf-c.h>
37 
38 #include "testutil.h"
39 
40 enum test_methods {
41 	TEST_GETUSERSHELL,
42 	TEST_BUILD_SNAPSHOT
43 };
44 
45 struct usershell {
46 	char *path;
47 };
48 
49 DECLARE_TEST_DATA(usershell)
50 DECLARE_TEST_FILE_SNAPSHOT(usershell)
51 DECLARE_2PASS_TEST(usershell)
52 
53 static void clone_usershell(struct usershell *, struct usershell const *);
54 static int compare_usershell(struct usershell *, struct usershell *, void *);
55 static void free_usershell(struct usershell *);
56 
57 static void sdump_usershell(struct usershell *, char *, size_t);
58 static void dump_usershell(struct usershell *);
59 
60 IMPLEMENT_TEST_DATA(usershell)
61 IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
62 IMPLEMENT_2PASS_TEST(usershell)
63 
64 static void
65 clone_usershell(struct usershell *dest, struct usershell const *src)
66 {
67 	assert(dest != NULL);
68 	assert(src != NULL);
69 
70 	if (src->path != NULL) {
71 		dest->path = strdup(src->path);
72 		assert(dest->path != NULL);
73 	}
74 }
75 
76 static int
77 compare_usershell(struct usershell *us1, struct usershell *us2,
78     void *mdata __unused)
79 {
80 	int rv;
81 
82 	assert(us1 != NULL);
83 	assert(us2 != NULL);
84 
85 	dump_usershell(us1);
86 	dump_usershell(us2);
87 
88 	if (us1 == us2)
89 		return (0);
90 
91 	rv = strcmp(us1->path, us2->path);
92 	if (rv != 0) {
93 		printf("following structures are not equal:\n");
94 		dump_usershell(us1);
95 		dump_usershell(us2);
96 	}
97 
98 	return (rv);
99 }
100 
101 static void
102 free_usershell(struct usershell *us)
103 {
104 	free(us->path);
105 }
106 
107 static void
108 sdump_usershell(struct usershell *us, char *buffer, size_t buflen)
109 {
110 	snprintf(buffer, buflen, "%s", us->path);
111 }
112 
113 static void
114 dump_usershell(struct usershell *us)
115 {
116 	if (us != NULL) {
117 		char buffer[2048];
118 		sdump_usershell(us, buffer, sizeof(buffer));
119 		printf("%s\n", buffer);
120 	} else
121 		printf("(null)\n");
122 }
123 
124 static int
125 usershell_read_snapshot_func(struct usershell *us, char *line)
126 {
127 
128 	us->path = strdup(line);
129 	ATF_REQUIRE(us->path != NULL);
130 
131 	return (0);
132 }
133 
134 static int
135 run_tests(const char *snapshot_file, enum test_methods method)
136 {
137 	struct usershell_test_data td, td_snap;
138 	struct usershell ushell;
139 	int rv;
140 
141 	rv = 0;
142 
143 	TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
144 	TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
145 
146 	setusershell();
147 	while ((ushell.path = getusershell()) != NULL) {
148 		printf("usershell found:\n");
149 		dump_usershell(&ushell);
150 		TEST_DATA_APPEND(usershell, &td, &ushell);
151 	}
152 	endusershell();
153 
154 	if (snapshot_file != NULL) {
155 		if (access(snapshot_file, W_OK | R_OK) != 0) {
156 			if (errno == ENOENT)
157 				method = TEST_BUILD_SNAPSHOT;
158 			else {
159 				printf("can't access the snapshot file %s\n",
160 				    snapshot_file);
161 
162 				rv = -1;
163 				goto fin;
164 			}
165 		} else {
166 			rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
167 				&td_snap, usershell_read_snapshot_func);
168 			if (rv != 0) {
169 				printf("error reading snapshot file\n");
170 				goto fin;
171 			}
172 		}
173 	}
174 
175 	switch (method) {
176 	case TEST_GETUSERSHELL:
177 		rv = DO_2PASS_TEST(usershell, &td, &td_snap,
178 			compare_usershell, NULL);
179 		break;
180 	case TEST_BUILD_SNAPSHOT:
181 		if (snapshot_file != NULL) {
182 			rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file,
183 			    &td, sdump_usershell);
184 		}
185 		break;
186 	default:
187 		rv = 0;
188 		break;
189 	}
190 
191 fin:
192 	TEST_DATA_DESTROY(usershell, &td_snap);
193 	TEST_DATA_DESTROY(usershell, &td);
194 
195 	return (rv);
196 }
197 
198 #define	SNAPSHOT_FILE	"snapshot_usershell"
199 
200 ATF_TC_WITHOUT_HEAD(getusershell_with_snapshot);
201 ATF_TC_BODY(getusershell_with_snapshot, tc)
202 {
203 
204 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
205 }
206 
207 ATF_TC_WITHOUT_HEAD(getusershell_with_two_pass);
208 ATF_TC_BODY(getusershell_with_two_pass, tc)
209 {
210 
211 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
212 	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETUSERSHELL) == 0);
213 }
214 
215 ATF_TP_ADD_TCS(tp)
216 {
217 
218 	ATF_TP_ADD_TC(tp, getusershell_with_snapshot);
219 	ATF_TP_ADD_TC(tp, getusershell_with_two_pass);
220 
221 	return (atf_no_error());
222 }
223