test21.c

Go to the documentation of this file.
00001 /* POSIX test program (21).                     Author: Andy Tanenbaum */
00002 
00003 /* The following POSIX calls are tested:
00004  *
00005  *      rename(),  mkdir(),  rmdir()
00006  */
00007 
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <errno.h>
00011 #include <fcntl.h>
00012 #include <limits.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <unistd.h>
00016 #include <stdio.h>
00017 
00018 #define ITERATIONS        1
00019 #define MAX_ERROR 4
00020 
00021 int subtest, errct;
00022 
00023 _PROTOTYPE(int main, (int argc, char *argv []));
00024 _PROTOTYPE(void test21a, (void));
00025 _PROTOTYPE(void test21b, (void));
00026 _PROTOTYPE(void test21c, (void));
00027 _PROTOTYPE(void test21d, (void));
00028 _PROTOTYPE(void test21e, (void));
00029 _PROTOTYPE(void test21f, (void));
00030 _PROTOTYPE(void test21g, (void));
00031 _PROTOTYPE(void test21h, (void));
00032 _PROTOTYPE(void test21i, (void));
00033 _PROTOTYPE(void test21k, (void));
00034 _PROTOTYPE(void test21l, (void));
00035 _PROTOTYPE(void test21m, (void));
00036 _PROTOTYPE(void test21n, (void));
00037 _PROTOTYPE(void test21o, (void));
00038 _PROTOTYPE(int get_link, (char *name));
00039 _PROTOTYPE(void e, (int n));
00040 _PROTOTYPE(void quit, (void));
00041 
00042 int main(argc, argv)
00043 int argc;
00044 char *argv[];
00045 {
00046 
00047   int i, m = 0xFFFF;
00048 
00049   sync();
00050   if (geteuid() == 0 || getuid() == 0) {
00051         printf("Test 21 cannot run as root; test aborted\n");
00052         exit(1);
00053   }
00054 
00055   if (argc == 2) m = atoi(argv[1]);
00056   printf("Test 21 ");
00057   fflush(stdout);
00058 
00059   system("rm -rf DIR_21; mkdir DIR_21");
00060   chdir("DIR_21");
00061 
00062   for (i = 0; i < ITERATIONS; i++) {
00063         if (m & 00001) test21a();
00064         if (m & 00002) test21b();
00065         if (m & 00004) test21c();
00066         if (m & 00010) test21d();
00067         if (m & 00020) test21e();
00068         if (m & 00040) test21f();
00069         if (m & 01000) test21g();
00070         if (m & 00200) test21h();
00071         if (m & 00400) test21i();
00072         if (m & 01000) test21k();
00073         if (m & 02000) test21l();
00074         if (m & 04000) test21m();
00075         if (m & 010000) test21n();
00076         if (m & 020000) test21o();
00077   }
00078   quit();
00079   return(-1);                   /* impossible */
00080 }
00081 
00082 void test21a()
00083 {
00084 /* Test rename(). */
00085 
00086   int fd, fd2;
00087   char buf[PATH_MAX+1], buf1[PATH_MAX+1], buf2[PATH_MAX+1];
00088   struct stat stat1, stat2;
00089 
00090   subtest = 1;
00091 
00092   unlink("A1");                 /* get rid of it if it exists */
00093   unlink("A2");                 /* get rid of it if it exists */
00094   unlink("A3");                 /* get rid of it if it exists */
00095   unlink("A4");                 /* get rid of it if it exists */
00096   unlink("A5");                 /* get rid of it if it exists */
00097   unlink("A6");                 /* get rid of it if it exists */
00098   unlink("A7");                 /* get rid of it if it exists */
00099 
00100   /* Basic test.  Rename A1 to A2 and then A2 to A3. */
00101   if ( (fd=creat("A1", 0666)) < 0) e(1);
00102   if (write(fd, buf, 20) != 20) e(2);
00103   if (close(fd) < 0) e(3);
00104   if (rename("A1", "A2") < 0) e(4);
00105   if ( (fd=open("A2", O_RDONLY)) < 0) e(5);
00106   if (rename("A2", "A3") < 0) e(6);
00107   if ( (fd2=open("A3", O_RDONLY)) < 0) e(7);
00108   if (close(fd) != 0) e(8);
00109   if (close(fd2) != 0) e(9);
00110   if (unlink("A3") != 0) e(10);
00111 
00112   /* Now get the absolute path name of the current directory using getcwd()
00113    * and use it to test RENAME using different combinations of relative and
00114    * absolute path names.
00115    */
00116   if (getcwd(buf, PATH_MAX) == (char *) NULL) e(11);
00117   if ( (fd = creat("A4", 0666)) < 0) e(12);
00118   if (write(fd, buf, 30) != 30) e(13);
00119   if (close(fd) != 0) e(14);
00120   strcpy(buf1, buf);
00121   strcat(buf1, "/A4");
00122   if (rename(buf1, "A5") != 0) e(15);   /* rename(absolute, relative) */
00123   if (access("A5", 6) != 0) e(16);      /* use access to see if file exists */
00124   strcpy(buf2, buf);
00125   strcat(buf2, "/A6");
00126   if (rename("A5", buf2) != 0) e(17);   /* rename(relative, absolute) */
00127   if (access("A6", 6) != 0) e(18);
00128   if (access(buf2, 6) != 0) e(19);
00129   strcpy(buf1, buf);
00130   strcat(buf1, "/A6");
00131   strcpy(buf2, buf);
00132   strcat(buf2, "/A7");
00133   if (rename(buf1, buf2) != 0) e(20);   /* rename(absolute, absolute) */
00134   if (access("A7", 6) != 0) e(21);
00135   if (access(buf2, 6) != 0) e(22);
00136 
00137   /* Try renaming using names like "./A8" */
00138   if (rename("A7", "./A8") != 0) e(23);
00139   if (access("A8", 6) != 0) e(24);
00140   if (rename("./A8", "A9") != 0) e(25);
00141   if (access("A9", 6) != 0) e(26);
00142   if (rename("./A9", "./A10") != 0) e(27);
00143   if (access("A10", 6) != 0) e(28);
00144   if (access("./A10", 6) != 0) e(29);
00145   if (unlink("A10") != 0) e(30);
00146 
00147   /* Now see if directories can be renamed. */
00148   if (system("rm -rf ?uzzy scsi") != 0) e(31);
00149   if (system("mkdir fuzzy") != 0) e(32);
00150   if (rename("fuzzy", "wuzzy") != 0) e(33);
00151   if ( (fd=creat("wuzzy/was_a_bear", 0666)) < 0) e(34);
00152   if (access("wuzzy/was_a_bear", 6) != 0) e(35);
00153   if (unlink("wuzzy/was_a_bear") != 0) e(36);
00154   if (close(fd) != 0) e(37);
00155   if (rename("wuzzy", "buzzy") != 0) e(38);
00156   if (system("rmdir buzzy") != 0) e(39);
00157 
00158   /* Now start testing the case that 'new' exists. */
00159   if ( (fd = creat("horse", 0666)) < 0) e(40);
00160   if ( (fd2 = creat("sheep", 0666)) < 0) e(41);
00161   if (write(fd, buf, PATH_MAX) != PATH_MAX) e(42);
00162   if (write(fd2, buf, 23) != 23) e(43);
00163   if (stat("horse", &stat1) != 0) e(44);
00164   if (rename("horse", "sheep") != 0) e(45);
00165   if (stat("sheep", &stat2) != 0) e(46);
00166   if (stat1.st_dev != stat2.st_dev) e(47);
00167   if (stat1.st_ino != stat2.st_ino) e(48);
00168   if (stat2.st_size != PATH_MAX) e(49);
00169   if (access("horse", 6) == 0) e(50);
00170   if (close(fd) != 0) e(51);
00171   if (close(fd2) != 0) e(52);
00172   if (rename("sheep", "sheep") != 0) e(53);
00173   if (unlink("sheep") != 0) e(54);
00174 
00175   /* Now try renaming something to a directory that already exists. */
00176   if (system("mkdir fuzzy wuzzy") != 0) e(55);
00177   if ( (fd = creat("fuzzy/was_a_bear", 0666)) < 0) e(56);
00178   if (close(fd) != 0) e(57);
00179   if (rename("fuzzy", "wuzzy") != 0) e(58);     /* 'new' is empty dir */
00180   if (system("mkdir scsi") != 0) e(59);
00181   if (rename("scsi", "wuzzy") == 0) e(60);      /* 'new' is full dir */
00182   if (errno != EEXIST && errno != ENOTEMPTY) e(61);
00183 
00184   /* Test 14 character names--always tricky. */
00185   if (rename("wuzzy/was_a_bear", "wuzzy/was_not_a_bear") != 0) e(62);
00186   if (access("wuzzy/was_not_a_bear", 6) != 0) e(63);
00187   if (rename("wuzzy/was_not_a_bear", "wuzzy/was_not_a_duck") != 0) e(64);
00188   if (access("wuzzy/was_not_a_duck", 6) != 0) e(65);
00189   if (rename("wuzzy/was_not_a_duck", "wuzzy/was_a_bird") != 0) e(65);
00190   if (access("wuzzy/was_a_bird", 6) != 0) e(66);
00191 
00192   /* Test moves between directories. */
00193   if (rename("wuzzy/was_a_bird", "beast") != 0) e(67);
00194   if (access("beast", 6) != 0) e(68);
00195   if (rename("beast", "wuzzy/was_a_cat") != 0) e(69);
00196   if (access("wuzzy/was_a_cat", 6) != 0) e(70);
00197 
00198   /* Test error conditions. 'scsi' and 'wuzzy/was_a_cat' exist now. */
00199   if (rename("wuzzy/was_a_cat", "wuzzy/was_a_dog") != 0) e(71);
00200   if (access("wuzzy/was_a_dog", 6) != 0) e(72);
00201   if (chmod("wuzzy", 0) != 0) e(73);
00202 
00203   errno = 0;
00204   if (rename("wuzzy/was_a_dog", "wuzzy/was_a_pig") != -1) e(74);
00205   if (errno != EACCES) e(75);
00206 
00207   errno = 0;
00208   if (rename("wuzzy/was_a_dog", "doggie") != -1) e(76);
00209   if (errno != EACCES) e(77);
00210 
00211   errno = 0;
00212   if ( (fd = creat("beast", 0666)) < 0) e(78);
00213   if (close(fd) != 0) e(79);
00214   if (rename("beast", "wuzzy/was_a_twit") != -1) e(80);
00215   if (errno != EACCES) e(81);
00216 
00217   errno = 0;
00218   if (rename("beast", "wuzzy") != -1) e(82);
00219   if (errno != EISDIR) e(83);
00220 
00221   errno = 0;
00222   if (rename("beest", "baste") != -1) e(84);
00223   if (errno != ENOENT) e(85);
00224 
00225   errno = 0;
00226   if (rename("wuzzy", "beast") != -1) e(86);
00227   if (errno != ENOTDIR) e(87);
00228 
00229   /* Test prefix rule. */
00230   errno = 0;
00231   if (chmod("wuzzy", 0777) != 0) e(88);
00232   if (unlink("wuzzy/was_a_dog") != 0) e(89);
00233   strcpy(buf1, buf);
00234   strcat(buf1, "/wuzzy");
00235   if (rename(buf, buf1) != -1) e(90);
00236   if (errno != EINVAL) e(91);
00237 
00238   if (system("rm -rf wuzzy beast scsi") != 0) e(92);
00239 }
00240 
00241   
00242 
00243 void test21b()
00244 {
00245 /* Test mkdir() and rmdir(). */
00246 
00247   int i;
00248   char name[3];
00249   struct stat statbuf;
00250 
00251   subtest = 2;
00252 
00253   /* Simple stuff. */
00254   if (mkdir("D1", 0700) != 0) e(1);
00255   if (stat("D1", &statbuf) != 0) e(2);
00256   if (!S_ISDIR(statbuf.st_mode)) e(3);
00257   if ( (statbuf.st_mode & 0777) != 0700) e(4);
00258   if (rmdir("D1") != 0) e(5);
00259 
00260   /* Make and remove 40 directories.  By doing so, the directory has to
00261    * grow to 2 blocks.  That presents plenty of opportunity for bugs.
00262    */
00263   name[0] = 'D';
00264   name[2] = '\0';
00265   for (i = 0; i < 40; i++) {
00266         name[1] = 'A' + i;
00267         if (mkdir(name, 0700 + i%7) != 0) e(10+i);      /* for simplicity */
00268   }
00269   for (i = 0; i < 40; i++) {
00270         name[1] = 'A' + i;
00271         if (rmdir(name) != 0) e(50+i);
00272   }
00273 }
00274 
00275 void test21c()
00276 {
00277 /* Test mkdir() and rmdir(). */
00278 
00279   subtest = 3;
00280 
00281   if (mkdir("D1", 0777) != 0) e(1);
00282   if (mkdir("D1/D2", 0777) != 0) e(2);
00283   if (mkdir("D1/D2/D3", 0777) != 0) e(3);
00284   if (mkdir("D1/D2/D3/D4", 0777) != 0) e(4);
00285   if (mkdir("D1/D2/D3/D4/D5", 0777) != 0) e(5);
00286   if (mkdir("D1/D2/D3/D4/D5/D6", 0777) != 0) e(6);
00287   if (mkdir("D1/D2/D3/D4/D5/D6/D7", 0777) != 0) e(7);
00288   if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8", 0777) != 0) e(8);
00289   if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8/D9", 0777) != 0) e(9);
00290   if (access("D1/D2/D3/D4/D5/D6/D7/D8/D9", 7) != 0) e(10);
00291   if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8/D9") != 0) e(11);
00292   if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8") != 0) e(12);
00293   if (rmdir("D1/D2/D3/D4/D5/D6/D7") != 0) e(13);
00294   if (rmdir("D1/D2/D3/D4/D5/D6") != 0) e(11);
00295   if (rmdir("D1/D2/D3/D4/D5") != 0) e(13);
00296   if (rmdir("D1/D2/D3/D4") != 0) e(14);
00297   if (rmdir("D1/D2/D3") != 0) e(15);
00298   if (rmdir("D1/D2") != 0) e(16);
00299   if (rmdir("D1") != 0) e(17);
00300 }
00301   
00302 void test21d()
00303 {
00304 /* Test making directories with files and directories in them. */
00305 
00306   int fd1, fd2, fd3, fd4, fd5, fd6, fd7, fd8, fd9;
00307 
00308   subtest = 4;
00309   
00310   if (mkdir("D1", 0777) != 0) e(1);
00311   if (mkdir("D1/D2", 0777) != 0) e(2);
00312   if (mkdir("./D1/D3", 0777) != 0) e(3);
00313   if (mkdir("././D1/D4", 0777) != 0) e(4);
00314   if ( (fd1 = creat("D1/D2/x", 0700)) < 0) e(5);
00315   if ( (fd2 = creat("D1/D2/y", 0700)) < 0) e(6);
00316   if ( (fd3 = creat("D1/D2/z", 0700)) < 0) e(7);
00317   if ( (fd4 = creat("D1/D3/x", 0700)) < 0) e(8);
00318   if ( (fd5 = creat("D1/D3/y", 0700)) < 0) e(9);
00319   if ( (fd6 = creat("D1/D3/z", 0700)) < 0) e(10);
00320   if ( (fd7 = creat("D1/D4/x", 0700)) < 0) e(11);
00321   if ( (fd8 = creat("D1/D4/y", 0700)) < 0) e(12);
00322   if ( (fd9 = creat("D1/D4/z", 0700)) < 0) e(13);
00323   if (unlink("D1/D2/z") != 0) e(14);
00324   if (unlink("D1/D2/y") != 0) e(15);
00325   if (unlink("D1/D2/x") != 0) e(16);
00326   if (unlink("D1/D3/x") != 0) e(17);
00327   if (unlink("D1/D3/z") != 0) e(18);
00328   if (unlink("D1/D3/y") != 0) e(19);
00329   if (unlink("D1/D4/y") != 0) e(20);
00330   if (unlink("D1/D4/z") != 0) e(21);
00331   if (unlink("D1/D4/x") != 0) e(22);
00332   if (rmdir("D1/D2") != 0) e(23);
00333   if (rmdir("D1/D3") != 0) e(24);
00334   if (rmdir("D1/D4") != 0) e(25);
00335   if (rmdir("D1") != 0) e(26);
00336   if (close(fd1) != 0) e(27);
00337   if (close(fd2) != 0) e(28);
00338   if (close(fd3) != 0) e(29);
00339   if (close(fd4) != 0) e(30);
00340   if (close(fd5) != 0) e(31);
00341   if (close(fd6) != 0) e(32);
00342   if (close(fd7) != 0) e(33);
00343   if (close(fd8) != 0) e(34);
00344   if (close(fd9) != 0) e(35);
00345 
00346 }
00347 
00348 void test21e()
00349 {
00350 /* Test error conditions. */
00351   
00352   subtest = 5;
00353 
00354   if (mkdir("D1", 0677) != 0) e(1);
00355   errno = 0;
00356   if (mkdir("D1/D2", 0777) != -1) e(2);
00357   if (errno != EACCES) e(3);
00358   if (chmod ("D1", 0577) != 0) e(4);
00359   errno = 0;
00360   if (mkdir("D1/D2", 0777) != -1) e(5);
00361   if (errno != EACCES) e(6);
00362   if (chmod ("D1", 0777) != 0) e(7);
00363   errno = 0;
00364   if (mkdir("D1", 0777) != -1) e(8);
00365   if (errno != EEXIST) e(9);
00366 #if NAME_MAX == 14
00367   if (mkdir("D1/ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0777) != 0) e(10);
00368   if (access("D1/ABCDEFGHIJKLMN", 7 ) != 0) e(11);
00369   if (rmdir("D1/ABCDEFGHIJKLMNOPQ") != 0) e(12);
00370   if (access("D1/ABCDEFGHIJKLMN", 7 ) != -1) e(13);
00371 #endif
00372   errno = 0;
00373   if (mkdir("D1/D2/x", 0777) != -1) e(14);
00374   if (errno != ENOENT) e(15);
00375 
00376   /* A particularly nasty test is when the parent has mode 0.  Although
00377    * this is unlikely to work, it had better not muck up the file system
00378    */
00379   if (mkdir("D1/D2", 0777) != 0) e(16);
00380   if (chmod("D1", 0) != 0) e(17);
00381   errno = 0;
00382   if (rmdir("D1/D2") != -1) e(18);
00383   if (errno != EACCES) e(19);
00384   if (chmod("D1", 0777) != 0) e(20);
00385   if (rmdir("D1/D2") != 0) e(21);
00386   if (rmdir("D1") != 0) e(22);
00387 }
00388 
00389 void test21f()
00390 {
00391 /* The rename() function affects the link count of all the files and
00392  * directories it goes near.  Test to make sure it gets everything ok.
00393  * There are four cases:
00394  *
00395  *   1. rename("d1/file1", "d1/file2"); - rename file without moving it
00396  *   2. rename("d1/file1", "d2/file2"); - move a file to another dir
00397  *   3. rename("d1/dir1", "d2/dir2");   - rename a dir without moving it
00398  *   4. rename("d1/dir1", "d2/dir2");   - move a dir to another dir
00399  *
00400  * Furthermore, a distinction has to be made when the target file exists
00401  * and when it does not exist, giving 8 cases in all.
00402  */
00403 
00404   int fd, D1_before, D1_after, x_link, y_link;
00405 
00406   /* Test case 1: renaming a file within the same directory. */
00407   subtest = 6;
00408   if (mkdir("D1", 0777) != 0) e(1);
00409   if ( (fd = creat("D1/x", 0777)) < 0) e(2);
00410   if (close(fd) != 0) e(3);
00411   D1_before = get_link("D1");
00412   x_link = get_link("D1/x");
00413   if (rename("D1/x", "D1/y") != 0) e(4);
00414   y_link = get_link("D1/y");
00415   D1_after = get_link("D1");
00416   if (D1_before != 2) e(5);
00417   if (D1_after != 2) e(6);
00418   if (x_link != 1) e(7);
00419   if (y_link != 1) e(8);
00420   if (access("D1/y", 7) != 0) e(9);
00421   if (unlink("D1/y") != 0) e(10);
00422   if (rmdir("D1") != 0) e(11);
00423 }
00424 
00425 void test21g()
00426 {
00427   int fd, D1_before, D1_after, D2_before, D2_after, x_link, y_link;
00428 
00429   /* Test case 2: move a file to a new directory. */
00430   subtest = 7;
00431   if (mkdir("D1", 0777) != 0) e(1);
00432   if (mkdir("D2", 0777) != 0) e(2);
00433   if ( (fd = creat("D1/x", 0777)) < 0) e(3);
00434   if (close(fd) != 0) e(4);
00435   D1_before = get_link("D1");
00436   D2_before = get_link("D2");
00437   x_link = get_link("D1/x");
00438   if (rename("D1/x", "D2/y") != 0) e(5);
00439   y_link = get_link("D2/y");
00440   D1_after = get_link("D1");
00441   D2_after = get_link("D2");
00442   if (D1_before != 2) e(6);
00443   if (D2_before != 2) e(7);
00444   if (D1_after != 2) e(8);
00445   if (D2_after != 2) e(9);
00446   if (x_link != 1) e(10);
00447   if (y_link != 1) e(11);
00448   if (access("D2/y", 7) != 0) e(12);
00449   if (unlink("D2/y") != 0) e(13);
00450   if (rmdir("D1") != 0) e(14);
00451   if (rmdir("D2") != 0) e(15);
00452 }
00453 
00454 void test21h()
00455 {
00456   int D1_before, D1_after, x_link, y_link;
00457 
00458   /* Test case 3: renaming a directory within the same directory. */
00459   subtest = 8;
00460   if (mkdir("D1", 0777) != 0) e(1);
00461   if (mkdir("D1/X", 0777) != 0) e(2);
00462   D1_before = get_link("D1");
00463   x_link = get_link("D1/X");
00464   if (rename("D1/X", "D1/Y") != 0) e(3);
00465   y_link = get_link("D1/Y");
00466   D1_after = get_link("D1");
00467   if (D1_before != 3) e(4);
00468   if (D1_after != 3) e(5);
00469   if (x_link != 2) e(6);
00470   if (y_link != 2) e(7);
00471   if (access("D1/Y", 7) != 0) e(8);
00472   if (rmdir("D1/Y") != 0) e(9);
00473   if (get_link("D1") != 2) e(10);
00474   if (rmdir("D1") != 0) e(11);
00475 }
00476 
00477 void test21i()
00478 {
00479   int D1_before, D1_after, D2_before, D2_after, x_link, y_link;
00480 
00481   /* Test case 4: move a directory to a new directory. */
00482   subtest = 9;
00483   if (mkdir("D1", 0777) != 0) e(1);
00484   if (mkdir("D2", 0777) != 0) e(2);
00485   if (mkdir("D1/X", 0777) != 0) e(3);
00486   D1_before = get_link("D1");
00487   D2_before = get_link("D2");
00488   x_link = get_link("D1/X");
00489   if (rename("D1/X", "D2/Y") != 0) e(4);
00490   y_link = get_link("D2/Y");
00491   D1_after = get_link("D1");
00492   D2_after = get_link("D2");
00493   if (D1_before != 3) e(5);
00494   if (D2_before != 2) e(6);
00495   if (D1_after != 2) e(7);
00496   if (D2_after != 3) e(8);
00497   if (x_link != 2) e(9);
00498   if (y_link != 2) e(10);
00499   if (access("D2/Y", 7) != 0) e(11);
00500   if (rename("D2/Y", "D1/Z") != 0) e(12);
00501   if (get_link("D1") != 3) e(13);
00502   if (get_link("D2") != 2) e(14);
00503   if (rmdir("D1/Z") != 0) e(15);
00504   if (get_link("D1") != 2) e(16);
00505   if (rmdir("D1") != 0) e(17);
00506   if (rmdir("D2") != 0) e(18);
00507 }
00508 
00509 void test21k()
00510 {
00511 /* Now test the same 4 cases, except when the target exists. */
00512 
00513   int fd, D1_before, D1_after, x_link, y_link;
00514 
00515   /* Test case 5: renaming a file within the same directory. */
00516   subtest = 10;
00517   if (mkdir("D1", 0777) != 0) e(1);
00518   if ( (fd = creat("D1/x", 0777)) < 0) e(2);
00519   if (close(fd) != 0) e(3);
00520   if ( (fd = creat("D1/y", 0777)) < 0) e(3);
00521   if (close(fd) != 0) e(4);
00522   D1_before = get_link("D1");
00523   x_link = get_link("D1/x");
00524   if (rename("D1/x", "D1/y") != 0) e(5);
00525   y_link = get_link("D1/y");
00526   D1_after = get_link("D1");
00527   if (D1_before != 2) e(6);
00528   if (D1_after != 2) e(7);
00529   if (x_link != 1) e(8);
00530   if (y_link != 1) e(9);
00531   if (access("D1/y", 7) != 0) e(10);
00532   if (unlink("D1/y") != 0) e(11);
00533   if (rmdir("D1") != 0) e(12);
00534 }
00535 
00536 void test21l()
00537 {
00538   int fd, D1_before, D1_after, D2_before, D2_after, x_link, y_link;
00539 
00540   /* Test case 6: move a file to a new directory. */
00541   subtest = 11;
00542   if (mkdir("D1", 0777) != 0) e(1);
00543   if (mkdir("D2", 0777) != 0) e(2);
00544   if ( (fd = creat("D1/x", 0777)) < 0) e(3);
00545   if (close(fd) != 0) e(4);
00546   if ( (fd = creat("D2/y", 0777)) < 0) e(5);
00547   if (close(fd) != 0) e(6);
00548   D1_before = get_link("D1");
00549   D2_before = get_link("D2");
00550   x_link = get_link("D1/x");
00551   if (rename("D1/x", "D2/y") != 0) e(7);
00552   y_link = get_link("D2/y");
00553   D1_after = get_link("D1");
00554   D2_after = get_link("D2");
00555   if (D1_before != 2) e(8);
00556   if (D2_before != 2) e(9);
00557   if (D1_after != 2) e(10);
00558   if (D2_after != 2) e(11);
00559   if (x_link != 1) e(12);
00560   if (y_link != 1) e(13);
00561   if (access("D2/y", 7) != 0) e(14);
00562   if (unlink("D2/y") != 0) e(15);
00563   if (rmdir("D1") != 0) e(16);
00564   if (rmdir("D2") != 0) e(17);
00565 }
00566 
00567 void test21m()
00568 {
00569   int D1_before, D1_after, x_link, y_link;
00570 
00571   /* Test case 7: renaming a directory within the same directory. */
00572   subtest = 12;
00573   if (mkdir("D1", 0777) != 0) e(1);
00574   if (mkdir("D1/X", 0777) != 0) e(2);
00575   if (mkdir("D1/Y", 0777) != 0) e(3);
00576   D1_before = get_link("D1");
00577   x_link = get_link("D1/X");
00578   if (rename("D1/X", "D1/Y") != 0) e(4);
00579   y_link = get_link("D1/Y");
00580   D1_after = get_link("D1");
00581   if (D1_before != 4) e(5);
00582   if (D1_after != 3) e(6);
00583   if (x_link != 2) e(7);
00584   if (y_link != 2) e(8);
00585   if (access("D1/Y", 7) != 0) e(9);
00586   if (rmdir("D1/Y") != 0) e(10);
00587   if (get_link("D1") != 2) e(11);
00588   if (rmdir("D1") != 0) e(12);
00589 }
00590 
00591 void test21n()
00592 {
00593   int D1_before, D1_after, D2_before, D2_after, x_link, y_link;
00594 
00595   /* Test case 8: move a directory to a new directory. */
00596   subtest = 13;
00597   if (mkdir("D1", 0777) != 0) e(1);
00598   if (mkdir("D2", 0777) != 0) e(2);
00599   if (mkdir("D1/X", 0777) != 0) e(3);
00600   if (mkdir("D2/Y", 0777) != 0) e(4);
00601   D1_before = get_link("D1");
00602   D2_before = get_link("D2");
00603   x_link = get_link("D1/X");
00604   if (rename("D1/X", "D2/Y") != 0) e(5);
00605   y_link = get_link("D2/Y");
00606   D1_after = get_link("D1");
00607   D2_after = get_link("D2");
00608   if (D1_before != 3) e(6);
00609   if (D2_before != 3) e(7);
00610   if (D1_after != 2) e(8);
00611   if (D2_after != 3) e(9);
00612   if (x_link != 2) e(10);
00613   if (y_link != 2) e(11);
00614   if (access("D2/Y", 7) != 0) e(12);
00615   if (rename("D2/Y", "D1/Z") != 0) e(13);
00616   if (get_link("D1") != 3) e(14);
00617   if (get_link("D2") != 2) e(15);
00618   if (rmdir("D1/Z") != 0) e(16);
00619   if (get_link("D1") != 2) e(17);
00620   if (rmdir("D1") != 0) e(18);
00621   if (rmdir("D2") != 0) e(19);
00622 }
00623 
00624 void test21o()
00625 {
00626   /* Test trying to remove . and .. */
00627   subtest = 14;
00628   if (mkdir("D1", 0777) != 0) e(1);
00629   if (chdir("D1") != 0) e(2);
00630   if (rmdir(".") == 0) e(3);
00631   if (rmdir("..") == 0) e(4);
00632   if (mkdir("D2", 0777) != 0) e(5);
00633   if (mkdir("D3", 0777) != 0) e(6);
00634   if (mkdir("D4", 0777) != 0) e(7);
00635   if (rmdir("D2/../D3/../D4") != 0) e(8);       /* legal way to remove D4 */
00636   if (rmdir("D2/../D3/../D2/..") == 0) e(9);    /* removing self is illegal */
00637   if (rmdir("D2/../D3/../D2/../..") == 0) e(10);/* removing parent is illegal*/
00638   if (rmdir("../D1/../D1/D3") != 0) e(11);      /* legal way to remove D3 */
00639   if (rmdir("./D2/../D2") != 0) e(12);          /* legal way to remove D2 */
00640   if (chdir("..") != 0) e(13);
00641   if (rmdir("D1") != 0) e(14);
00642 }
00643 
00644 int get_link(name)
00645 char *name;
00646 {
00647   struct stat statbuf;
00648 
00649   if (stat(name, &statbuf) != 0) {
00650         printf("Unable to stat %s\n", name);
00651         errct++;
00652         return(-1);
00653   }
00654   return(statbuf.st_nlink);
00655 }
00656 
00657 void e(n)
00658 int n;
00659 {
00660   int err_num = errno;          /* save errno in case printf clobbers it */
00661 
00662   printf("Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
00663   errno = err_num;              /* restore errno, just in case */
00664   perror("");
00665   if (errct++ > MAX_ERROR) {
00666         printf("Too many errors; test aborted\n");
00667         chdir("..");
00668         system("rm -rf DIR*");
00669         exit(1);
00670   }
00671 }
00672 
00673 void quit()
00674 {
00675   chdir("..");
00676   system("rm -rf DIR*");
00677 
00678   if (errct == 0) {
00679         printf("ok\n");
00680         exit(0);
00681   } else {
00682         printf("%d errors\n", errct);
00683         exit(1);
00684   }
00685 }

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