[9] | 1 | /* POSIX test program (21). Author: Andy Tanenbaum */
|
---|
| 2 |
|
---|
| 3 | /* The following POSIX calls are tested:
|
---|
| 4 | *
|
---|
| 5 | * rename(), mkdir(), rmdir()
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include <sys/types.h>
|
---|
| 9 | #include <sys/stat.h>
|
---|
| 10 | #include <errno.h>
|
---|
| 11 | #include <fcntl.h>
|
---|
| 12 | #include <limits.h>
|
---|
| 13 | #include <stdlib.h>
|
---|
| 14 | #include <string.h>
|
---|
| 15 | #include <unistd.h>
|
---|
| 16 | #include <stdio.h>
|
---|
| 17 |
|
---|
| 18 | #define ITERATIONS 1
|
---|
| 19 | #define MAX_ERROR 4
|
---|
| 20 |
|
---|
| 21 | int subtest, errct;
|
---|
| 22 |
|
---|
| 23 | _PROTOTYPE(int main, (int argc, char *argv []));
|
---|
| 24 | _PROTOTYPE(void test21a, (void));
|
---|
| 25 | _PROTOTYPE(void test21b, (void));
|
---|
| 26 | _PROTOTYPE(void test21c, (void));
|
---|
| 27 | _PROTOTYPE(void test21d, (void));
|
---|
| 28 | _PROTOTYPE(void test21e, (void));
|
---|
| 29 | _PROTOTYPE(void test21f, (void));
|
---|
| 30 | _PROTOTYPE(void test21g, (void));
|
---|
| 31 | _PROTOTYPE(void test21h, (void));
|
---|
| 32 | _PROTOTYPE(void test21i, (void));
|
---|
| 33 | _PROTOTYPE(void test21k, (void));
|
---|
| 34 | _PROTOTYPE(void test21l, (void));
|
---|
| 35 | _PROTOTYPE(void test21m, (void));
|
---|
| 36 | _PROTOTYPE(void test21n, (void));
|
---|
| 37 | _PROTOTYPE(void test21o, (void));
|
---|
| 38 | _PROTOTYPE(int get_link, (char *name));
|
---|
| 39 | _PROTOTYPE(void e, (int n));
|
---|
| 40 | _PROTOTYPE(void quit, (void));
|
---|
| 41 |
|
---|
| 42 | int main(argc, argv)
|
---|
| 43 | int argc;
|
---|
| 44 | char *argv[];
|
---|
| 45 | {
|
---|
| 46 |
|
---|
| 47 | int i, m = 0xFFFF;
|
---|
| 48 |
|
---|
| 49 | sync();
|
---|
| 50 | if (geteuid() == 0 || getuid() == 0) {
|
---|
| 51 | printf("Test 21 cannot run as root; test aborted\n");
|
---|
| 52 | exit(1);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | if (argc == 2) m = atoi(argv[1]);
|
---|
| 56 | printf("Test 21 ");
|
---|
| 57 | fflush(stdout);
|
---|
| 58 |
|
---|
| 59 | system("rm -rf DIR_21; mkdir DIR_21");
|
---|
| 60 | chdir("DIR_21");
|
---|
| 61 |
|
---|
| 62 | for (i = 0; i < ITERATIONS; i++) {
|
---|
| 63 | if (m & 00001) test21a();
|
---|
| 64 | if (m & 00002) test21b();
|
---|
| 65 | if (m & 00004) test21c();
|
---|
| 66 | if (m & 00010) test21d();
|
---|
| 67 | if (m & 00020) test21e();
|
---|
| 68 | if (m & 00040) test21f();
|
---|
| 69 | if (m & 01000) test21g();
|
---|
| 70 | if (m & 00200) test21h();
|
---|
| 71 | if (m & 00400) test21i();
|
---|
| 72 | if (m & 01000) test21k();
|
---|
| 73 | if (m & 02000) test21l();
|
---|
| 74 | if (m & 04000) test21m();
|
---|
| 75 | if (m & 010000) test21n();
|
---|
| 76 | if (m & 020000) test21o();
|
---|
| 77 | }
|
---|
| 78 | quit();
|
---|
| 79 | return(-1); /* impossible */
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void test21a()
|
---|
| 83 | {
|
---|
| 84 | /* Test rename(). */
|
---|
| 85 |
|
---|
| 86 | int fd, fd2;
|
---|
| 87 | char buf[PATH_MAX+1], buf1[PATH_MAX+1], buf2[PATH_MAX+1];
|
---|
| 88 | struct stat stat1, stat2;
|
---|
| 89 |
|
---|
| 90 | subtest = 1;
|
---|
| 91 |
|
---|
| 92 | unlink("A1"); /* get rid of it if it exists */
|
---|
| 93 | unlink("A2"); /* get rid of it if it exists */
|
---|
| 94 | unlink("A3"); /* get rid of it if it exists */
|
---|
| 95 | unlink("A4"); /* get rid of it if it exists */
|
---|
| 96 | unlink("A5"); /* get rid of it if it exists */
|
---|
| 97 | unlink("A6"); /* get rid of it if it exists */
|
---|
| 98 | unlink("A7"); /* get rid of it if it exists */
|
---|
| 99 |
|
---|
| 100 | /* Basic test. Rename A1 to A2 and then A2 to A3. */
|
---|
| 101 | if ( (fd=creat("A1", 0666)) < 0) e(1);
|
---|
| 102 | if (write(fd, buf, 20) != 20) e(2);
|
---|
| 103 | if (close(fd) < 0) e(3);
|
---|
| 104 | if (rename("A1", "A2") < 0) e(4);
|
---|
| 105 | if ( (fd=open("A2", O_RDONLY)) < 0) e(5);
|
---|
| 106 | if (rename("A2", "A3") < 0) e(6);
|
---|
| 107 | if ( (fd2=open("A3", O_RDONLY)) < 0) e(7);
|
---|
| 108 | if (close(fd) != 0) e(8);
|
---|
| 109 | if (close(fd2) != 0) e(9);
|
---|
| 110 | if (unlink("A3") != 0) e(10);
|
---|
| 111 |
|
---|
| 112 | /* Now get the absolute path name of the current directory using getcwd()
|
---|
| 113 | * and use it to test RENAME using different combinations of relative and
|
---|
| 114 | * absolute path names.
|
---|
| 115 | */
|
---|
| 116 | if (getcwd(buf, PATH_MAX) == (char *) NULL) e(11);
|
---|
| 117 | if ( (fd = creat("A4", 0666)) < 0) e(12);
|
---|
| 118 | if (write(fd, buf, 30) != 30) e(13);
|
---|
| 119 | if (close(fd) != 0) e(14);
|
---|
| 120 | strcpy(buf1, buf);
|
---|
| 121 | strcat(buf1, "/A4");
|
---|
| 122 | if (rename(buf1, "A5") != 0) e(15); /* rename(absolute, relative) */
|
---|
| 123 | if (access("A5", 6) != 0) e(16); /* use access to see if file exists */
|
---|
| 124 | strcpy(buf2, buf);
|
---|
| 125 | strcat(buf2, "/A6");
|
---|
| 126 | if (rename("A5", buf2) != 0) e(17); /* rename(relative, absolute) */
|
---|
| 127 | if (access("A6", 6) != 0) e(18);
|
---|
| 128 | if (access(buf2, 6) != 0) e(19);
|
---|
| 129 | strcpy(buf1, buf);
|
---|
| 130 | strcat(buf1, "/A6");
|
---|
| 131 | strcpy(buf2, buf);
|
---|
| 132 | strcat(buf2, "/A7");
|
---|
| 133 | if (rename(buf1, buf2) != 0) e(20); /* rename(absolute, absolute) */
|
---|
| 134 | if (access("A7", 6) != 0) e(21);
|
---|
| 135 | if (access(buf2, 6) != 0) e(22);
|
---|
| 136 |
|
---|
| 137 | /* Try renaming using names like "./A8" */
|
---|
| 138 | if (rename("A7", "./A8") != 0) e(23);
|
---|
| 139 | if (access("A8", 6) != 0) e(24);
|
---|
| 140 | if (rename("./A8", "A9") != 0) e(25);
|
---|
| 141 | if (access("A9", 6) != 0) e(26);
|
---|
| 142 | if (rename("./A9", "./A10") != 0) e(27);
|
---|
| 143 | if (access("A10", 6) != 0) e(28);
|
---|
| 144 | if (access("./A10", 6) != 0) e(29);
|
---|
| 145 | if (unlink("A10") != 0) e(30);
|
---|
| 146 |
|
---|
| 147 | /* Now see if directories can be renamed. */
|
---|
| 148 | if (system("rm -rf ?uzzy scsi") != 0) e(31);
|
---|
| 149 | if (system("mkdir fuzzy") != 0) e(32);
|
---|
| 150 | if (rename("fuzzy", "wuzzy") != 0) e(33);
|
---|
| 151 | if ( (fd=creat("wuzzy/was_a_bear", 0666)) < 0) e(34);
|
---|
| 152 | if (access("wuzzy/was_a_bear", 6) != 0) e(35);
|
---|
| 153 | if (unlink("wuzzy/was_a_bear") != 0) e(36);
|
---|
| 154 | if (close(fd) != 0) e(37);
|
---|
| 155 | if (rename("wuzzy", "buzzy") != 0) e(38);
|
---|
| 156 | if (system("rmdir buzzy") != 0) e(39);
|
---|
| 157 |
|
---|
| 158 | /* Now start testing the case that 'new' exists. */
|
---|
| 159 | if ( (fd = creat("horse", 0666)) < 0) e(40);
|
---|
| 160 | if ( (fd2 = creat("sheep", 0666)) < 0) e(41);
|
---|
| 161 | if (write(fd, buf, PATH_MAX) != PATH_MAX) e(42);
|
---|
| 162 | if (write(fd2, buf, 23) != 23) e(43);
|
---|
| 163 | if (stat("horse", &stat1) != 0) e(44);
|
---|
| 164 | if (rename("horse", "sheep") != 0) e(45);
|
---|
| 165 | if (stat("sheep", &stat2) != 0) e(46);
|
---|
| 166 | if (stat1.st_dev != stat2.st_dev) e(47);
|
---|
| 167 | if (stat1.st_ino != stat2.st_ino) e(48);
|
---|
| 168 | if (stat2.st_size != PATH_MAX) e(49);
|
---|
| 169 | if (access("horse", 6) == 0) e(50);
|
---|
| 170 | if (close(fd) != 0) e(51);
|
---|
| 171 | if (close(fd2) != 0) e(52);
|
---|
| 172 | if (rename("sheep", "sheep") != 0) e(53);
|
---|
| 173 | if (unlink("sheep") != 0) e(54);
|
---|
| 174 |
|
---|
| 175 | /* Now try renaming something to a directory that already exists. */
|
---|
| 176 | if (system("mkdir fuzzy wuzzy") != 0) e(55);
|
---|
| 177 | if ( (fd = creat("fuzzy/was_a_bear", 0666)) < 0) e(56);
|
---|
| 178 | if (close(fd) != 0) e(57);
|
---|
| 179 | if (rename("fuzzy", "wuzzy") != 0) e(58); /* 'new' is empty dir */
|
---|
| 180 | if (system("mkdir scsi") != 0) e(59);
|
---|
| 181 | if (rename("scsi", "wuzzy") == 0) e(60); /* 'new' is full dir */
|
---|
| 182 | if (errno != EEXIST && errno != ENOTEMPTY) e(61);
|
---|
| 183 |
|
---|
| 184 | /* Test 14 character names--always tricky. */
|
---|
| 185 | if (rename("wuzzy/was_a_bear", "wuzzy/was_not_a_bear") != 0) e(62);
|
---|
| 186 | if (access("wuzzy/was_not_a_bear", 6) != 0) e(63);
|
---|
| 187 | if (rename("wuzzy/was_not_a_bear", "wuzzy/was_not_a_duck") != 0) e(64);
|
---|
| 188 | if (access("wuzzy/was_not_a_duck", 6) != 0) e(65);
|
---|
| 189 | if (rename("wuzzy/was_not_a_duck", "wuzzy/was_a_bird") != 0) e(65);
|
---|
| 190 | if (access("wuzzy/was_a_bird", 6) != 0) e(66);
|
---|
| 191 |
|
---|
| 192 | /* Test moves between directories. */
|
---|
| 193 | if (rename("wuzzy/was_a_bird", "beast") != 0) e(67);
|
---|
| 194 | if (access("beast", 6) != 0) e(68);
|
---|
| 195 | if (rename("beast", "wuzzy/was_a_cat") != 0) e(69);
|
---|
| 196 | if (access("wuzzy/was_a_cat", 6) != 0) e(70);
|
---|
| 197 |
|
---|
| 198 | /* Test error conditions. 'scsi' and 'wuzzy/was_a_cat' exist now. */
|
---|
| 199 | if (rename("wuzzy/was_a_cat", "wuzzy/was_a_dog") != 0) e(71);
|
---|
| 200 | if (access("wuzzy/was_a_dog", 6) != 0) e(72);
|
---|
| 201 | if (chmod("wuzzy", 0) != 0) e(73);
|
---|
| 202 |
|
---|
| 203 | errno = 0;
|
---|
| 204 | if (rename("wuzzy/was_a_dog", "wuzzy/was_a_pig") != -1) e(74);
|
---|
| 205 | if (errno != EACCES) e(75);
|
---|
| 206 |
|
---|
| 207 | errno = 0;
|
---|
| 208 | if (rename("wuzzy/was_a_dog", "doggie") != -1) e(76);
|
---|
| 209 | if (errno != EACCES) e(77);
|
---|
| 210 |
|
---|
| 211 | errno = 0;
|
---|
| 212 | if ( (fd = creat("beast", 0666)) < 0) e(78);
|
---|
| 213 | if (close(fd) != 0) e(79);
|
---|
| 214 | if (rename("beast", "wuzzy/was_a_twit") != -1) e(80);
|
---|
| 215 | if (errno != EACCES) e(81);
|
---|
| 216 |
|
---|
| 217 | errno = 0;
|
---|
| 218 | if (rename("beast", "wuzzy") != -1) e(82);
|
---|
| 219 | if (errno != EISDIR) e(83);
|
---|
| 220 |
|
---|
| 221 | errno = 0;
|
---|
| 222 | if (rename("beest", "baste") != -1) e(84);
|
---|
| 223 | if (errno != ENOENT) e(85);
|
---|
| 224 |
|
---|
| 225 | errno = 0;
|
---|
| 226 | if (rename("wuzzy", "beast") != -1) e(86);
|
---|
| 227 | if (errno != ENOTDIR) e(87);
|
---|
| 228 |
|
---|
| 229 | /* Test prefix rule. */
|
---|
| 230 | errno = 0;
|
---|
| 231 | if (chmod("wuzzy", 0777) != 0) e(88);
|
---|
| 232 | if (unlink("wuzzy/was_a_dog") != 0) e(89);
|
---|
| 233 | strcpy(buf1, buf);
|
---|
| 234 | strcat(buf1, "/wuzzy");
|
---|
| 235 | if (rename(buf, buf1) != -1) e(90);
|
---|
| 236 | if (errno != EINVAL) e(91);
|
---|
| 237 |
|
---|
| 238 | if (system("rm -rf wuzzy beast scsi") != 0) e(92);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 | void test21b()
|
---|
| 244 | {
|
---|
| 245 | /* Test mkdir() and rmdir(). */
|
---|
| 246 |
|
---|
| 247 | int i;
|
---|
| 248 | char name[3];
|
---|
| 249 | struct stat statbuf;
|
---|
| 250 |
|
---|
| 251 | subtest = 2;
|
---|
| 252 |
|
---|
| 253 | /* Simple stuff. */
|
---|
| 254 | if (mkdir("D1", 0700) != 0) e(1);
|
---|
| 255 | if (stat("D1", &statbuf) != 0) e(2);
|
---|
| 256 | if (!S_ISDIR(statbuf.st_mode)) e(3);
|
---|
| 257 | if ( (statbuf.st_mode & 0777) != 0700) e(4);
|
---|
| 258 | if (rmdir("D1") != 0) e(5);
|
---|
| 259 |
|
---|
| 260 | /* Make and remove 40 directories. By doing so, the directory has to
|
---|
| 261 | * grow to 2 blocks. That presents plenty of opportunity for bugs.
|
---|
| 262 | */
|
---|
| 263 | name[0] = 'D';
|
---|
| 264 | name[2] = '\0';
|
---|
| 265 | for (i = 0; i < 40; i++) {
|
---|
| 266 | name[1] = 'A' + i;
|
---|
| 267 | if (mkdir(name, 0700 + i%7) != 0) e(10+i); /* for simplicity */
|
---|
| 268 | }
|
---|
| 269 | for (i = 0; i < 40; i++) {
|
---|
| 270 | name[1] = 'A' + i;
|
---|
| 271 | if (rmdir(name) != 0) e(50+i);
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | void test21c()
|
---|
| 276 | {
|
---|
| 277 | /* Test mkdir() and rmdir(). */
|
---|
| 278 |
|
---|
| 279 | subtest = 3;
|
---|
| 280 |
|
---|
| 281 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 282 | if (mkdir("D1/D2", 0777) != 0) e(2);
|
---|
| 283 | if (mkdir("D1/D2/D3", 0777) != 0) e(3);
|
---|
| 284 | if (mkdir("D1/D2/D3/D4", 0777) != 0) e(4);
|
---|
| 285 | if (mkdir("D1/D2/D3/D4/D5", 0777) != 0) e(5);
|
---|
| 286 | if (mkdir("D1/D2/D3/D4/D5/D6", 0777) != 0) e(6);
|
---|
| 287 | if (mkdir("D1/D2/D3/D4/D5/D6/D7", 0777) != 0) e(7);
|
---|
| 288 | if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8", 0777) != 0) e(8);
|
---|
| 289 | if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8/D9", 0777) != 0) e(9);
|
---|
| 290 | if (access("D1/D2/D3/D4/D5/D6/D7/D8/D9", 7) != 0) e(10);
|
---|
| 291 | if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8/D9") != 0) e(11);
|
---|
| 292 | if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8") != 0) e(12);
|
---|
| 293 | if (rmdir("D1/D2/D3/D4/D5/D6/D7") != 0) e(13);
|
---|
| 294 | if (rmdir("D1/D2/D3/D4/D5/D6") != 0) e(11);
|
---|
| 295 | if (rmdir("D1/D2/D3/D4/D5") != 0) e(13);
|
---|
| 296 | if (rmdir("D1/D2/D3/D4") != 0) e(14);
|
---|
| 297 | if (rmdir("D1/D2/D3") != 0) e(15);
|
---|
| 298 | if (rmdir("D1/D2") != 0) e(16);
|
---|
| 299 | if (rmdir("D1") != 0) e(17);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | void test21d()
|
---|
| 303 | {
|
---|
| 304 | /* Test making directories with files and directories in them. */
|
---|
| 305 |
|
---|
| 306 | int fd1, fd2, fd3, fd4, fd5, fd6, fd7, fd8, fd9;
|
---|
| 307 |
|
---|
| 308 | subtest = 4;
|
---|
| 309 |
|
---|
| 310 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 311 | if (mkdir("D1/D2", 0777) != 0) e(2);
|
---|
| 312 | if (mkdir("./D1/D3", 0777) != 0) e(3);
|
---|
| 313 | if (mkdir("././D1/D4", 0777) != 0) e(4);
|
---|
| 314 | if ( (fd1 = creat("D1/D2/x", 0700)) < 0) e(5);
|
---|
| 315 | if ( (fd2 = creat("D1/D2/y", 0700)) < 0) e(6);
|
---|
| 316 | if ( (fd3 = creat("D1/D2/z", 0700)) < 0) e(7);
|
---|
| 317 | if ( (fd4 = creat("D1/D3/x", 0700)) < 0) e(8);
|
---|
| 318 | if ( (fd5 = creat("D1/D3/y", 0700)) < 0) e(9);
|
---|
| 319 | if ( (fd6 = creat("D1/D3/z", 0700)) < 0) e(10);
|
---|
| 320 | if ( (fd7 = creat("D1/D4/x", 0700)) < 0) e(11);
|
---|
| 321 | if ( (fd8 = creat("D1/D4/y", 0700)) < 0) e(12);
|
---|
| 322 | if ( (fd9 = creat("D1/D4/z", 0700)) < 0) e(13);
|
---|
| 323 | if (unlink("D1/D2/z") != 0) e(14);
|
---|
| 324 | if (unlink("D1/D2/y") != 0) e(15);
|
---|
| 325 | if (unlink("D1/D2/x") != 0) e(16);
|
---|
| 326 | if (unlink("D1/D3/x") != 0) e(17);
|
---|
| 327 | if (unlink("D1/D3/z") != 0) e(18);
|
---|
| 328 | if (unlink("D1/D3/y") != 0) e(19);
|
---|
| 329 | if (unlink("D1/D4/y") != 0) e(20);
|
---|
| 330 | if (unlink("D1/D4/z") != 0) e(21);
|
---|
| 331 | if (unlink("D1/D4/x") != 0) e(22);
|
---|
| 332 | if (rmdir("D1/D2") != 0) e(23);
|
---|
| 333 | if (rmdir("D1/D3") != 0) e(24);
|
---|
| 334 | if (rmdir("D1/D4") != 0) e(25);
|
---|
| 335 | if (rmdir("D1") != 0) e(26);
|
---|
| 336 | if (close(fd1) != 0) e(27);
|
---|
| 337 | if (close(fd2) != 0) e(28);
|
---|
| 338 | if (close(fd3) != 0) e(29);
|
---|
| 339 | if (close(fd4) != 0) e(30);
|
---|
| 340 | if (close(fd5) != 0) e(31);
|
---|
| 341 | if (close(fd6) != 0) e(32);
|
---|
| 342 | if (close(fd7) != 0) e(33);
|
---|
| 343 | if (close(fd8) != 0) e(34);
|
---|
| 344 | if (close(fd9) != 0) e(35);
|
---|
| 345 |
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | void test21e()
|
---|
| 349 | {
|
---|
| 350 | /* Test error conditions. */
|
---|
| 351 |
|
---|
| 352 | subtest = 5;
|
---|
| 353 |
|
---|
| 354 | if (mkdir("D1", 0677) != 0) e(1);
|
---|
| 355 | errno = 0;
|
---|
| 356 | if (mkdir("D1/D2", 0777) != -1) e(2);
|
---|
| 357 | if (errno != EACCES) e(3);
|
---|
| 358 | if (chmod ("D1", 0577) != 0) e(4);
|
---|
| 359 | errno = 0;
|
---|
| 360 | if (mkdir("D1/D2", 0777) != -1) e(5);
|
---|
| 361 | if (errno != EACCES) e(6);
|
---|
| 362 | if (chmod ("D1", 0777) != 0) e(7);
|
---|
| 363 | errno = 0;
|
---|
| 364 | if (mkdir("D1", 0777) != -1) e(8);
|
---|
| 365 | if (errno != EEXIST) e(9);
|
---|
| 366 | #if NAME_MAX == 14
|
---|
| 367 | if (mkdir("D1/ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0777) != 0) e(10);
|
---|
| 368 | if (access("D1/ABCDEFGHIJKLMN", 7 ) != 0) e(11);
|
---|
| 369 | if (rmdir("D1/ABCDEFGHIJKLMNOPQ") != 0) e(12);
|
---|
| 370 | if (access("D1/ABCDEFGHIJKLMN", 7 ) != -1) e(13);
|
---|
| 371 | #endif
|
---|
| 372 | errno = 0;
|
---|
| 373 | if (mkdir("D1/D2/x", 0777) != -1) e(14);
|
---|
| 374 | if (errno != ENOENT) e(15);
|
---|
| 375 |
|
---|
| 376 | /* A particularly nasty test is when the parent has mode 0. Although
|
---|
| 377 | * this is unlikely to work, it had better not muck up the file system
|
---|
| 378 | */
|
---|
| 379 | if (mkdir("D1/D2", 0777) != 0) e(16);
|
---|
| 380 | if (chmod("D1", 0) != 0) e(17);
|
---|
| 381 | errno = 0;
|
---|
| 382 | if (rmdir("D1/D2") != -1) e(18);
|
---|
| 383 | if (errno != EACCES) e(19);
|
---|
| 384 | if (chmod("D1", 0777) != 0) e(20);
|
---|
| 385 | if (rmdir("D1/D2") != 0) e(21);
|
---|
| 386 | if (rmdir("D1") != 0) e(22);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | void test21f()
|
---|
| 390 | {
|
---|
| 391 | /* The rename() function affects the link count of all the files and
|
---|
| 392 | * directories it goes near. Test to make sure it gets everything ok.
|
---|
| 393 | * There are four cases:
|
---|
| 394 | *
|
---|
| 395 | * 1. rename("d1/file1", "d1/file2"); - rename file without moving it
|
---|
| 396 | * 2. rename("d1/file1", "d2/file2"); - move a file to another dir
|
---|
| 397 | * 3. rename("d1/dir1", "d2/dir2"); - rename a dir without moving it
|
---|
| 398 | * 4. rename("d1/dir1", "d2/dir2"); - move a dir to another dir
|
---|
| 399 | *
|
---|
| 400 | * Furthermore, a distinction has to be made when the target file exists
|
---|
| 401 | * and when it does not exist, giving 8 cases in all.
|
---|
| 402 | */
|
---|
| 403 |
|
---|
| 404 | int fd, D1_before, D1_after, x_link, y_link;
|
---|
| 405 |
|
---|
| 406 | /* Test case 1: renaming a file within the same directory. */
|
---|
| 407 | subtest = 6;
|
---|
| 408 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 409 | if ( (fd = creat("D1/x", 0777)) < 0) e(2);
|
---|
| 410 | if (close(fd) != 0) e(3);
|
---|
| 411 | D1_before = get_link("D1");
|
---|
| 412 | x_link = get_link("D1/x");
|
---|
| 413 | if (rename("D1/x", "D1/y") != 0) e(4);
|
---|
| 414 | y_link = get_link("D1/y");
|
---|
| 415 | D1_after = get_link("D1");
|
---|
| 416 | if (D1_before != 2) e(5);
|
---|
| 417 | if (D1_after != 2) e(6);
|
---|
| 418 | if (x_link != 1) e(7);
|
---|
| 419 | if (y_link != 1) e(8);
|
---|
| 420 | if (access("D1/y", 7) != 0) e(9);
|
---|
| 421 | if (unlink("D1/y") != 0) e(10);
|
---|
| 422 | if (rmdir("D1") != 0) e(11);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | void test21g()
|
---|
| 426 | {
|
---|
| 427 | int fd, D1_before, D1_after, D2_before, D2_after, x_link, y_link;
|
---|
| 428 |
|
---|
| 429 | /* Test case 2: move a file to a new directory. */
|
---|
| 430 | subtest = 7;
|
---|
| 431 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 432 | if (mkdir("D2", 0777) != 0) e(2);
|
---|
| 433 | if ( (fd = creat("D1/x", 0777)) < 0) e(3);
|
---|
| 434 | if (close(fd) != 0) e(4);
|
---|
| 435 | D1_before = get_link("D1");
|
---|
| 436 | D2_before = get_link("D2");
|
---|
| 437 | x_link = get_link("D1/x");
|
---|
| 438 | if (rename("D1/x", "D2/y") != 0) e(5);
|
---|
| 439 | y_link = get_link("D2/y");
|
---|
| 440 | D1_after = get_link("D1");
|
---|
| 441 | D2_after = get_link("D2");
|
---|
| 442 | if (D1_before != 2) e(6);
|
---|
| 443 | if (D2_before != 2) e(7);
|
---|
| 444 | if (D1_after != 2) e(8);
|
---|
| 445 | if (D2_after != 2) e(9);
|
---|
| 446 | if (x_link != 1) e(10);
|
---|
| 447 | if (y_link != 1) e(11);
|
---|
| 448 | if (access("D2/y", 7) != 0) e(12);
|
---|
| 449 | if (unlink("D2/y") != 0) e(13);
|
---|
| 450 | if (rmdir("D1") != 0) e(14);
|
---|
| 451 | if (rmdir("D2") != 0) e(15);
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | void test21h()
|
---|
| 455 | {
|
---|
| 456 | int D1_before, D1_after, x_link, y_link;
|
---|
| 457 |
|
---|
| 458 | /* Test case 3: renaming a directory within the same directory. */
|
---|
| 459 | subtest = 8;
|
---|
| 460 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 461 | if (mkdir("D1/X", 0777) != 0) e(2);
|
---|
| 462 | D1_before = get_link("D1");
|
---|
| 463 | x_link = get_link("D1/X");
|
---|
| 464 | if (rename("D1/X", "D1/Y") != 0) e(3);
|
---|
| 465 | y_link = get_link("D1/Y");
|
---|
| 466 | D1_after = get_link("D1");
|
---|
| 467 | if (D1_before != 3) e(4);
|
---|
| 468 | if (D1_after != 3) e(5);
|
---|
| 469 | if (x_link != 2) e(6);
|
---|
| 470 | if (y_link != 2) e(7);
|
---|
| 471 | if (access("D1/Y", 7) != 0) e(8);
|
---|
| 472 | if (rmdir("D1/Y") != 0) e(9);
|
---|
| 473 | if (get_link("D1") != 2) e(10);
|
---|
| 474 | if (rmdir("D1") != 0) e(11);
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | void test21i()
|
---|
| 478 | {
|
---|
| 479 | int D1_before, D1_after, D2_before, D2_after, x_link, y_link;
|
---|
| 480 |
|
---|
| 481 | /* Test case 4: move a directory to a new directory. */
|
---|
| 482 | subtest = 9;
|
---|
| 483 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 484 | if (mkdir("D2", 0777) != 0) e(2);
|
---|
| 485 | if (mkdir("D1/X", 0777) != 0) e(3);
|
---|
| 486 | D1_before = get_link("D1");
|
---|
| 487 | D2_before = get_link("D2");
|
---|
| 488 | x_link = get_link("D1/X");
|
---|
| 489 | if (rename("D1/X", "D2/Y") != 0) e(4);
|
---|
| 490 | y_link = get_link("D2/Y");
|
---|
| 491 | D1_after = get_link("D1");
|
---|
| 492 | D2_after = get_link("D2");
|
---|
| 493 | if (D1_before != 3) e(5);
|
---|
| 494 | if (D2_before != 2) e(6);
|
---|
| 495 | if (D1_after != 2) e(7);
|
---|
| 496 | if (D2_after != 3) e(8);
|
---|
| 497 | if (x_link != 2) e(9);
|
---|
| 498 | if (y_link != 2) e(10);
|
---|
| 499 | if (access("D2/Y", 7) != 0) e(11);
|
---|
| 500 | if (rename("D2/Y", "D1/Z") != 0) e(12);
|
---|
| 501 | if (get_link("D1") != 3) e(13);
|
---|
| 502 | if (get_link("D2") != 2) e(14);
|
---|
| 503 | if (rmdir("D1/Z") != 0) e(15);
|
---|
| 504 | if (get_link("D1") != 2) e(16);
|
---|
| 505 | if (rmdir("D1") != 0) e(17);
|
---|
| 506 | if (rmdir("D2") != 0) e(18);
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | void test21k()
|
---|
| 510 | {
|
---|
| 511 | /* Now test the same 4 cases, except when the target exists. */
|
---|
| 512 |
|
---|
| 513 | int fd, D1_before, D1_after, x_link, y_link;
|
---|
| 514 |
|
---|
| 515 | /* Test case 5: renaming a file within the same directory. */
|
---|
| 516 | subtest = 10;
|
---|
| 517 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 518 | if ( (fd = creat("D1/x", 0777)) < 0) e(2);
|
---|
| 519 | if (close(fd) != 0) e(3);
|
---|
| 520 | if ( (fd = creat("D1/y", 0777)) < 0) e(3);
|
---|
| 521 | if (close(fd) != 0) e(4);
|
---|
| 522 | D1_before = get_link("D1");
|
---|
| 523 | x_link = get_link("D1/x");
|
---|
| 524 | if (rename("D1/x", "D1/y") != 0) e(5);
|
---|
| 525 | y_link = get_link("D1/y");
|
---|
| 526 | D1_after = get_link("D1");
|
---|
| 527 | if (D1_before != 2) e(6);
|
---|
| 528 | if (D1_after != 2) e(7);
|
---|
| 529 | if (x_link != 1) e(8);
|
---|
| 530 | if (y_link != 1) e(9);
|
---|
| 531 | if (access("D1/y", 7) != 0) e(10);
|
---|
| 532 | if (unlink("D1/y") != 0) e(11);
|
---|
| 533 | if (rmdir("D1") != 0) e(12);
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | void test21l()
|
---|
| 537 | {
|
---|
| 538 | int fd, D1_before, D1_after, D2_before, D2_after, x_link, y_link;
|
---|
| 539 |
|
---|
| 540 | /* Test case 6: move a file to a new directory. */
|
---|
| 541 | subtest = 11;
|
---|
| 542 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 543 | if (mkdir("D2", 0777) != 0) e(2);
|
---|
| 544 | if ( (fd = creat("D1/x", 0777)) < 0) e(3);
|
---|
| 545 | if (close(fd) != 0) e(4);
|
---|
| 546 | if ( (fd = creat("D2/y", 0777)) < 0) e(5);
|
---|
| 547 | if (close(fd) != 0) e(6);
|
---|
| 548 | D1_before = get_link("D1");
|
---|
| 549 | D2_before = get_link("D2");
|
---|
| 550 | x_link = get_link("D1/x");
|
---|
| 551 | if (rename("D1/x", "D2/y") != 0) e(7);
|
---|
| 552 | y_link = get_link("D2/y");
|
---|
| 553 | D1_after = get_link("D1");
|
---|
| 554 | D2_after = get_link("D2");
|
---|
| 555 | if (D1_before != 2) e(8);
|
---|
| 556 | if (D2_before != 2) e(9);
|
---|
| 557 | if (D1_after != 2) e(10);
|
---|
| 558 | if (D2_after != 2) e(11);
|
---|
| 559 | if (x_link != 1) e(12);
|
---|
| 560 | if (y_link != 1) e(13);
|
---|
| 561 | if (access("D2/y", 7) != 0) e(14);
|
---|
| 562 | if (unlink("D2/y") != 0) e(15);
|
---|
| 563 | if (rmdir("D1") != 0) e(16);
|
---|
| 564 | if (rmdir("D2") != 0) e(17);
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | void test21m()
|
---|
| 568 | {
|
---|
| 569 | int D1_before, D1_after, x_link, y_link;
|
---|
| 570 |
|
---|
| 571 | /* Test case 7: renaming a directory within the same directory. */
|
---|
| 572 | subtest = 12;
|
---|
| 573 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 574 | if (mkdir("D1/X", 0777) != 0) e(2);
|
---|
| 575 | if (mkdir("D1/Y", 0777) != 0) e(3);
|
---|
| 576 | D1_before = get_link("D1");
|
---|
| 577 | x_link = get_link("D1/X");
|
---|
| 578 | if (rename("D1/X", "D1/Y") != 0) e(4);
|
---|
| 579 | y_link = get_link("D1/Y");
|
---|
| 580 | D1_after = get_link("D1");
|
---|
| 581 | if (D1_before != 4) e(5);
|
---|
| 582 | if (D1_after != 3) e(6);
|
---|
| 583 | if (x_link != 2) e(7);
|
---|
| 584 | if (y_link != 2) e(8);
|
---|
| 585 | if (access("D1/Y", 7) != 0) e(9);
|
---|
| 586 | if (rmdir("D1/Y") != 0) e(10);
|
---|
| 587 | if (get_link("D1") != 2) e(11);
|
---|
| 588 | if (rmdir("D1") != 0) e(12);
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | void test21n()
|
---|
| 592 | {
|
---|
| 593 | int D1_before, D1_after, D2_before, D2_after, x_link, y_link;
|
---|
| 594 |
|
---|
| 595 | /* Test case 8: move a directory to a new directory. */
|
---|
| 596 | subtest = 13;
|
---|
| 597 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 598 | if (mkdir("D2", 0777) != 0) e(2);
|
---|
| 599 | if (mkdir("D1/X", 0777) != 0) e(3);
|
---|
| 600 | if (mkdir("D2/Y", 0777) != 0) e(4);
|
---|
| 601 | D1_before = get_link("D1");
|
---|
| 602 | D2_before = get_link("D2");
|
---|
| 603 | x_link = get_link("D1/X");
|
---|
| 604 | if (rename("D1/X", "D2/Y") != 0) e(5);
|
---|
| 605 | y_link = get_link("D2/Y");
|
---|
| 606 | D1_after = get_link("D1");
|
---|
| 607 | D2_after = get_link("D2");
|
---|
| 608 | if (D1_before != 3) e(6);
|
---|
| 609 | if (D2_before != 3) e(7);
|
---|
| 610 | if (D1_after != 2) e(8);
|
---|
| 611 | if (D2_after != 3) e(9);
|
---|
| 612 | if (x_link != 2) e(10);
|
---|
| 613 | if (y_link != 2) e(11);
|
---|
| 614 | if (access("D2/Y", 7) != 0) e(12);
|
---|
| 615 | if (rename("D2/Y", "D1/Z") != 0) e(13);
|
---|
| 616 | if (get_link("D1") != 3) e(14);
|
---|
| 617 | if (get_link("D2") != 2) e(15);
|
---|
| 618 | if (rmdir("D1/Z") != 0) e(16);
|
---|
| 619 | if (get_link("D1") != 2) e(17);
|
---|
| 620 | if (rmdir("D1") != 0) e(18);
|
---|
| 621 | if (rmdir("D2") != 0) e(19);
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | void test21o()
|
---|
| 625 | {
|
---|
| 626 | /* Test trying to remove . and .. */
|
---|
| 627 | subtest = 14;
|
---|
| 628 | if (mkdir("D1", 0777) != 0) e(1);
|
---|
| 629 | if (chdir("D1") != 0) e(2);
|
---|
| 630 | if (rmdir(".") == 0) e(3);
|
---|
| 631 | if (rmdir("..") == 0) e(4);
|
---|
| 632 | if (mkdir("D2", 0777) != 0) e(5);
|
---|
| 633 | if (mkdir("D3", 0777) != 0) e(6);
|
---|
| 634 | if (mkdir("D4", 0777) != 0) e(7);
|
---|
| 635 | if (rmdir("D2/../D3/../D4") != 0) e(8); /* legal way to remove D4 */
|
---|
| 636 | if (rmdir("D2/../D3/../D2/..") == 0) e(9); /* removing self is illegal */
|
---|
| 637 | if (rmdir("D2/../D3/../D2/../..") == 0) e(10);/* removing parent is illegal*/
|
---|
| 638 | if (rmdir("../D1/../D1/D3") != 0) e(11); /* legal way to remove D3 */
|
---|
| 639 | if (rmdir("./D2/../D2") != 0) e(12); /* legal way to remove D2 */
|
---|
| 640 | if (chdir("..") != 0) e(13);
|
---|
| 641 | if (rmdir("D1") != 0) e(14);
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | int get_link(name)
|
---|
| 645 | char *name;
|
---|
| 646 | {
|
---|
| 647 | struct stat statbuf;
|
---|
| 648 |
|
---|
| 649 | if (stat(name, &statbuf) != 0) {
|
---|
| 650 | printf("Unable to stat %s\n", name);
|
---|
| 651 | errct++;
|
---|
| 652 | return(-1);
|
---|
| 653 | }
|
---|
| 654 | return(statbuf.st_nlink);
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | void e(n)
|
---|
| 658 | int n;
|
---|
| 659 | {
|
---|
| 660 | int err_num = errno; /* save errno in case printf clobbers it */
|
---|
| 661 |
|
---|
| 662 | printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
|
---|
| 663 | errno = err_num; /* restore errno, just in case */
|
---|
| 664 | perror("");
|
---|
| 665 | if (errct++ > MAX_ERROR) {
|
---|
| 666 | printf("Too many errors; test aborted\n");
|
---|
| 667 | chdir("..");
|
---|
| 668 | system("rm -rf DIR*");
|
---|
| 669 | exit(1);
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | void quit()
|
---|
| 674 | {
|
---|
| 675 | chdir("..");
|
---|
| 676 | system("rm -rf DIR*");
|
---|
| 677 |
|
---|
| 678 | if (errct == 0) {
|
---|
| 679 | printf("ok\n");
|
---|
| 680 | exit(0);
|
---|
| 681 | } else {
|
---|
| 682 | printf("%d errors\n", errct);
|
---|
| 683 | exit(1);
|
---|
| 684 | }
|
---|
| 685 | }
|
---|