📄 test18.c
字号:
else {
if (lseek(n, 0L, SEEK_SET) != 0) err(5, LSEEK, "'file04'");
read_standards(n, a);
read_more(n, a);
}
try_close(n, "'file04'");
}
/* Test read after OPEN */
if ((n = open("file04", R)) < 0)
err(5, OPEN, "'file04'");
else {
read_standards(n, a);
read_more(n, a);
try_close(n, "'file04'");
}
/* Test read after DUP */
if ((n = open("file04", R)) < 0) err(5, OPEN, "'file04'");
if ((n1 = dup(n)) < 0)
err(5, DUP, "'file04'");
else {
read_standards(n1, a);
read_more(n1, a);
try_close(n1, "duplicated fd 'file04'");
}
/* Remove testfile */
Remove(n, "file04");
/* Test read after pipe */
if (pipe(fd) < 0)
err(5, PIPE, NIL);
else {
if (write(fd[1], a, ARSIZE) != ARSIZE) {
err(5, WRITE, "'fd[1]'");
try_close(fd[1], "'fd[1]'");
} else {
try_close(fd[1], "'fd[1]'");
read_standards(fd[0], a);
}
try_close(fd[0], "'fd[0]'");
}
/* Last test: try to read a read-protected file */
if ((n = open("drwx/-wx", W)) < 0)
err(5, OPEN, "'drwx/-wx'");
else {
if (read(n, a, ARSIZE) != FAIL)
err(11, READ, "read a non-read. file");
else
check(READ, EBADF);
try_close(n, "'/drwx/-wx'");
}
} /* test04 */
void read_standards(filedes, a)
int filedes;
char a[];
{
char b[ARSIZE];
clear_array(b);
if (read(filedes, b, ARSIZE) != ARSIZE)
err(1, READ, "bad");
else if (comp_array(a, b, ARSIZE) != OK)
err(7, "read/write", "values");
else if (read(filedes, b, ARSIZE) != READ_EOF)
err(11, READ, "read beyond endoffile");
/* Try giving read wrong input: wrong filedes */
if (read(FAIL, b, ARSIZE) != FAIL)
err(2, READ, "filedes");
else
check(READ, EBADF);
/* Wrong length */
if (read(filedes, b, -ARSIZE) != FAIL)
err(2, READ, "length");
else
check(READ, EINVAL);
} /* read_standards */
void read_more(filedes, a)
int filedes;
char a[];
/* Separated from read_standards() because the PIPE test * would fail. */
{
int i;
char b[ARSIZE];
if (lseek(filedes, (long) (ARSIZE / 2), SEEK_SET) != ARSIZE / 2)
err(5, LSEEK, "to location ARSIZE/2");
clear_array(b);
if (read(filedes, b, ARSIZE) != ARSIZE / 2) err(1, READ, "bad");
for (i = 0; i < ARSIZE / 2; i++)
if (b[i] != a[(ARSIZE / 2) + i])
err(7, READ, "from location ARSIZE/2");
}
/*****************************************************************************
* test OPEN/CLOSE *
****************************************************************************/
void test05()
{
int n, n1, mode, fd[2];
char b[ARSIZE];
/* Test open after CREAT */
if ((n = creat("file05", 0700)) != FF) /* no other open files left */
err(13, CREAT, "'file05'");
else {
if ((n1 = open("file05", RW)) != FF + 1)
err(13, OPEN, "'file05' after creation");
try_close(n1, "'file05' (open after creation)");
try_close(n, "'file05'");
if ((n = open("file05", R)) != FF)
err(13, OPEN, "after closing");
else
try_close(n, "'file05' (open after closing)");
/* Remove testfile */
try_unlink("file05");
}
/* Test all possible modes, try_open not only opens file (sometimes) *
* but closes files too (when opened) */
if ((n = creat("file05", 0700)) < 0) /* no other files left */
err(5, CREAT, "'file05' (2nd time)");
else {
try_close(n, "file05");
for (mode = 0; mode <= 0700; mode += 0100) {
if (chmod("file05", mode) != OK) err(5, CHMOD, "'file05'");
if (mode <= 0100) {
try_open("file05", R, FAIL);
try_open("file05", W, FAIL);
try_open("file05", RW, FAIL);
} else if (mode >= 0200 && mode <= 0300) {
try_open("file05", R, FAIL);
try_open("file05", W, FF);
try_open("file05", RW, FAIL);
} else if (mode >= 0400 && mode <= 0500) {
try_open("file05", R, FF);
try_open("file05", W, FAIL);
try_open("file05", RW, FAIL);
} else {
try_open("file05", R, FF);
try_open("file05", W, FF);
try_open("file05", RW, FF);
}
}
}
/* Test opening existing file */
if ((n = open("drwx/rwx", R)) < 0)
err(13, OPEN, "existing file");
else { /* test close after DUP */
if ((n1 = dup(n)) < 0)
err(13, DUP, "'drwx/rwx'");
else {
try_close(n1, "duplicated fd 'drwx/rwx'");
if (read(n1, b, ARSIZE) != FAIL)
err(11, READ, "on closed dupped fd 'drwx/rwx'");
else
check(READ, EBADF);
if (read(n, b, ARSIZE) == FAIL) /* should read an eof */
err(13, READ, "on fd '/drwx/rwx'");
}
try_close(n, "'drwx/rwx'");
}
/* Test close after PIPE */
if (pipe(fd) < 0)
err(13, PIPE, NIL);
else {
try_close(fd[1], "duplicated fd 'fd[1]'");
/* Fd[1] really should be closed now; check */
clear_array(b);
if (read(fd[0], b, ARSIZE) != READ_EOF)
err(11, READ, "read on empty pipe (and fd[1] was closed)");
try_close(fd[0], "duplicated fd 'fd[0]'");
}
/* Try to open a non-existing file */
if (open("non-file", R) != FAIL)
err(11, OPEN, "open non-executable file");
else
check(OPEN, ENOENT);
/* Dir does not exist */
if (open("dzzz/file05", R) != FAIL)
err(11, OPEN, "open in an non-searchable dir");
else
check(OPEN, ENOENT);
/* Dir is not searchable */
if (n = open("drw-/rwx", R) != FAIL)
err(11, OPEN, "open in an non-searchabledir");
else
check(OPEN, EACCES);
/* Unlink testfile */
try_unlink("file05");
/* File is not readable */
if (open("drwx/-wx", R) != FAIL)
err(11, OPEN, "open unreadable file for reading");
else
check(OPEN, EACCES);
/* File is not writable */
if (open("drwx/r-x", W) != FAIL)
err(11, OPEN, "open unwritable file for writing");
else
check(OPEN, EACCES);
/* Try opening more than MAXOPEN ('extra' (19-8-85)) files */
if ((n = open_alot()) != MAXOPEN)
err(13, OPEN, "MAXOPEN files");
else
/* Maximum # of files opened now, another open should fail
* because * all filedescriptors have already been used. */
if (open("drwx/rwx", RW) != FAIL)
err(9, OPEN, "open");
else
check(OPEN, EMFILE);
if (close_alot(n) != n) err(5, CLOSE, "all opened files");
/* Can close make mistakes ? */
if (close(-1) != FAIL)
err(2, CLOSE, "filedes");
else
check(CLOSE, EBADF);
} /* test05 */
void try_open(fname, mode, test)
int mode, test;
char *fname;
{
int n;
if ((n = open(fname, mode)) != test)
err(11, OPEN, "break through filepermission with an incorrect mode");
if (n != FAIL) try_close(n, fname); /* cleanup */
} /* try_open */
/*****************************************************************************
* test LSEEK *
****************************************************************************/
void test06()
{
char a[ARSIZE], b[ARSIZE];
int fd;
if ((fd = open("drwx/rwx", RW)) != FF) /* there should be no */
err(13, OPEN, "'drwx/rwx'"); /* other open files */
else {
init_array(a);
if (write(fd, a, 10) != 10)
err(1, WRITE, "bad");
else {
/* Lseek back to begin file */
if (lseek(fd, 0L, SEEK_SET) != 0)
err(5, LSEEK, "to begin file");
else if (read(fd, b, 10) != 10)
err(1, READ, "bad");
else if (comp_array(a, b, 10) != OK)
err(7, LSEEK, "values r/w after lseek to begin");
/* Lseek to endoffile */
if (lseek(fd, 0L, SEEK_END) != 10)
err(5, LSEEK, "to end of file");
else if (read(fd, b, 1) != READ_EOF)
err(7, LSEEK, "read at end of file");
/* Lseek beyond file */
if (lseek(fd, 10L, SEEK_CUR) != 20)
err(5, LSEEK, "beyond end of file");
else if (write(fd, a, 10) != 10)
err(1, WRITE, "bad");
else {
/* Lseek to begin second write */
if (lseek(fd, 20L, SEEK_SET) != 20)
err(5, LSEEK, "'/drwx/rwx'");
if (read(fd, b, 10) != 10)
err(1, READ, "bad");
else if (comp_array(a, b, 10) != OK)
err(7, LSEEK,
"values read after lseek MAXOPEN");
}
}
/* Lseek to position before begin of file */
if (lseek(fd, -1L, 0) != FAIL)
err(11, LSEEK, "lseek before beginning of file");
try_close(fd, "'drwx/rwx'");
}
/* Lseek on invalid filediscriptor */
if (lseek(-1, 0L, SEEK_SET) != FAIL)
err(2, LSEEK, "filedes");
else
check(LSEEK, EBADF);
}
/* "t3.c", created by Rene Montsma and Menno Wilcke */
/*****************************************************************************
* test ACCESS *
****************************************************************************/
void test07()
{
/* Check with proper parameters */
if (access("drwx/rwx", RWX) != OK) err(5, ACCESS, "accessible file");
if (access("./././drwx/././rwx", 0) != OK)
err(5, ACCESS, "'/./.(etc)/drwx///rwx'");
/* Check 8 files with 8 different modes on 8 accesses */
if (chdir("drwx") != OK) err(5, CHDIR, "'drwx'");
access_standards();
if (chdir("..") != OK) err(5, CHDIR, "'..'");
/* Check several wrong combinations */
/* File does not exist */
if (access("non-file", 0) != FAIL)
err(11, ACCESS, "access non-existing file");
else
check(ACCESS, ENOENT);
/* Non-searchable dir */
if (access("drw-/rwx", 0) != FAIL)
err(4, ACCESS, "'drw-'");
else
check(ACCESS, EACCES);
/* Searchable dir, but wrong file-mode */
if (access("drwx/--x", RWX) != FAIL)
err(11, ACCESS, "a non accessible file");
else
check(ACCESS, EACCES);
} /* test07 */
void access_standards()
{
int i, mode = 0;
for (i = 0; i < 8; i++)
if (i == 0)
try_access(fnames[mode], i, OK);
else
try_access(fnames[mode], i, FAIL);
mode++;
for (i = 0; i < 8; i++)
if (i < 2)
try_access(fnames[mode], i, OK);
else
try_access(fnames[mode], i, FAIL);
mode++;
for (i = 0; i < 8; i++)
if (i == 0 || i == 2)
try_access(fnames[mode], i, OK);
else
try_access(fnames[mode], i, FAIL);
mode++;
for (i = 0; i < 8; i++)
if (i < 4)
try_access(fnames[mode], i, OK);
else
try_access(fnames[mode], i, FAIL);
mode++;
for (i = 0; i < 8; i++)
if (i == 0 || i == 4)
try_access(fnames[mode], i, OK);
else
try_access(fnames[mode], i, FAIL);
mode++;
for (i = 0; i < 8; i++)
if (i == 0 || i == 1 || i == 4 || i == 5)
try_access(fnames[mode], i, OK);
else
try_access(fnames[mode], i, FAIL);
mode++;
for (i = 0; i < 8; i++)
if (i % 2 == 0)
try_access(fnames[mode], i, OK);
else
try_access(fnames[mode], i, FAIL);
mode++;
for (i = 0; i < 8; i++) try_access(fnames[mode], i, OK);
} /* access_standards */
void try_access(fname, mode, test)
int mode, test;
char *fname;
{
if (access(fname, mode) != test)
err(100, ACCESS, "incorrect access on a file (try_access)");
} /* try_access */
/* "support.c", created by Rene Montsma and Menno Wilcke */
/* Err, make_and_fill_dirs, init_array, clear_array, comp_array,
try_close, try_unlink, Remove, get_mode, check, open_alot,
close_alot, clean_up_the_mess.
*/
/***********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -