Deleted Added
full compact
getusershell_test.c (291363) getusershell_test.c (292323)
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

--- 12 unchanged lines hidden (view full) ---

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>
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

--- 12 unchanged lines hidden (view full) ---

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__FBSDID("$FreeBSD: head/tools/regression/lib/libc/nss/test-getusershell.c 291363 2015-11-26 08:58:13Z ngie $");
29__FBSDID("$FreeBSD: head/lib/libc/tests/nss/getusershell_test.c 292323 2015-12-16 08:09:03Z ngie $");
30
31#include <assert.h>
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
30
31#include <assert.h>
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include <atf-c.h>
39
37#include "testutil.h"
38
39enum test_methods {
40 TEST_GETUSERSHELL,
41 TEST_BUILD_SNAPSHOT
42};
43
44struct usershell {
45 char *path;
46};
47
40#include "testutil.h"
41
42enum test_methods {
43 TEST_GETUSERSHELL,
44 TEST_BUILD_SNAPSHOT
45};
46
47struct usershell {
48 char *path;
49};
50
48static int debug = 0;
49static enum test_methods method = TEST_GETUSERSHELL;
50
51DECLARE_TEST_DATA(usershell)
52DECLARE_TEST_FILE_SNAPSHOT(usershell)
53DECLARE_2PASS_TEST(usershell)
54
55static void clone_usershell(struct usershell *, struct usershell const *);
56static int compare_usershell(struct usershell *, struct usershell *, void *);
57static void free_usershell(struct usershell *);
58
59static void sdump_usershell(struct usershell *, char *, size_t);
60static void dump_usershell(struct usershell *);
61
51static enum test_methods method = TEST_GETUSERSHELL;
52
53DECLARE_TEST_DATA(usershell)
54DECLARE_TEST_FILE_SNAPSHOT(usershell)
55DECLARE_2PASS_TEST(usershell)
56
57static void clone_usershell(struct usershell *, struct usershell const *);
58static int compare_usershell(struct usershell *, struct usershell *, void *);
59static void free_usershell(struct usershell *);
60
61static void sdump_usershell(struct usershell *, char *, size_t);
62static void dump_usershell(struct usershell *);
63
62static int usershell_read_snapshot_func(struct usershell *, char *);
63
64static void usage(void) __attribute__((__noreturn__));
65
66IMPLEMENT_TEST_DATA(usershell)
67IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
68IMPLEMENT_2PASS_TEST(usershell)
69
70static void
71clone_usershell(struct usershell *dest, struct usershell const *src)
72{
73 assert(dest != NULL);

--- 50 unchanged lines hidden (view full) ---

124 printf("%s\n", buffer);
125 } else
126 printf("(null)\n");
127}
128
129static int
130usershell_read_snapshot_func(struct usershell *us, char *line)
131{
64IMPLEMENT_TEST_DATA(usershell)
65IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
66IMPLEMENT_2PASS_TEST(usershell)
67
68static void
69clone_usershell(struct usershell *dest, struct usershell const *src)
70{
71 assert(dest != NULL);

--- 50 unchanged lines hidden (view full) ---

122 printf("%s\n", buffer);
123 } else
124 printf("(null)\n");
125}
126
127static int
128usershell_read_snapshot_func(struct usershell *us, char *line)
129{
130
132 us->path = strdup(line);
131 us->path = strdup(line);
133 assert(us->path != NULL);
132 ATF_REQUIRE(us->path != NULL);
134
135 return (0);
136}
137
133
134 return (0);
135}
136
138static void
139usage(void)
140{
141 (void)fprintf(stderr,
142 "Usage: %s [-d] -s <file>\n",
143 getprogname());
144 exit(1);
145}
146
147int
137int
148main(int argc, char **argv)
138run_tests(const char *snapshot_file, enum test_methods method)
149{
150 struct usershell_test_data td, td_snap;
151 struct usershell ushell;
139{
140 struct usershell_test_data td, td_snap;
141 struct usershell ushell;
152 char *snapshot_file;
153 int rv;
142 int rv;
154 int c;
155
143
156 if (argc < 2)
157 usage();
158
159 rv = 0;
144 rv = 0;
160 snapshot_file = NULL;
161 while ((c = getopt(argc, argv, "ds:")) != -1) {
162 switch (c) {
163 case 'd':
164 debug = 1;
165 break;
166 case 's':
167 snapshot_file = strdup(optarg);
168 break;
169 default:
170 usage();
171 }
172 }
173
174 TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
175 TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
176
177 setusershell();
178 while ((ushell.path = getusershell()) != NULL) {
145
146 TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
147 TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
148
149 setusershell();
150 while ((ushell.path = getusershell()) != NULL) {
179 if (debug) {
180 printf("usershell found:\n");
181 dump_usershell(&ushell);
182 }
151 printf("usershell found:\n");
152 dump_usershell(&ushell);
183 TEST_DATA_APPEND(usershell, &td, &ushell);
184 }
185 endusershell();
186
153 TEST_DATA_APPEND(usershell, &td, &ushell);
154 }
155 endusershell();
156
187
188 if (snapshot_file != NULL) {
189 if (access(snapshot_file, W_OK | R_OK) != 0) {
190 if (errno == ENOENT)
191 method = TEST_BUILD_SNAPSHOT;
192 else {
157 if (snapshot_file != NULL) {
158 if (access(snapshot_file, W_OK | R_OK) != 0) {
159 if (errno == ENOENT)
160 method = TEST_BUILD_SNAPSHOT;
161 else {
193 if (debug)
194 printf("can't access the snapshot file %s\n",
162 printf("can't access the snapshot file %s\n",
195 snapshot_file);
196
197 rv = -1;
198 goto fin;
199 }
200 } else {
201 rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
202 &td_snap, usershell_read_snapshot_func);
203 if (rv != 0) {
163 snapshot_file);
164
165 rv = -1;
166 goto fin;
167 }
168 } else {
169 rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
170 &td_snap, usershell_read_snapshot_func);
171 if (rv != 0) {
204 if (debug)
205 printf("error reading snapshot file\n");
172 printf("error reading snapshot file\n");
206 goto fin;
207 }
208 }
209 }
210
211 switch (method) {
212 case TEST_GETUSERSHELL:
173 goto fin;
174 }
175 }
176 }
177
178 switch (method) {
179 case TEST_GETUSERSHELL:
213 if (snapshot_file != NULL) {
214 rv = DO_2PASS_TEST(usershell, &td, &td_snap,
215 compare_usershell, NULL);
216 }
180 rv = DO_2PASS_TEST(usershell, &td, &td_snap,
181 compare_usershell, NULL);
217 break;
218 case TEST_BUILD_SNAPSHOT:
219 if (snapshot_file != NULL) {
182 break;
183 case TEST_BUILD_SNAPSHOT:
184 if (snapshot_file != NULL) {
220 rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file, &td,
221 sdump_usershell);
185 rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file,
186 &td, sdump_usershell);
222 }
223 break;
224 default:
225 rv = 0;
226 break;
187 }
188 break;
189 default:
190 rv = 0;
191 break;
227 };
192 }
228
229fin:
230 TEST_DATA_DESTROY(usershell, &td_snap);
231 TEST_DATA_DESTROY(usershell, &td);
193
194fin:
195 TEST_DATA_DESTROY(usershell, &td_snap);
196 TEST_DATA_DESTROY(usershell, &td);
232 free(snapshot_file);
197
233 return (rv);
198 return (rv);
199}
234
200
201#define SNAPSHOT_FILE "snapshot_usershell"
202
203ATF_TC_WITHOUT_HEAD(getusershell_with_snapshot);
204ATF_TC_BODY(getusershell_with_snapshot, tc)
205{
206
207 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
235}
208}
209
210ATF_TC_WITHOUT_HEAD(getusershell_with_two_pass);
211ATF_TC_BODY(getusershell_with_two_pass, tc)
212{
213
214 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
215 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETUSERSHELL) == 0);
216}
217
218ATF_TP_ADD_TCS(tp)
219{
220
221 ATF_TP_ADD_TC(tp, getusershell_with_snapshot);
222 ATF_TP_ADD_TC(tp, getusershell_with_two_pass);
223
224 return (atf_no_error());
225}