vm: use arch_map2str to print pagefault info, to properly display code addrs
[minix.git] / test / test19.c
blobe51f0b20216d298324a6bed626c6f216a6f3dfa7
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <limits.h>
6 #include <signal.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
11 #define MAX_ERROR 4
12 #define NB 30L
13 #define NBOUNDS 6
15 int errct, subtest, passes, pipesigs;
16 long t1;
18 char aa[100];
19 char b[4] = {0, 1, 2, 3}, c[4] = {10, 20, 30, 40}, d[4] = {6, 7, 8, 9};
20 long bounds[NBOUNDS] = {7, 9, 50, 519, 520, 40000L};
21 char buff[30000];
23 _PROTOTYPE(int main, (int argc, char *argv[]));
24 _PROTOTYPE(void test19a, (void));
25 _PROTOTYPE(void test19b, (void));
26 _PROTOTYPE(void test19c, (void));
27 _PROTOTYPE(void test19d, (void));
28 _PROTOTYPE(void test19e, (void));
29 _PROTOTYPE(void test19f, (void));
30 _PROTOTYPE(void test19g, (void));
31 _PROTOTYPE(void clraa, (void));
32 _PROTOTYPE(void pipecatcher, (int s));
33 _PROTOTYPE(void e, (int n));
34 _PROTOTYPE(void quit, (void));
36 int main(argc, argv)
37 int argc;
38 char *argv[];
40 char buffer[PATH_MAX + 1];
41 int i, m;
43 m = (argc == 2 ? atoi(argv[1]) : 0xFFFF);
45 if (geteuid() == 0 || getuid() == 0) {
46 realpath(argv[0], buffer);
47 execl("/usr/bin/su", "/usr/bin/su", "-", "ast", "-c", buffer, NULL);
48 printf("Test 19 cannot run as root; test aborted\n");
49 exit(1);
52 system("rm -rf DIR_19; mkdir DIR_19");
53 chdir("DIR_19");
55 printf("Test 19 ");
56 fflush(stdout);
57 for (i = 0; i < 4; i++) {
58 if (m & 0001) test19a();
59 if (m & 0002) test19b();
60 if (m & 0004) test19c();
61 if (m & 0010) test19d();
62 if (m & 0020) test19e();
63 if (m & 0040) test19f();
64 if (m & 0100) test19g();
65 passes++;
67 quit();
68 return(-1); /* impossible */
71 void test19a()
73 /* Test open with O_CREAT and O_EXCL. */
75 int fd;
77 subtest = 1;
79 if ( (fd = creat("T19.a1", 0777)) != 3) e(1); /* create test file */
80 if (close(fd) != 0) e(2);
81 if ( (fd = open("T19.a1", O_RDONLY)) != 3) e(3);
82 if (close(fd) != 0) e(4);
83 if ( (fd = open("T19.a1", O_WRONLY)) != 3) e(5);
84 if (close(fd) != 0) e(6);
85 if ( (fd = open("T19.a1", O_RDWR)) != 3) e(7);
86 if (close(fd) != 0) e(8);
88 /* See if O_CREAT actually creates a file. */
89 if ( (fd = open("T19.a2", O_RDONLY)) != -1) e(9); /* must fail */
90 if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0444)) != 3) e(10);
91 if (close(fd) != 0) e(11);
92 if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(12);
93 if (close(fd) != 0) e(13);
94 if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(14);
95 if ( (fd = open("T19.a2", O_RDWR)) != -1) e(15);
97 /* See what O_CREAT does on an existing file. */
98 if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0777)) != 3) e(16);
99 if (close(fd) != 0) e(17);
100 if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(18);
101 if (close(fd) != 0) e(19);
102 if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(20);
103 if ( (fd = open("T19.a2", O_RDWR)) != -1) e(21);
105 /* See if O_EXCL works. */
106 if ( (fd = open("T19.a2", O_RDONLY | O_EXCL)) != 3) e(22);
107 if (close(fd) != 0) e(23);
108 if ( (fd = open("T19.a2", O_WRONLY | O_EXCL)) != -1) e(24);
109 if ( (fd = open("T19.a3", O_RDONLY | O_EXCL)) != -1) e(25);
110 if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != 3) e(26);
111 if (close(fd) != 0) e(27);
112 errno = 0;
113 if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != -1) e(28);
114 if (errno != EEXIST) e(29);
116 if (unlink("T19.a1") != 0) e(30);
117 if (unlink("T19.a2") != 0) e(31);
118 if (unlink("T19.a3") != 0) e(32);
121 void test19b()
123 /* Test open with O_APPEND and O_TRUNC. */
125 int fd;
127 subtest = 2;
129 if ( (fd = creat("T19.b1", 0777)) != 3) e(1); /* create test file */
130 if (write(fd, b, 4) != 4) e(2);
131 if (close(fd) != 0) e(3);
132 clraa();
133 if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(4);
134 if (read(fd, aa, 100) != 4) e(5);
135 if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(6);
136 if (close(fd) != 0) e(7);
137 if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(8);
138 if (write(fd, b, 4) != 4) e(9);
139 if (lseek(fd, 0L, SEEK_SET) != 0L) e(10);
140 clraa();
141 if (read(fd, aa, 100) != 8) e(11);
142 if (aa[4] != 0 || aa[5] != 1 || aa[6] != 2 || aa[7] != 3) e(12);
143 if (close(fd) != 0) e(13);
145 if ( (fd = open("T19.b1", O_RDWR | O_TRUNC)) != 3) e(14);
146 if (read(fd, aa, 100) != 0) e(15);
147 if (close(fd) != 0) e(16);
149 unlink("T19.b1");
152 void test19c()
154 /* Test program for open(), close(), creat(), read(), write(), lseek(). */
156 int i, n, n1, n2;
158 subtest = 3;
159 if ((n = creat("foop", 0777)) != 3) e(1);
160 if ((n1 = creat("foop", 0777)) != 4) e(2);
161 if ((n2 = creat("/", 0777)) != -1) e(3);
162 if (close(n) != 0) e(4);
163 if ((n = open("foop", O_RDONLY)) != 3) e(5);
164 if ((n2 = open("nofile", O_RDONLY)) != -1) e(6);
165 if (close(n1) != 0) e(7);
167 /* N is the only one open now. */
168 for (i = 0; i < 2; i++) {
169 n1 = creat("File2", 0777);
170 if (n1 != 4) {
171 printf("creat yielded fd=%d, expected 4\n", n1);
172 e(8);
174 if ((n2 = open("File2", O_RDONLY)) != 5) e(9);
175 if (close(n1) != 0) e(10);
176 if (close(n2) != 0) e(11);
178 unlink("File2");
179 if (close(n) != 0) e(12);
181 /* All files closed now. */
182 for (i = 0; i < 2; i++) {
183 if ((n = creat("foop", 0777)) != 3) e(13);
184 if (close(n) != 0) e(14);
185 if ((n = open("foop", O_RDWR)) != 3) e(15);
187 /* Read/write tests */
188 if (write(n, b, 4) != 4) e(16);
189 if (read(n, aa, 4) != 0) e(17);
190 if (lseek(n, 0L, SEEK_SET) != 0L) e(18);
191 if (read(n, aa, 4) != 4) e(19);
192 if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(20);
193 if (lseek(n, 0L, SEEK_SET) != 0L) e(21);
194 if (lseek(n, 2L, SEEK_CUR) != 2L) e(22);
195 if (read(n, aa, 4) != 2) e(23);
196 if (aa[0] != 2 || aa[1] != 3 || aa[2] != 2 || aa[3] != 3) e(24);
197 if (lseek(n, 2L, SEEK_SET) != 2L) e(25);
198 clraa();
199 if (write(n, c, 4) != 4) e(26);
200 if (lseek(n, 0L, SEEK_SET) != 0L) e(27);
201 if (read(n, aa, 10) != 6) e(28);
202 if (aa[0] != 0 || aa[1] != 1 || aa[2] != 10 || aa[3] != 20) e(29);
203 if (lseek(n, 16L, SEEK_SET) != 16L) e(30);
204 if (lseek(n, 2040L, SEEK_END) != 2046L) e(31);
205 if (read(n, aa, 10) != 0) e(32);
206 if (lseek(n, 0L, SEEK_CUR) != 2046L) e(33);
207 clraa();
208 if (write(n, c, 4) != 4) e(34);
209 if (lseek(n, 0L, SEEK_CUR) != 2050L) e(35);
210 if (lseek(n, 2040L, SEEK_SET) != 2040L) e(36);
211 clraa();
212 if (read(n, aa, 20) != 10) e(37);
213 if (aa[0] != 0 || aa[5] != 0 || aa[6] != 10 || aa[9] != 40) e(38);
214 if (lseek(n, 10239L, SEEK_SET) != 10239L) e(39);
215 if (write(n, d, 2) != 2) e(40);
216 if (lseek(n, -2L, SEEK_END) != 10239L) e(41);
217 if (read(n, aa, 2) != 2) e(42);
218 if (aa[0] != 6 || aa[1] != 7) e(43);
219 if (lseek(n, NB * 1024L - 2L, SEEK_SET) != NB * 1024L - 2L) e(44);
220 if (write(n, b, 4) != 4) e(45);
221 if (lseek(n, 0L, SEEK_SET) != 0L) e(46);
222 if (lseek(n, -6L, SEEK_END) != 1024L * NB - 4) e(47);
223 clraa();
224 if (read(n, aa, 100) != 6) e(48);
225 if (aa[0] != 0 || aa[1] != 0 || aa[3] != 1 || aa[4] != 2|| aa[5] != 3)
226 e(49);
227 if (lseek(n, 20000L, SEEK_SET) != 20000L) e(50);
228 if (write(n, c, 4) != 4) e(51);
229 if (lseek(n, -4L, SEEK_CUR) != 20000L) e(52);
230 if (read(n, aa, 4) != 4) e(53);
231 if (aa[0] != 10 || aa[1] != 20 || aa[2] != 30 || aa[3] != 40) e(54);
232 if (close(n) != 0) e(55);
233 if ((n1 = creat("foop", 0777)) != 3) e(56);
234 if (close(n1) != 0) e(57);
235 unlink("foop");
240 void test19d()
242 /* Test read. */
244 int i, fd, pd[2];
245 char bb[100];
247 subtest = 4;
249 for (i = 0; i < 100; i++) bb[i] = i;
250 if ( (fd = creat("T19.d1", 0777)) != 3) e(1); /* create test file */
251 if (write(fd, bb, 100) != 100) e(2);
252 if (close(fd) != 0) e(3);
253 clraa();
254 if ( (fd = open("T19.d1", O_RDONLY)) != 3) e(4);
255 errno = 1000;
256 if (read(fd, aa, 0) != 0) e(5);
257 if (errno != 1000) e(6);
258 if (read(fd, aa, 100) != 100) e(7);
259 if (lseek(fd, 37L, SEEK_SET) != 37L) e(8);
260 if (read(fd, aa, 10) != 10) e(9);
261 if (lseek(fd, 0L, SEEK_CUR) != 47L) e(10);
262 if (read(fd, aa, 100) != 53) e(11);
263 if (aa[0] != 47) e(12);
264 if (read(fd, aa, 1) != 0) e(13);
265 if (close(fd) != 0) e(14);
267 /* Read from pipe with no writer open. */
268 if (pipe(pd) != 0) e(15);
269 if (close(pd[1]) != 0) e(16);
270 errno = 2000;
271 if (read(pd[0], aa, 1) != 0) e(17); /* must return EOF */
272 if (errno != 2000) e(18);
274 /* Read from a pipe with O_NONBLOCK set. */
275 if (fcntl(pd[0], F_SETFL, O_NONBLOCK) != 0) e(19); /* set O_NONBLOCK */
277 if (read(pd[0], aa, 1) != -1) e(20);
278 if (errno != EAGAIN) e(21);
280 if (close(pd[0]) != 0) e(19);
281 if (unlink("T19.d1") != 0) e(20);
284 void test19e()
286 /* Test link, unlink, stat, fstat, dup, umask. */
288 int i, j, n, n1, flag;
289 char a[255], b[255];
290 struct stat s, s1;
292 subtest = 5;
293 for (i = 0; i < 2; i++) {
294 umask(0);
296 if ((n = creat("T3", 0702)) < 0) e(1);
297 if (link("T3", "newT3") < 0) e(2);
298 if ((n1 = open("newT3", O_RDWR)) < 0) e(3);
299 for (j = 0; j < 255; j++) a[j] = j;
300 if (write(n, a, 255) != 255) e(4);
301 if (read(n1, b, 255) != 255) e(5);
302 flag = 0;
303 for (j = 0; j < 255; j++)
304 if (a[j] != b[j]) flag++;
305 if (flag) e(6);
306 if (unlink("T3") < 0) e(7);
307 if (close(n) < 0) e(8);
308 if (close(n1) < 0) e(9);
309 if ((n1 = open("newT3", O_RDONLY)) < 0) e(10);
310 if (read(n1, b, 255) != 255) e(11);
311 flag = 0;
312 for (j = 0; j < 255; j++)
313 if (a[j] != b[j]) flag++;
314 if (flag) e(12);
316 /* Now check out stat, fstat. */
317 if (stat("newT3", &s) < 0) e(13);
318 if (s.st_mode != (mode_t) 0100702) e(14);
319 /* The cast was because regular modes are
320 * negative :-(. Anyway, the magic number
321 * should be (S_IFREG | S_IRWXU | S_IWOTH)
322 * for POSIX.
324 if (s.st_nlink != 1) e(15);
325 if (s.st_size != 255L) e(16);
326 if (fstat(n1, &s1) < 0) e(17);
327 if (s.st_dev != s1.st_dev) e(18);
328 if (s.st_ino != s1.st_ino) e(19);
329 if (s.st_mode != s1.st_mode) e(20);
330 if (s.st_nlink != s1.st_nlink) e(21);
331 if (s.st_uid != s1.st_uid) e(22);
332 if (s.st_gid != s1.st_gid) e(23);
333 if (s.st_rdev != s1.st_rdev) e(24);
334 if (s.st_size != s1.st_size) e(25);
335 if (s.st_atime != s1.st_atime) e(26);
336 if (close(n1) < 0) e(27);
337 if (unlink("newT3") < 0) e(28);
339 umask(040);
340 if ((n = creat("T3a", 0777)) < 0) e(29);
341 if (stat("T3a", &s) < 0) e(30);
342 if (s.st_mode != (mode_t) 0100737) e(31); /* negative :-( */
343 if (unlink("T3a") < 0) e(32);
344 if (close(n1) < 0) e(33);
346 /* Dup */
347 if ((n = creat("T3b", 0777)) < 0) e(34);
348 if (close(n) < 0) e(35);
349 if ((n = open("T3b", O_RDWR)) < 0) e(36);
350 if ((n1 = dup(n)) != n + 1) e(37);
351 if (write(n, a, 255) != 255) e(38);
352 read(n1, b, 20);
353 if (lseek(n, 0L, SEEK_SET) != 0L) e(39);
354 if ((j = read(n1, b, 512)) != 255) e(40);
355 if (unlink("T3b") < 0) e(41);
356 if (close(n) < 0) e(42);
357 if (close(n1) < 0) e(43);
362 void test19f()
364 /* Test large files to see if indirect block stuff works. */
366 int fd, i;
367 long pos;
369 subtest = 6;
371 if (passes > 0) return; /* takes too long to repeat this test */
372 for (i = 0; i < NBOUNDS; i ++) {
373 pos = 1024L * bounds[i];
374 fd = creat("T19f", 0777);
375 if (fd < 0) e(10*i+1);
376 if (lseek(fd, pos, 0) < 0) e(10*i+2);
377 if (write(fd, buff, 30720) != 30720) e(10*i+3);
378 if (close(fd) < 0) e(10*i+3);
379 if (unlink("T19f") < 0) e(10*i+4);
383 void test19g()
385 /* Test POSIX calls for pipe, read, write, lseek and close. */
387 int pipefd[2], n, i, fd;
388 char buf[512], buf2[512];
390 subtest = 7;
392 for (i = 0; i < 512; i++) buf[i] = i % 128;
394 if (pipe(pipefd) < 0) e(1);
395 if (write(pipefd[1], buf, 512) != 512) e(2);
396 if (read(pipefd[0], buf2, 512) != 512) e(3);
397 if (close(pipefd[1]) != 0) e(4);
398 if (close(pipefd[1]) >= 0) e(5);
399 if (read(pipefd[0], buf2, 1) != 0) e(6);
400 if (close(pipefd[0]) != 0) e(7);
402 /* Test O_NONBLOCK on pipes. */
403 if (pipe(pipefd) < 0) e(8);
404 if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0) e(9);
405 if (read(pipefd[0], buf2, 1) != -1) e(10);
406 if (errno != EAGAIN) e(11);
407 if (close(pipefd[0]) != 0) e(12);
408 if (close(pipefd[1]) != 0) e(13);
410 /* Test read and lseek. */
411 if ( (fd = creat("T19.g1", 0777)) != 3) e(14); /* create test file */
412 if (write(fd, buf, 512) != 512) e(15);
413 errno = 3000;
414 if (read(fd, buf2, 512) != -1) e(16);
415 if (errno != EBADF) e(17);
416 if (close(fd) != 0) e(18);
417 if ( (fd = open("T19.g1", O_RDWR)) != 3) e(19);
418 if (read(fd, buf2, 512) != 512) e(20);
419 if (read(fd, buf2, 512) != 0) e(21);
420 if (lseek(fd, 100L, SEEK_SET) != 100L) e(22);
421 if (read(fd, buf2, 512) != 412) e(23);
422 if (lseek(fd, 1000L, SEEK_SET) != 1000L) e(24);
424 /* Test write. */
425 if (lseek(fd, -1000L, SEEK_CUR) != 0) e(25);
426 if (write(fd, buf, 512) != 512) e(26);
427 if (lseek(fd, 2L, SEEK_SET) != 2) e(27);
428 if (write(fd, buf, 3) != 3) e(28);
429 if (lseek(fd, -2L, SEEK_CUR) != 3) e(29);
430 if (write(fd, &buf[30], 1) != 1) e(30);
431 if (lseek(fd, 2L, SEEK_CUR) != 6) e(31);
432 if (write(fd, &buf[60], 1) != 1) e(32);
433 if (lseek(fd, -512L, SEEK_END) != 0) e(33);
434 if (read(fd, buf2, 8) != 8) e(34);
435 errno = 4000;
436 if (buf2[0] != 0 || buf2[1] != 1 || buf2[2] != 0 || buf2[3] != 30) e(35);
437 if (buf2[4] != 2 || buf2[5] != 5 || buf2[6] != 60 || buf2[7] != 7) e(36);
439 /* Turn the O_APPEND flag on. */
440 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(37);
441 if (lseek(fd, 0L, SEEK_SET) != 0) e(38);
442 if (write(fd, &buf[100], 1) != 1) e(39);
443 if (lseek(fd, 0L, SEEK_SET) != 0) e(40);
444 if (read(fd, buf2, 10) != 10) e(41);
445 if (buf2[0] != 0) e(42);
446 if (lseek(fd, -1L, SEEK_END) != 512) e(43);
447 if (read(fd, buf2, 10) != 1) e(44);
448 if (buf2[0] != 100) e(45);
449 if (close(fd) != 0) e(46);
451 /* Now try write with O_NONBLOCK. */
452 if (pipe(pipefd) != 0) e(47);
453 if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) e(48);
454 if (write(pipefd[1], buf, 512) != 512) e(49);
455 if (write(pipefd[1], buf, 512) != 512) e(50);
456 errno = 0;
457 for (i = 1; i < 20; i++) {
458 n = write(pipefd[1], buf, 512);
459 if (n == 512) continue;
460 if (n != -1 || errno != EAGAIN) {e(51); break;}
462 if (read(pipefd[0], buf, 512) != 512) e(52);
463 if (close(pipefd[0]) != 0) e(53);
465 /* Write to a pipe with no reader. This should generate a signal. */
466 signal(SIGPIPE, pipecatcher);
467 errno = 0;
468 if (write(pipefd[1], buf, 1) != -1) e(54);
469 if (errno != EPIPE) e(55);
470 if (pipesigs != passes + 1) e(56); /* we should have had the sig now */
471 if (close(pipefd[1]) != 0) e(57);
472 errno = 0;
473 if (write(100, buf, 512) != -1) e(58);
474 if (errno != EBADF) e(59);
475 if (unlink("T19.g1") != 0) e(60);
478 void clraa()
480 int i;
481 for (i = 0; i < 100; i++) aa[i] = 0;
484 void pipecatcher(s)
485 int s; /* it is supposed to have an arg */
487 pipesigs++;
490 void e(n)
491 int n;
493 int err_num = errno; /* save errno in case printf clobbers it */
495 printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
496 fflush(stdout); /* aargh! Most results go to stdout and are
497 * messed up by perror going to stderr.
498 * Should replace perror by printf and strerror
499 * in all the tests.
501 errno = err_num; /* restore errno, just in case */
502 perror("");
503 if (errct++ > MAX_ERROR) {
504 printf("Too many errors; test aborted\n");
505 chdir("..");
506 system("rm -rf DIR*");
507 exit(1);
511 void quit()
514 chdir("..");
515 system("rm -rf DIR*");
517 if (errct == 0) {
518 printf("ok\n");
519 exit(0);
520 } else {
521 printf("%d errors\n", errct);
522 exit(1);