8000 minix/test/test18.c at v3.1.0 · devinblood/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1278 lines (1096 loc) · 32.4 KB

test18.c

File metadata and controls

1278 lines (1096 loc) · 32.4 KB
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-mo 10689 de */
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.
*/
/***********************************************************************
* EXTENDED FIONS *
**********************************************************************/
/* First extended functions (i.e. not oldfashioned monixcalls.
e(), nlcr(), octal.*/
void e(string)
char *string;
{
printf("Test program error: %s\n", string);
errct++;
}
void nlcr()
{
printf("\n");
}
void str(s)
char *s;
{
printf(s);
}
/*****************************************************************************
* *
* ERR(or) messages *
* *
*****************************************************************************/
void err(number, scall, name)
/* Give nice error messages */
char *scall, *name;
int number;
{
errct++;
if (errct > MAXERR) {
printf("Too many errors; test aborted\n");
chdir("..");
system("rm -rf DIR*");
quit();
}
e("");
str("\t");
switch (number) {
case 0:
str(scall);
str(": illegal initial value.");
break;
case 1:
str(scall);
str(": ");
str(name);
str(" value returned.");
break;
case 2:
str(scall);
str(": accepting illegal ");
str(name);
str(".");
break;
case 3:
str(scall);
str(": accepting non-existing file.");
break;
case 4:
str(scall);
str(": could search non-searchable dir (");
str(name);
str(").");
break;
case 5:
str(scall);
str(": cannot ");
str(scall);
str(" ");
str(name);
str(".");
break;
case 7:
str(scall);
str(": incorrect ");
str(name);
str(".");
break;
case 8:
str(scall);
str(": wrong values.");
break;
case 9:
str(scall);
str(": accepting too many ");
str(name);
str(" files.");
break;
case 10:
str(scall);
str(": even a superuser can't do anything!");
break;
case 11:
str(scall);
str(": could ");
str(name);
str(".");
break;
case 12:
str(scall);
str(": could write in non-writable dir (");
str(name);
str(").");
break;
case 13:
str(scall);
str(": wrong filedes returned (");
str(name);
str(").");
break;
case 100:
str(scall); /* very common */
str(": ");
str(name);
str(".");
break;
default: str("errornumber does not exist!\n");
}
nlcr();
} /* err */
/*****************************************************************************
* *
* MAKE_AND_FILL_DIRS *
* *
*****************************************************************************/
void make_and_fill_dirs()
/* Create 8 dir.'s: "d---", "d--x", "d-w-", "d-wx", "dr--", "dr-x", *
* "drw-", "drwx". * Then create 8 files
* in "drwx", and some needed files in other dirs. */
{
int mode, i;
for (i = 0; i < 8; i++) {
mkdir(dir[i], 0700);
chown(dir[i], USER_ID, GROUP_ID);
}
setuid(USER_ID);
0