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