[9] | 1 | /* POSIX test program (39). Author: Andy Tanenbaum */
|
---|
| 2 |
|
---|
| 3 | /* The following POSIX calls are tested:
|
---|
| 4 | *
|
---|
| 5 | * opendir()
|
---|
| 6 | * readdir()
|
---|
| 7 | * rewinddir()
|
---|
| 8 | * closedir()
|
---|
| 9 | * chdir()
|
---|
| 10 | * getcwd()
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #include <sys/types.h>
|
---|
| 14 | #include <sys/stat.h>
|
---|
| 15 | #include <dirent.h>
|
---|
| 16 | #include <fcntl.h>
|
---|
| 17 | #include <errno.h>
|
---|
| 18 | #include <limits.h>
|
---|
| 19 | #include <stdlib.h>
|
---|
| 20 | #include <string.h>
|
---|
| 21 | #include <time.h>
|
---|
| 22 | #include <unistd.h>
|
---|
| 23 | #include <utime.h>
|
---|
| 24 | #include <stdio.h>
|
---|
| 25 |
|
---|
| 26 | #define DIR_NULL (DIR*) NULL
|
---|
| 27 | #define ITERATIONS 3 /* LINK_MAX is high, so time consuming */
|
---|
| 28 | #define MAX_FD 100 /* must be large enough to cause error */
|
---|
| 29 | #define BUF_SIZE PATH_MAX+20
|
---|
| 30 | #define ERR_CODE -1 /* error return */
|
---|
| 31 | #define RD_BUF 200
|
---|
| 32 | #define MAX_ERROR 4
|
---|
| 33 |
|
---|
| 34 | char str[] = {"The time has come the walrus said to talk of many things.\n"};
|
---|
| 35 | char str2[] = {"Of ships and shoes and sealing wax, of cabbages and kings.\n"};
|
---|
| 36 | char str3[] = {"Of why the sea is boiling hot and whether pigs have wings\n"};
|
---|
| 37 |
|
---|
| 38 | int subtest, errct;
|
---|
| 39 |
|
---|
| 40 | _PROTOTYPE(int main, (int argc, char *argv []));
|
---|
| 41 | _PROTOTYPE(void test39a, (void));
|
---|
| 42 | _PROTOTYPE(void checkdir, (DIR *dirp, int t));
|
---|
| 43 | _PROTOTYPE(void test39b, (void));
|
---|
| 44 | _PROTOTYPE(void test39c, (void));
|
---|
| 45 | _PROTOTYPE(void test39d, (void));
|
---|
| 46 | _PROTOTYPE(void test39e, (void));
|
---|
| 47 | _PROTOTYPE(void test39f, (void));
|
---|
| 48 | _PROTOTYPE(void test39g, (void));
|
---|
| 49 | _PROTOTYPE(void test39h, (void));
|
---|
| 50 | _PROTOTYPE(void test39i, (void));
|
---|
| 51 | _PROTOTYPE(void test39j, (void));
|
---|
| 52 | _PROTOTYPE(void e, (int n));
|
---|
| 53 | _PROTOTYPE(void quit, (void));
|
---|
| 54 |
|
---|
| 55 | int main(argc, argv)
|
---|
| 56 | int argc;
|
---|
| 57 | char *argv[];
|
---|
| 58 | {
|
---|
| 59 |
|
---|
| 60 | int i, m = 0xFFFF;
|
---|
| 61 |
|
---|
| 62 | sync();
|
---|
| 63 | if (geteuid() == 0 || getuid() == 0) {
|
---|
| 64 | printf("Test 39 cannot run as root; test aborted\n");
|
---|
| 65 | exit(1);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | if (argc == 2) m = atoi(argv[1]);
|
---|
| 69 | printf("Test 39 ");
|
---|
| 70 | fflush(stdout);
|
---|
| 71 |
|
---|
| 72 | system("rm -rf DIR_39; mkdir DIR_39");
|
---|
| 73 | chdir("DIR_39");
|
---|
| 74 |
|
---|
| 75 | for (i = 0; i < ITERATIONS; i++) {
|
---|
| 76 | if (m & 00001) test39a(); /* test for correct operation */
|
---|
| 77 | if (m & 00002) test39b(); /* test general error handling */
|
---|
| 78 | if (m & 00004) test39c(); /* test for EMFILE error */
|
---|
| 79 | if (m & 00010) test39d(); /* test chdir() and getcwd() */
|
---|
| 80 | if (m & 00020) test39e(); /* test open() */
|
---|
| 81 | if (m & 00040) test39f(); /* test umask(), stat(), fstat() */
|
---|
| 82 | if (m & 00100) test39g(); /* test link() and unlink() */
|
---|
| 83 | if (m & 00200) test39h(); /* test access() */
|
---|
| 84 | if (m & 00400) test39i(); /* test chmod() and chown() */
|
---|
| 85 | if (m & 01000) test39j(); /* test utime() */
|
---|
| 86 | }
|
---|
| 87 | quit();
|
---|
| 88 | return(-1); /* impossible */
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | void test39a()
|
---|
| 92 | {
|
---|
| 93 | /* Subtest 1. Correct operation */
|
---|
| 94 |
|
---|
| 95 | int f1, f2, f3, f4, f5;
|
---|
| 96 | DIR *dirp;
|
---|
| 97 |
|
---|
| 98 | /* Remove any residue of previous tests. */
|
---|
| 99 | subtest = 1;
|
---|
| 100 |
|
---|
| 101 | system("rm -rf foo");
|
---|
| 102 |
|
---|
| 103 | /* Create a directory foo with 5 files in it. */
|
---|
| 104 | mkdir("foo", 0777);
|
---|
| 105 | if ((f1 = creat("foo/f1", 0666)) < 0) e(1);
|
---|
| 106 | if ((f2 = creat("foo/f2", 0666)) < 0) e(2);
|
---|
| 107 | if ((f3 = creat("foo/f3", 0666)) < 0) e(3);
|
---|
| 108 | if ((f4 = creat("foo/f4", 0666)) < 0) e(4);
|
---|
| 109 | if ((f5 = creat("foo/f5", 0666)) < 0) e(5);
|
---|
| 110 |
|
---|
| 111 | /* Now remove 2 files to create holes in the directory. */
|
---|
| 112 | if (unlink("foo/f2") < 0) e(6);
|
---|
| 113 | if (unlink("foo/f4") < 0) e(7);
|
---|
| 114 |
|
---|
| 115 | /* Close the files. */
|
---|
| 116 | close(f1);
|
---|
| 117 | close(f2);
|
---|
| 118 | close(f3);
|
---|
| 119 | close(f4);
|
---|
| 120 | close(f5);
|
---|
| 121 |
|
---|
| 122 | /* Open the directory. */
|
---|
| 123 | dirp = opendir("./foo");
|
---|
| 124 | if (dirp == DIR_NULL) e(6);
|
---|
| 125 |
|
---|
| 126 | /* Read the 5 files from it. */
|
---|
| 127 | checkdir(dirp, 2);
|
---|
| 128 |
|
---|
| 129 | /* Rewind dir and test again. */
|
---|
| 130 | rewinddir(dirp);
|
---|
| 131 | checkdir(dirp, 3);
|
---|
| 132 |
|
---|
| 133 | /* We're done. Close the directory stream. */
|
---|
| 134 | if (closedir(dirp) < 0) e(7);
|
---|
| 135 |
|
---|
| 136 | /* Remove dir for next time. */
|
---|
| 137 | system("rm -rf foo");
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void checkdir(dirp, t)
|
---|
| 141 | DIR *dirp; /* poinrter to directory stream */
|
---|
| 142 | int t; /* subtest number to use */
|
---|
| 143 | {
|
---|
| 144 |
|
---|
| 145 | int i, f1, f2, f3, f4, f5, dot, dotdot, subt;
|
---|
| 146 | struct dirent *d;
|
---|
| 147 | char *s;
|
---|
| 148 |
|
---|
| 149 | /* Save subtest number */
|
---|
| 150 | subt = subtest;
|
---|
| 151 | subtest = t;
|
---|
| 152 |
|
---|
| 153 | /* Clear the counters. */
|
---|
| 154 | f1 = 0;
|
---|
| 155 | f2 = 0;
|
---|
| 156 | f3 = 0;
|
---|
| 157 | f4 = 0;
|
---|
| 158 | f5 = 0;
|
---|
| 159 | dot = 0;
|
---|
| 160 | dotdot = 0;
|
---|
| 161 |
|
---|
| 162 | /* Read the directory. It should contain 5 entries, ".", ".." and 3
|
---|
| 163 | * files. */
|
---|
| 164 | for (i = 0; i < 5; i++) {
|
---|
| 165 | d = readdir(dirp);
|
---|
| 166 | if (d == (struct dirent *) NULL) {
|
---|
| 167 | e(1);
|
---|
| 168 | subtest = subt; /* restore subtest number */
|
---|
| 169 | return;
|
---|
| 170 | }
|
---|
| 171 | s = d->d_name;
|
---|
| 172 | if (strcmp(s, ".") == 0) dot++;
|
---|
| 173 | if (strcmp(s, "..") == 0) dotdot++;
|
---|
| 174 | if (strcmp(s, "f1") == 0) f1++;
|
---|
| 175 | if (strcmp(s, "f2") == 0) f2++;
|
---|
| 176 | if (strcmp(s, "f3") == 0) f3++;
|
---|
| 177 | if (strcmp(s, "f4") == 0) f4++;
|
---|
| 178 | if (strcmp(s, "f5") == 0) f5++;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /* Check results. */
|
---|
| 182 | d = readdir(dirp);
|
---|
| 183 | if (d != (struct dirent *) NULL) e(2);
|
---|
| 184 | if (f1 != 1 || f3 != 1 || f5 != 1) e(3);
|
---|
| 185 | if (f2 != 0 || f4 != 0) e(4);
|
---|
| 186 | if (dot != 1 || dotdot != 1) e(5);
|
---|
| 187 | subtest = subt;
|
---|
| 188 | return;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | void test39b()
|
---|
| 192 | {
|
---|
| 193 | /* Subtest 4. Test error handling. */
|
---|
| 194 |
|
---|
| 195 | int fd;
|
---|
| 196 | DIR *dirp;
|
---|
| 197 |
|
---|
| 198 | subtest = 4;
|
---|
| 199 |
|
---|
| 200 | if (opendir("foo/xyz/---") != DIR_NULL) e(1);
|
---|
| 201 | if (errno != ENOENT) e(2);
|
---|
| 202 | if (mkdir("foo", 0777) < 0) e(3);
|
---|
| 203 | if (chmod("foo", 0) < 0) e(4);
|
---|
| 204 | if (opendir("foo/xyz/--") != DIR_NULL) e(5);
|
---|
| 205 | if (errno != EACCES) e(6);
|
---|
| 206 | if (chmod("foo", 0777) != 0) e(7);
|
---|
| 207 | if (rmdir("foo") != 0) e(8);
|
---|
| 208 | if ((fd = creat("abc", 0666)) < 0) e(9);
|
---|
| 209 | if (close(fd) < 0) e(10);
|
---|
| 210 | if (opendir("abc/xyz") != DIR_NULL) e(11);
|
---|
| 211 | if (errno != ENOTDIR) e(12);
|
---|
| 212 | if ((dirp = opendir(".")) == DIR_NULL) e(13);
|
---|
| 213 | if (closedir(dirp) != 0) e(14);
|
---|
| 214 | if (unlink("abc") != 0) e(15);
|
---|
| 215 |
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | void test39c()
|
---|
| 219 | {
|
---|
| 220 | /* Subtest 5. See what happens if we open too many directory streams. */
|
---|
| 221 |
|
---|
| 222 | int i, j;
|
---|
| 223 | DIR *dirp[MAX_FD];
|
---|
| 224 |
|
---|
| 225 | subtest = 5;
|
---|
| 226 |
|
---|
| 227 | for (i = 0; i < MAX_FD; i++) {
|
---|
| 228 | dirp[i] = opendir(".");
|
---|
| 229 | if (dirp[i] == (DIR *) NULL) {
|
---|
| 230 | /* We have hit the limit. */
|
---|
| 231 | if (errno != EMFILE && errno != ENOMEM) e(1);
|
---|
| 232 | for (j = 0; j < i; j++) {
|
---|
| 233 | if (closedir(dirp[j]) != 0) e(2); /* close */
|
---|
| 234 | }
|
---|
| 235 | return;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /* Control should never come here. This is an error. */
|
---|
| 240 | e(3);
|
---|
| 241 | for (i = 0; i < MAX_FD; i++) closedir(dirp[i]); /* don't check */
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | void test39d()
|
---|
| 245 | {
|
---|
| 246 | /* Test chdir and getcwd(). */
|
---|
| 247 |
|
---|
| 248 | int fd;
|
---|
| 249 | char *s;
|
---|
| 250 | char base[BUF_SIZE], buf2[BUF_SIZE], tmp[BUF_SIZE];
|
---|
| 251 |
|
---|
| 252 | subtest = 6;
|
---|
| 253 |
|
---|
| 254 | if (getcwd(base, BUF_SIZE) == (char *) NULL) e(1); /* get test dir's path */
|
---|
| 255 | if (system("rm -rf Dir") != 0) e(2); /* remove residue of previous test */
|
---|
| 256 | if (mkdir("Dir", 0777) < 0) e(3); /* create directory called "Dir" */
|
---|
| 257 |
|
---|
| 258 | /* Change to Dir and verify that it worked. */
|
---|
| 259 | if (chdir("Dir") < 0) e(4); /* go to Dir */
|
---|
| 260 | s = getcwd(buf2, BUF_SIZE); /* get full path of Dir */
|
---|
| 261 | if (s == (char *) NULL) e(5); /* check for error return */
|
---|
| 262 | if (s != buf2) e(6); /* if successful, first arg is returned */
|
---|
| 263 | strcpy(tmp, base); /* concatenate base name and "/Dir" */
|
---|
| 264 | strcat(tmp, "/");
|
---|
| 265 | strcat(tmp, "Dir");
|
---|
| 266 | if (strcmp(tmp, s) != 0) e(7);
|
---|
| 267 |
|
---|
| 268 | /* Change to ".." and verify that it worked. */
|
---|
| 269 | if (chdir("..") < 0) e(8);
|
---|
| 270 | if (getcwd(buf2, BUF_SIZE) != buf2) e(9);
|
---|
| 271 | if (strcmp(buf2, base) != 0) e(10);
|
---|
| 272 |
|
---|
| 273 | /* Now make calls that do nothing, but do it in a strange way. */
|
---|
| 274 | if (chdir("Dir/..") < 0) e(11);
|
---|
| 275 | if (getcwd(buf2, BUF_SIZE) != buf2) e(12);
|
---|
| 276 | if (strcmp(buf2, base) != 0) e(13);
|
---|
| 277 |
|
---|
| 278 | if (chdir("Dir/../Dir/..") < 0) e(14);
|
---|
| 279 | if (getcwd(buf2, BUF_SIZE) != buf2) e(15);
|
---|
| 280 | if (strcmp(buf2, base) != 0) e(16);
|
---|
| 281 |
|
---|
| 282 | if (chdir("Dir/../Dir/../Dir/../Dir/../Dir/../Dir/../Dir/..") < 0) e(17);
|
---|
| 283 | if (getcwd(buf2, BUF_SIZE) != buf2) e(18);
|
---|
| 284 | if (strcmp(buf2, base) != 0) e(19);
|
---|
| 285 |
|
---|
| 286 | /* Make Dir unreadable and unsearchable. Check error message. */
|
---|
| 287 | if (chmod("Dir", 0) < 0) e(20);
|
---|
| 288 | if (chdir("Dir") >= 0) e(21);
|
---|
| 289 | if (errno != EACCES) e(22);
|
---|
| 290 |
|
---|
| 291 | /* Check error message for bad path. */
|
---|
| 292 | if (chmod("Dir", 0777) < 0) e(23);
|
---|
| 293 | if (chdir("Dir/x/y") != ERR_CODE) e(24);
|
---|
| 294 | if (errno != ENOENT) e(25);
|
---|
| 295 |
|
---|
| 296 | if ( (fd=creat("Dir/x", 0777)) < 0) e(26);
|
---|
| 297 | if (close(fd) != 0) e(27);
|
---|
| 298 | if (chdir("Dir/x/y") != ERR_CODE) e(28);
|
---|
| 299 | if (errno != ENOTDIR) e(29);
|
---|
| 300 |
|
---|
| 301 | /* Check empty string. */
|
---|
| 302 | if (chdir("") != ERR_CODE) e(30);
|
---|
| 303 | if (errno != ENOENT) e(31);
|
---|
| 304 |
|
---|
| 305 | /* Remove the directory. */
|
---|
| 306 | if (unlink("Dir/x") != 0) e(32);
|
---|
| 307 | if (system("rmdir Dir") != 0) e(33);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | void test39e()
|
---|
| 311 | {
|
---|
| 312 | /* Test open. */
|
---|
| 313 |
|
---|
| 314 | int fd, bytes, bytes2;
|
---|
| 315 | char buf[RD_BUF];
|
---|
| 316 |
|
---|
| 317 | subtest = 7;
|
---|
| 318 |
|
---|
| 319 | unlink("T39"); /* get rid of it in case it exists */
|
---|
| 320 |
|
---|
| 321 | /* Create a test file. */
|
---|
| 322 | bytes = strlen(str);
|
---|
| 323 | bytes2 = strlen(str2);
|
---|
| 324 | if ((fd = creat("T39", 0777)) < 0) e(1);
|
---|
| 325 | if (write(fd, str, bytes) != bytes) e(2); /* T39 now has 'bytes' bytes */
|
---|
| 326 | if (close(fd) != 0) e(3);
|
---|
| 327 |
|
---|
| 328 | /* Test opening a file with O_RDONLY. */
|
---|
| 329 | if ((fd = open("T39", O_RDONLY)) < 0) e(4);
|
---|
| 330 | buf[0] = '\0';
|
---|
| 331 | if (read(fd, buf, RD_BUF) != bytes) e(5);
|
---|
| 332 | if (strncmp(buf, str, bytes) != 0) e(6);
|
---|
| 333 | if (close(fd) < 0) e(7);
|
---|
| 334 |
|
---|
| 335 | /* Test the same thing, only with O_RDWR now. */
|
---|
| 336 | if ((fd = open("T39", O_RDWR)) < 0) e(8);
|
---|
| 337 | buf[0] = '\0';
|
---|
| 338 | if (read(fd, buf, RD_BUF) != bytes) e(9);
|
---|
| 339 | if (strncmp(buf, str, bytes) != 0) e(10);
|
---|
| 340 | if (close(fd) < 0) e(11);
|
---|
| 341 |
|
---|
| 342 | /* Try opening and reading with O_WRONLY. It should fail. */
|
---|
| 343 | if ((fd = open("T39", O_WRONLY)) < 0) e(12);
|
---|
| 344 | buf[0] = '\0';
|
---|
| 345 | if (read(fd, buf, RD_BUF) >= 0) e(13);
|
---|
| 346 | if (close(fd) != 0) e(14);
|
---|
| 347 |
|
---|
| 348 | /* Test O_APPEND. */
|
---|
| 349 | if ((fd = open("T39", O_RDWR | O_APPEND)) < 0) e(15);
|
---|
| 350 | if (lseek(fd, 0L, SEEK_SET) < 0) e(16); /* go to start of file */
|
---|
| 351 | if ( write(fd, str2, bytes2) != bytes2) e(17); /* write at start of file */
|
---|
| 352 | if (lseek(fd, 0L, SEEK_SET) < 0) e(18); /* go back to start again */
|
---|
| 353 | if (read(fd, buf, RD_BUF) != bytes + bytes2) e(19); /* read whole file */
|
---|
| 354 | if (strncmp(buf, str, bytes) != 0) e(20);
|
---|
| 355 | if (close(fd) != 0) e(21);
|
---|
| 356 |
|
---|
| 357 | /* Get rid of the file. */
|
---|
| 358 | if (unlink("T39") < 0) e(22);
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | void test39f()
|
---|
| 362 | {
|
---|
| 363 | /* Test stat, fstat, umask. */
|
---|
| 364 | int i, fd;
|
---|
| 365 | mode_t m1;
|
---|
| 366 | struct stat stbuf1, stbuf2;
|
---|
| 367 | time_t t, t1;
|
---|
| 368 |
|
---|
| 369 | subtest = 8;
|
---|
| 370 |
|
---|
| 371 | m1 = umask(~0777);
|
---|
| 372 | if (system("rm -rf foo xxx") != 0) e(1);
|
---|
| 373 | if ((fd = creat("foo", 0777)) < 0) e(2);
|
---|
| 374 | if (stat("foo", &stbuf1) < 0) e(3);
|
---|
| 375 | if (fstat(fd, &stbuf2) < 0) e(4);
|
---|
| 376 | if (stbuf1.st_mode != stbuf2.st_mode) e(5);
|
---|
| 377 | if (stbuf1.st_ino != stbuf2.st_ino) e(6);
|
---|
| 378 | if (stbuf1.st_dev != stbuf2.st_dev) e(7);
|
---|
| 379 | if (stbuf1.st_nlink != stbuf2.st_nlink) e(8);
|
---|
| 380 | if (stbuf1.st_uid != stbuf2.st_uid) e(9);
|
---|
| 381 | if (stbuf1.st_gid != stbuf2.st_gid) e(10);
|
---|
| 382 | if (stbuf1.st_size != stbuf2.st_size) e(11);
|
---|
| 383 | if (stbuf1.st_atime != stbuf2.st_atime) e(12);
|
---|
| 384 | if (stbuf1.st_mtime != stbuf2.st_mtime) e(13);
|
---|
| 385 | if (stbuf1.st_ctime != stbuf2.st_ctime) e(14);
|
---|
| 386 |
|
---|
| 387 | if (!S_ISREG(stbuf1.st_mode)) e(15);
|
---|
| 388 | if (S_ISDIR(stbuf1.st_mode)) e(16);
|
---|
| 389 | if (S_ISCHR(stbuf1.st_mode)) e(17);
|
---|
| 390 | if (S_ISBLK(stbuf1.st_mode)) e(18);
|
---|
| 391 | if (S_ISFIFO(stbuf1.st_mode)) e(19);
|
---|
| 392 |
|
---|
| 393 | if ((stbuf1.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != 0777) e(20);
|
---|
| 394 | if (stbuf1.st_nlink != 1) e(21);
|
---|
| 395 | if (stbuf1.st_uid != getuid()) e(22);
|
---|
| 396 | if (stbuf1.st_gid != getgid()) e(23);
|
---|
| 397 | if (stbuf1.st_size != 0L) e(24);
|
---|
| 398 |
|
---|
| 399 | /* First unlink, then close -- harder test */
|
---|
| 400 | if (unlink("foo") < 0) e(25);
|
---|
| 401 | if (close(fd) < 0) e(26);
|
---|
| 402 |
|
---|
| 403 | /* Now try umask a bit more. */
|
---|
| 404 | fd = 0;
|
---|
| 405 | if ((i = umask(~0704)) != 0) e(27);
|
---|
| 406 | if ((fd = creat("foo", 0777)) < 0) e(28);
|
---|
| 407 | if (stat("foo", &stbuf1) < 0) e(29);
|
---|
| 408 | if (fstat(fd, &stbuf2) < 0) e(30);
|
---|
| 409 | if (stbuf1.st_mode != stbuf2.st_mode) e(31);
|
---|
| 410 | if ((stbuf1.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != 0704) e(32);
|
---|
| 411 |
|
---|
| 412 | /* First unlink, then close -- harder test */
|
---|
| 413 | if (unlink("foo") < 0) e(33);
|
---|
| 414 | if (close(fd) < 0) e(34);
|
---|
| 415 | if (umask(m1) != 073) e(35);
|
---|
| 416 |
|
---|
| 417 | /* Test some errors. */
|
---|
| 418 | if (system("mkdir Dir; date >Dir/x; chmod 666 Dir") != 0) e(36);
|
---|
| 419 | if (stat("Dir/x", &stbuf1) >= 0) e(37);
|
---|
| 420 | if (errno != EACCES) e(38);
|
---|
| 421 | if (stat("......", &stbuf1) >= 0) e(39);
|
---|
| 422 | if (errno != ENOENT) e(40);
|
---|
| 423 | if (stat("", &stbuf1) >= 0) e(41);
|
---|
| 424 | if (errno != ENOENT) e(42);
|
---|
| 425 | if (stat("xxx/yyy/zzz", &stbuf1) >= 0) e(43);
|
---|
| 426 | if (errno != ENOENT) e(44);
|
---|
| 427 | if (fstat(10000, &stbuf1) >= 0) e(45);
|
---|
| 428 | if (errno != EBADF) e(46);
|
---|
| 429 | if (chmod("Dir", 0777) != 0) e(47);
|
---|
| 430 | if (system("rm -rf foo Dir") != 0) e(48);
|
---|
| 431 |
|
---|
| 432 | /* See if time looks reasonable. */
|
---|
| 433 | errno = 0;
|
---|
| 434 | t = time(&t1); /* current time */
|
---|
| 435 | if (t < 650000000L) e(49); /* 650000000 is Sept. 1990 */
|
---|
| 436 | unlink("T39f");
|
---|
| 437 | fd = creat("T39f", 0777);
|
---|
| 438 | if (fd < 0) e(50);
|
---|
| 439 | if (close(fd) < 0) e(51);
|
---|
| 440 | if (stat("T39f", &stbuf1) < 0) e(52);
|
---|
| 441 | if (stbuf1.st_mtime < t) e(53);
|
---|
| 442 | if (unlink("T39f") < 0) e(54);
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | void test39g()
|
---|
| 446 | {
|
---|
| 447 | /* Test link and unlink. */
|
---|
| 448 | int i, fd;
|
---|
| 449 | struct stat stbuf;
|
---|
| 450 | char name[20];
|
---|
| 451 |
|
---|
| 452 | subtest = 9;
|
---|
| 453 |
|
---|
| 454 | if (system("rm -rf L? L?? Dir; mkdir Dir") != 0) e(1);
|
---|
| 455 | if ( (fd = creat("L1", 0666)) < 0) e(2);
|
---|
| 456 | if (fstat(fd, &stbuf) != 0) e(3);
|
---|
| 457 | if (stbuf.st_nlink != 1) e(4);
|
---|
| 458 | if (link("L1", "L2") != 0) e(5);
|
---|
| 459 | if (fstat(fd, &stbuf) != 0) e(6);
|
---|
| 460 | if (stbuf.st_nlink != 2) e(7);
|
---|
| 461 | if (unlink("L2") != 0) e(8);
|
---|
| 462 | if (link("L1", "L2") != 0) e(9);
|
---|
| 463 | if (unlink("L1") != 0) e(10);
|
---|
| 464 | if (close(fd) != 0) e(11);
|
---|
| 465 |
|
---|
| 466 | /* L2 exists at this point. */
|
---|
| 467 | if ( (fd = creat("L1", 0666)) < 0) e(12);
|
---|
| 468 | if (stat("L1", &stbuf) != 0) e(13);
|
---|
| 469 | if (stbuf.st_nlink != 1) e(14);
|
---|
| 470 | if (link("L1", "Dir/L2") != 0) e(15);
|
---|
| 471 | if (stat("L1", &stbuf) != 0) e(16);
|
---|
| 472 | if (stbuf.st_nlink != 2) e(17);
|
---|
| 473 | if (stat("Dir/L2", &stbuf) != 0) e(18);
|
---|
| 474 | if (stbuf.st_nlink != 2) e(19);
|
---|
| 475 |
|
---|
| 476 | /* L1, L2, and Dir/L2 exist at this point. */
|
---|
| 477 | if (unlink("Dir/L2") != 0) e(20);
|
---|
| 478 | if (link("L1", "Dir/L2") != 0) e(21);
|
---|
| 479 | if (unlink("L1") != 0) e(22);
|
---|
| 480 | if (close(fd) != 0) e(23);
|
---|
| 481 | if (chdir("Dir") != 0) e(24);
|
---|
| 482 | if (unlink("L2") != 0) e(25);
|
---|
| 483 | if (chdir("..") != 0) e(26);
|
---|
| 484 |
|
---|
| 485 | /* L2 exists at this point. Test linking to unsearchable dir. */
|
---|
| 486 | if (link("L2", "Dir/L2") != 0) e(27);
|
---|
| 487 | if (chmod("Dir", 0666) != 0) e(27);
|
---|
| 488 | if (link("L2", "Dir/L2") != -1) e(28);
|
---|
| 489 | if (errno != EACCES) e(29);
|
---|
| 490 | errno = 0;
|
---|
| 491 | if (link("Dir/L2", "L3") != -1) e(30);
|
---|
| 492 | if (errno != EACCES) e(31);
|
---|
| 493 | if (chmod("Dir", 0777) != 0) e(32);
|
---|
| 494 | if (unlink("Dir/L2") != 0) e(33);
|
---|
| 495 | if (unlink("L3") == 0) e(34);
|
---|
| 496 |
|
---|
| 497 | /* L2 exists at this point. Test linking to unwriteable dir. */
|
---|
| 498 | if (chmod("Dir", 0555) != 0) e(35);
|
---|
| 499 | if (link("L2", "Dir/L2") != -1) e(36);
|
---|
| 500 | if (errno != EACCES) e(37);
|
---|
| 501 | if (chmod("Dir", 0777) != 0) e(38);
|
---|
| 502 |
|
---|
| 503 | /* L2 exists at this point. Test linking mode 0 file. */
|
---|
| 504 | if (chmod("L2", 0) != 0) e(39);
|
---|
| 505 | if (link("L2", "L3") != 0) e(40);
|
---|
| 506 | if (stat("L3", &stbuf) != 0) e(41);
|
---|
| 507 | if (stbuf.st_nlink != 2) e(42);
|
---|
| 508 | if (unlink("L2") != 0) e(43);
|
---|
| 509 |
|
---|
| 510 | /* L3 exists at this point. Test linking to an existing file. */
|
---|
| 511 | if ( (fd = creat("L1", 0666)) < 0) e(44);
|
---|
| 512 | if (link("L1", "L3") != -1) e(45);
|
---|
| 513 | if (errno != EEXIST) e(46);
|
---|
| 514 | errno = 0;
|
---|
| 515 | if (link("L1", "L1") != -1) e(47);
|
---|
| 516 | if (errno != EEXIST) e(48);
|
---|
| 517 | if (unlink("L3") != 0) e(49);
|
---|
| 518 |
|
---|
| 519 | /* L1 exists at this point. Test creating too many links. */
|
---|
| 520 | for (i = 2; i <= LINK_MAX; i++) {
|
---|
| 521 | sprintf(name, "Lx%d", i);
|
---|
| 522 | if (link("L1", name) != 0) e(50);
|
---|
| 523 | }
|
---|
| 524 | if (stat("L1", &stbuf) != 0) e(51);
|
---|
| 525 | if (stbuf.st_nlink != LINK_MAX) e(52);
|
---|
| 526 | if (link("L1", "L2") != -1) e(53);
|
---|
| 527 | if (errno != EMLINK) e(54);
|
---|
| 528 | for (i = 2; i <= LINK_MAX; i++) {
|
---|
| 529 | sprintf(name, "Lx%d", i);
|
---|
| 530 | if (unlink(name) != 0) e(55);
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | if (stat("L1", &stbuf) != 0) e(56);
|
---|
| 534 | if (stbuf.st_nlink != 1) e(57);
|
---|
| 535 |
|
---|
| 536 | /* L1 exists. Test ENOENT. */
|
---|
| 537 | errno = 0;
|
---|
| 538 | if (link("xx/L1", "L2") != -1) e(58);
|
---|
| 539 | if (errno != ENOENT) e(59);
|
---|
| 540 | errno = 0;
|
---|
| 541 | if (link("L1", "xx/L2") != -1) e(60);
|
---|
| 542 | if (errno != ENOENT) e(61);
|
---|
| 543 | errno = 0;
|
---|
| 544 | if (link("L4", "L5") != -1) e(62);
|
---|
| 545 | if (errno != ENOENT) e(63);
|
---|
| 546 | errno = 0;
|
---|
| 547 | if (link("", "L5") != -1) e(64);
|
---|
| 548 | if (errno != ENOENT) e(65);
|
---|
| 549 | errno = 0;
|
---|
| 550 | if (link("L1", "") != -1) e(66);
|
---|
| 551 | if (errno != ENOENT) e(67);
|
---|
| 552 |
|
---|
| 553 | /* L1 exists. Test ENOTDIR. */
|
---|
| 554 | errno = 0;
|
---|
| 555 | if (link("/dev/tty/x", "L2") != -1) e(68);
|
---|
| 556 | if (errno != ENOTDIR) e(69);
|
---|
| 557 |
|
---|
| 558 | /* L1 exists. Test EPERM. */
|
---|
| 559 | if (link(".", "L2") != -1) e(70);
|
---|
| 560 | if (errno != EPERM) e(71);
|
---|
| 561 |
|
---|
| 562 | /* L1 exists. Test unlink. */
|
---|
| 563 | if (link("L1", "Dir/L1") != 0) e(72);
|
---|
| 564 | if (chmod("Dir", 0666) != 0) e(73);
|
---|
| 565 | if (unlink("Dir/L1") != -1) e(74);
|
---|
| 566 | if (errno != EACCES) e(75);
|
---|
| 567 | errno = 0;
|
---|
| 568 | if (chmod("Dir", 0555) != 0) e(76);
|
---|
| 569 | if (unlink("Dir/L1") != -1) e(77);
|
---|
| 570 | if (errno != EACCES) e(78);
|
---|
| 571 |
|
---|
| 572 | if (unlink("L7") != -1) e(79);
|
---|
| 573 | if (errno != ENOENT) e(80);
|
---|
| 574 | errno = 0;
|
---|
| 575 | if (unlink("") != -1) e(81);
|
---|
| 576 | if (errno != ENOENT) e(82);
|
---|
| 577 |
|
---|
| 578 | if (unlink("Dir/L1/L2") != -1) e(83);
|
---|
| 579 | if (errno != ENOTDIR) e(84);
|
---|
| 580 |
|
---|
| 581 | if (chmod("Dir", 0777) != 0) e(85);
|
---|
| 582 | if (unlink("Dir/L1") != 0) e(86);
|
---|
| 583 | if (unlink("Dir") != -1) e(87);
|
---|
| 584 | if (errno != EPERM) e(88);
|
---|
| 585 | if (unlink("L1") != 0) e(89);
|
---|
| 586 | if (system("rm -rf Dir") != 0) e(90);
|
---|
| 587 | if (close(fd) != 0) e(91);
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | void test39h()
|
---|
| 591 | {
|
---|
| 592 | /* Test access. */
|
---|
| 593 |
|
---|
| 594 | int fd;
|
---|
| 595 |
|
---|
| 596 | subtest = 10;
|
---|
| 597 | system("rm -rf A1");
|
---|
| 598 | if ( (fd = creat("A1", 0777)) < 0) e(1);
|
---|
| 599 | if (close(fd) != 0) e(2);
|
---|
| 600 | if (access("A1", R_OK) != 0) e(3);
|
---|
| 601 | if (access("A1", W_OK) != 0) e(4);
|
---|
| 602 | if (access("A1", X_OK) != 0) e(5);
|
---|
| 603 | if (access("A1", (R_OK|W_OK|X_OK)) != 0) e(6);
|
---|
| 604 |
|
---|
| 605 | if (chmod("A1", 0400) != 0) e(7);
|
---|
| 606 | if (access("A1", R_OK) != 0) e(8);
|
---|
| 607 | if (access("A1", W_OK) != -1) e(9);
|
---|
| 608 | if (access("A1", X_OK) != -1) e(10);
|
---|
| 609 | if (access("A1", (R_OK|W_OK|X_OK)) != -1) e(11);
|
---|
| 610 |
|
---|
| 611 | if (chmod("A1", 0077) != 0) e(12);
|
---|
| 612 | if (access("A1", R_OK) != -1) e(13);
|
---|
| 613 | if (access("A1", W_OK) != -1) e(14);
|
---|
| 614 | if (access("A1", X_OK) != -1) e(15);
|
---|
| 615 | if (access("A1", (R_OK|W_OK|X_OK)) != -1) e(16);
|
---|
| 616 | if (errno != EACCES) e(17);
|
---|
| 617 |
|
---|
| 618 | if (access("", R_OK) != -1) e(18);
|
---|
| 619 | if (errno != ENOENT) e(19);
|
---|
| 620 | if (access("./A1/x", R_OK) != -1) e(20);
|
---|
| 621 | if (errno != ENOTDIR) e(21);
|
---|
| 622 |
|
---|
| 623 | if (unlink("A1") != 0) e(22);
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | void test39i()
|
---|
| 627 | {
|
---|
| 628 | /* Test chmod. */
|
---|
| 629 |
|
---|
| 630 | int fd, i;
|
---|
| 631 | struct stat stbuf;
|
---|
| 632 |
|
---|
| 633 | subtest = 11;
|
---|
| 634 | system("rm -rf A1");
|
---|
| 635 | if ( (fd = creat("A1", 0777)) < 0) e(1);
|
---|
| 636 |
|
---|
| 637 | for (i = 0; i < 511; i++) {
|
---|
| 638 | if (chmod("A1", i) != 0) e(100+i);
|
---|
| 639 | if (fstat(fd, &stbuf) != 0) e(200+i);
|
---|
| 640 | if ( (stbuf.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)) != i) e(300+i);
|
---|
| 641 | }
|
---|
| 642 | if (close(fd) != 0) e(2);
|
---|
| 643 |
|
---|
| 644 | if (chmod("A1/x", 0777) != -1) e(3);
|
---|
| 645 | if (errno != ENOTDIR) e(4);
|
---|
| 646 | if (chmod("Axxx", 0777) != -1) e(5);
|
---|
| 647 | if (errno != ENOENT) e(6);
|
---|
| 648 | errno = 0;
|
---|
| 649 | if (chmod ("", 0777) != -1) e(7);
|
---|
| 650 | if (errno != ENOENT) e(8);
|
---|
| 651 |
|
---|
| 652 | /* Now perform limited chown tests. These should work even as non su */
|
---|
| 653 | i = getuid();
|
---|
| 654 | /* DEBUG -- Not yet implemented
|
---|
| 655 | if (chown("A1", i, 0) != 0) e(9);
|
---|
| 656 | if (chown("A1", i, 1) != 0) e(10);
|
---|
| 657 | if (chown("A1", i, 2) != 0) e(11);
|
---|
| 658 | if (chown("A1", i, 3) != 0) e(12);
|
---|
| 659 | if (chown("A1", i, 4) != 0) e(13);
|
---|
| 660 | if (chown("A1", i, 0) != 0) e(14);
|
---|
| 661 | */
|
---|
| 662 |
|
---|
| 663 | if (unlink("A1") != 0) e(9);
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | void test39j()
|
---|
| 667 | {
|
---|
| 668 | /* Test utime. */
|
---|
| 669 |
|
---|
| 670 | int fd;
|
---|
| 671 | time_t tloc;
|
---|
| 672 | struct utimbuf times;
|
---|
| 673 | struct stat stbuf;
|
---|
| 674 |
|
---|
| 675 | subtest = 12;
|
---|
| 676 | if (system("rm -rf A2") != 0) e(1);
|
---|
| 677 | if ( (fd = creat("A2", 0666)) < 0) e(2);
|
---|
| 678 | times.modtime = 100;
|
---|
| 679 | if (utime("A2", ×) != 0) e(3);
|
---|
| 680 | if (stat("A2", &stbuf) != 0) e(4);
|
---|
| 681 | if (stbuf.st_mtime != 100) e(5);
|
---|
| 682 |
|
---|
| 683 | tloc = time((time_t *)NULL); /* get current time */
|
---|
| 684 | times.modtime = tloc;
|
---|
| 685 | if (utime("A2", ×) != 0) e(6);
|
---|
| 686 | if (stat("A2", &stbuf) != 0) e(7);
|
---|
| 687 | if (stbuf.st_mtime != tloc) e(8);
|
---|
| 688 | if (close(fd) != 0) e(9);
|
---|
| 689 | if (unlink("A2") != 0) e(10);
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 | void e(n)
|
---|
| 693 | int n;
|
---|
| 694 | {
|
---|
| 695 | int err_num = errno; /* save errno in case printf clobbers it */
|
---|
| 696 |
|
---|
| 697 | printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
|
---|
| 698 | fflush(stdout); /* stdout and stderr are mixed horribly */
|
---|
| 699 | errno = err_num; /* restore errno, just in case */
|
---|
| 700 | perror("");
|
---|
| 701 | if (errct++ > MAX_ERROR) {
|
---|
| 702 | printf("Too many errors; test aborted\n");
|
---|
| 703 | chdir("..");
|
---|
| 704 | system("rm -rf DIR*");
|
---|
| 705 | exit(1);
|
---|
| 706 | }
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | void quit()
|
---|
| 710 | {
|
---|
| 711 |
|
---|
| 712 | chdir("..");
|
---|
| 713 | system("rm -rf DIR*");
|
---|
| 714 |
|
---|
| 715 | if (errct == 0) {
|
---|
| 716 | printf("ok\n");
|
---|
| 717 | exit(0);
|
---|
| 718 | } else {
|
---|
| 719 | printf("%d errors\n", errct);
|
---|
| 720 | exit(1);
|
---|
| 721 | }
|
---|
| 722 | }
|
---|