int main () { /* Don't change the order of the calls. They partly depend on each other */ test_open (); test_write (); test_read (); test_lseek (); test_close (); test_stat (); test_fstat (); test_isatty (); test_system (); test_rename (); test_unlink (); test_time (); return 0; }
int main(int argc, char * argv[]) { (void) argc; (void) argv; printf("\n**************************\n"); printf("Errno Handling Test\n"); printf("***************************\n"); test_open(); test_close(); test_write(); test_read(); test_lseek(); test_dup2(); return 0; }
int main(int argc, char *argv[]) { strcpy(temp_dir, "/tmp/fifo_misc.XXXXXXXXXXX"); if (mkdtemp(temp_dir) == NULL) err(-1, "mkdtemp"); atexit(atexit_temp_dir); if (chdir(temp_dir) < 0) err(-1, "chdir %s", temp_dir); test_lseek(); test_truncate(); test_ioctl(); return (0); }
static void vfstest_read(void) { #define READ_BUFSIZE 256 int fd, ret; char buf[READ_BUFSIZE]; struct stat s; syscall_success(mkdir("read", 0777)); syscall_success(chdir("read")); /* Can read and write to a file */ syscall_success(fd = open("file01", O_RDWR | O_CREAT, 0)); syscall_success(ret = write(fd, "hello", 5)); test_assert(5 == ret, "write(%d, \"hello\", 5) returned %d", fd, ret); syscall_success(ret = lseek(fd, 0, SEEK_SET)); test_assert(0 == ret, "lseek(%d, 0, SEEK_SET) returned %d", fd, ret); read_fd(fd, READ_BUFSIZE, "hello"); syscall_success(close(fd)); /* cannot read from a directory */ syscall_success(mkdir("dir01", 0)); syscall_success(fd = open("dir01", O_RDONLY, 0)); syscall_fail(read(fd, buf, READ_BUFSIZE), EISDIR); syscall_success(close(fd)); /* Can seek to beginning, middle, and end of file */ syscall_success(fd = open("file02", O_RDWR | O_CREAT, 0)); syscall_success(write(fd, "hello", 5)); #define test_lseek(expr, res) \ do { \ int __r = (expr); \ test_assert((res) == __r, # expr " returned %d, expected %d", __r, res);\ } while (0); test_lseek(lseek(fd, 0, SEEK_CUR), 5); read_fd(fd, 10, ""); test_lseek(lseek(fd, -1, SEEK_CUR), 4); read_fd(fd, 10, "o"); test_lseek(lseek(fd, 2, SEEK_CUR), 7); read_fd(fd, 10, ""); syscall_fail(lseek(fd, -8, SEEK_CUR), EINVAL); test_lseek(lseek(fd, 0, SEEK_SET), 0); read_fd(fd, 10, "hello"); test_lseek(lseek(fd, 3, SEEK_SET), 3); read_fd(fd, 10, "lo"); test_lseek(lseek(fd, 7, SEEK_SET), 7); read_fd(fd, 10, ""); syscall_fail(lseek(fd, -1, SEEK_SET), EINVAL); test_lseek(lseek(fd, 0, SEEK_END), 5); read_fd(fd, 10, ""); test_lseek(lseek(fd, -2, SEEK_END), 3); read_fd(fd, 10, "lo"); test_lseek(lseek(fd, 3, SEEK_END), 8); read_fd(fd, 10, ""); syscall_fail(lseek(fd, -8, SEEK_END), EINVAL); syscall_fail(lseek(fd, 0, SEEK_SET + SEEK_CUR + SEEK_END), EINVAL); syscall_success(close(fd)); /* O_APPEND works properly */ create_file("file03"); syscall_success(fd = open("file03", O_RDWR, 0)); test_fpos(fd, 0); syscall_success(write(fd, "hello", 5)); test_fpos(fd, 5); syscall_success(close(fd)); syscall_success(fd = open("file03", O_RDWR | O_APPEND, 0)); test_fpos(fd, 0); syscall_success(write(fd, "hello", 5)); test_fpos(fd, 10); syscall_success(lseek(fd, 0, SEEK_SET)); test_fpos(fd, 0); read_fd(fd, 10, "hellohello"); syscall_success(lseek(fd, 5, SEEK_SET)); test_fpos(fd, 5); syscall_success(write(fd, "again", 5)); test_fpos(fd, 15); syscall_success(lseek(fd, 0, SEEK_SET)); test_fpos(fd, 0); read_fd(fd, 15, "hellohelloagain"); syscall_success(close(fd)); /* seek and write beyond end of file */ create_file("file04"); syscall_success(fd = open("file04", O_RDWR, 0)); syscall_success(write(fd, "hello", 5)); test_fpos(fd, 5); test_lseek(lseek(fd, 10, SEEK_SET), 10); syscall_success(write(fd, "again", 5)); syscall_success(stat("file04", &s)); test_assert(s.st_size == 15, "actual size: %d", s.st_size); test_lseek(lseek(fd, 0, SEEK_SET), 0); test_assert(15 == read(fd, buf, READ_BUFSIZE), "unexpected number of bytes read"); test_assert(0 == memcmp(buf, "hello\0\0\0\0\0again", 15), "unexpected data read"); syscall_success(close(fd)); syscall_success(chdir("..")); }