2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
49 static enum test_methods method = TEST_GETUSERSHELL;
51 DECLARE_TEST_DATA(usershell)
52 DECLARE_TEST_FILE_SNAPSHOT(usershell)
53 DECLARE_2PASS_TEST(usershell)
55 static void clone_usershell(struct usershell *, struct usershell const *);
56 static int compare_usershell(struct usershell *, struct usershell *, void *);
57 static void free_usershell(struct usershell *);
59 static void sdump_usershell(struct usershell *, char *, size_t);
60 static void dump_usershell(struct usershell *);
62 static int usershell_read_snapshot_func(struct usershell *, char *);
64 static void usage(void) __attribute__((__noreturn__));
66 IMPLEMENT_TEST_DATA(usershell)
67 IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
68 IMPLEMENT_2PASS_TEST(usershell)
71 clone_usershell(struct usershell *dest, struct usershell const *src)
76 if (src->path != NULL) {
77 dest->path = strdup(src->path);
78 assert(dest->path != NULL);
83 compare_usershell(struct usershell *us1, struct usershell *us2, void *mdata)
96 rv = strcmp(us1->path, us2->path);
98 printf("following structures are not equal:\n");
107 free_usershell(struct usershell *us)
113 sdump_usershell(struct usershell *us, char *buffer, size_t buflen)
115 snprintf(buffer, buflen, "%s", us->path);
119 dump_usershell(struct usershell *us)
123 sdump_usershell(us, buffer, sizeof(buffer));
124 printf("%s\n", buffer);
130 usershell_read_snapshot_func(struct usershell *us, char *line)
132 us->path = strdup(line);
133 assert(us->path != NULL);
141 (void)fprintf(stderr,
142 "Usage: %s [-d] -s <file>\n",
148 main(int argc, char **argv)
150 struct usershell_test_data td, td_snap;
151 struct usershell ushell;
160 snapshot_file = NULL;
161 while ((c = getopt(argc, argv, "ds:")) != -1) {
167 snapshot_file = strdup(optarg);
174 TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
175 TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
178 while ((ushell.path = getusershell()) != NULL) {
180 printf("usershell found:\n");
181 dump_usershell(&ushell);
183 TEST_DATA_APPEND(usershell, &td, &ushell);
188 if (snapshot_file != NULL) {
189 if (access(snapshot_file, W_OK | R_OK) != 0) {
191 method = TEST_BUILD_SNAPSHOT;
194 printf("can't access the snapshot file %s\n",
201 rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
202 &td_snap, usershell_read_snapshot_func);
205 printf("error reading snapshot file\n");
212 case TEST_GETUSERSHELL:
213 if (snapshot_file != NULL) {
214 rv = DO_2PASS_TEST(usershell, &td, &td_snap,
215 compare_usershell, NULL);
218 case TEST_BUILD_SNAPSHOT:
219 if (snapshot_file != NULL) {
220 rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file, &td,
230 TEST_DATA_DESTROY(usershell, &td_snap);
231 TEST_DATA_DESTROY(usershell, &td);