8000 minix/minix/tests/test17.c at master · ze-gois/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
866 lines (703 loc) · 21.8 KB

test17.c

File metadata and controls

866 lines (703 loc) · 21.8 KB
if (write(fd[1], a, ARSIZE) != ARSIZE) e(2);
else if (read(fd[0], b, (ARSIZE / 2)) != (ARSIZE / 2)) e(3);
else if (comp_array(a, b, (ARSIZE / 2)) != OK) e(4);
else if (read(fd[0], b, (ARSIZE / 2)) != (ARSIZE / 2)) e(5);
else if (comp_array(&a[ARSIZE / 2], b, (ARSIZE / 2)) != OK) e(6);
/* Try to let the pipe make a mistake */
if (write(fd[0], a, ARSIZE) != FAIL) e(7);
if (read(fd[1], b, ARSIZE) != FAIL) e(8);
try_close(fd[1], "'fd[1]'");
/* Now we shouldn't be able to read, because fd[1] has been closed */
if (read(fd[0], b, ARSIZE) != END_FILE) e(9);
try_close(fd[0], "'fd[0]'");
}
if (pipe(fd) < 0) e(10);
else {
/* Test lseek on a pipe: should fail */
if (write(fd[1], a, ARSIZE) != ARSIZE) e(11);
if (lseek(fd[1], 10L, SEEK_SET) != FAIL) e(12);
else
if (errno != ESPIPE) e(13);
/* Eat half of the pipe: no writing should be possible */
try_close(fd[0], "'fd[0]' (2nd time)");
/* This makes UNIX crash: omit it if pdp or VAX */
#ifndef NOCRASH
if (write(fd[1], a, ARSIZE) != FAIL) e(14);
else
if (errno != EPIPE) e(15);
#endif
try_close(fd[1], "'fd[1]' (2nd time)");
}
/* BUG : *
* Here we planned to test if we could write 4K bytes on a pipe. *
* However, this was not possible to implement, because the whole *
* Monix system crashed when we tried to write more then 3584 bytes *
* (3.5K) on a pipe. That's why we try to write only 3.5K in the *
* folowing test. */
if (pipe(fd) < 0) e(16);
else {
for (n = 0; n < (PIPESIZE / ARSIZE); n++)
if (write(fd[1], a, ARSIZE) != ARSIZE) e(17);
try_close(fd[1], "'fd[1]' (3rd time)");
for (n = 0; n < (PIPESIZE / ARSIZE); n++)
if (read(fd[0], b, ARSIZE) != ARSIZE) e(18);
try_close(fd[0], "'fd[0]' (3rd time)");
}
/* Test opening a lot of files */
if ((n = open_alot()) != MAXOPEN) e(19);
if (pipe(fd) != FAIL) e(20);
else
if (errno != EMFILE) e(21);
if (close_alot(n) != n) e(22);
} /* test11 */
/* Err, test03, init_array, clear_array, comp_array,
try_close, try_unlink, Remove, get_mode, check, open_alot,
close_alot, clean_up_the_mess.
*/
/*****************************************************************************
* *
* MAKE_AND_FILL_DIRS *
* *
*****************************************************************************/
void test03()
/* 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;
subtest = 3;
for (i = 0; i < MODES; i++) {
mkdir(mode_dir[i], 0700);
chown(mode_dir[i], USER_ID, GROUP_ID);
}
setuid(USER_ID);
setgid(GROUP_ID);
for (mode = 0; mode < 8; mode++) put_file_in_dir("drwx", mode);
put_file_in_dir("d-wx", RWX);
put_file_in_dir("dr-x", RWX);
put_file_in_dir("drw-", RWX);
chmod_8_dirs(8); /* 8 means; 8 different modes */
} /* test03 */
void put_file_in_dir(dirname, mode)
char *dirname;
int mode;
/* Fill directory 'dirname' with file with mode 'mode'. */
{
int nr;
if (chdir(dirname) != OK) e(1);
else {
/* Creat the file */
assert(mode >= 0 && mode < MODES);
if ((nr = creat(mode_fnames[mode], mode * 0100)) < 0) e(2);
else {
try_close(nr, mode_fnames[mode]);
}
if (chdir("..") != OK) e(3);
}
} /* put_file_in_dir */
/*****************************************************************************
* *
* MISCELLANEOUS *
* *
*(all about arrays, 'try_close', 'try_unlink', 'Remove', 'get_mode')*
* *
*****************************************************************************/
void init_array(a)
char *a;
{
int i;
i = 0;
while (i++ < ARSIZE) *a++ = 'a' + (i % 26);
} /* init_array */
void clear_array(b)
char *b;
{
int i;
i = 0;
while (i++ < ARSIZE) *b++ = '0';
} /* clear_array */
int comp_array(a, b, range)
char *a, *b;
int range;
{
assert(range >= 0 && range <= ARSIZE);
while (range-- && (*a++ == *b++));
if (*--a == *--b)
return(OK);
else
return(FAIL);
} /* comp_array */
void try_close(filedes, name)
int filedes;
char *name;
{
if (close(filedes) != OK) e(90);
} /* try_close */
void try_unlink(fname)
char *fname;
{
if (unlink(fname) != 0) e(91);
} /* try_unlink */
void Remove(fdes, fname)
int fdes;
char *fname;
{
try_close(fdes, fname);
try_unlink(fname);
} /* Remove */
int get_mode(name)
char *name;
{
struct stat stbf1;
if (stat(name, &stbf1) != OK) {
e(92);
return(stbf1.st_mode); /* return a mode which will cause *
* error in the calling function *
* (file/dir bit) */
} else
return(stbf1.st_mode & 07777); /* take last 4 bits */
} /* get_mode */
/*****************************************************************************
* *
* ALOT-functions *
* *
*****************************************************************************/
int open_alot()
{
int i;
for (i = 0; i < MAXOPEN; i++)
if (open(filenames[i], R) == FAIL) break;
if (i == 0) e(93);
return(i);
} /* open_alot */
int close_alot(number)
int number;
{
int i, count = 0;
if (number > MAXOPEN)
e(94);
else
for (i = FF; i < number + FF; i++)
if (close(i) != OK) count++;
return(number - count); /* return number of closed files */
} /* close_alot */
/*****************************************************************************
* *
* CLEAN UP THE MESS *
* *
*****************************************************************************/
void clean_up_the_mess()
{
int i;
char dirname[6];
/* First remove 'a lot' files */
for (i = 0; i < MAXOPEN; i++) {
try_unlink(filenames[i]);
}
/* Unlink the files in dir 'drwx' */
if (chdir("drwx") != OK)
e(95);
else {
for (i = 0; i < MODES; i++) {
try_unlink(mode_fnames[i]);
}
if (chdir("..") != OK) e(96);
}
/* Before unlinking files in some dirs, make them writable */
chmod_8_dirs(RWX);
/* Unlink files in other dirs */
try_unlink("d-wx/rwx");
try_unlink("dr-x/rwx");
try_unlink("drw-/rwx");
/* Unlink dirs */
for (i = 0; i < MODES; i++) {
strcpy(dirname, "d");
strcat(dirname, mode_fnames[i]);
/* 'dirname' contains the directoryname */
rmdir(dirname);
}
/* FINISH */
} /* clean_up_the_mess */
void chmod_8_dirs(sw)
int sw; /* if switch == 8, give all different
* mode,else the same mode */
{
int mode;
int i;
if (sw == 8)
mode = 0;
else
mode = sw;
for (i = 0; i < MODES; i++) {
chmod(mode_dir[i], 040000 + mode * 0100);
if (sw == 8) mode++;
}
}
0