/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- finderror
- report_survival
- report_test
- report_test2
- open_testfile
- create_testfile
- create_testdir
- create_testlink
- menu
- runit
- main
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3 * The President and Fellows of Harvard College.
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 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <err.h>
38
39 #include "config.h"
40 #include "test.h"
41
42 ////////////////////////////////////////////////////////////
43
44 static
45 int
46 finderror(int rv, int error)
47 {
48 if (rv==-1) {
49 return error;
50 }
51 else {
52 return 0;
53 }
54 }
55
56 void
57 report_survival(int rv, int error, const char *desc)
58 {
59 /* allow any error as long as we survive */
60 errno = finderror(rv, error);
61 warn("passed: %s", desc);
62 }
63
64 void
65 report_test(int rv, int error, int right_error, const char *desc)
66 {
67 int goterror = finderror(rv, error);
68
69 if (goterror == right_error) {
70 warnx("passed: %s", desc);
71 }
72 else if (goterror == EUNIMP || goterror == ENOSYS) {
73 warnx("------: %s (unimplemented)", desc);
74 }
75 else {
76 errno = goterror;
77 warn("FAILURE: %s", desc);
78 }
79 }
80
81 void
82 report_test2(int rv, int error, int okerr1, int okerr2, const char *desc)
83 {
84 int goterror = finderror(rv, error);
85 if (goterror == okerr1 || goterror == okerr2) {
86 warnx("passed: %s", desc);
87 }
88 else if (goterror == EUNIMP || goterror == ENOSYS) {
89 warnx("------: %s (unimplemented)", desc);
90 }
91 else {
92 errno = goterror;
93 warn("FAILURE: %s", desc);
94 }
95 }
96
97 ////////////////////////////////////////////////////////////
98
99 int
100 open_testfile(const char *string)
101 {
102 int fd, rv;
103 size_t len;
104
105 fd = open(TESTFILE, O_RDWR|O_CREAT|O_TRUNC);
106 if (fd<0) {
107 warn("UH-OH: creating %s: failed", TESTFILE);
108 return -1;
109 }
110
111 if (string) {
112 len = strlen(string);
113 rv = write(fd, string, len);
114 if (rv<0) {
115 warn("UH-OH: write to %s failed", TESTFILE);
116 close(fd);
117 remove(TESTFILE);
118 return -1;
119 }
120 if ((unsigned)rv != len) {
121 warnx("UH-OH: write to %s got short count", TESTFILE);
122 close(fd);
123 remove(TESTFILE);
124 return -1;
125 }
126 rv = lseek(fd, 0, SEEK_SET);
127 if (rv<0) {
128 warn("UH-OH: rewind of %s failed", TESTFILE);
129 close(fd);
130 remove(TESTFILE);
131 return -1;
132 }
133 }
134 return fd;
135 }
136
137 int
138 create_testfile(void)
139 {
140 int fd, rv;
141
142 fd = open_testfile(NULL);
143 if (fd<0) {
144 return -1;
145 }
146
147 rv = close(fd);
148 if (rv<0) {
149 warn("UH-OH: closing %s failed", TESTFILE);
150 return -1;
151 }
152
153 return 0;
154 }
155
156 int
157 create_testdir(void)
158 {
159 int rv;
160 rv = mkdir(TESTDIR, 0775);
161 if (rv<0) {
162 warn("UH-OH: mkdir %s failed", TESTDIR);
163 return -1;
164 }
165 return 0;
166 }
167
168 int
169 create_testlink(void)
170 {
171 int rv;
172 rv = symlink("blahblah", TESTLINK);
173 if (rv<0) {
174 warn("UH-OH: making symlink %s failed", TESTLINK);
175 return -1;
176 }
177 return 0;
178 }
179
180 ////////////////////////////////////////////////////////////
181
182 static
183 struct {
184 int ch;
185 int asst;
186 const char *name;
187 void (*f)(void);
188 } ops[] = {
189 { 'a', 2, "execv", test_execv },
190 { 'b', 2, "waitpid", test_waitpid },
191 { 'c', 2, "open", test_open },
192 { 'd', 2, "read", test_read },
193 { 'e', 2, "write", test_write },
194 { 'f', 2, "close", test_close },
195 { 'g', 0, "reboot", test_reboot },
196 { 'h', 3, "sbrk", test_sbrk },
197 { 'i', 5, "ioctl", test_ioctl },
198 { 'j', 2, "lseek", test_lseek },
199 { 'k', 4, "fsync", test_fsync },
200 { 'l', 4, "ftruncate", test_ftruncate },
201 { 'm', 4, "fstat", test_fstat },
202 { 'n', 4, "remove", test_remove },
203 { 'o', 4, "rename", test_rename },
204 { 'p', 5, "link", test_link },
205 { 'q', 4, "mkdir", test_mkdir },
206 { 'r', 4, "rmdir", test_rmdir },
207 { 's', 2, "chdir", test_chdir },
208 { 't', 4, "getdirentry", test_getdirentry },
209 { 'u', 5, "symlink", test_symlink },
210 { 'v', 5, "readlink", test_readlink },
211 { 'w', 2, "dup2", test_dup2 },
212 { 'x', 5, "pipe", test_pipe },
213 { 'y', 5, "__time", test_time },
214 { 'z', 2, "__getcwd", test_getcwd },
215 { '{', 5, "stat", test_stat },
216 { '|', 5, "lstat", test_lstat },
217 { 0, 0, NULL, NULL }
218 };
219
220 #define LOWEST 'a'
221 #define HIGHEST '|'
222
223 static
224 void
225 menu(void)
226 {
227 int i;
228 for (i=0; ops[i].name; i++) {
229 printf("[%c] %-24s", ops[i].ch, ops[i].name);
230 if (i%2==1) {
231 printf("\n");
232 }
233 }
234 if (i%2==1) {
235 printf("\n");
236 }
237 printf("[1] %-24s", "asst1");
238 printf("[2] %-24s\n", "asst2");
239 printf("[3] %-24s", "asst3");
240 printf("[4] %-24s\n", "asst4");
241 printf("[*] %-24s", "all");
242 printf("[!] %-24s\n", "quit");
243 }
244
245 static
246 void
247 runit(int op)
248 {
249 int i, k;
250
251 if (op=='!') {
252 exit(0);
253 }
254
255 if (op=='?') {
256 menu();
257 return;
258 }
259
260 if (op=='*') {
261 for (i=0; ops[i].name; i++) {
262 printf("[%s]\n", ops[i].name);
263 ops[i].f();
264 }
265 return;
266 }
267
268 if (op>='1' && op <= '4') {
269 k = op-'0';
270 for (i=0; ops[i].name; i++) {
271 if (ops[i].asst <= k) {
272 printf("[%s]\n", ops[i].name);
273 ops[i].f();
274 }
275 }
276 return;
277 }
278
279 if (op < LOWEST || op > HIGHEST) {
280 printf("Invalid request %c\n", op);
281 return;
282 }
283
284 ops[op-'a'].f();
285 }
286
287 int
288 main(int argc, char **argv)
289 {
290 int op, i, j;
291
292 printf("[%c-%c, 1-4, *, ?=menu, !=quit]\n", LOWEST, HIGHEST);
293
294 if (argc > 1) {
295 for (i=1; i<argc; i++) {
296 for (j=0; argv[i][j]; j++) {
297 printf("Choose: %c\n",
298 argv[i][j]);
299 runit(argv[i][j]);
300 }
301 }
302 }
303 else {
304 menu();
305 while (1) {
306 printf("Choose: ");
307 op = getchar();
308 if (op==EOF) {
309 break;
310 }
311 printf("%c\n", op);
312 runit(op);
313 }
314 }
315
316 return 0;
317 }