test19.c

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <errno.h>
00004 #include <fcntl.h>
00005 #include <signal.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <stdio.h>
00009 
00010 #define MAX_ERROR 4
00011 #define NB 30L
00012 #define NBOUNDS 6
00013 
00014 int errct, subtest, passes, pipesigs;
00015 long t1;
00016 
00017 char aa[100];
00018 char b[4] = {0, 1, 2, 3}, c[4] = {10, 20, 30, 40}, d[4] = {6, 7, 8, 9};
00019 long bounds[NBOUNDS] = {7, 9, 50, 519, 520, 40000L};
00020 char buff[30000];
00021 
00022 _PROTOTYPE(int main, (int argc, char *argv[]));
00023 _PROTOTYPE(void test19a, (void));
00024 _PROTOTYPE(void test19b, (void));
00025 _PROTOTYPE(void test19c, (void));
00026 _PROTOTYPE(void test19d, (void));
00027 _PROTOTYPE(void test19e, (void));
00028 _PROTOTYPE(void test19f, (void));
00029 _PROTOTYPE(void test19g, (void));
00030 _PROTOTYPE(void clraa, (void));
00031 _PROTOTYPE(void pipecatcher, (int s));
00032 _PROTOTYPE(void e, (int n));
00033 _PROTOTYPE(void quit, (void));
00034 
00035 int main(argc, argv)
00036 int argc;
00037 char *argv[];
00038 {
00039   int i, m;
00040 
00041   m = (argc == 2 ? atoi(argv[1]) : 0xFFFF);
00042 
00043   if (geteuid() == 0 || getuid() == 0) {
00044         printf("Test 19 cannot run as root; test aborted\n");
00045         exit(1);
00046   }
00047 
00048   system("rm -rf DIR_19; mkdir DIR_19");
00049   chdir("DIR_19");
00050 
00051   printf("Test 19 ");
00052   fflush(stdout);
00053   for (i = 0; i < 4; i++) {
00054         if (m & 0001) test19a();
00055         if (m & 0002) test19b();
00056         if (m & 0004) test19c();
00057         if (m & 0010) test19d();
00058         if (m & 0020) test19e();
00059         if (m & 0040) test19f();
00060         if (m & 0100) test19g();
00061         passes++;
00062   }
00063   quit();
00064   return(-1);                   /* impossible */
00065 }
00066 
00067 void test19a()
00068 {
00069 /* Test open with O_CREAT and O_EXCL. */
00070 
00071   int fd;
00072 
00073   subtest = 1;
00074   
00075   if ( (fd = creat("T19.a1", 0777)) != 3) e(1); /* create test file */
00076   if (close(fd) != 0) e(2);
00077   if ( (fd = open("T19.a1", O_RDONLY)) != 3) e(3);
00078   if (close(fd) != 0) e(4);
00079   if ( (fd = open("T19.a1", O_WRONLY)) != 3) e(5);
00080   if (close(fd) != 0) e(6);
00081   if ( (fd = open("T19.a1", O_RDWR)) != 3) e(7);
00082   if (close(fd) != 0) e(8);
00083 
00084   /* See if O_CREAT actually creates a file. */
00085   if ( (fd = open("T19.a2", O_RDONLY)) != -1) e(9);     /* must fail */
00086   if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0444)) != 3) e(10);
00087   if (close(fd) != 0) e(11);
00088   if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(12);
00089   if (close(fd) != 0) e(13);
00090   if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(14);
00091   if ( (fd = open("T19.a2", O_RDWR)) != -1) e(15);
00092 
00093   /* See what O_CREAT does on an existing file. */
00094   if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0777)) != 3) e(16);
00095   if (close(fd) != 0) e(17);
00096   if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(18);
00097   if (close(fd) != 0) e(19);
00098   if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(20);
00099   if ( (fd = open("T19.a2", O_RDWR)) != -1) e(21);
00100 
00101   /* See if O_EXCL works. */
00102   if ( (fd = open("T19.a2", O_RDONLY | O_EXCL)) != 3) e(22);
00103   if (close(fd) != 0) e(23);
00104   if ( (fd = open("T19.a2", O_WRONLY | O_EXCL)) != -1) e(24);
00105   if ( (fd = open("T19.a3", O_RDONLY | O_EXCL)) != -1) e(25);
00106   if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != 3) e(26);
00107   if (close(fd) != 0) e(27);
00108   errno = 0;
00109   if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != -1) e(28);
00110   if (errno != EEXIST) e(29);
00111  
00112   if (unlink("T19.a1") != 0) e(30);
00113   if (unlink("T19.a2") != 0) e(31);
00114   if (unlink("T19.a3") != 0) e(32);
00115 }
00116 
00117 void test19b()
00118 {
00119 /* Test open with O_APPEND and O_TRUNC. */
00120 
00121   int fd;
00122 
00123   subtest = 2;
00124   
00125   if ( (fd = creat("T19.b1", 0777)) != 3) e(1); /* create test file */
00126   if (write(fd, b, 4) != 4) e(2);
00127   if (close(fd) != 0) e(3);
00128   clraa();
00129   if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(4);
00130   if (read(fd, aa, 100) != 4) e(5);
00131   if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(6);
00132   if (close(fd) != 0) e(7);
00133   if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(8);
00134   if (write(fd, b, 4) != 4) e(9);
00135   if (lseek(fd, 0L, SEEK_SET) != 0L) e(10);
00136   clraa();
00137   if (read(fd, aa, 100) != 8) e(11);
00138   if (aa[4] != 0 || aa[5] != 1 || aa[6] != 2 || aa[7] != 3) e(12);
00139   if (close(fd) != 0) e(13);
00140 
00141   if ( (fd = open("T19.b1", O_RDWR | O_TRUNC)) != 3) e(14);
00142   if (read(fd, aa, 100) != 0) e(15);
00143   if (close(fd) != 0) e(16);
00144 
00145   unlink("T19.b1");
00146 }
00147 
00148 void test19c()
00149 {
00150 /* Test program for open(), close(), creat(), read(), write(), lseek(). */
00151 
00152   int i, n, n1, n2;
00153 
00154   subtest = 3;
00155   if ((n = creat("foop", 0777)) != 3) e(1);
00156   if ((n1 = creat("foop", 0777)) != 4) e(2);
00157   if ((n2 = creat("/", 0777)) != -1) e(3);
00158   if (close(n) != 0) e(4);
00159   if ((n = open("foop", O_RDONLY)) != 3) e(5);
00160   if ((n2 = open("nofile", O_RDONLY)) != -1) e(6);
00161   if (close(n1) != 0) e(7);
00162 
00163   /* N is the only one open now. */
00164   for (i = 0; i < 2; i++) {
00165         n1 = creat("File2", 0777);
00166         if (n1 != 4) {
00167                 printf("creat yielded fd=%d, expected 4\n", n1);
00168                 e(8);
00169         }
00170         if ((n2 = open("File2", O_RDONLY)) != 5) e(9);
00171         if (close(n1) != 0) e(10);
00172         if (close(n2) != 0) e(11);
00173   }
00174   unlink("File2");
00175   if (close(n) != 0) e(12);
00176 
00177   /* All files closed now. */
00178   for (i = 0; i < 2; i++) {
00179         if ((n = creat("foop", 0777)) != 3) e(13);
00180         if (close(n) != 0) e(14);
00181         if ((n = open("foop", O_RDWR)) != 3) e(15);
00182 
00183         /* Read/write tests */
00184         if (write(n, b, 4) != 4) e(16);
00185         if (read(n, aa, 4) != 0) e(17);
00186         if (lseek(n, 0L, SEEK_SET) != 0L) e(18);
00187         if (read(n, aa, 4) != 4) e(19);
00188         if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(20);
00189         if (lseek(n, 0L, SEEK_SET) != 0L) e(21);
00190         if (lseek(n, 2L, SEEK_CUR) != 2L) e(22);
00191         if (read(n, aa, 4) != 2) e(23);
00192         if (aa[0] != 2 || aa[1] != 3 || aa[2] != 2 || aa[3] != 3) e(24);
00193         if (lseek(n, 2L, SEEK_SET) != 2L) e(25);
00194         clraa();
00195         if (write(n, c, 4) != 4) e(26);
00196         if (lseek(n, 0L, SEEK_SET) != 0L) e(27);
00197         if (read(n, aa, 10) != 6) e(28);
00198         if (aa[0] != 0 || aa[1] != 1 || aa[2] != 10 || aa[3] != 20) e(29);
00199         if (lseek(n, 16L, SEEK_SET) != 16L) e(30);
00200         if (lseek(n, 2040L, SEEK_END) != 2046L) e(31);
00201         if (read(n, aa, 10) != 0) e(32);
00202         if (lseek(n, 0L, SEEK_CUR) != 2046L) e(33);
00203         clraa();
00204         if (write(n, c, 4) != 4) e(34);
00205         if (lseek(n, 0L, SEEK_CUR) != 2050L) e(35);
00206         if (lseek(n, 2040L, SEEK_SET) != 2040L) e(36);
00207         clraa();
00208         if (read(n, aa, 20) != 10) e(37);
00209         if (aa[0] != 0 || aa[5] != 0 || aa[6] != 10 || aa[9] != 40) e(38);
00210         if (lseek(n, 10239L, SEEK_SET) != 10239L) e(39);
00211         if (write(n, d, 2) != 2) e(40);
00212         if (lseek(n, -2L, SEEK_END) != 10239L) e(41);
00213         if (read(n, aa, 2) != 2) e(42);
00214         if (aa[0] != 6 || aa[1] != 7) e(43);
00215         if (lseek(n, NB * 1024L - 2L, SEEK_SET) != NB * 1024L - 2L) e(44);
00216         if (write(n, b, 4) != 4) e(45);
00217         if (lseek(n, 0L, SEEK_SET) != 0L) e(46);
00218         if (lseek(n, -6L, SEEK_END) != 1024L * NB - 4) e(47);
00219         clraa();
00220         if (read(n, aa, 100) != 6) e(48);
00221         if (aa[0] != 0 || aa[1] != 0 || aa[3] != 1 || aa[4] != 2|| aa[5] != 3)
00222                 e(49);
00223         if (lseek(n, 20000L, SEEK_SET) != 20000L) e(50);
00224         if (write(n, c, 4) != 4) e(51);
00225         if (lseek(n, -4L, SEEK_CUR) != 20000L) e(52);
00226         if (read(n, aa, 4) != 4) e(53);
00227         if (aa[0] != 10 || aa[1] != 20 || aa[2] != 30 || aa[3] != 40) e(54);
00228         if (close(n) != 0) e(55);
00229         if ((n1 = creat("foop", 0777)) != 3) e(56);
00230         if (close(n1) != 0) e(57);
00231         unlink("foop");
00232 
00233   }
00234 }
00235 
00236 void test19d()
00237 {
00238 /* Test read. */
00239 
00240   int i, fd, pd[2];
00241   char bb[100];
00242 
00243   subtest = 4;
00244   
00245   for (i = 0; i < 100; i++) bb[i] = i;
00246   if ( (fd = creat("T19.d1", 0777)) != 3) e(1); /* create test file */
00247   if (write(fd, bb, 100) != 100) e(2);
00248   if (close(fd) != 0) e(3);
00249   clraa();
00250   if ( (fd = open("T19.d1", O_RDONLY)) != 3) e(4);
00251   errno = 1000;
00252   if (read(fd, aa, 0) != 0) e(5);
00253   if (errno != 1000) e(6);
00254   if (read(fd, aa, 100) != 100) e(7);
00255   if (lseek(fd, 37L, SEEK_SET) != 37L) e(8);
00256   if (read(fd, aa, 10) != 10) e(9);
00257   if (lseek(fd, 0L, SEEK_CUR) != 47L) e(10);
00258   if (read(fd, aa, 100) != 53) e(11);
00259   if (aa[0] != 47) e(12);
00260   if (read(fd, aa, 1) != 0) e(13);
00261   if (close(fd) != 0) e(14);
00262 
00263   /* Read from pipe with no writer open. */
00264   if (pipe(pd) != 0) e(15);
00265   if (close(pd[1]) != 0) e(16);
00266   errno = 2000;
00267   if (read(pd[0], aa, 1) != 0) e(17);   /* must return EOF */
00268   if (errno != 2000) e(18);
00269 
00270   /* Read from a pipe with O_NONBLOCK set. */
00271   if (fcntl(pd[0], F_SETFL, O_NONBLOCK) != 0) e(19);      /* set O_NONBLOCK */
00272 /*
00273   if (read(pd[0], aa, 1) != -1) e(20);
00274   if (errno != EAGAIN) e(21);
00275 */
00276   if (close(pd[0]) != 0) e(19);
00277   if (unlink("T19.d1") != 0) e(20);
00278 }
00279 
00280 void test19e()
00281 {
00282 /* Test link, unlink, stat, fstat, dup, umask.  */
00283 
00284   int i, j, n, n1, flag;
00285   char a[255], b[255];
00286   struct stat s, s1;
00287 
00288   subtest = 5;
00289   for (i = 0; i < 2; i++) {
00290         umask(0);
00291 
00292         if ((n = creat("T3", 0702)) < 0) e(1);
00293         if (link("T3", "newT3") < 0) e(2);
00294         if ((n1 = open("newT3", O_RDWR)) < 0) e(3);
00295         for (j = 0; j < 255; j++) a[j] = j;
00296         if (write(n, a, 255) != 255) e(4);
00297         if (read(n1, b, 255) != 255) e(5);
00298         flag = 0;
00299         for (j = 0; j < 255; j++)
00300                 if (a[j] != b[j]) flag++;
00301         if (flag) e(6);
00302         if (unlink("T3") < 0) e(7);
00303         if (close(n) < 0) e(8);
00304         if (close(n1) < 0) e(9);
00305         if ((n1 = open("newT3", O_RDONLY)) < 0) e(10);
00306         if (read(n1, b, 255) != 255) e(11);
00307         flag = 0;
00308         for (j = 0; j < 255; j++)
00309                 if (a[j] != b[j]) flag++;
00310         if (flag) e(12);
00311 
00312         /* Now check out stat, fstat. */
00313         if (stat("newT3", &s) < 0) e(13);
00314         if (s.st_mode != (mode_t) 0100702) e(14);
00315                                 /* The cast was because regular modes are
00316                                  * negative :-(.  Anyway, the magic number
00317                                  * should be (S_IFREG | S_IRWXU | S_IWOTH)
00318                                  * for POSIX.
00319                                  */
00320         if (s.st_nlink != 1) e(15);
00321         if (s.st_size != 255L) e(16);
00322         if (fstat(n1, &s1) < 0) e(17);
00323         if (s.st_dev != s1.st_dev) e(18);
00324         if (s.st_ino != s1.st_ino) e(19);
00325         if (s.st_mode != s1.st_mode) e(20);
00326         if (s.st_nlink != s1.st_nlink) e(21);
00327         if (s.st_uid != s1.st_uid) e(22);
00328         if (s.st_gid != s1.st_gid) e(23);
00329         if (s.st_rdev != s1.st_rdev) e(24);
00330         if (s.st_size != s1.st_size) e(25);
00331         if (s.st_atime != s1.st_atime) e(26);
00332         if (close(n1) < 0) e(27);
00333         if (unlink("newT3") < 0) e(28);
00334 
00335         umask(040);
00336         if ((n = creat("T3a", 0777)) < 0) e(29);
00337         if (stat("T3a", &s) < 0) e(30);
00338         if (s.st_mode != (mode_t) 0100737) e(31);       /* negative :-( */
00339         if (unlink("T3a") < 0) e(32);
00340         if (close(n1) < 0) e(33);
00341 
00342         /* Dup */
00343         if ((n = creat("T3b", 0777)) < 0) e(34);
00344         if (close(n) < 0) e(35);
00345         if ((n = open("T3b", O_RDWR)) < 0) e(36);
00346         if ((n1 = dup(n)) != n + 1) e(37);
00347         if (write(n, a, 255) != 255) e(38);
00348         read(n1, b, 20);
00349         if (lseek(n, 0L, SEEK_SET) != 0L) e(39);
00350         if ((j = read(n1, b, 512)) != 255) e(40);
00351         if (unlink("T3b") < 0) e(41);
00352         if (close(n) < 0) e(42);
00353         if (close(n1) < 0) e(43);
00354 
00355   }
00356 }
00357 
00358 void test19f()
00359 {
00360 /* Test large files to see if indirect block stuff works. */
00361 
00362   int fd, i;
00363   long pos;
00364 
00365   subtest = 6;
00366 
00367   if (passes > 0) return;       /* takes too long to repeat this test */
00368   for (i = 0; i < NBOUNDS; i ++) {
00369         pos = 1024L * bounds[i];
00370         fd = creat("T19f", 0777);
00371         if (fd < 0) e(10*i+1);
00372         if (lseek(fd, pos, 0) < 0) e(10*i+2);
00373         if (write(fd, buff, 30720) != 30720) e(10*i+3);
00374         if (close(fd) < 0) e(10*i+3);
00375         if (unlink("T19f") < 0) e(10*i+4);
00376   }
00377 }
00378 
00379 void test19g()
00380 {
00381 /* Test POSIX calls for pipe, read, write, lseek and close. */
00382 
00383   int pipefd[2], n, i, fd;
00384   char buf[512], buf2[512];
00385 
00386   subtest = 7;
00387 
00388   for (i = 0; i < 512; i++) buf[i] = i % 128;
00389 
00390   if (pipe(pipefd) < 0) e(1);
00391   if (write(pipefd[1], buf, 512) != 512) e(2);
00392   if (read(pipefd[0], buf2, 512) != 512) e(3);
00393   if (close(pipefd[1]) != 0) e(4);
00394   if (close(pipefd[1]) >= 0) e(5);
00395   if (read(pipefd[0], buf2, 1) != 0) e(6);
00396   if (close(pipefd[0]) != 0) e(7);
00397 
00398   /* Test O_NONBLOCK on pipes. */
00399   if (pipe(pipefd) < 0) e(8);
00400   if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0) e(9);
00401   if (read(pipefd[0], buf2, 1) != -1) e(10);
00402   if (errno != EAGAIN) e(11);
00403   if (close(pipefd[0]) != 0) e(12);
00404   if (close(pipefd[1]) != 0) e(13);
00405 
00406   /* Test read and lseek. */
00407   if ( (fd = creat("T19.g1", 0777)) != 3) e(14);        /* create test file */
00408   if (write(fd, buf, 512) != 512) e(15);
00409   errno = 3000;
00410   if (read(fd, buf2, 512) != -1) e(16);
00411   if (errno != EBADF) e(17);
00412   if (close(fd) != 0) e(18);
00413   if ( (fd = open("T19.g1", O_RDWR)) != 3) e(19);
00414   if (read(fd, buf2, 512) != 512) e(20);
00415   if (read(fd, buf2, 512) != 0) e(21);
00416   if (lseek(fd, 100L, SEEK_SET) != 100L) e(22);
00417   if (read(fd, buf2, 512) != 412) e(23);
00418   if (lseek(fd, 1000L, SEEK_SET) != 1000L) e(24);
00419 
00420   /* Test write. */
00421   if (lseek(fd, -1000L, SEEK_CUR) != 0) e(25);
00422   if (write(fd, buf, 512) != 512) e(26);
00423   if (lseek(fd, 2L, SEEK_SET) != 2) e(27);
00424   if (write(fd, buf, 3) != 3) e(28);
00425   if (lseek(fd, -2L, SEEK_CUR) != 3) e(29);
00426   if (write(fd, &buf[30], 1) != 1) e(30);
00427   if (lseek(fd, 2L, SEEK_CUR) != 6) e(31);
00428   if (write(fd, &buf[60], 1) != 1) e(32);
00429   if (lseek(fd, -512L, SEEK_END) != 0) e(33);
00430   if (read(fd, buf2, 8) != 8) e(34);
00431   errno = 4000;
00432   if (buf2[0] != 0 || buf2[1] != 1 || buf2[2] != 0 || buf2[3] != 30) e(35);
00433   if (buf2[4] != 2 || buf2[5] != 5 || buf2[6] != 60 || buf2[7] != 7) e(36);
00434 
00435   /* Turn the O_APPEND flag on. */
00436   if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(37);
00437   if (lseek(fd, 0L, SEEK_SET) != 0) e(38);
00438   if (write(fd, &buf[100], 1) != 1) e(39);
00439   if (lseek(fd, 0L, SEEK_SET) != 0) e(40);
00440   if (read(fd, buf2, 10) != 10) e(41);
00441   if (buf2[0] != 0) e(42);
00442   if (lseek(fd, -1L, SEEK_END) != 512) e(43);
00443   if (read(fd, buf2, 10) != 1) e(44);
00444   if (buf2[0] != 100) e(45);
00445   if (close(fd) != 0) e(46);
00446 
00447   /* Now try write with O_NONBLOCK. */
00448   if (pipe(pipefd) != 0) e(47);
00449   if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) e(48);
00450   if (write(pipefd[1], buf, 512) != 512) e(49);
00451   if (write(pipefd[1], buf, 512) != 512) e(50);
00452   errno = 0;
00453   for (i = 1; i < 20; i++) {
00454         n = write(pipefd[1], buf, 512);
00455         if (n == 512) continue;
00456         if (n != -1 || errno != EAGAIN) {e(51); break;}
00457   }
00458   if (read(pipefd[0], buf, 512) != 512) e(52);
00459   if (close(pipefd[0]) != 0) e(53);
00460 
00461   /* Write to a pipe with no reader.  This should generate a signal. */
00462   signal(SIGPIPE, pipecatcher);
00463   errno = 0;
00464   if (write(pipefd[1], buf, 1) != -1) e(54);
00465   if (errno != EPIPE) e(55);
00466   if (pipesigs != passes + 1) e(56);    /* we should have had the sig now */
00467   if (close(pipefd[1]) != 0) e(57);
00468   errno = 0;
00469   if (write(100, buf, 512) != -1) e(58);
00470   if (errno != EBADF) e(59);
00471   if (unlink("T19.g1") != 0) e(60);
00472 }
00473 
00474 void clraa()
00475 {
00476   int i;
00477   for (i = 0; i < 100; i++) aa[i] = 0;
00478 }
00479 
00480 void pipecatcher(s)
00481 int s;                          /* it is supposed to have an arg */
00482 {
00483   pipesigs++;
00484 }
00485 
00486 void e(n)
00487 int n;
00488 {
00489   int err_num = errno;          /* save errno in case printf clobbers it */
00490 
00491   printf("Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
00492   fflush(stdout);               /* aargh!  Most results go to stdout and are
00493                                  * messed up by perror going to stderr.
00494                                  * Should replace perror by printf and strerror
00495                                  * in all the tests.
00496                                  */
00497   errno = err_num;              /* restore errno, just in case */
00498   perror("");
00499   if (errct++ > MAX_ERROR) {
00500         printf("Too many errors; test aborted\n");
00501         chdir("..");
00502         system("rm -rf DIR*");
00503         exit(1);
00504   }
00505 }
00506 
00507 void quit()
00508 {
00509 
00510   chdir("..");
00511   system("rm -rf DIR*");
00512 
00513   if (errct == 0) {
00514         printf("ok\n");
00515         exit(0);
00516   } else {
00517         printf("%d errors\n", errct);
00518         exit(1);
00519   }
00520 }
00521 

Generated on Fri Apr 14 22:57:33 2006 for minix by  doxygen 1.4.6