📄 test21.c
字号:
/* POSIX test program (21). Author: Andy Tanenbaum */
/* The following POSIX calls are tested:
*
* rename(), mkdir(), rmdir()
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#define ITERATIONS 1
#define MAX_ERROR 4
int subtest, errct;
_PROTOTYPE(int main, (int argc, char *argv []));
_PROTOTYPE(void test21a, (void));
_PROTOTYPE(void test21b, (void));
_PROTOTYPE(void test21c, (void));
_PROTOTYPE(void test21d, (void));
_PROTOTYPE(void test21e, (void));
_PROTOTYPE(void test21f, (void));
_PROTOTYPE(void test21g, (void));
_PROTOTYPE(void test21h, (void));
_PROTOTYPE(void test21i, (void));
_PROTOTYPE(void test21k, (void));
_PROTOTYPE(void test21l, (void));
_PROTOTYPE(void test21m, (void));
_PROTOTYPE(void test21n, (void));
_PROTOTYPE(void test21o, (void));
_PROTOTYPE(int get_link, (char *name));
_PROTOTYPE(void e, (int n));
_PROTOTYPE(void quit, (void));
int main(argc, argv)
int argc;
char *argv[];
{
int i, m = 0xFFFF;
sync();
if (geteuid() == 0 || getuid() == 0) {
printf("Test 21 cannot run as root; test aborted\n");
exit(1);
}
if (argc == 2) m = atoi(argv[1]);
printf("Test 21 ");
fflush(stdout);
system("rm -rf DIR_21; mkdir DIR_21");
chdir("DIR_21");
for (i = 0; i < ITERATIONS; i++) {
if (m & 00001) test21a();
if (m & 00002) test21b();
if (m & 00004) test21c();
if (m & 00010) test21d();
if (m & 00020) test21e();
if (m & 00040) test21f();
if (m & 01000) test21g();
if (m & 00200) test21h();
if (m & 00400) test21i();
if (m & 01000) test21k();
if (m & 02000) test21l();
if (m & 04000) test21m();
if (m & 010000) test21n();
if (m & 020000) test21o();
}
quit();
return(-1); /* impossible */
}
void test21a()
{
/* Test rename(). */
int fd, fd2;
char buf[PATH_MAX+1], buf1[PATH_MAX+1], buf2[PATH_MAX+1];
struct stat stat1, stat2;
subtest = 1;
unlink("A1"); /* get rid of it if it exists */
unlink("A2"); /* get rid of it if it exists */
unlink("A3"); /* get rid of it if it exists */
unlink("A4"); /* get rid of it if it exists */
unlink("A5"); /* get rid of it if it exists */
unlink("A6"); /* get rid of it if it exists */
unlink("A7"); /* get rid of it if it exists */
/* Basic test. Rename A1 to A2 and then A2 to A3. */
if ( (fd=creat("A1", 0666)) < 0) e(1);
if (write(fd, buf, 20) != 20) e(2);
if (close(fd) < 0) e(3);
if (rename("A1", "A2") < 0) e(4);
if ( (fd=open("A2", O_RDONLY)) < 0) e(5);
if (rename("A2", "A3") < 0) e(6);
if ( (fd2=open("A3", O_RDONLY)) < 0) e(7);
if (close(fd) != 0) e(8);
if (close(fd2) != 0) e(9);
if (unlink("A3") != 0) e(10);
/* Now get the absolute path name of the current directory using getcwd()
* and use it to test RENAME using different combinations of relative and
* absolute path names.
*/
if (getcwd(buf, PATH_MAX) == (char *) NULL) e(11);
if ( (fd = creat("A4", 0666)) < 0) e(12);
if (write(fd, buf, 30) != 30) e(13);
if (close(fd) != 0) e(14);
strcpy(buf1, buf);
strcat(buf1, "/A4");
if (rename(buf1, "A5") != 0) e(15); /* rename(absolute, relative) */
if (access("A5", 6) != 0) e(16); /* use access to see if file exists */
strcpy(buf2, buf);
strcat(buf2, "/A6");
if (rename("A5", buf2) != 0) e(17); /* rename(relative, absolute) */
if (access("A6", 6) != 0) e(18);
if (access(buf2, 6) != 0) e(19);
strcpy(buf1, buf);
strcat(buf1, "/A6");
strcpy(buf2, buf);
strcat(buf2, "/A7");
if (rename(buf1, buf2) != 0) e(20); /* rename(absolute, absolute) */
if (access("A7", 6) != 0) e(21);
if (access(buf2, 6) != 0) e(22);
/* Try renaming using names like "./A8" */
if (rename("A7", "./A8") != 0) e(23);
if (access("A8", 6) != 0) e(24);
if (rename("./A8", "A9") != 0) e(25);
if (access("A9", 6) != 0) e(26);
if (rename("./A9", "./A10") != 0) e(27);
if (access("A10", 6) != 0) e(28);
if (access("./A10", 6) != 0) e(29);
if (unlink("A10") != 0) e(30);
/* Now see if directories can be renamed. */
if (system("rm -rf ?uzzy scsi") != 0) e(31);
if (system("mkdir fuzzy") != 0) e(32);
if (rename("fuzzy", "wuzzy") != 0) e(33);
if ( (fd=creat("wuzzy/was_a_bear", 0666)) < 0) e(34);
if (access("wuzzy/was_a_bear", 6) != 0) e(35);
if (unlink("wuzzy/was_a_bear") != 0) e(36);
if (close(fd) != 0) e(37);
if (rename("wuzzy", "buzzy") != 0) e(38);
if (system("rmdir buzzy") != 0) e(39);
/* Now start testing the case that 'new' exists. */
if ( (fd = creat("horse", 0666)) < 0) e(40);
if ( (fd2 = creat("sheep", 0666)) < 0) e(41);
if (write(fd, buf, PATH_MAX) != PATH_MAX) e(42);
if (write(fd2, buf, 23) != 23) e(43);
if (stat("horse", &stat1) != 0) e(44);
if (rename("horse", "sheep") != 0) e(45);
if (stat("sheep", &stat2) != 0) e(46);
if (stat1.st_dev != stat2.st_dev) e(47);
if (stat1.st_ino != stat2.st_ino) e(48);
if (stat2.st_size != PATH_MAX) e(49);
if (access("horse", 6) == 0) e(50);
if (close(fd) != 0) e(51);
if (close(fd2) != 0) e(52);
if (rename("sheep", "sheep") != 0) e(53);
if (unlink("sheep") != 0) e(54);
/* Now try renaming something to a directory that already exists. */
if (system("mkdir fuzzy wuzzy") != 0) e(55);
if ( (fd = creat("fuzzy/was_a_bear", 0666)) < 0) e(56);
if (close(fd) != 0) e(57);
if (rename("fuzzy", "wuzzy") != 0) e(58); /* 'new' is empty dir */
if (system("mkdir scsi") != 0) e(59);
if (rename("scsi", "wuzzy") == 0) e(60); /* 'new' is full dir */
if (errno != EEXIST && errno != ENOTEMPTY) e(61);
/* Test 14 character names--always tricky. */
if (rename("wuzzy/was_a_bear", "wuzzy/was_not_a_bear") != 0) e(62);
if (access("wuzzy/was_not_a_bear", 6) != 0) e(63);
if (rename("wuzzy/was_not_a_bear", "wuzzy/was_not_a_duck") != 0) e(64);
if (access("wuzzy/was_not_a_duck", 6) != 0) e(65);
if (rename("wuzzy/was_not_a_duck", "wuzzy/was_a_bird") != 0) e(65);
if (access("wuzzy/was_a_bird", 6) != 0) e(66);
/* Test moves between directories. */
if (rename("wuzzy/was_a_bird", "beast") != 0) e(67);
if (access("beast", 6) != 0) e(68);
if (rename("beast", "wuzzy/was_a_cat") != 0) e(69);
if (access("wuzzy/was_a_cat", 6) != 0) e(70);
/* Test error conditions. 'scsi' and 'wuzzy/was_a_cat' exist now. */
if (rename("wuzzy/was_a_cat", "wuzzy/was_a_dog") != 0) e(71);
if (access("wuzzy/was_a_dog", 6) != 0) e(72);
if (chmod("wuzzy", 0) != 0) e(73);
errno = 0;
if (rename("wuzzy/was_a_dog", "wuzzy/was_a_pig") != -1) e(74);
if (errno != EACCES) e(75);
errno = 0;
if (rename("wuzzy/was_a_dog", "doggie") != -1) e(76);
if (errno != EACCES) e(77);
errno = 0;
if ( (fd = creat("beast", 0666)) < 0) e(78);
if (close(fd) != 0) e(79);
if (rename("beast", "wuzzy/was_a_twit") != -1) e(80);
if (errno != EACCES) e(81);
errno = 0;
if (rename("beast", "wuzzy") != -1) e(82);
if (errno != EISDIR) e(83);
errno = 0;
if (rename("beest", "baste") != -1) e(84);
if (errno != ENOENT) e(85);
errno = 0;
if (rename("wuzzy", "beast") != -1) e(86);
if (errno != ENOTDIR) e(87);
/* Test prefix rule. */
errno = 0;
if (chmod("wuzzy", 0777) != 0) e(88);
if (unlink("wuzzy/was_a_dog") != 0) e(89);
strcpy(buf1, buf);
strcat(buf1, "/wuzzy");
if (rename(buf, buf1) != -1) e(90);
if (errno != EINVAL) e(91);
if (system("rm -rf wuzzy beast scsi") != 0) e(92);
}
void test21b()
{
/* Test mkdir() and rmdir(). */
int i;
char name[3];
struct stat statbuf;
subtest = 2;
/* Simple stuff. */
if (mkdir("D1", 0700) != 0) e(1);
if (stat("D1", &statbuf) != 0) e(2);
if (!S_ISDIR(statbuf.st_mode)) e(3);
if ( (statbuf.st_mode & 0777) != 0700) e(4);
if (rmdir("D1") != 0) e(5);
/* Make and remove 40 directories. By doing so, the directory has to
* grow to 2 blocks. That presents plenty of opportunity for bugs.
*/
name[0] = 'D';
name[2] = '\0';
for (i = 0; i < 40; i++) {
name[1] = 'A' + i;
if (mkdir(name, 0700 + i%7) != 0) e(10+i); /* for simplicity */
}
for (i = 0; i < 40; i++) {
name[1] = 'A' + i;
if (rmdir(name) != 0) e(50+i);
}
}
void test21c()
{
/* Test mkdir() and rmdir(). */
subtest = 3;
if (mkdir("D1", 0777) != 0) e(1);
if (mkdir("D1/D2", 0777) != 0) e(2);
if (mkdir("D1/D2/D3", 0777) != 0) e(3);
if (mkdir("D1/D2/D3/D4", 0777) != 0) e(4);
if (mkdir("D1/D2/D3/D4/D5", 0777) != 0) e(5);
if (mkdir("D1/D2/D3/D4/D5/D6", 0777) != 0) e(6);
if (mkdir("D1/D2/D3/D4/D5/D6/D7", 0777) != 0) e(7);
if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8", 0777) != 0) e(8);
if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8/D9", 0777) != 0) e(9);
if (access("D1/D2/D3/D4/D5/D6/D7/D8/D9", 7) != 0) e(10);
if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8/D9") != 0) e(11);
if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8") != 0) e(12);
if (rmdir("D1/D2/D3/D4/D5/D6/D7") != 0) e(13);
if (rmdir("D1/D2/D3/D4/D5/D6") != 0) e(11);
if (rmdir("D1/D2/D3/D4/D5") != 0) e(13);
if (rmdir("D1/D2/D3/D4") != 0) e(14);
if (rmdir("D1/D2/D3") != 0) e(15);
if (rmdir("D1/D2") != 0) e(16);
if (rmdir("D1") != 0) e(17);
}
void test21d()
{
/* Test making directories with files and directories in them. */
int fd1, fd2, fd3, fd4, fd5, fd6, fd7, fd8, fd9;
subtest = 4;
if (mkdir("D1", 0777) != 0) e(1);
if (mkdir("D1/D2", 0777) != 0) e(2);
if (mkdir("./D1/D3", 0777) != 0) e(3);
if (mkdir("././D1/D4", 0777) != 0) e(4);
if ( (fd1 = creat("D1/D2/x", 0700)) < 0) e(5);
if ( (fd2 = creat("D1/D2/y", 0700)) < 0) e(6);
if ( (fd3 = creat("D1/D2/z", 0700)) < 0) e(7);
if ( (fd4 = creat("D1/D3/x", 0700)) < 0) e(8);
if ( (fd5 = creat("D1/D3/y", 0700)) < 0) e(9);
if ( (fd6 = creat("D1/D3/z", 0700)) < 0) e(10);
if ( (fd7 = creat("D1/D4/x", 0700)) < 0) e(11);
if ( (fd8 = creat("D1/D4/y", 0700)) < 0) e(12);
if ( (fd9 = creat("D1/D4/z", 0700)) < 0) e(13);
if (unlink("D1/D2/z") != 0) e(14);
if (unlink("D1/D2/y") != 0) e(15);
if (unlink("D1/D2/x") != 0) e(16);
if (unlink("D1/D3/x") != 0) e(17);
if (unlink("D1/D3/z") != 0) e(18);
if (unlink("D1/D3/y") != 0) e(19);
if (unlink("D1/D4/y") != 0) e(20);
if (unlink("D1/D4/z") != 0) e(21);
if (unlink("D1/D4/x") != 0) e(22);
if (rmdir("D1/D2") != 0) e(23);
if (rmdir("D1/D3") != 0) e(24);
if (rmdir("D1/D4") != 0) e(25);
if (rmdir("D1") != 0) e(26);
if (close(fd1) != 0) e(27);
if (close(fd2) != 0) e(28);
if (close(fd3) != 0) e(29);
if (close(fd4) != 0) e(30);
if (close(fd5) != 0) e(31);
if (close(fd6) != 0) e(32);
if (close(fd7) != 0) e(33);
if (close(fd8) != 0) e(34);
if (close(fd9) != 0) e(35);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -