test39.c

Go to the documentation of this file.
00001 /* POSIX test program (39).                     Author: Andy Tanenbaum */
00002 
00003 /* The following POSIX calls are tested:
00004  *
00005  *      opendir()
00006  *      readdir()
00007  *      rewinddir()
00008  *      closedir()
00009  *      chdir()
00010  *      getcwd()
00011  */
00012 
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <dirent.h>
00016 #include <fcntl.h>
00017 #include <errno.h>
00018 #include <limits.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <time.h>
00022 #include <unistd.h>
00023 #include <utime.h>
00024 #include <stdio.h>
00025 
00026 #define DIR_NULL (DIR*) NULL
00027 #define ITERATIONS         3    /* LINK_MAX is high, so time consuming */
00028 #define MAX_FD           100    /* must be large enough to cause error */
00029 #define BUF_SIZE PATH_MAX+20
00030 #define ERR_CODE          -1    /* error return */
00031 #define RD_BUF           200
00032 #define MAX_ERROR          4
00033 
00034 char str[] = {"The time has come the walrus said to talk of many things.\n"};
00035 char str2[] = {"Of ships and shoes and sealing wax, of cabbages and kings.\n"};
00036 char str3[] = {"Of why the sea is boiling hot and whether pigs have wings\n"};
00037 
00038 int subtest, errct;
00039 
00040 _PROTOTYPE(int main, (int argc, char *argv []));
00041 _PROTOTYPE(void test39a, (void));
00042 _PROTOTYPE(void checkdir, (DIR *dirp, int t));
00043 _PROTOTYPE(void test39b, (void));
00044 _PROTOTYPE(void test39c, (void));
00045 _PROTOTYPE(void test39d, (void));
00046 _PROTOTYPE(void test39e, (void));
00047 _PROTOTYPE(void test39f, (void));
00048 _PROTOTYPE(void test39g, (void));
00049 _PROTOTYPE(void test39h, (void));
00050 _PROTOTYPE(void test39i, (void));
00051 _PROTOTYPE(void test39j, (void));
00052 _PROTOTYPE(void e, (int n));
00053 _PROTOTYPE(void quit, (void));
00054 
00055 int main(argc, argv)
00056 int argc;
00057 char *argv[];
00058 {
00059 
00060   int i, m = 0xFFFF;
00061 
00062   sync();
00063   if (geteuid() == 0 || getuid() == 0) {
00064         printf("Test 39 cannot run as root; test aborted\n");
00065         exit(1);
00066   }
00067 
00068   if (argc == 2) m = atoi(argv[1]);
00069   printf("Test 39 ");
00070   fflush(stdout);
00071 
00072   system("rm -rf DIR_39; mkdir DIR_39");
00073   chdir("DIR_39");
00074 
00075   for (i = 0; i < ITERATIONS; i++) {
00076         if (m & 00001) test39a();       /* test for correct operation */
00077         if (m & 00002) test39b();       /* test general error handling */
00078         if (m & 00004) test39c();       /* test for EMFILE error */
00079         if (m & 00010) test39d();       /* test chdir() and getcwd() */
00080         if (m & 00020) test39e();       /* test open() */
00081         if (m & 00040) test39f();       /* test umask(), stat(), fstat() */
00082         if (m & 00100) test39g();       /* test link() and unlink() */
00083         if (m & 00200) test39h();       /* test access() */
00084         if (m & 00400) test39i();       /* test chmod() and chown() */
00085         if (m & 01000) test39j();       /* test utime() */
00086   }
00087   quit();
00088   return(-1);                   /* impossible */
00089 }
00090 
00091 void test39a()
00092 {
00093 /* Subtest 1. Correct operation */
00094 
00095   int f1, f2, f3, f4, f5;
00096   DIR *dirp;
00097 
00098   /* Remove any residue of previous tests. */
00099   subtest = 1;
00100 
00101   system("rm -rf foo");
00102 
00103   /* Create a directory foo with 5 files in it. */
00104   mkdir("foo", 0777);
00105   if ((f1 = creat("foo/f1", 0666)) < 0) e(1);
00106   if ((f2 = creat("foo/f2", 0666)) < 0) e(2);
00107   if ((f3 = creat("foo/f3", 0666)) < 0) e(3);
00108   if ((f4 = creat("foo/f4", 0666)) < 0) e(4);
00109   if ((f5 = creat("foo/f5", 0666)) < 0) e(5);
00110 
00111   /* Now remove 2 files to create holes in the directory. */
00112   if (unlink("foo/f2") < 0) e(6);
00113   if (unlink("foo/f4") < 0) e(7);
00114 
00115   /* Close the files. */
00116   close(f1);
00117   close(f2);
00118   close(f3);
00119   close(f4);
00120   close(f5);
00121 
00122   /* Open the directory. */
00123   dirp = opendir("./foo");
00124   if (dirp == DIR_NULL) e(6);
00125 
00126   /* Read the 5 files from it. */
00127   checkdir(dirp, 2); 
00128 
00129   /* Rewind dir and test again. */
00130   rewinddir(dirp);
00131   checkdir(dirp, 3);
00132 
00133   /* We're done.  Close the directory stream. */
00134   if (closedir(dirp) < 0) e(7);
00135 
00136   /* Remove dir for next time. */
00137   system("rm -rf foo");
00138 }
00139 
00140 void checkdir(dirp, t)
00141 DIR *dirp;                      /* poinrter to directory stream */
00142 int t;                          /* subtest number to use */
00143 {
00144 
00145   int i, f1, f2, f3, f4, f5, dot, dotdot, subt;
00146   struct dirent *d;
00147   char *s;
00148 
00149   /* Save subtest number */
00150   subt = subtest;
00151   subtest = t;
00152 
00153   /* Clear the counters. */
00154   f1 = 0;
00155   f2 = 0;
00156   f3 = 0;
00157   f4 = 0;
00158   f5 = 0;
00159   dot = 0;
00160   dotdot = 0;
00161 
00162   /* Read the directory.  It should contain 5 entries, ".", ".." and 3
00163    * files. */
00164   for (i = 0; i < 5; i++) {
00165         d = readdir(dirp);
00166         if (d == (struct dirent *) NULL) {
00167                 e(1);
00168                 subtest = subt; /* restore subtest number */
00169                 return;
00170         }
00171         s = d->d_name;
00172         if (strcmp(s, ".") == 0) dot++;
00173         if (strcmp(s, "..") == 0) dotdot++;
00174         if (strcmp(s, "f1") == 0) f1++;
00175         if (strcmp(s, "f2") == 0) f2++;
00176         if (strcmp(s, "f3") == 0) f3++;
00177         if (strcmp(s, "f4") == 0) f4++;
00178         if (strcmp(s, "f5") == 0) f5++;
00179   }
00180 
00181   /* Check results. */
00182   d = readdir(dirp);
00183   if (d != (struct dirent *) NULL) e(2);
00184   if (f1 != 1 || f3 != 1 || f5 != 1) e(3);
00185   if (f2 != 0 || f4 != 0) e(4);
00186   if (dot != 1 || dotdot != 1) e(5);
00187   subtest = subt;
00188   return;
00189 }
00190 
00191 void test39b()
00192 {
00193 /* Subtest 4.  Test error handling. */
00194 
00195   int fd;
00196   DIR *dirp;
00197 
00198   subtest = 4;
00199 
00200   if (opendir("foo/xyz/---") != DIR_NULL) e(1);
00201   if (errno != ENOENT) e(2);
00202   if (mkdir("foo", 0777) < 0) e(3);
00203   if (chmod("foo", 0) < 0) e(4);
00204   if (opendir("foo/xyz/--") != DIR_NULL) e(5);
00205   if (errno != EACCES) e(6);
00206   if (chmod("foo", 0777) != 0) e(7);
00207   if (rmdir("foo") != 0) e(8);
00208   if ((fd = creat("abc", 0666)) < 0) e(9);
00209   if (close(fd) < 0) e(10);
00210   if (opendir("abc/xyz") != DIR_NULL) e(11);
00211   if (errno != ENOTDIR) e(12);
00212   if ((dirp = opendir(".")) == DIR_NULL) e(13);
00213   if (closedir(dirp) != 0) e(14);
00214   if (unlink("abc") != 0) e(15);
00215 
00216 }
00217 
00218 void test39c()
00219 {
00220 /* Subtest 5.  See what happens if we open too many directory streams. */
00221 
00222   int i, j;
00223   DIR *dirp[MAX_FD];
00224 
00225   subtest = 5;
00226 
00227   for (i = 0; i < MAX_FD; i++) {
00228         dirp[i] = opendir(".");
00229         if (dirp[i] == (DIR *) NULL) {
00230                 /* We have hit the limit. */
00231                 if (errno != EMFILE && errno != ENOMEM) e(1);
00232                 for (j = 0; j < i; j++) {
00233                         if (closedir(dirp[j]) != 0) e(2);       /* close */
00234                 }
00235                 return;
00236         }
00237   }
00238 
00239   /* Control should never come here.  This is an error. */
00240   e(3);
00241   for (i = 0; i < MAX_FD; i++) closedir(dirp[i]);       /* don't check */
00242 }
00243 
00244 void test39d()
00245 {
00246 /* Test chdir and getcwd(). */
00247 
00248   int fd;
00249   char *s;
00250   char base[BUF_SIZE], buf2[BUF_SIZE], tmp[BUF_SIZE];
00251 
00252   subtest = 6;
00253 
00254   if (getcwd(base, BUF_SIZE) == (char *) NULL) e(1); /* get test dir's path */
00255   if (system("rm -rf Dir") != 0) e(2);  /* remove residue of previous test */
00256   if (mkdir("Dir", 0777) < 0) e(3);     /* create directory called "Dir" */
00257 
00258   /* Change to Dir and verify that it worked. */
00259   if (chdir("Dir") < 0) e(4);   /* go to Dir */
00260   s = getcwd(buf2, BUF_SIZE);   /* get full path of Dir */
00261   if (s == (char *) NULL) e(5); /* check for error return */
00262   if (s != buf2) e(6);          /* if successful, first arg is returned */
00263   strcpy(tmp, base);            /* concatenate base name and "/Dir" */
00264   strcat(tmp, "/");
00265   strcat(tmp, "Dir");
00266   if (strcmp(tmp, s) != 0) e(7);
00267 
00268   /* Change to ".." and verify that it worked. */
00269   if (chdir("..") < 0) e(8);
00270   if (getcwd(buf2, BUF_SIZE) != buf2) e(9);
00271   if (strcmp(buf2, base) != 0) e(10);
00272 
00273   /* Now make calls that do nothing, but do it in a strange way. */
00274   if (chdir("Dir/..") < 0) e(11);
00275   if (getcwd(buf2, BUF_SIZE) != buf2) e(12);
00276   if (strcmp(buf2, base) != 0) e(13);
00277 
00278   if (chdir("Dir/../Dir/..") < 0) e(14);
00279   if (getcwd(buf2, BUF_SIZE) != buf2) e(15);
00280   if (strcmp(buf2, base) != 0) e(16);
00281 
00282   if (chdir("Dir/../Dir/../Dir/../Dir/../Dir/../Dir/../Dir/..") < 0) e(17);
00283   if (getcwd(buf2, BUF_SIZE) != buf2) e(18);
00284   if (strcmp(buf2, base) != 0) e(19);
00285 
00286   /* Make Dir unreadable and unsearchable.  Check error message. */
00287   if (chmod("Dir", 0) < 0) e(20);
00288   if (chdir("Dir") >= 0) e(21);
00289   if (errno != EACCES) e(22);
00290 
00291   /* Check error message for bad path. */
00292   if (chmod("Dir", 0777) < 0) e(23);
00293   if (chdir("Dir/x/y") != ERR_CODE) e(24);
00294   if (errno != ENOENT) e(25);
00295 
00296   if ( (fd=creat("Dir/x", 0777)) < 0) e(26);
00297   if (close(fd) != 0) e(27);
00298   if (chdir("Dir/x/y") != ERR_CODE) e(28);
00299   if (errno != ENOTDIR) e(29);  
00300 
00301   /* Check empty string. */
00302   if (chdir("") != ERR_CODE) e(30);
00303   if (errno != ENOENT) e(31);
00304 
00305   /* Remove the directory. */
00306   if (unlink("Dir/x") != 0) e(32);
00307   if (system("rmdir Dir") != 0) e(33);
00308 }
00309 
00310 void test39e()
00311 {
00312 /* Test open. */
00313 
00314   int fd, bytes, bytes2;
00315   char buf[RD_BUF];
00316 
00317   subtest = 7;
00318 
00319   unlink("T39");                /* get rid of it in case it exists */
00320 
00321   /* Create a test file. */
00322   bytes = strlen(str);
00323   bytes2 = strlen(str2);
00324   if ((fd = creat("T39", 0777)) < 0) e(1);
00325   if (write(fd, str, bytes) != bytes) e(2);     /* T39 now has 'bytes' bytes */
00326   if (close(fd) != 0) e(3);
00327 
00328   /* Test opening a file with O_RDONLY. */
00329   if ((fd = open("T39", O_RDONLY)) < 0) e(4);
00330   buf[0] = '\0';
00331   if (read(fd, buf, RD_BUF) != bytes) e(5);
00332   if (strncmp(buf, str, bytes) != 0) e(6);
00333   if (close(fd) < 0) e(7);
00334 
00335   /* Test the same thing, only with O_RDWR now. */
00336   if ((fd = open("T39", O_RDWR)) < 0) e(8);
00337   buf[0] = '\0';
00338   if (read(fd, buf, RD_BUF) != bytes) e(9);
00339   if (strncmp(buf, str, bytes) != 0) e(10);
00340   if (close(fd) < 0) e(11);
00341 
00342   /* Try opening and reading with O_WRONLY.  It should fail. */
00343   if ((fd = open("T39", O_WRONLY)) < 0) e(12);
00344   buf[0] = '\0';
00345   if (read(fd, buf, RD_BUF) >= 0) e(13);
00346   if (close(fd) != 0) e(14);
00347 
00348   /* Test O_APPEND. */
00349   if ((fd = open("T39", O_RDWR | O_APPEND)) < 0) e(15);
00350   if (lseek(fd, 0L, SEEK_SET) < 0) e(16);       /* go to start of file */
00351   if ( write(fd, str2, bytes2) != bytes2) e(17); /* write at start of file */
00352   if (lseek(fd, 0L, SEEK_SET) < 0) e(18);       /* go back to start again */
00353   if (read(fd, buf, RD_BUF) != bytes + bytes2) e(19); /* read whole file */
00354   if (strncmp(buf, str, bytes) != 0) e(20);
00355   if (close(fd) != 0) e(21);
00356 
00357   /* Get rid of the file. */
00358   if (unlink("T39") < 0) e(22);
00359 }
00360 
00361 void test39f()
00362 {
00363 /* Test stat, fstat, umask. */
00364   int i, fd;
00365   mode_t m1;
00366   struct stat stbuf1, stbuf2;
00367   time_t t, t1;
00368 
00369   subtest = 8;
00370 
00371   m1 = umask(~0777);
00372   if (system("rm -rf foo xxx") != 0) e(1);
00373   if ((fd = creat("foo", 0777)) < 0) e(2);
00374   if (stat("foo", &stbuf1) < 0) e(3);
00375   if (fstat(fd, &stbuf2) < 0) e(4);
00376   if (stbuf1.st_mode != stbuf2.st_mode) e(5);
00377   if (stbuf1.st_ino != stbuf2.st_ino) e(6);
00378   if (stbuf1.st_dev != stbuf2.st_dev) e(7);
00379   if (stbuf1.st_nlink != stbuf2.st_nlink) e(8);
00380   if (stbuf1.st_uid != stbuf2.st_uid) e(9);
00381   if (stbuf1.st_gid != stbuf2.st_gid) e(10);
00382   if (stbuf1.st_size != stbuf2.st_size) e(11);
00383   if (stbuf1.st_atime != stbuf2.st_atime) e(12);
00384   if (stbuf1.st_mtime != stbuf2.st_mtime) e(13);
00385   if (stbuf1.st_ctime != stbuf2.st_ctime) e(14);
00386 
00387   if (!S_ISREG(stbuf1.st_mode)) e(15);
00388   if (S_ISDIR(stbuf1.st_mode)) e(16);
00389   if (S_ISCHR(stbuf1.st_mode)) e(17);
00390   if (S_ISBLK(stbuf1.st_mode)) e(18);
00391   if (S_ISFIFO(stbuf1.st_mode)) e(19);
00392 
00393   if ((stbuf1.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != 0777) e(20);
00394   if (stbuf1.st_nlink != 1) e(21);
00395   if (stbuf1.st_uid != getuid()) e(22);
00396   if (stbuf1.st_gid != getgid()) e(23);
00397   if (stbuf1.st_size != 0L) e(24);
00398 
00399   /* First unlink, then close -- harder test */
00400   if (unlink("foo") < 0) e(25);
00401   if (close(fd) < 0) e(26);
00402 
00403   /* Now try umask a bit more. */
00404   fd = 0;
00405   if ((i = umask(~0704)) != 0) e(27);
00406   if ((fd = creat("foo", 0777)) < 0) e(28);
00407   if (stat("foo", &stbuf1) < 0) e(29);
00408   if (fstat(fd, &stbuf2) < 0) e(30);
00409   if (stbuf1.st_mode != stbuf2.st_mode) e(31);
00410   if ((stbuf1.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != 0704) e(32);
00411 
00412   /* First unlink, then close -- harder test */
00413   if (unlink("foo") < 0) e(33);
00414   if (close(fd) < 0) e(34);
00415   if (umask(m1) != 073) e(35);
00416 
00417   /* Test some errors. */
00418   if (system("mkdir Dir; date >Dir/x; chmod 666 Dir") != 0) e(36);
00419   if (stat("Dir/x", &stbuf1) >= 0) e(37);
00420   if (errno != EACCES) e(38);
00421   if (stat("......", &stbuf1) >= 0) e(39);
00422   if (errno != ENOENT) e(40);
00423   if (stat("", &stbuf1) >= 0) e(41);
00424   if (errno != ENOENT) e(42);
00425   if (stat("xxx/yyy/zzz", &stbuf1) >= 0) e(43);
00426   if (errno != ENOENT) e(44);
00427   if (fstat(10000, &stbuf1) >= 0) e(45);
00428   if (errno != EBADF) e(46);
00429   if (chmod("Dir", 0777) != 0) e(47);
00430   if (system("rm -rf foo Dir") != 0) e(48);
00431 
00432   /* See if time looks reasonable. */
00433   errno = 0;
00434   t = time(&t1);                /* current time */
00435   if (t < 650000000L) e(49);    /* 650000000 is Sept. 1990 */
00436   unlink("T39f");
00437   fd = creat("T39f", 0777);
00438   if (fd < 0) e(50);
00439   if (close(fd) < 0) e(51);
00440   if (stat("T39f", &stbuf1) < 0) e(52);
00441   if (stbuf1.st_mtime < t) e(53);
00442   if (unlink("T39f") < 0) e(54);
00443 }
00444 
00445 void test39g()
00446 {
00447 /* Test link and unlink. */
00448   int i, fd;
00449   struct stat stbuf;
00450   char name[20];
00451 
00452   subtest = 9;
00453 
00454   if (system("rm -rf L? L?? Dir; mkdir Dir") != 0) e(1);
00455   if ( (fd = creat("L1", 0666)) < 0) e(2);
00456   if (fstat(fd, &stbuf) != 0) e(3);
00457   if (stbuf.st_nlink != 1) e(4);
00458   if (link("L1", "L2") != 0) e(5);
00459   if (fstat(fd, &stbuf) != 0) e(6);
00460   if (stbuf.st_nlink != 2) e(7);
00461   if (unlink("L2") != 0) e(8);
00462   if (link("L1", "L2") != 0) e(9);
00463   if (unlink("L1") != 0) e(10);
00464   if (close(fd) != 0) e(11);
00465 
00466   /* L2 exists at this point. */
00467   if ( (fd = creat("L1", 0666)) < 0) e(12);
00468   if (stat("L1", &stbuf) != 0) e(13);
00469   if (stbuf.st_nlink != 1) e(14);
00470   if (link("L1", "Dir/L2") != 0) e(15);
00471   if (stat("L1", &stbuf) != 0) e(16);
00472   if (stbuf.st_nlink != 2) e(17);
00473   if (stat("Dir/L2", &stbuf) != 0) e(18);
00474   if (stbuf.st_nlink != 2) e(19);
00475 
00476   /* L1, L2, and Dir/L2 exist at this point. */
00477   if (unlink("Dir/L2") != 0) e(20);
00478   if (link("L1", "Dir/L2") != 0) e(21);
00479   if (unlink("L1") != 0) e(22);
00480   if (close(fd) != 0) e(23);
00481   if (chdir("Dir") != 0) e(24);
00482   if (unlink("L2") != 0) e(25);
00483   if (chdir("..") != 0) e(26);
00484   
00485   /* L2 exists at this point. Test linking to unsearchable dir. */
00486   if (link("L2", "Dir/L2") != 0) e(27);
00487   if (chmod("Dir", 0666) != 0) e(27);
00488   if (link("L2", "Dir/L2") != -1) e(28);
00489   if (errno != EACCES) e(29);
00490   errno = 0;
00491   if (link("Dir/L2", "L3") != -1) e(30);
00492   if (errno != EACCES) e(31);
00493   if (chmod("Dir", 0777) != 0) e(32);
00494   if (unlink("Dir/L2") != 0) e(33);
00495   if (unlink("L3") == 0) e(34);
00496 
00497   /* L2 exists at this point. Test linking to unwriteable dir. */
00498   if (chmod("Dir", 0555) != 0) e(35);
00499   if (link("L2", "Dir/L2") != -1) e(36);
00500   if (errno != EACCES) e(37);
00501   if (chmod("Dir", 0777) != 0) e(38);
00502 
00503   /* L2 exists at this point.  Test linking mode 0 file. */
00504   if (chmod("L2", 0) != 0) e(39);
00505   if (link("L2", "L3") != 0) e(40);
00506   if (stat("L3", &stbuf) != 0) e(41);
00507   if (stbuf.st_nlink != 2) e(42);
00508   if (unlink("L2") != 0) e(43);
00509 
00510   /* L3 exists at this point.  Test linking to an existing file. */
00511   if ( (fd = creat("L1", 0666)) < 0) e(44);
00512   if (link("L1", "L3") != -1) e(45);
00513   if (errno != EEXIST) e(46);
00514   errno = 0;
00515   if (link("L1", "L1") != -1) e(47);
00516   if (errno != EEXIST) e(48);
00517   if (unlink("L3") != 0) e(49);
00518 
00519   /* L1 exists at this point. Test creating too many links. */
00520   for (i = 2; i <= LINK_MAX; i++) {
00521         sprintf(name, "Lx%d", i);
00522         if (link("L1", name) != 0) e(50);
00523   }
00524   if (stat("L1", &stbuf) != 0) e(51);
00525   if (stbuf.st_nlink != LINK_MAX) e(52);
00526   if (link("L1", "L2") != -1) e(53);
00527   if (errno != EMLINK) e(54);
00528   for (i = 2; i <= LINK_MAX; i++) {
00529         sprintf(name, "Lx%d", i);
00530         if (unlink(name) != 0) e(55);
00531   }
00532 
00533   if (stat("L1", &stbuf) != 0) e(56);
00534   if (stbuf.st_nlink != 1) e(57);
00535 
00536   /* L1 exists.  Test ENOENT. */
00537   errno = 0;
00538   if (link("xx/L1", "L2") != -1) e(58);
00539   if (errno != ENOENT) e(59);
00540   errno = 0;
00541   if (link("L1", "xx/L2") != -1) e(60);
00542   if (errno != ENOENT) e(61);
00543   errno = 0;
00544   if (link("L4", "L5") != -1) e(62);
00545   if (errno != ENOENT) e(63);
00546   errno = 0;
00547   if (link("", "L5") != -1) e(64);
00548   if (errno != ENOENT) e(65);
00549   errno = 0;
00550   if (link("L1", "") != -1) e(66);
00551   if (errno != ENOENT) e(67);
00552 
00553   /* L1 exists.  Test ENOTDIR. */
00554   errno = 0;
00555   if (link("/dev/tty/x", "L2") != -1) e(68);
00556   if (errno != ENOTDIR) e(69);
00557 
00558   /* L1 exists.  Test EPERM. */
00559   if (link(".", "L2") != -1) e(70);
00560   if (errno != EPERM) e(71);
00561 
00562   /* L1 exists. Test unlink. */
00563   if (link("L1", "Dir/L1") != 0) e(72);
00564   if (chmod("Dir", 0666) != 0) e(73);
00565   if (unlink("Dir/L1") != -1) e(74);
00566   if (errno != EACCES) e(75);
00567   errno = 0;
00568   if (chmod("Dir", 0555) != 0) e(76);
00569   if (unlink("Dir/L1") != -1) e(77);
00570   if (errno != EACCES) e(78);
00571 
00572   if (unlink("L7") != -1) e(79);
00573   if (errno != ENOENT) e(80);
00574   errno = 0;
00575   if (unlink("") != -1) e(81);
00576   if (errno != ENOENT) e(82);
00577 
00578   if (unlink("Dir/L1/L2") != -1) e(83);
00579   if (errno != ENOTDIR) e(84);
00580  
00581   if (chmod("Dir", 0777) != 0) e(85);
00582   if (unlink("Dir/L1") != 0) e(86);
00583   if (unlink("Dir") != -1) e(87);
00584   if (errno != EPERM) e(88);
00585   if (unlink("L1") != 0) e(89);
00586   if (system("rm -rf Dir") != 0) e(90);
00587   if (close(fd) != 0) e(91);  
00588 }
00589 
00590 void test39h()
00591 {
00592 /* Test access. */
00593 
00594   int fd;
00595 
00596   subtest = 10;
00597   system("rm -rf A1");
00598   if ( (fd = creat("A1", 0777)) < 0) e(1);
00599   if (close(fd) != 0) e(2);
00600   if (access("A1", R_OK) != 0) e(3);
00601   if (access("A1", W_OK) != 0) e(4);
00602   if (access("A1", X_OK) != 0) e(5);
00603   if (access("A1", (R_OK|W_OK|X_OK)) != 0) e(6);
00604   
00605   if (chmod("A1", 0400) != 0) e(7);
00606   if (access("A1", R_OK) != 0) e(8);
00607   if (access("A1", W_OK) != -1) e(9);
00608   if (access("A1", X_OK) != -1) e(10);
00609   if (access("A1", (R_OK|W_OK|X_OK)) != -1) e(11);
00610   
00611   if (chmod("A1", 0077) != 0) e(12);
00612   if (access("A1", R_OK) != -1) e(13);
00613   if (access("A1", W_OK) != -1) e(14);
00614   if (access("A1", X_OK) != -1) e(15);
00615   if (access("A1", (R_OK|W_OK|X_OK)) != -1) e(16);
00616   if (errno != EACCES) e(17);
00617 
00618   if (access("", R_OK) != -1) e(18);
00619   if (errno != ENOENT) e(19);
00620   if (access("./A1/x", R_OK) != -1) e(20);
00621   if (errno != ENOTDIR) e(21);
00622 
00623   if (unlink("A1") != 0) e(22);
00624 }
00625 
00626 void test39i()
00627 {
00628 /* Test chmod. */
00629 
00630   int fd, i;
00631   struct stat stbuf;
00632 
00633   subtest = 11;
00634   system("rm -rf A1");
00635   if ( (fd = creat("A1", 0777)) < 0) e(1);
00636 
00637   for (i = 0; i < 511; i++) {
00638         if (chmod("A1", i) != 0) e(100+i);
00639         if (fstat(fd, &stbuf) != 0) e(200+i);
00640         if ( (stbuf.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)) != i) e(300+i);
00641   }
00642   if (close(fd) != 0) e(2);
00643 
00644   if (chmod("A1/x", 0777) != -1) e(3);
00645   if (errno != ENOTDIR) e(4);
00646   if (chmod("Axxx", 0777) != -1) e(5);
00647   if (errno != ENOENT) e(6);
00648   errno = 0;
00649   if (chmod ("", 0777) != -1) e(7);
00650   if (errno != ENOENT) e(8);
00651 
00652   /* Now perform limited chown tests.  These should work even as non su */
00653   i = getuid();
00654 /* DEBUG -- Not yet implemented 
00655   if (chown("A1", i, 0) != 0) e(9);
00656   if (chown("A1", i, 1) != 0) e(10);
00657   if (chown("A1", i, 2) != 0) e(11);
00658   if (chown("A1", i, 3) != 0) e(12);
00659   if (chown("A1", i, 4) != 0) e(13);
00660   if (chown("A1", i, 0) != 0) e(14);
00661 */
00662 
00663   if (unlink("A1") != 0) e(9);
00664 }
00665 
00666 void test39j()
00667 {
00668 /* Test utime. */
00669 
00670   int fd;
00671   time_t tloc;
00672   struct utimbuf times;
00673   struct stat stbuf;
00674 
00675   subtest = 12;
00676   if (system("rm -rf A2") != 0) e(1);
00677   if ( (fd = creat("A2", 0666)) < 0) e(2);
00678   times.modtime = 100;
00679   if (utime("A2", &times) != 0) e(3);
00680   if (stat("A2", &stbuf) != 0) e(4);
00681   if (stbuf.st_mtime != 100) e(5);
00682 
00683   tloc = time((time_t *)NULL);          /* get current time */
00684   times.modtime = tloc;
00685   if (utime("A2", &times) != 0) e(6);
00686   if (stat("A2", &stbuf) != 0) e(7);
00687   if (stbuf.st_mtime != tloc) e(8);
00688   if (close(fd) != 0) e(9);
00689   if (unlink("A2") != 0) e(10);
00690 }
00691 
00692 void e(n)
00693 int n;
00694 {
00695   int err_num = errno;          /* save errno in case printf clobbers it */
00696 
00697   printf("Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
00698   fflush(stdout);               /* stdout and stderr are mixed horribly */
00699   errno = err_num;              /* restore errno, just in case */
00700   perror("");
00701   if (errct++ > MAX_ERROR) {
00702         printf("Too many errors; test aborted\n");
00703         chdir("..");
00704         system("rm -rf DIR*");
00705         exit(1);
00706   }
00707 }
00708 
00709 void quit()
00710 {
00711 
00712   chdir("..");
00713   system("rm -rf DIR*");
00714 
00715   if (errct == 0) {
00716         printf("ok\n");
00717         exit(0);
00718   } else {
00719         printf("%d errors\n", errct);
00720         exit(1);
00721   }
00722 }

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