kernel: some boottime sanitychecks
[minix.git] / test / test2.c
blob0e20d01ed51ce4c5f6a1d034b63bc14652142838
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, subtest;
17 int iteration, kk = 0;
18 char buf[2048];
20 #include "common.c"
22 int main(int argc, char *argv []);
23 void test2a(void);
24 void test2b(void);
25 void test2c(void);
26 void test2d(void);
27 void test2e(void);
28 void test2f(void);
29 void test2g(void);
30 void sigpip(int s);
32 int main(argc, argv)
33 int argc;
34 char *argv[];
36 int i, m = 0xFFFF;
38 start(2);
40 for (i = 0; i < ITERATIONS; i++) {
41 iteration = i;
42 if (m & 0001) test2a();
43 if (m & 0002) test2b();
44 if (m & 0004) test2c();
45 if (m & 0010) test2d();
46 if (m & 0020) test2e();
47 if (m & 0040) test2f();
48 if (m & 0100) test2g();
50 subtest = 100;
51 if (cumsig != ITERATIONS) e(101);
52 quit();
53 return(-1); /* impossible */
56 void test2a()
58 /* Test pipes */
60 int fd[2];
61 int n, i, j, q = 0;
63 subtest = 1;
64 if (pipe(fd) < 0) {
65 printf("pipe error. errno= %d\n", errno);
66 errct++;
67 quit();
69 i = fork();
70 if (i < 0) {
71 printf("fork failed\n");
72 errct++;
73 quit();
75 if (i != 0) {
76 /* Parent code */
77 close(fd[0]);
78 for (i = 0; i < 2048; i++) buf[i] = i & 0377;
79 for (q = 0; q < 8; q++) {
80 if (write(fd[1], buf, 2048) < 0) {
81 printf("write pipe err. errno=%d\n", errno);
82 errct++;
83 quit();
86 close(fd[1]);
87 wait(&q);
88 if (q != 256 * 58) {
89 printf("wrong exit code %d\n", q);
90 errct++;
91 quit();
93 } else {
94 /* Child code */
95 close(fd[1]);
96 for (q = 0; q < 32; q++) {
97 n = read(fd[0], buf, 512);
98 if (n != 512) {
99 printf("read yielded %d bytes, not 512\n", n);
100 errct++;
101 quit();
103 for (j = 0; j < n; j++)
104 if ((buf[j] & 0377) != (kk & 0377)) {
105 printf("wrong data: %d %d %d \n ",
106 j, buf[j] & 0377, kk & 0377);
107 } else {
108 kk++;
111 exit(58);
115 void test2b()
117 int fd[2], n;
118 char buf[4];
120 subtest = 2;
121 sigct = 0;
122 signal(SIGPIPE, sigpip);
123 pipe(fd);
124 if (fork()) {
125 /* Parent */
126 close(fd[0]);
127 while (sigct == 0) {
128 write(fd[1], buf, 1);
130 wait(&n);
131 } else {
132 /* Child */
133 close(fd[0]);
134 close(fd[1]);
135 exit(0);
139 void test2c()
141 int n;
143 subtest = 3;
144 signal(SIGINT, SIG_DFL);
145 is = 0;
146 if ((array[is++] = fork()) > 0) {
147 if ((array[is++] = fork()) > 0) {
148 if ((array[is++] = fork()) > 0) {
149 if ((array[is++] = fork()) > 0) {
150 signal(SIGINT, SIG_IGN);
151 kill(array[0], SIGINT);
152 kill(array[1], SIGINT);
153 kill(array[2], SIGINT);
154 kill(array[3], SIGINT);
155 wait(&n);
156 wait(&n);
157 wait(&n);
158 wait(&n);
159 } else {
160 pause();
162 } else {
163 pause();
165 } else {
166 pause();
168 } else {
169 pause();
173 void test2d()
176 int pid, stat_loc, s;
178 /* Test waitpid. */
179 subtest = 4;
181 /* Test waitpid(pid, arg2, 0) */
182 pid = fork();
183 if (pid < 0) e(1);
184 if (pid > 0) {
185 /* Parent. */
186 s = waitpid(pid, &stat_loc, 0);
187 if (s != pid) e(2);
188 if (WIFEXITED(stat_loc) == 0) e(3);
189 if (WIFSIGNALED(stat_loc) != 0) e(4);
190 if (WEXITSTATUS(stat_loc) != 22) e(5);
191 } else {
192 /* Child */
193 exit(22);
196 /* Test waitpid(-1, arg2, 0) */
197 pid = fork();
198 if (pid < 0) e(6);
199 if (pid > 0) {
200 /* Parent. */
201 s = waitpid(-1, &stat_loc, 0);
202 if (s != pid) e(7);
203 if (WIFEXITED(stat_loc) == 0) e(8);
204 if (WIFSIGNALED(stat_loc) != 0) e(9);
205 if (WEXITSTATUS(stat_loc) != 33) e(10);
206 } else {
207 /* Child */
208 exit(33);
211 /* Test waitpid(0, arg2, 0) */
212 pid = fork();
213 if (pid < 0) e(11);
214 if (pid > 0) {
215 /* Parent. */
216 s = waitpid(0, &stat_loc, 0);
217 if (s != pid) e(12);
218 if (WIFEXITED(stat_loc) == 0) e(13);
219 if (WIFSIGNALED(stat_loc) != 0) e(14);
220 if (WEXITSTATUS(stat_loc) != 44) e(15);
221 } else {
222 /* Child */
223 exit(44);
226 /* Test waitpid(0, arg2, WNOHANG) */
227 signal(SIGTERM, SIG_DFL);
228 pid = fork();
229 if (pid < 0) e(16);
230 if (pid > 0) {
231 /* Parent. */
232 s = waitpid(0, &stat_loc, WNOHANG);
233 if (s != 0) e(17);
234 if (kill(pid, SIGTERM) != 0) e(18);
235 if (waitpid(pid, &stat_loc, 0) != pid) e(19);
236 if (WIFEXITED(stat_loc) != 0) e(20);
237 if (WIFSIGNALED(stat_loc) == 0) e(21);
238 if (WTERMSIG(stat_loc) != SIGTERM) e(22);
239 } else {
240 /* Child */
241 pause();
244 /* Test some error conditions. */
245 errno = 9999;
246 if (waitpid(0, &stat_loc, 0) != -1) e(23);
247 if (errno != ECHILD) e(24);
248 errno = 9999;
249 if (waitpid(0, &stat_loc, WNOHANG) != -1) e(25);
250 if (errno != ECHILD) e(26);
253 void test2e()
256 int pid1, pid2, stat_loc, s;
258 /* Test waitpid with two children. */
259 subtest = 5;
260 if (iteration > 1) return; /* slow test, don't do it too much */
261 if ( (pid1 = fork())) {
262 /* Parent. */
263 if ( (pid2 = fork()) ) {
264 /* Parent. Collect second child first. */
265 s = waitpid(pid2, &stat_loc, 0);
266 if (s != pid2) e(1);
267 if (WIFEXITED(stat_loc) == 0) e(2);
268 if (WIFSIGNALED(stat_loc) != 0) e(3);
269 if (WEXITSTATUS(stat_loc) != 222) e(4);
271 /* Now collect first child. */
272 s = waitpid(pid1, &stat_loc, 0);
273 if (s != pid1) e(5);
274 if (WIFEXITED(stat_loc) == 0) e(6);
275 if (WIFSIGNALED(stat_loc) != 0) e(7);
276 if (WEXITSTATUS(stat_loc) != 111) e(8);
277 } else {
278 /* Child 2. */
279 sleep(2); /* child 2 delays before exiting. */
280 exit(222);
282 } else {
283 /* Child 1. */
284 exit(111); /* child 1 exits immediately */
289 void test2f()
291 /* test getpid, getppid, getuid, etc. */
293 pid_t pid, pid1, ppid, cpid, stat_loc, err;
295 subtest = 6;
296 errno = -2000;
297 err = 0;
298 pid = getpid();
299 if ( (pid1 = fork())) {
300 /* Parent. Do nothing. */
301 if (wait(&stat_loc) != pid1) e(1);
302 if (WEXITSTATUS(stat_loc) != (pid1 & 0377)) e(2);
303 } else {
304 /* Child. Get ppid. */
305 cpid = getpid();
306 ppid = getppid();
307 if (ppid != pid) err = 3;
308 if (cpid == ppid) err = 4;
309 exit(cpid & 0377);
311 if (err != 0) e(err);
314 void test2g()
316 /* test time(), times() */
318 time_t t1, t2;
319 clock_t t3, t4;
320 struct tms tmsbuf;
322 subtest = 7;
323 errno = -7000;
325 /* First time(). */
326 t1 = -1;
327 t2 = -2;
328 t1 = time(&t2);
329 if (t1 < 650000000L) e(1); /* 650000000 is Sept. 1990 */
330 if (t1 != t2) e(2);
331 t1 = -1;
332 t1 = time( (time_t *) NULL);
333 if (t1 < 650000000L) e(3);
334 t3 = times(&tmsbuf);
335 sleep(1);
336 t2 = time( (time_t *) NULL);
337 if (t2 < 0L) e(4);
338 if (t2 - t1 < 1) e(5);
340 /* Now times(). */
341 t4 = times(&tmsbuf);
342 if ( t4 == (clock_t) -1) e(6);
343 if (t4 - t3 < CLOCKS_PER_SEC) e(7);
344 if (tmsbuf.tms_utime < 0) e(8);
345 if (tmsbuf.tms_stime < 0) e(9);
346 if (tmsbuf.tms_cutime < 0) e(10);
347 if (tmsbuf.tms_cstime < 0) e(11);
350 void sigpip(s)
351 int s; /* for ANSI */
353 sigct++;
354 cumsig++;