📄 test20.c
字号:
/* Test stat, fstat, umask. */
int i, fd;
mode_t m1;
struct stat stbuf1, stbuf2;
time_t t, t1;
subtest = 8;
m1 = umask(~0777);
if (system("rm -rf foo xxx") != 0) e(1);
if ((fd = creat("foo", 0777)) < 0) e(2);
if (stat("foo", &stbuf1) < 0) e(3);
if (fstat(fd, &stbuf2) < 0) e(4);
if (stbuf1.st_mode != stbuf2.st_mode) e(5);
if (stbuf1.st_ino != stbuf2.st_ino) e(6);
if (stbuf1.st_dev != stbuf2.st_dev) e(7);
if (stbuf1.st_nlink != stbuf2.st_nlink) e(8);
if (stbuf1.st_uid != stbuf2.st_uid) e(9);
if (stbuf1.st_gid != stbuf2.st_gid) e(10);
if (stbuf1.st_size != stbuf2.st_size) e(11);
if (stbuf1.st_atime != stbuf2.st_atime) e(12);
if (stbuf1.st_mtime != stbuf2.st_mtime) e(13);
if (stbuf1.st_ctime != stbuf2.st_ctime) e(14);
if (!S_ISREG(stbuf1.st_mode)) e(15);
if (S_ISDIR(stbuf1.st_mode)) e(16);
if (S_ISCHR(stbuf1.st_mode)) e(17);
if (S_ISBLK(stbuf1.st_mode)) e(18);
if (S_ISFIFO(stbuf1.st_mode)) e(19);
if ((stbuf1.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != 0777) e(20);
if (stbuf1.st_nlink != 1) e(21);
if (stbuf1.st_uid != getuid()) e(22);
if (stbuf1.st_gid != getgid()) e(23);
if (stbuf1.st_size != 0L) e(24);
/* First unlink, then close -- harder test */
if (unlink("foo") < 0) e(25);
if (close(fd) < 0) e(26);
/* Now try umask a bit more. */
fd = 0;
if ((i = umask(~0704)) != 0) e(27);
if ((fd = creat("foo", 0777)) < 0) e(28);
if (stat("foo", &stbuf1) < 0) e(29);
if (fstat(fd, &stbuf2) < 0) e(30);
if (stbuf1.st_mode != stbuf2.st_mode) e(31);
if ((stbuf1.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != 0704) e(32);
/* First unlink, then close -- harder test */
if (unlink("foo") < 0) e(33);
if (close(fd) < 0) e(34);
if (umask(m1) != 073) e(35);
/* Test some errors. */
if (system("mkdir Dir; date >Dir/x; chmod 666 Dir") != 0) e(36);
if (stat("Dir/x", &stbuf1) >= 0) e(37);
if (errno != EACCES) e(38);
if (stat("......", &stbuf1) >= 0) e(39);
if (errno != ENOENT) e(40);
if (stat("", &stbuf1) >= 0) e(41);
if (errno != ENOENT) e(42);
if (stat("xxx/yyy/zzz", &stbuf1) >= 0) e(43);
if (errno != ENOENT) e(44);
if (fstat(10000, &stbuf1) >= 0) e(45);
if (errno != EBADF) e(46);
if (chmod("Dir", 0777) != 0) e(47);
if (system("rm -rf foo Dir") != 0) e(48);
/* See if time looks reasonable. */
errno = 0;
t = time(&t1); /* current time */
if (t < 650000000L) e(49); /* 650000000 is Sept. 1990 */
unlink("T20f");
fd = creat("T20f", 0777);
if (fd < 0) e(50);
if (close(fd) < 0) e(51);
if (stat("T20f", &stbuf1) < 0) e(52);
if (stbuf1.st_mtime < t) e(53);
if (unlink("T20f") < 0) e(54);
}
void test20g()
{
/* Test link and unlink. */
int i, fd;
struct stat stbuf;
char name[4];
subtest = 9;
if (system("rm -rf L? L?? Dir; mkdir Dir") != 0) e(1);
if ( (fd = creat("L1", 0666)) < 0) e(2);
if (fstat(fd, &stbuf) != 0) e(3);
if (stbuf.st_nlink != 1) e(4);
if (link("L1", "L2") != 0) e(5);
if (fstat(fd, &stbuf) != 0) e(6);
if (stbuf.st_nlink != 2) e(7);
if (unlink("L2") != 0) e(8);
if (link("L1", "L2") != 0) e(9);
if (unlink("L1") != 0) e(10);
if (close(fd) != 0) e(11);
/* L2 exists at this point. */
if ( (fd = creat("L1", 0666)) < 0) e(12);
if (stat("L1", &stbuf) != 0) e(13);
if (stbuf.st_nlink != 1) e(14);
if (link("L1", "Dir/L2") != 0) e(15);
if (stat("L1", &stbuf) != 0) e(16);
if (stbuf.st_nlink != 2) e(17);
if (stat("Dir/L2", &stbuf) != 0) e(18);
if (stbuf.st_nlink != 2) e(19);
/* L1, L2, and Dir/L2 exist at this point. */
if (unlink("Dir/L2") != 0) e(20);
if (link("L1", "Dir/L2") != 0) e(21);
if (unlink("L1") != 0) e(22);
if (close(fd) != 0) e(23);
if (chdir("Dir") != 0) e(24);
if (unlink("L2") != 0) e(25);
if (chdir("..") != 0) e(26);
/* L2 exists at this point. Test linking to unsearchable dir. */
if (link("L2", "Dir/L2") != 0) e(27);
if (chmod("Dir", 0666) != 0) e(27);
if (link("L2", "Dir/L2") != -1) e(28);
if (errno != EACCES) e(29);
errno = 0;
if (link("Dir/L2", "L3") != -1) e(30);
if (errno != EACCES) e(31);
if (chmod("Dir", 0777) != 0) e(32);
if (unlink("Dir/L2") != 0) e(33);
if (unlink("L3") == 0) e(34);
/* L2 exists at this point. Test linking to unwriteable dir. */
if (chmod("Dir", 0555) != 0) e(35);
if (link("L2", "Dir/L2") != -1) e(36);
if (errno != EACCES) e(37);
if (chmod("Dir", 0777) != 0) e(38);
/* L2 exists at this point. Test linking mode 0 file. */
if (chmod("L2", 0) != 0) e(39);
if (link("L2", "L3") != 0) e(40);
if (stat("L3", &stbuf) != 0) e(41);
if (stbuf.st_nlink != 2) e(42);
if (unlink("L2") != 0) e(43);
/* L3 exists at this point. Test linking to an existing file. */
if ( (fd = creat("L1", 0666)) < 0) e(44);
if (link("L1", "L3") != -1) e(45);
if (errno != EEXIST) e(46);
errno = 0;
if (link("L1", "L1") != -1) e(47);
if (errno != EEXIST) e(48);
if (unlink("L3") != 0) e(49);
/* L1 exists at this point. Test creating too many links. */
name[0] = 'L';
name[1] = 'x';
name[2] = 1;
name[3] = 0;
for (i = 2; i <= LINK_MAX; i++) {
if (link("L1", name) != 0) e(50);
name[2]++;
}
if (stat("L1", &stbuf) != 0) e(51);
if (stbuf.st_nlink != LINK_MAX) e(52);
if (link("L1", "L2") != -1) e(53);
if (errno != EMLINK) e(54);
name[2] = 1;
for (i = 2; i <= LINK_MAX; i++) {
if (unlink(name) != 0) e(55);
name[2]++;
}
if (stat("L1", &stbuf) != 0) e(56);
if (stbuf.st_nlink != 1) e(57);
/* L1 exists. Test ENOENT. */
errno = 0;
if (link("xx/L1", "L2") != -1) e(58);
if (errno != ENOENT) e(59);
errno = 0;
if (link("L1", "xx/L2") != -1) e(60);
if (errno != ENOENT) e(61);
errno = 0;
if (link("L4", "L5") != -1) e(62);
if (errno != ENOENT) e(63);
errno = 0;
if (link("", "L5") != -1) e(64);
if (errno != ENOENT) e(65);
errno = 0;
if (link("L1", "") != -1) e(66);
if (errno != ENOENT) e(67);
/* L1 exists. Test ENOTDIR. */
errno = 0;
if (link("/dev/tty/x", "L2") != -1) e(68);
if (errno != ENOTDIR) e(69);
/* L1 exists. Test EPERM. */
if (link(".", "L2") != -1) e(70);
if (errno != EPERM) e(71);
/* L1 exists. Test unlink. */
if (link("L1", "Dir/L1") != 0) e(72);
if (chmod("Dir", 0666) != 0) e(73);
if (unlink("Dir/L1") != -1) e(74);
if (errno != EACCES) e(75);
errno = 0;
if (chmod("Dir", 0555) != 0) e(76);
if (unlink("Dir/L1") != -1) e(77);
if (errno != EACCES) e(78);
if (unlink("L7") != -1) e(79);
if (errno != ENOENT) e(80);
errno = 0;
if (unlink("") != -1) e(81);
if (errno != ENOENT) e(82);
if (unlink("Dir/L1/L2") != -1) e(83);
if (errno != ENOTDIR) e(84);
if (chmod("Dir", 0777) != 0) e(85);
if (unlink("Dir/L1") != 0) e(86);
if (unlink("Dir") != -1) e(87);
if (errno != EPERM) e(88);
if (unlink("L1") != 0) e(89);
if (system("rm -rf Dir") != 0) e(90);
if (close(fd) != 0) e(91);
}
void test20h()
{
/* Test access. */
int fd;
subtest = 10;
system("rm -rf A1");
if ( (fd = creat("A1", 0777)) < 0) e(1);
if (close(fd) != 0) e(2);
if (access("A1", R_OK) != 0) e(3);
if (access("A1", W_OK) != 0) e(4);
if (access("A1", X_OK) != 0) e(5);
if (access("A1", (R_OK|W_OK|X_OK)) != 0) e(6);
if (chmod("A1", 0400) != 0) e(7);
if (access("A1", R_OK) != 0) e(8);
if (access("A1", W_OK) != -1) e(9);
if (access("A1", X_OK) != -1) e(10);
if (access("A1", (R_OK|W_OK|X_OK)) != -1) e(11);
if (chmod("A1", 0077) != 0) e(12);
if (access("A1", R_OK) != -1) e(13);
if (access("A1", W_OK) != -1) e(14);
if (access("A1", X_OK) != -1) e(15);
if (access("A1", (R_OK|W_OK|X_OK)) != -1) e(16);
if (errno != EACCES) e(17);
if (access("", R_OK) != -1) e(18);
if (errno != ENOENT) e(19);
if (access("./A1/x", R_OK) != -1) e(20);
if (errno != ENOTDIR) e(21);
if (unlink("A1") != 0) e(22);
}
void test20i()
{
/* Test chmod. */
int fd, i;
struct stat stbuf;
subtest = 11;
system("rm -rf A1");
if ( (fd = creat("A1", 0777)) < 0) e(1);
for (i = 0; i < 511; i++) {
if (chmod("A1", i) != 0) e(100+i);
if (fstat(fd, &stbuf) != 0) e(200+i);
if ( (stbuf.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)) != i) e(300+i);
}
if (close(fd) != 0) e(2);
if (chmod("A1/x", 0777) != -1) e(3);
if (errno != ENOTDIR) e(4);
if (chmod("Axxx", 0777) != -1) e(5);
if (errno != ENOENT) e(6);
errno = 0;
if (chmod ("", 0777) != -1) e(7);
if (errno != ENOENT) e(8);
/* Now perform limited chown tests. These should work even as non su */
i = getuid();
/* DEBUG -- Not yet implemented
if (chown("A1", i, 0) != 0) e(9);
if (chown("A1", i, 1) != 0) e(10);
if (chown("A1", i, 2) != 0) e(11);
if (chown("A1", i, 3) != 0) e(12);
if (chown("A1", i, 4) != 0) e(13);
if (chown("A1", i, 0) != 0) e(14);
*/
if (unlink("A1") != 0) e(9);
}
void test20j()
{
/* Test utime. */
int fd;
time_t tloc;
struct utimbuf times;
struct stat stbuf;
subtest = 12;
if (system("rm -rf A2") != 0) e(1);
if ( (fd = creat("A2", 0666)) < 0) e(2);
times.modtime = 100;
if (utime("A2", ×) != 0) e(3);
if (stat("A2", &stbuf) != 0) e(4);
if (stbuf.st_mtime != 100) e(5);
tloc = time((time_t *)NULL); /* get current time */
times.modtime = tloc;
if (utime("A2", ×) != 0) e(6);
if (stat("A2", &stbuf) != 0) e(7);
if (stbuf.st_mtime != tloc) e(8);
if (close(fd) != 0) e(9);
if (unlink("A2") != 0) e(10);
}
void e(n)
int n;
{
int err_num = errno; /* save errno in case printf clobbers it */
printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
fflush(stdout); /* stdout and stderr are mixed horribly */
errno = err_num; /* restore errno, just in case */
perror("");
if (errct++ > MAX_ERROR) {
printf("Too many errors; test aborted\n");
chdir("..");
system("rm -rf DIR*");
exit(1);
}
}
void quit()
{
chdir("..");
system("rm -rf DIR*");
if (errct == 0) {
printf("ok\n");
exit(0);
} else {
printf("%d errors\n", errct);
exit(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -