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$");
51 static enum test_methods method = TEST_GETUSERSHELL;
53 DECLARE_TEST_DATA(usershell)
54 DECLARE_TEST_FILE_SNAPSHOT(usershell)
55 DECLARE_2PASS_TEST(usershell)
57 static void clone_usershell(struct usershell *, struct usershell const *);
58 static int compare_usershell(struct usershell *, struct usershell *, void *);
59 static void free_usershell(struct usershell *);
61 static void sdump_usershell(struct usershell *, char *, size_t);
62 static void dump_usershell(struct usershell *);
64 IMPLEMENT_TEST_DATA(usershell)
65 IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
66 IMPLEMENT_2PASS_TEST(usershell)
69 clone_usershell(struct usershell *dest, struct usershell const *src)
74 if (src->path != NULL) {
75 dest->path = strdup(src->path);
76 assert(dest->path != NULL);
81 compare_usershell(struct usershell *us1, struct usershell *us2, void *mdata)
94 rv = strcmp(us1->path, us2->path);
96 printf("following structures are not equal:\n");
105 free_usershell(struct usershell *us)
111 sdump_usershell(struct usershell *us, char *buffer, size_t buflen)
113 snprintf(buffer, buflen, "%s", us->path);
117 dump_usershell(struct usershell *us)
121 sdump_usershell(us, buffer, sizeof(buffer));
122 printf("%s\n", buffer);
128 usershell_read_snapshot_func(struct usershell *us, char *line)
131 us->path = strdup(line);
132 ATF_REQUIRE(us->path != NULL);
138 run_tests(const char *snapshot_file, enum test_methods method)
140 struct usershell_test_data td, td_snap;
141 struct usershell ushell;
146 TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
147 TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
150 while ((ushell.path = getusershell()) != NULL) {
151 printf("usershell found:\n");
152 dump_usershell(&ushell);
153 TEST_DATA_APPEND(usershell, &td, &ushell);
157 if (snapshot_file != NULL) {
158 if (access(snapshot_file, W_OK | R_OK) != 0) {
160 method = TEST_BUILD_SNAPSHOT;
162 printf("can't access the snapshot file %s\n",
169 rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
170 &td_snap, usershell_read_snapshot_func);
172 printf("error reading snapshot file\n");
179 case TEST_GETUSERSHELL:
180 rv = DO_2PASS_TEST(usershell, &td, &td_snap,
181 compare_usershell, NULL);
183 case TEST_BUILD_SNAPSHOT:
184 if (snapshot_file != NULL) {
185 rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file,
186 &td, sdump_usershell);
195 TEST_DATA_DESTROY(usershell, &td_snap);
196 TEST_DATA_DESTROY(usershell, &td);
201 #define SNAPSHOT_FILE "snapshot_usershell"
203 ATF_TC_WITHOUT_HEAD(getusershell_with_snapshot);
204 ATF_TC_BODY(getusershell_with_snapshot, tc)
207 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
210 ATF_TC_WITHOUT_HEAD(getusershell_with_two_pass);
211 ATF_TC_BODY(getusershell_with_two_pass, tc)
214 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
215 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETUSERSHELL) == 0);
221 ATF_TP_ADD_TC(tp, getusershell_with_snapshot);
222 ATF_TP_ADD_TC(tp, getusershell_with_two_pass);
224 return (atf_no_error());