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