make minix lwip make explicit use of 'int'
[minix3.git] / test / test19.c
blob015c63c84c0f915c730f517c24cd193492a77b68
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 3
12 #define NB 30L
13 #define NBOUNDS 6
15 int 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 #include "common.c"
25 int main(int argc, char *argv[]);
26 void test19a(void);
27 void test19b(void);
28 void test19c(void);
29 void test19d(void);
30 void test19e(void);
31 void test19f(void);
32 void test19g(void);
33 void clraa(void);
34 void pipecatcher(int s);
36 int main(argc, argv)
37 int argc;
38 char *argv[];
40 int i, m;
42 start(19);
44 m = (argc == 2 ? atoi(argv[1]) : 0xFFFF);
46 for (i = 0; i < 4; i++) {
47 if (m & 0001) test19a();
48 if (m & 0002) test19b();
49 if (m & 0004) test19c();
50 if (m & 0010) test19d();
51 if (m & 0020) test19e();
52 if (m & 0040) test19f();
53 if (m & 0100) test19g();
54 passes++;
56 quit();
57 return(-1); /* impossible */
60 void test19a()
62 /* Test open with O_CREAT and O_EXCL. */
64 int fd;
66 subtest = 1;
68 if ( (fd = creat("T19.a1", 0777)) != 3) e(1); /* create test file */
69 if (close(fd) != 0) e(2);
70 if ( (fd = open("T19.a1", O_RDONLY)) != 3) e(3);
71 if (close(fd) != 0) e(4);
72 if ( (fd = open("T19.a1", O_WRONLY)) != 3) e(5);
73 if (close(fd) != 0) e(6);
74 if ( (fd = open("T19.a1", O_RDWR)) != 3) e(7);
75 if (close(fd) != 0) e(8);
77 /* See if O_CREAT actually creates a file. */
78 if ( (fd = open("T19.a2", O_RDONLY)) != -1) e(9); /* must fail */
79 if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0444)) != 3) e(10);
80 if (close(fd) != 0) e(11);
81 if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(12);
82 if (close(fd) != 0) e(13);
83 if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(14);
84 if ( (fd = open("T19.a2", O_RDWR)) != -1) e(15);
86 /* See what O_CREAT does on an existing file. */
87 if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0777)) != 3) e(16);
88 if (close(fd) != 0) e(17);
89 if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(18);
90 if (close(fd) != 0) e(19);
91 if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(20);
92 if ( (fd = open("T19.a2", O_RDWR)) != -1) e(21);
94 /* See if O_EXCL works. */
95 if ( (fd = open("T19.a2", O_RDONLY | O_EXCL)) != 3) e(22);
96 if (close(fd) != 0) e(23);
97 if ( (fd = open("T19.a2", O_WRONLY | O_EXCL)) != -1) e(24);
98 if ( (fd = open("T19.a3", O_RDONLY | O_EXCL)) != -1) e(25);
99 if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != 3) e(26);
100 if (close(fd) != 0) e(27);
101 errno = 0;
102 if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != -1) e(28);
103 if (errno != EEXIST) e(29);
105 if (unlink("T19.a1") != 0) e(30);
106 if (unlink("T19.a2") != 0) e(31);
107 if (unlink("T19.a3") != 0) e(32);
110 void test19b()
112 /* Test open with O_APPEND and O_TRUNC. */
114 int fd;
116 subtest = 2;
118 if ( (fd = creat("T19.b1", 0777)) != 3) e(1); /* create test file */
119 if (write(fd, b, 4) != 4) e(2);
120 if (close(fd) != 0) e(3);
121 clraa();
122 if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(4);
123 if (read(fd, aa, 100) != 4) e(5);
124 if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(6);
125 if (close(fd) != 0) e(7);
126 if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(8);
127 if (write(fd, b, 4) != 4) e(9);
128 if (lseek(fd, 0L, SEEK_SET) != 0L) e(10);
129 clraa();
130 if (read(fd, aa, 100) != 8) e(11);
131 if (aa[4] != 0 || aa[5] != 1 || aa[6] != 2 || aa[7] != 3) e(12);
132 if (close(fd) != 0) e(13);
134 if ( (fd = open("T19.b1", O_RDWR | O_TRUNC)) != 3) e(14);
135 if (read(fd, aa, 100) != 0) e(15);
136 if (close(fd) != 0) e(16);
138 unlink("T19.b1");
141 void test19c()
143 /* Test program for open(), close(), creat(), read(), write(), lseek(). */
145 int i, n, n1, n2;
147 subtest = 3;
148 if ((n = creat("foop", 0777)) != 3) e(1);
149 if ((n1 = creat("foop", 0777)) != 4) e(2);
150 if ((n2 = creat("/", 0777)) != -1) e(3);
151 if (close(n) != 0) e(4);
152 if ((n = open("foop", O_RDONLY)) != 3) e(5);
153 if ((n2 = open("nofile", O_RDONLY)) != -1) e(6);
154 if (close(n1) != 0) e(7);
156 /* N is the only one open now. */
157 for (i = 0; i < 2; i++) {
158 n1 = creat("File2", 0777);
159 if (n1 != 4) {
160 printf("creat yielded fd=%d, expected 4\n", n1);
161 e(8);
163 if ((n2 = open("File2", O_RDONLY)) != 5) e(9);
164 if (close(n1) != 0) e(10);
165 if (close(n2) != 0) e(11);
167 unlink("File2");
168 if (close(n) != 0) e(12);
170 /* All files closed now. */
171 for (i = 0; i < 2; i++) {
172 if ((n = creat("foop", 0777)) != 3) e(13);
173 if (close(n) != 0) e(14);
174 if ((n = open("foop", O_RDWR)) != 3) e(15);
176 /* Read/write tests */
177 if (write(n, b, 4) != 4) e(16);
178 if (read(n, aa, 4) != 0) e(17);
179 if (lseek(n, 0L, SEEK_SET) != 0L) e(18);
180 if (read(n, aa, 4) != 4) e(19);
181 if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(20);
182 if (lseek(n, 0L, SEEK_SET) != 0L) e(21);
183 if (lseek(n, 2L, SEEK_CUR) != 2L) e(22);
184 if (read(n, aa, 4) != 2) e(23);
185 if (aa[0] != 2 || aa[1] != 3 || aa[2] != 2 || aa[3] != 3) e(24);
186 if (lseek(n, 2L, SEEK_SET) != 2L) e(25);
187 clraa();
188 if (write(n, c, 4) != 4) e(26);
189 if (lseek(n, 0L, SEEK_SET) != 0L) e(27);
190 if (read(n, aa, 10) != 6) e(28);
191 if (aa[0] != 0 || aa[1] != 1 || aa[2] != 10 || aa[3] != 20) e(29);
192 if (lseek(n, 16L, SEEK_SET) != 16L) e(30);
193 if (lseek(n, 2040L, SEEK_END) != 2046L) e(31);
194 if (read(n, aa, 10) != 0) e(32);
195 if (lseek(n, 0L, SEEK_CUR) != 2046L) e(33);
196 clraa();
197 if (write(n, c, 4) != 4) e(34);
198 if (lseek(n, 0L, SEEK_CUR) != 2050L) e(35);
199 if (lseek(n, 2040L, SEEK_SET) != 2040L) e(36);
200 clraa();
201 if (read(n, aa, 20) != 10) e(37);
202 if (aa[0] != 0 || aa[5] != 0 || aa[6] != 10 || aa[9] != 40) e(38);
203 if (lseek(n, 10239L, SEEK_SET) != 10239L) e(39);
204 if (write(n, d, 2) != 2) e(40);
205 if (lseek(n, -2L, SEEK_END) != 10239L) e(41);
206 if (read(n, aa, 2) != 2) e(42);
207 if (aa[0] != 6 || aa[1] != 7) e(43);
208 if (lseek(n, NB * 1024L - 2L, SEEK_SET) != NB * 1024L - 2L) e(44);
209 if (write(n, b, 4) != 4) e(45);
210 if (lseek(n, 0L, SEEK_SET) != 0L) e(46);
211 if (lseek(n, -6L, SEEK_END) != 1024L * NB - 4) e(47);
212 clraa();
213 if (read(n, aa, 100) != 6) e(48);
214 if (aa[0] != 0 || aa[1] != 0 || aa[3] != 1 || aa[4] != 2|| aa[5] != 3)
215 e(49);
216 if (lseek(n, 20000L, SEEK_SET) != 20000L) e(50);
217 if (write(n, c, 4) != 4) e(51);
218 if (lseek(n, -4L, SEEK_CUR) != 20000L) e(52);
219 if (read(n, aa, 4) != 4) e(53);
220 if (aa[0] != 10 || aa[1] != 20 || aa[2] != 30 || aa[3] != 40) e(54);
221 if (close(n) != 0) e(55);
222 if ((n1 = creat("foop", 0777)) != 3) e(56);
223 if (close(n1) != 0) e(57);
224 unlink("foop");
229 void test19d()
231 /* Test read. */
233 int i, fd, pd[2];
234 char bb[100];
236 subtest = 4;
238 for (i = 0; i < 100; i++) bb[i] = i;
239 if ( (fd = creat("T19.d1", 0777)) != 3) e(1); /* create test file */
240 if (write(fd, bb, 100) != 100) e(2);
241 if (close(fd) != 0) e(3);
242 clraa();
243 if ( (fd = open("T19.d1", O_RDONLY)) != 3) e(4);
244 errno = 1000;
245 if (read(fd, aa, 0) != 0) e(5);
246 if (errno != 1000) e(6);
247 if (read(fd, aa, 100) != 100) e(7);
248 if (lseek(fd, 37L, SEEK_SET) != 37L) e(8);
249 if (read(fd, aa, 10) != 10) e(9);
250 if (lseek(fd, 0L, SEEK_CUR) != 47L) e(10);
251 if (read(fd, aa, 100) != 53) e(11);
252 if (aa[0] != 47) e(12);
253 if (read(fd, aa, 1) != 0) e(13);
254 if (close(fd) != 0) e(14);
256 /* Read from pipe with no writer open. */
257 if (pipe(pd) != 0) e(15);
258 if (close(pd[1]) != 0) e(16);
259 errno = 2000;
260 if (read(pd[0], aa, 1) != 0) e(17); /* must return EOF */
261 if (errno != 2000) e(18);
263 /* Read from a pipe with O_NONBLOCK set. */
264 if (fcntl(pd[0], F_SETFL, O_NONBLOCK) != 0) e(19); /* set O_NONBLOCK */
266 if (read(pd[0], aa, 1) != -1) e(20);
267 if (errno != EAGAIN) e(21);
269 if (close(pd[0]) != 0) e(19);
270 if (unlink("T19.d1") != 0) e(20);
273 void test19e()
275 /* Test link, unlink, stat, fstat, dup, umask. */
277 int i, j, n, n1, flag;
278 char a[255], b[255];
279 struct stat s, s1;
281 subtest = 5;
282 for (i = 0; i < 2; i++) {
283 umask(0);
285 if ((n = creat("T3", 0702)) < 0) e(1);
286 if (link("T3", "newT3") < 0) e(2);
287 if ((n1 = open("newT3", O_RDWR)) < 0) e(3);
288 for (j = 0; j < 255; j++) a[j] = j;
289 if (write(n, a, 255) != 255) e(4);
290 if (read(n1, b, 255) != 255) e(5);
291 flag = 0;
292 for (j = 0; j < 255; j++)
293 if (a[j] != b[j]) flag++;
294 if (flag) e(6);
295 if (unlink("T3") < 0) e(7);
296 if (close(n) < 0) e(8);
297 if (close(n1) < 0) e(9);
298 if ((n1 = open("newT3", O_RDONLY)) < 0) e(10);
299 if (read(n1, b, 255) != 255) e(11);
300 flag = 0;
301 for (j = 0; j < 255; j++)
302 if (a[j] != b[j]) flag++;
303 if (flag) e(12);
305 /* Now check out stat, fstat. */
306 if (stat("newT3", &s) < 0) e(13);
307 if (s.st_mode != (mode_t) 0100702) e(14);
308 /* The cast was because regular modes are
309 * negative :-(. Anyway, the magic number
310 * should be (S_IFREG | S_IRWXU | S_IWOTH)
311 * for POSIX.
313 if (s.st_nlink != 1) e(15);
314 if (s.st_size != 255L) e(16);
315 if (fstat(n1, &s1) < 0) e(17);
316 if (s.st_dev != s1.st_dev) e(18);
317 if (s.st_ino != s1.st_ino) e(19);
318 if (s.st_mode != s1.st_mode) e(20);
319 if (s.st_nlink != s1.st_nlink) e(21);
320 if (s.st_uid != s1.st_uid) e(22);
321 if (s.st_gid != s1.st_gid) e(23);
322 if (s.st_rdev != s1.st_rdev) e(24);
323 if (s.st_size != s1.st_size) e(25);
324 if (s.st_atime != s1.st_atime) e(26);
325 if (close(n1) < 0) e(27);
326 if (unlink("newT3") < 0) e(28);
328 umask(040);
329 if ((n = creat("T3a", 0777)) < 0) e(29);
330 if (stat("T3a", &s) < 0) e(30);
331 if (s.st_mode != (mode_t) 0100737) e(31); /* negative :-( */
332 if (unlink("T3a") < 0) e(32);
333 if (close(n1) < 0) e(33);
335 /* Dup */
336 if ((n = creat("T3b", 0777)) < 0) e(34);
337 if (close(n) < 0) e(35);
338 if ((n = open("T3b", O_RDWR)) < 0) e(36);
339 if ((n1 = dup(n)) != n + 1) e(37);
340 if (write(n, a, 255) != 255) e(38);
341 read(n1, b, 20);
342 if (lseek(n, 0L, SEEK_SET) != 0L) e(39);
343 if ((j = read(n1, b, 512)) != 255) e(40);
344 if (unlink("T3b") < 0) e(41);
345 if (close(n) < 0) e(42);
346 if (close(n1) < 0) e(43);
351 void test19f()
353 /* Test large files to see if indirect block stuff works. */
355 int fd, i;
356 long pos;
358 subtest = 6;
360 if (passes > 0) return; /* takes too long to repeat this test */
361 for (i = 0; i < NBOUNDS; i ++) {
362 pos = 1024L * bounds[i];
363 fd = creat("T19f", 0777);
364 if (fd < 0) e(10*i+1);
365 if (lseek(fd, pos, 0) < 0) e(10*i+2);
366 if (write(fd, buff, 30720) != 30720) e(10*i+3);
367 if (close(fd) < 0) e(10*i+3);
368 if (unlink("T19f") < 0) e(10*i+4);
372 void test19g()
374 /* Test POSIX calls for pipe, read, write, lseek and close. */
376 int pipefd[2], n, i, fd;
377 char buf[512], buf2[512];
379 subtest = 7;
381 for (i = 0; i < 512; i++) buf[i] = i % 128;
383 if (pipe(pipefd) < 0) e(1);
384 if (write(pipefd[1], buf, 512) != 512) e(2);
385 if (read(pipefd[0], buf2, 512) != 512) e(3);
386 if (close(pipefd[1]) != 0) e(4);
387 if (close(pipefd[1]) >= 0) e(5);
388 if (read(pipefd[0], buf2, 1) != 0) e(6);
389 if (close(pipefd[0]) != 0) e(7);
391 /* Test O_NONBLOCK on pipes. */
392 if (pipe(pipefd) < 0) e(8);
393 if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0) e(9);
394 if (read(pipefd[0], buf2, 1) != -1) e(10);
395 if (errno != EAGAIN) e(11);
396 if (close(pipefd[0]) != 0) e(12);
397 if (close(pipefd[1]) != 0) e(13);
399 /* Test read and lseek. */
400 if ( (fd = creat("T19.g1", 0777)) != 3) e(14); /* create test file */
401 if (write(fd, buf, 512) != 512) e(15);
402 errno = 3000;
403 if (read(fd, buf2, 512) != -1) e(16);
404 if (errno != EBADF) e(17);
405 if (close(fd) != 0) e(18);
406 if ( (fd = open("T19.g1", O_RDWR)) != 3) e(19);
407 if (read(fd, buf2, 512) != 512) e(20);
408 if (read(fd, buf2, 512) != 0) e(21);
409 if (lseek(fd, 100L, SEEK_SET) != 100L) e(22);
410 if (read(fd, buf2, 512) != 412) e(23);
411 if (lseek(fd, 1000L, SEEK_SET) != 1000L) e(24);
413 /* Test write. */
414 if (lseek(fd, -1000L, SEEK_CUR) != 0) e(25);
415 if (write(fd, buf, 512) != 512) e(26);
416 if (lseek(fd, 2L, SEEK_SET) != 2) e(27);
417 if (write(fd, buf, 3) != 3) e(28);
418 if (lseek(fd, -2L, SEEK_CUR) != 3) e(29);
419 if (write(fd, &buf[30], 1) != 1) e(30);
420 if (lseek(fd, 2L, SEEK_CUR) != 6) e(31);
421 if (write(fd, &buf[60], 1) != 1) e(32);
422 if (lseek(fd, -512L, SEEK_END) != 0) e(33);
423 if (read(fd, buf2, 8) != 8) e(34);
424 errno = 4000;
425 if (buf2[0] != 0 || buf2[1] != 1 || buf2[2] != 0 || buf2[3] != 30) e(35);
426 if (buf2[4] != 2 || buf2[5] != 5 || buf2[6] != 60 || buf2[7] != 7) e(36);
428 /* Turn the O_APPEND flag on. */
429 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(37);
430 if (lseek(fd, 0L, SEEK_SET) != 0) e(38);
431 if (write(fd, &buf[100], 1) != 1) e(39);
432 if (lseek(fd, 0L, SEEK_SET) != 0) e(40);
433 if (read(fd, buf2, 10) != 10) e(41);
434 if (buf2[0] != 0) e(42);
435 if (lseek(fd, -1L, SEEK_END) != 512) e(43);
436 if (read(fd, buf2, 10) != 1) e(44);
437 if (buf2[0] != 100) e(45);
438 if (close(fd) != 0) e(46);
440 /* Now try write with O_NONBLOCK. */
441 if (pipe(pipefd) != 0) e(47);
442 if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) e(48);
443 if (write(pipefd[1], buf, 512) != 512) e(49);
444 if (write(pipefd[1], buf, 512) != 512) e(50);
445 errno = 0;
446 for (i = 1; i < 20; i++) {
447 n = write(pipefd[1], buf, 512);
448 if (n == 512) continue;
449 if (n != -1 || errno != EAGAIN) {e(51); break;}
451 if (read(pipefd[0], buf, 512) != 512) e(52);
452 if (close(pipefd[0]) != 0) e(53);
454 /* Write to a pipe with no reader. This should generate a signal. */
455 signal(SIGPIPE, pipecatcher);
456 errno = 0;
457 if (write(pipefd[1], buf, 1) != -1) e(54);
458 if (errno != EPIPE) e(55);
459 if (pipesigs != passes + 1) e(56); /* we should have had the sig now */
460 if (close(pipefd[1]) != 0) e(57);
461 errno = 0;
462 if (write(100, buf, 512) != -1) e(58);
463 if (errno != EBADF) e(59);
464 if (unlink("T19.g1") != 0) e(60);
467 void clraa()
469 int i;
470 for (i = 0; i < 100; i++) aa[i] = 0;
473 void pipecatcher(s)
474 int s; /* it is supposed to have an arg */
476 pipesigs++;