Minor change to path lookup that fixes the bug that creating a file
[minix3.git] / test / test2.c
bloba12077717efa243d992ee773140f676d38d5f2df
1 /* test 2 */
3 #include <sys/types.h>
4 #include <sys/times.h>
5 #include <sys/wait.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <time.h>
11 #include <stdio.h>
13 #define ITERATIONS 5
14 #define MAX_ERROR 4
16 int is, array[4], parsigs, parcum, sigct, cumsig, errct, subtest;
17 int iteration, kk = 0, errct = 0;
18 char buf[2048];
20 _PROTOTYPE(int main, (int argc, char *argv []));
21 _PROTOTYPE(void test2a, (void));
22 _PROTOTYPE(void test2b, (void));
23 _PROTOTYPE(void test2c, (void));
24 _PROTOTYPE(void test2d, (void));
25 _PROTOTYPE(void test2e, (void));
26 _PROTOTYPE(void test2f, (void));
27 _PROTOTYPE(void test2g, (void));
28 _PROTOTYPE(void test2h, (void));
29 _PROTOTYPE(void sigpip, (int s));
30 _PROTOTYPE(void quit, (void));
31 _PROTOTYPE(void e, (int n));
33 int main(argc, argv)
34 int argc;
35 char *argv[];
37 int i, m = 0xFFFF;
39 sync();
41 if (argc == 2) m = atoi(argv[1]);
43 printf("Test 2 ");
44 fflush(stdout); /* have to flush for child's benefit */
46 system("rm -rf DIR_02; mkdir DIR_02");
47 chdir("DIR_02");
49 for (i = 0; i < ITERATIONS; i++) {
50 iteration = i;
51 if (m & 0001) test2a();
52 if (m & 0002) test2b();
53 if (m & 0004) test2c();
54 if (m & 0010) test2d();
55 if (m & 0020) test2e();
56 if (m & 0040) test2f();
57 if (m & 0100) test2g();
58 if (m & 0200) test2h();
60 subtest = 100;
61 if (cumsig != ITERATIONS) e(101);
62 quit();
63 return(-1); /* impossible */
66 void test2a()
68 /* Test pipes */
70 int fd[2];
71 int n, i, j, q = 0;
73 subtest = 1;
74 if (pipe(fd) < 0) {
75 printf("pipe error. errno= %d\n", errno);
76 errct++;
77 quit();
79 i = fork();
80 if (i < 0) {
81 printf("fork failed\n");
82 errct++;
83 quit();
85 if (i != 0) {
86 /* Parent code */
87 close(fd[0]);
88 for (i = 0; i < 2048; i++) buf[i] = i & 0377;
89 for (q = 0; q < 8; q++) {
90 if (write(fd[1], buf, 2048) < 0) {
91 printf("write pipe err. errno=%d\n", errno);
92 errct++;
93 quit();
96 close(fd[1]);
97 wait(&q);
98 if (q != 256 * 58) {
99 printf("wrong exit code %d\n", q);
100 errct++;
101 quit();
103 } else {
104 /* Child code */
105 close(fd[1]);
106 for (q = 0; q < 32; q++) {
107 n = read(fd[0], buf, 512);
108 if (n != 512) {
109 printf("read yielded %d bytes, not 512\n", n);
110 errct++;
111 quit();
113 for (j = 0; j < n; j++)
114 if ((buf[j] & 0377) != (kk & 0377)) {
115 printf("wrong data: %d %d %d \n ",
116 j, buf[j] & 0377, kk & 0377);
117 } else {
118 kk++;
121 exit(58);
125 void test2b()
127 int fd[2], n;
128 char buf[4];
130 subtest = 2;
131 sigct = 0;
132 signal(SIGPIPE, sigpip);
133 pipe(fd);
134 if (fork()) {
135 /* Parent */
136 close(fd[0]);
137 while (sigct == 0) {
138 write(fd[1], buf, 1);
140 wait(&n);
141 } else {
142 /* Child */
143 close(fd[0]);
144 close(fd[1]);
145 exit(0);
149 void test2c()
151 int n;
153 subtest = 3;
154 signal(SIGINT, SIG_DFL);
155 is = 0;
156 if ((array[is++] = fork()) > 0) {
157 if ((array[is++] = fork()) > 0) {
158 if ((array[is++] = fork()) > 0) {
159 if ((array[is++] = fork()) > 0) {
160 signal(SIGINT, SIG_IGN);
161 kill(array[0], SIGINT);
162 kill(array[1], SIGINT);
163 kill(array[2], SIGINT);
164 kill(array[3], SIGINT);
165 wait(&n);
166 wait(&n);
167 wait(&n);
168 wait(&n);
169 } else {
170 pause();
172 } else {
173 pause();
175 } else {
176 pause();
178 } else {
179 pause();
183 void test2d()
186 int pid, stat_loc, s;
188 /* Test waitpid. */
189 subtest = 4;
191 /* Test waitpid(pid, arg2, 0) */
192 pid = fork();
193 if (pid < 0) e(1);
194 if (pid > 0) {
195 /* Parent. */
196 s = waitpid(pid, &stat_loc, 0);
197 if (s != pid) e(2);
198 if (WIFEXITED(stat_loc) == 0) e(3);
199 if (WIFSIGNALED(stat_loc) != 0) e(4);
200 if (WEXITSTATUS(stat_loc) != 22) e(5);
201 } else {
202 /* Child */
203 exit(22);
206 /* Test waitpid(-1, arg2, 0) */
207 pid = fork();
208 if (pid < 0) e(6);
209 if (pid > 0) {
210 /* Parent. */
211 s = waitpid(-1, &stat_loc, 0);
212 if (s != pid) e(7);
213 if (WIFEXITED(stat_loc) == 0) e(8);
214 if (WIFSIGNALED(stat_loc) != 0) e(9);
215 if (WEXITSTATUS(stat_loc) != 33) e(10);
216 } else {
217 /* Child */
218 exit(33);
221 /* Test waitpid(0, arg2, 0) */
222 pid = fork();
223 if (pid < 0) e(11);
224 if (pid > 0) {
225 /* Parent. */
226 s = waitpid(0, &stat_loc, 0);
227 if (s != pid) e(12);
228 if (WIFEXITED(stat_loc) == 0) e(13);
229 if (WIFSIGNALED(stat_loc) != 0) e(14);
230 if (WEXITSTATUS(stat_loc) != 44) e(15);
231 } else {
232 /* Child */
233 exit(44);
236 /* Test waitpid(0, arg2, WNOHANG) */
237 signal(SIGTERM, SIG_DFL);
238 pid = fork();
239 if (pid < 0) e(16);
240 if (pid > 0) {
241 /* Parent. */
242 s = waitpid(0, &stat_loc, WNOHANG);
243 if (s != 0) e(17);
244 if (kill(pid, SIGTERM) != 0) e(18);
245 if (waitpid(pid, &stat_loc, 0) != pid) e(19);
246 if (WIFEXITED(stat_loc) != 0) e(20);
247 if (WIFSIGNALED(stat_loc) == 0) e(21);
248 if (WTERMSIG(stat_loc) != SIGTERM) e(22);
249 } else {
250 /* Child */
251 pause();
254 /* Test some error conditions. */
255 errno = 9999;
256 if (waitpid(0, &stat_loc, 0) != -1) e(23);
257 if (errno != ECHILD) e(24);
258 errno = 9999;
259 if (waitpid(0, &stat_loc, WNOHANG) != -1) e(25);
260 if (errno != ECHILD) e(26);
263 void test2e()
266 int pid1, pid2, stat_loc, s;
268 /* Test waitpid with two children. */
269 subtest = 5;
270 if (iteration > 1) return; /* slow test, don't do it too much */
271 if ( (pid1 = fork())) {
272 /* Parent. */
273 if ( (pid2 = fork()) ) {
274 /* Parent. Collect second child first. */
275 s = waitpid(pid2, &stat_loc, 0);
276 if (s != pid2) e(1);
277 if (WIFEXITED(stat_loc) == 0) e(2);
278 if (WIFSIGNALED(stat_loc) != 0) e(3);
279 if (WEXITSTATUS(stat_loc) != 222) e(4);
281 /* Now collect first child. */
282 s = waitpid(pid1, &stat_loc, 0);
283 if (s != pid1) e(5);
284 if (WIFEXITED(stat_loc) == 0) e(6);
285 if (WIFSIGNALED(stat_loc) != 0) e(7);
286 if (WEXITSTATUS(stat_loc) != 111) e(8);
287 } else {
288 /* Child 2. */
289 sleep(2); /* child 2 delays before exiting. */
290 exit(222);
292 } else {
293 /* Child 1. */
294 exit(111); /* child 1 exits immediately */
299 void test2f()
301 /* test getpid, getppid, getuid, etc. */
303 pid_t pid, pid1, ppid, cpid, stat_loc, err;
305 subtest = 6;
306 errno = -2000;
307 err = 0;
308 pid = getpid();
309 if ( (pid1 = fork())) {
310 /* Parent. Do nothing. */
311 if (wait(&stat_loc) != pid1) e(1);
312 if (WEXITSTATUS(stat_loc) != (pid1 & 0377)) e(2);
313 } else {
314 /* Child. Get ppid. */
315 cpid = getpid();
316 ppid = getppid();
317 if (ppid != pid) err = 3;
318 if (cpid == ppid) err = 4;
319 exit(cpid & 0377);
321 if (err != 0) e(err);
324 void test2g()
326 /* test time(), times() */
328 time_t t1, t2;
329 clock_t t3, t4;
330 struct tms tmsbuf;
332 subtest = 7;
333 errno = -7000;
335 /* First time(). */
336 t1 = -1;
337 t2 = -2;
338 t1 = time(&t2);
339 if (t1 < 650000000L) e(1); /* 650000000 is Sept. 1990 */
340 if (t1 != t2) e(2);
341 t1 = -1;
342 t1 = time( (time_t *) NULL);
343 if (t1 < 650000000L) e(3);
344 t3 = times(&tmsbuf);
345 sleep(1);
346 t2 = time( (time_t *) NULL);
347 if (t2 < 0L) e(4);
348 if (t2 - t1 < 1) e(5);
350 /* Now times(). */
351 t4 = times(&tmsbuf);
352 if ( t4 == (clock_t) -1) e(6);
353 if (t4 - t3 < CLOCKS_PER_SEC) e(7);
354 if (tmsbuf.tms_utime < 0) e(8);
355 if (tmsbuf.tms_stime < 0) e(9);
356 if (tmsbuf.tms_cutime < 0) e(10);
357 if (tmsbuf.tms_cstime < 0) e(11);
360 void test2h()
362 /* Test getgroups(). */
364 gid_t g[10];
366 subtest = 8;
367 errno = -8000;
368 if (getgroups(10, g) != 0) e(1);
369 if (getgroups(1, g) != 0) e(2);
370 if (getgroups(0, g) != 0) e(3);
373 void sigpip(s)
374 int s; /* for ANSI */
376 sigct++;
377 cumsig++;
380 void quit()
383 chdir("..");
384 system("rm -rf DIR*");
386 if (errct == 0) {
387 printf("ok\n");
388 exit(0);
389 } else {
390 printf("%d errors\n", errct);
391 exit(4);
395 void e(n)
396 int n;
398 int err_num = errno; /* save errno in case printf clobbers it */
400 printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
401 errno = err_num; /* restore errno, just in case */
402 perror("");
403 if (errct++ > MAX_ERROR) {
404 printf("Too many errors; test aborted\n");
405 chdir("..");
406 system("rm -rf DIR*");
407 exit(1);