/*various time functions tests for better tests coverage*/ void test_issue_128(){ int ret; struct timezone tz; struct timezone *tz_p=NULL; //pass null pointer instead NULL, to avoid warnings struct timeval *tv_p=NULL; TEST_OPERATION_RESULT( gettimeofday(tv_p, &tz), &ret, ret!=0&&errno==EFAULT); TEST_OPERATION_RESULT( gettimeofday(tv_p, tz_p), &ret, ret!=0&&errno==EFAULT); test_clock(); }
int main(int argc, char **argv) { int fd, ret; FILE *f, *f2; TEST_OPERATION_RESULT( open("/newfile123", O_CREAT|O_RDWR), &fd, fd!=-1&&errno==0 ); TEST_OPERATION_RESULT( fdopen(fd, "r"), &f, f!=NULL&&errno==0); TEST_OPERATION_RESULT( fdopen(fd, "w"), &f2, f2!=NULL&&errno==0); TEST_OPERATION_RESULT( close(fd), &ret, ret==0&&errno==0); test_zrt_issue_79(); }
void test_zrt_issue_70(){ /*Errno should be set to EBADF (9) when doing read, write, close with already closed files.*/ int fd, ret; char buf[2]; TEST_OPERATION_RESULT( open("foo2", O_RDWR|O_CREAT), &fd, ret!=-1&&errno==0); TEST_OPERATION_RESULT( close(fd), &ret, ret!=-1&&errno==0); TEST_OPERATION_RESULT( read(fd, buf, 1), &ret, ret==-1&&errno==EBADF); TEST_OPERATION_RESULT( write(fd, buf, 1), &ret, ret==-1&&errno==EBADF); TEST_OPERATION_RESULT( close(fd), &ret, ret==-1&&errno==EBADF); }
void zrt_test_issue71() { char filename[] = "@tmp_1_test"; int fd, ret; TEST_OPERATION_RESULT( open(filename, O_CREAT | O_RDWR), &fd, fd!=-1&&errno==0 ); TEST_OPERATION_RESULT( write(fd, "12345678901", 11), &ret, ret==11&&errno==0 ); TEST_OPERATION_RESULT( lseek(fd, -5, SEEK_END), &ret, ret==6&&errno==0); TEST_OPERATION_RESULT( tell(fd), &ret, ret==6&&errno==0); TEST_OPERATION_RESULT( close(fd), &ret, ret==0&&errno==0 ); REMOVE_EXISTING_FILEPATH(filename); }
void constr() { /*test constructor whatever was called*/ int ret; ++s_test_value; int res = fprintf(stderr, "constr value=%d\n", s_test_value); TEST_OPERATION_RESULT(res, &ret, ret>0); const char* envvar_testdir = getenv("TESTDIR"); TEST_OPERATION_RESULT(envvar_testdir!=NULL, &ret, ret==1); /*create directory to be access it at the main, relation of filesystem and constructor*/ mkpath_recursively(envvar_testdir, 0666); }
void zrt_test_issue72(){ char filename[] = "@tmp_1_test"; off_t LARGE = 2147483648; int fd, ret; TEST_OPERATION_RESULT( open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666), &fd, fd!=-1&&errno==0 ); off_t s = lseek(fd, LARGE, SEEK_SET); fprintf(stderr, "lseek=%lld\n", s); TEST_OPERATION_RESULT( s==LARGE&&errno==0, &ret, ret==1 ); s = tell(fd); fprintf(stderr, "tell=%lld\n", s); TEST_OPERATION_RESULT( s==LARGE&&errno==0, &ret, ret==1); TEST_OPERATION_RESULT( write(fd, "12345678901", 11), &ret, ret==11&&errno==0); TEST_OPERATION_RESULT( close(fd), &ret, ret==0&&errno==0 ); REMOVE_EXISTING_FILEPATH(filename); }
void constr2() { /*test constructor whatever was called*/ int ret; ++s_test_value; int res = fprintf(stderr, "constr2 value=%d\n", s_test_value); TEST_OPERATION_RESULT(res, &ret, ret>0); }
int main(int argc, char **argv) { int ret; FILE *f; const char* fname1 = "/seeker.data"; const char* fname2 = "/seeker2.data"; const char *str = "hip hop ololey"; const char *str2 = "opa-opa-opa-pa"; const char *str3 = "hip hop opa-opa-opa-pa"; zrt_test_issue71(); zrt_test_issue72(); zrt_test_issue_102_103(); f = fopen( fname1, "w" ); TEST_OPERATION_RESULT( f>0, &ret, ret==1); TEST_OPERATION_RESULT( fseek(f, -1, SEEK_SET), &ret, ret==-1&&errno==EINVAL); TEST_OPERATION_RESULT( fwrite(str, 1, strlen(str), f), &ret, ret==strlen(str)&&errno==0); TEST_OPERATION_RESULT( fseek( f, 0, SEEK_CUR ), &ret, ret==0&&errno==0); TEST_OPERATION_RESULT( ftell(f) == strlen(str), &ret, ret==1); TEST_OPERATION_RESULT( fseek( f, -6, SEEK_CUR ), &ret, ret==0); TEST_OPERATION_RESULT( ftell(f) == strlen(str)-6, &ret, ret==1); TEST_OPERATION_RESULT( fwrite(str2, 1, strlen(str2), f ), &ret, ret==strlen(str2)); //fseek( f, 0, SEEK_END ); /*zrt known issue 5.1: SEEK_END replace by NACL on SEEK_SET*/ fprintf(stderr, "file pos=%ld, expected pos=%u\n", ftell(f), strlen(str3) ); TEST_OPERATION_RESULT( ftell(f) == strlen(str3), &ret, ret==1); TEST_OPERATION_RESULT( fclose(f), &ret, ret==0 ); int fd = open( fname2, O_CREAT | O_WRONLY ); assert( fd >= 0 ); write(fd, str, strlen(str)); off_t off; off = lseek( fd, 0, SEEK_CUR ); assert( off == strlen(str) ); off = lseek( fd, -6, SEEK_CUR ); assert( off == strlen(str)-6 ); write(fd, str2, strlen(str2) ); off = lseek( fd, 0, SEEK_END ); fprintf(stderr, "file pos=%lld, expected pos=%u\n", off, strlen(str3) ); assert( off == strlen(str3) ); close(fd); return 0; }
int main(int argc, char **argv) { int ret; test_issue_128(); test_sleep(1, 0, 1, 0); test_nanosleep(0, 0, 0, 1000); /*if 0 time is passed it should take anyway 1 microsecond*/ test_nanosleep(0, 1998, 0, 1998); test_select_timeout(0, 0, 0, 1000); test_select_timeout(0, 1000, 0, 1000); { struct timespec ts, ts2; struct timeval tv_inc, tv, tv2; /*compare time before and after select, measure in nanoseconds*/ TEST_OPERATION_RESULT( clock_gettime(CLOCK_REALTIME, &ts), &ret, ret==0 ); /*select can only wait for timeout when no file descriptors specified*/ struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 1000; TEST_OPERATION_RESULT( select(0, NULL, NULL, NULL, &timeout), &ret, ret==0); timeout.tv_usec += 1; /*clock_gettime done +1 microsecond*/ TEST_OPERATION_RESULT( clock_gettime(CLOCK_REALTIME, &ts2), &ret, ret==0 ); TIMESPEC_TO_TIMEVAL(&tv, &ts); TIMESPEC_TO_TIMEVAL(&tv2, &ts2); timeradd(&tv, &timeout, &tv_inc); TEST_OPERATION_RESULT( timercmp(&tv_inc, &tv2, <= ), &ret, ret==1); /*select's synchronous multiplexing is supported now*/ fd_set readfds; FD_ZERO (&readfds); FD_SET (0, &readfds); TEST_OPERATION_RESULT( select(FD_SETSIZE, &readfds, NULL, NULL, &timeout), &ret, errno!=ENOSYS&&ret!=0); } return 0; }
int main(int argc, char **argv) { int ret; char* nullstr = NULL; TEST_OPERATION_RESULT( mkdir(nullstr, S_IRWXU), &ret, ret==-1&&errno==EINVAL ); TEST_OPERATION_RESULT( mkdir("/" DIR_NAME, S_IRWXU), &ret, ret==0 ); TEST_OPERATION_RESULT( mkdir(DIR_NAME, S_IRWXU), &ret, ret==-1&&errno==EEXIST ); CHECK_PATH_EXISTANCE("/" DIR_NAME); return 0; }
int main(int argc, char **argv) { int ret; TEST_OPERATION_RESULT(s_test_value, &ret, ret==2); const char* envvar_testdir = getenv("TESTDIR"); CHECK_PATH_EXISTANCE(envvar_testdir); fprintf(stderr, "main value=%d\n", s_test_value); return 0; }
void test_sleep(int sec, int nsec, int expected_sec, int expected_nsec){ int ret; struct timespec ts, expected_timeout_ts; /*save current time*/ prepare_time_test(&ts); /*wait until timeout exceeded*/ TEST_OPERATION_RESULT( sleep(sec), &ret, ret==0 ); expected_timeout_ts.tv_sec = expected_sec; expected_timeout_ts.tv_nsec = expected_nsec; /*check the passed amount time as expected*/ complete_nanosec_test(&ts, &expected_timeout_ts); }
int main(int argc, char**argv){ CREATE_FILE(TEST_FILE, DATA_FOR_FILE, DATASIZE_FOR_FILE); /*unlink*/ REMOVE_EXISTING_FILEPATH(TEST_FILE); CREATE_FILE(TEST_FILE2, DATA_FOR_FILE, DATASIZE_FOR_FILE); int ret; TEST_OPERATION_RESULT( link(TEST_FILE2, TEST_FILE), &ret, ret!=-1); /*now we have 2 hardlinks for a single file*/ /*compare files*/ int fd1, fd2; char *data1, *data2; off_t filesize1, filesize2; MMAP_READONLY_SHARED_FILE(TEST_FILE, 0, &fd1, data1); MMAP_READONLY_SHARED_FILE(TEST_FILE2, 0, &fd2, data2); GET_FILE_SIZE(TEST_FILE, &filesize1); GET_FILE_SIZE(TEST_FILE2, &filesize2); TEST_OPERATION_RESULT( (filesize1==filesize2), &ret, ret==1); CMP_MEM_DATA(data1, data2, filesize1); //check stat data struct stat st1; struct stat st2; TEST_OPERATION_RESULT( stat(TEST_FILE, &st1), &ret, ret==0); TEST_OPERATION_RESULT( stat(TEST_FILE2, &st2), &ret, ret==0); TEST_OPERATION_RESULT( st1.st_nlink==st2.st_nlink, &ret, ret!=0); TEST_OPERATION_RESULT( st1.st_ino==st2.st_ino, &ret, ret!=0); MUNMAP_FILE(data1, filesize1); MUNMAP_FILE(data2, filesize2); CLOSE_FILE(fd1); CLOSE_FILE(fd2); test_zrt_issue_67(TMP_TEST_DIR, TMP_TEST_FILE); test_zrt_issue_67(TMP_TEST_DIR, TMP_TEST_FILE); test_zrt_issue_70(); test_zrt_issue_77(TMP_TEST_DIR, TMP_TEST_FILE); return 0; }
void test_zrt_issue_77(const char* dirname, const char* name){ int fd; int ret; char fullpath[PATH_MAX]; int mode = O_RDWR|O_CREAT|O_EXCL; const char data[] = "sadkjhaskjdhaskj"; snprintf(fullpath, sizeof(fullpath), "%s/%s", dirname, name ); TEST_OPERATION_RESULT( mkdir(dirname, 0700), &ret, ret==0&&errno==0 ); TEST_OPERATION_RESULT( open(fullpath, mode, 0600), &fd, ret!=-1&&errno==0); TEST_OPERATION_RESULT( fcntl(fd, F_GETFD), &ret, ret==0&&errno==0 ); TEST_OPERATION_RESULT( fcntl(fd, F_SETFD, FD_CLOEXEC), &ret, ret==0&&errno==0 ); TEST_OPERATION_RESULT( unlink(fullpath), &ret, ret==0&&errno==0); TEST_OPERATION_RESULT( write(fd, data, sizeof(data)), &ret, ret==17&&errno==0); TEST_OPERATION_RESULT( close(fd), &ret, ret!=-1&&errno==0); TEST_OPERATION_RESULT( rmdir(dirname), &ret, ret==0&&errno==0 ); }
void test_select_timeout(int sec, int nsec, int expected_sec, int expected_nsec){ int ret; struct timespec ts, expected_timeout_ts; /*save current time*/ prepare_time_test(&ts); /*wait until timeout exceeded*/ struct timeval timeout_tv; timeout_tv.tv_sec = sec; timeout_tv.tv_usec = nsec/1000; TEST_OPERATION_RESULT( select(0, NULL, NULL, NULL, &timeout_tv), &ret, ret==0); expected_timeout_ts.tv_sec = expected_sec; expected_timeout_ts.tv_nsec = expected_nsec; /*check the passed amount time as expected*/ complete_nanosec_test(&ts, &expected_timeout_ts); }
void complete_nanosec_test(struct timespec *prepared_ts, struct timespec *timeout_ts){ int ret; struct timespec new_ts; timeout_ts->tv_nsec += 1000; /*clock_gettime take additional time*/ TEST_OPERATION_RESULT( clock_gettime(CLOCK_REALTIME, &new_ts), &ret, ret==0 ); struct timeval prepared_tv; TIMESPEC_TO_TIMEVAL(&prepared_tv, prepared_ts); struct timeval new_tv; TIMESPEC_TO_TIMEVAL(&new_tv, &new_ts); struct timeval timeout_tv; TIMESPEC_TO_TIMEVAL(&timeout_tv, timeout_ts); struct timeval tv_inc; timeradd(&prepared_tv, &timeout_tv, &tv_inc); TEST_OPERATION_RESULT( timercmp(&tv_inc, &new_tv, <= ), &ret, ret==1); }
int main(int argc, char **argv) { int res; int datalen, datalen1; char testpath[PATH_MAX]; snprintf(testpath, sizeof(testpath), "/test/%s", FILENAME_WITH_DYNAMIC_CONTENTS ); fprintf(stderr, "%s\n", testpath); /*Read files at mountpoint*/ char* contents1 = read_file_contents( testpath, &datalen1 ); CMP_MEM_DATA(MOUNT_CONTENTS, contents1, strlen(MOUNT_CONTENTS) ); char* contents = read_file_contents( FILENAME_WITH_DYNAMIC_CONTENTS, &datalen ); CMP_MEM_DATA(MOUNT_CONTENTS, contents, strlen(MOUNT_CONTENTS) ); TEST_OPERATION_RESULT(strcmp("1", getenv("new")), &res, res==0); struct stat st; TEST_OPERATION_RESULT( lstat("/dev/read-write", &st), &res, res==0 ); TEST_OPERATION_RESULT( (st.st_mode&S_IFCHR)==S_IFCHR, &res, res==1 ); res = zfork(); free(contents); free(contents1); /*After fork it is expected that this file remains unchanged*/ contents1 = read_file_contents( testpath, &datalen1 ); CMP_MEM_DATA(MOUNT_CONTENTS, contents1, strlen(MOUNT_CONTENTS) ); /*After fork it is expected that this file is changed*/ contents = read_file_contents( FILENAME_WITH_DYNAMIC_CONTENTS, &datalen ); CMP_MEM_DATA(REMOUNT_CONTENTS, contents, strlen(REMOUNT_CONTENTS) ); /*After fork env var changed*/ TEST_OPERATION_RESULT(strcmp("2", getenv("new")), &res, res==0); /*After fork channels mapping is changed*/ TEST_OPERATION_RESULT( lstat("/dev/read-write", &st), &res, res==0 ); TEST_OPERATION_RESULT( (st.st_mode&S_IFREG)==S_IFREG, &res, res==1 ); free(contents); free(contents1); return 0; }
void test_emu_devices_for_writing(){ int ret; char* emu_channel_name; char buf[10000]; int fd; /*test emulated channels*/ emu_channel_name = "/dev/zero"; TEST_OPERATION_RESULT( open(emu_channel_name, O_RDWR), &ret, ret != -1 ); fd = ret; TEST_OPERATION_RESULT( write(fd, buf, sizeof(buf)), &ret, ret == sizeof(buf) ); TEST_OPERATION_RESULT( close(fd), &ret, ret != -1 ); emu_channel_name = "/dev/null"; TEST_OPERATION_RESULT( open(emu_channel_name, O_RDWR), &ret, ret != -1 ); fd = ret; TEST_OPERATION_RESULT( write(fd, buf, sizeof(buf)), &ret, ret == sizeof(buf) ); TEST_OPERATION_RESULT( close(fd), &ret, ret != -1 ); emu_channel_name = "/dev/full"; TEST_OPERATION_RESULT( open(emu_channel_name, O_RDWR), &ret, ret != -1 ); fd = ret; TEST_OPERATION_RESULT( write(fd, buf, sizeof(buf)), &ret, ret==-1&&errno==ENOSPC ); TEST_OPERATION_RESULT( close(fd), &ret, ret != -1 ); emu_channel_name = "/dev/random"; TEST_OPERATION_RESULT( open(emu_channel_name, O_RDWR), &ret, ret != -1 ); fd = ret; TEST_OPERATION_RESULT( write(fd, buf, sizeof(buf)), &ret, ret == sizeof(buf) ); TEST_OPERATION_RESULT( close(fd), &ret, ret != -1 ); emu_channel_name = "/dev/urandom"; TEST_OPERATION_RESULT( open(emu_channel_name, O_RDONLY), &ret, ret != -1 ); fd = ret; TEST_OPERATION_RESULT( write(fd, buf, sizeof(buf)), &ret, ret == sizeof(buf) ); TEST_OPERATION_RESULT( close(fd), &ret, ret != -1 ); }
void test_writeonly_channel(const char* name){ int ret; int ret2; struct stat st; fprintf(stderr, "testing channel %s\n", name); /*test various of opens*/ TEST_OPERATION_RESULT( open(name, O_RDWR), &ret, ret == -1 ); TEST_OPERATION_RESULT( open(name, O_RDONLY), &ret, ret == -1 ); TEST_OPERATION_RESULT( open(name, O_WRONLY), &ret, ret != -1 ); /*test i/o*/ TEST_OPERATION_RESULT( lseek(ret, 0, SEEK_CUR), &ret2, ret2 == 0 ); TEST_OPERATION_RESULT( read(ret, s_buffer, 10), &ret2, ret2 == -1 ); TEST_OPERATION_RESULT( lseek(ret, 0, SEEK_CUR), &ret2, ret2 == 0 ); TEST_OPERATION_RESULT( write(ret, s_buffer, 10), &ret2, ret2 == 10 ); TEST_OPERATION_RESULT( lseek(ret, 0, SEEK_CUR), &ret2, ret2 == 10 ); TEST_OPERATION_RESULT( lseek(ret, 10, SEEK_SET), &ret2, ret2 == -1 ); TEST_OPERATION_RESULT( fstat(ret, &st), &ret2, ret2 != -1 ); TEST_OPERATION_RESULT( st.st_mode&S_IFMT, &ret2, ret2 == SEQUENTIAL_CHANNEL ); TEST_OPERATION_RESULT( close(ret), &ret2, ret2 != -1 ); TEST_OPERATION_RESULT( close(ret), &ret, ret == -1 ); TEST_OPERATION_RESULT( stat(name, &st), &ret2, ret2 != -1 ); TEST_OPERATION_RESULT( st.st_mode&S_IFMT, &ret2, ret2 == SEQUENTIAL_CHANNEL ); }
void prepare_time_test(struct timespec *ts){ int ret; /*compare time before and after timeout, measure in nanoseconds*/ TEST_OPERATION_RESULT( clock_gettime(CLOCK_REALTIME, ts), &ret, ret==0 ); }
void destr() { int ret; TEST_OPERATION_RESULT(--s_test_value, &ret, ret==1); fprintf(stderr, "destr value=%d\n", s_test_value); }
void test_zrt_issue_79(){ const char fdev[] = "/dev/null"; const char fname[] = "/newfile124"; char buf[PATH_MAX]; int fdr, fdw, ret; int fd1, fd2; /*Devices fs test*/ TEST_OPERATION_RESULT( open(fdev, O_WRONLY), &fd1, fd1!=-1&&errno==0 ); TEST_OPERATION_RESULT( open(fdev, O_WRONLY), &fd2, fd2!=-1&&errno==0 ); TEST_OPERATION_RESULT( write(fd1, fname, strlen(fname) ), &ret, ret!=-1&&ret==strlen(fname)&&errno==0 ); TEST_OPERATION_RESULT( write(fd2, fname, strlen(fname) ), &ret, ret!=-1&&ret==strlen(fname)&&errno==0 ); TEST_OPERATION_RESULT( close(fd1), &ret, ret==0&&errno==0); TEST_OPERATION_RESULT( close(fd2), &ret, ret==0&&errno==0); /*InMemory fs test*/ TEST_OPERATION_RESULT( open(fname, O_CREAT|O_WRONLY), &fdw, fdw!=-1&&errno==0 ); TEST_OPERATION_RESULT( open(fname, O_CREAT|O_RDONLY), &fdr, fdr!=-1&&errno==0 ); TEST_OPERATION_RESULT( write(fdw, fname, strlen(fname) ), &ret, ret!=-1&&ret==strlen(fname)&&errno==0 ); TEST_OPERATION_RESULT( read(fdr, buf, PATH_MAX ), &ret, ret!=-1&&ret==strlen(fname)&&errno==0 ); TEST_OPERATION_RESULT( close(fdw), &ret, ret==0&&errno==0); TEST_OPERATION_RESULT( close(fdr), &ret, ret==0&&errno==0); }
void test_zrt_issue_67(const char* dirname, const char* name){ //https://github.com/zerovm/zrt/issues/67 /*Since unlink returns no error if file in use, then unlinked file should not be available for getdents/stat; but must be available for fstat/read/write and another functions that get fd/FILE as argument.*/ int ret; struct stat st; char fullpath[PATH_MAX]; snprintf(fullpath, sizeof(fullpath), "%s/%s", dirname, name ); fprintf(stderr, "dirname=%s, fullpath=%s\n", dirname, fullpath); /*create test dir, and delete for final test */ TEST_OPERATION_RESULT( mkdir(dirname, 0700), &ret, ret==0&&errno==0); /*open file, now it's referenced and it's means that file in use*/ int fd = open(fullpath, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); TEST_OPERATION_RESULT( fd>=0&&errno==0, &ret, ret!=0); /*file can be accessed as dir item via getdents */ TEST_OPERATION_RESULT( match_file_inode_in_dir(dirname, name), &ret, ret!=-1); /*try to unlink file that in use - file removing is postponed */ TEST_OPERATION_RESULT( unlink(fullpath), &ret, ret==0&&errno==0); /*can be accessed by fd via fstat*/ TEST_OPERATION_RESULT( fstat(fd, &st), &ret, ret==0&&errno==0); /*can't be accessed by name via stat*/ CHECK_PATH_NOT_EXIST( name ); /*can't be accessed as dir item via getdents */ TEST_OPERATION_RESULT( match_file_inode_in_dir(dirname, name), &ret, ret==-1); errno=0; TEST_OPERATION_RESULT( write(fd, name, 2), &ret, ret==2&&errno==0 ) printf("res=%d err=%d\n", ret, errno); /*create file with the same name as unlink'ing now, it should be valid*/ { int fd2 = open(fullpath, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); TEST_OPERATION_RESULT( fd2>=0&&errno==0, &ret, ret!=0); CHECK_PATH_EXISTANCE( fullpath ); close(fd2); TEST_OPERATION_RESULT( unlink(fullpath), &ret, ret==0&&errno==0); } /*delete dir with opened file */ TEST_OPERATION_RESULT( rmdir(dirname), &ret, ret==0&&errno==0); TEST_OPERATION_RESULT( close(fd), &ret, ret==0&&errno==0); /*file closed, from now it should not be available at all*/ /*delete dir for final test */ TEST_OPERATION_RESULT( rmdir(dirname), &ret, ret==-1&&errno==ENOENT); /*can't be accessed by fd via fstat*/ TEST_OPERATION_RESULT( fstat(fd, &st), &ret, ret==-1&&errno==EBADF); }
void zrt_test_issue_102_103(){ const char* append_only_file = "/dev/read-write"; int fd, ret; TEST_OPERATION_RESULT( open(append_only_file, O_RDWR|O_CREAT|O_APPEND, 0666), &fd, fd!=-1&&errno==0 ); TEST_OPERATION_RESULT( write(fd, append_only_file, strlen(append_only_file)), &ret, ret==strlen(append_only_file)&&errno==0 ); TEST_OPERATION_RESULT( lseek(fd, 0, SEEK_END), &ret, ret==strlen(append_only_file)&&errno==0 ); TEST_OPERATION_RESULT( close(fd), &ret, ret==0&&errno==0 ); TEST_OPERATION_RESULT( open(append_only_file, O_RDWR, 0666), &fd, fd==-1&&errno==EPERM ); TEST_OPERATION_RESULT( open(append_only_file, O_RDWR|O_APPEND|O_TRUNC, 0666), &fd, fd==-1&&errno==EPERM ); TEST_OPERATION_RESULT( open(append_only_file, O_RDWR|O_APPEND, 0666), &fd, fd!=-1&&errno==0 ); TEST_OPERATION_RESULT( lseek(fd, 0, SEEK_END), &ret, ret==strlen(append_only_file)&&errno==0 ); TEST_OPERATION_RESULT( write(fd, append_only_file, strlen(append_only_file)), &ret, ret==strlen(append_only_file)&&errno==0 ); TEST_OPERATION_RESULT( lseek(fd, 0, SEEK_END), &ret, ret==(2*strlen(append_only_file))&&errno==0 ); TEST_OPERATION_RESULT( close(fd), &ret, ret==0&&errno==0 ); }
int main(int argc, char**argv){ int ret; struct stat st; //lstat test TEST_OPERATION_RESULT( lstat(TEST_FILE, &st), &ret, (ret==-1&&errno==ENOENT)); CREATE_FILE(TEST_FILE, DATA_FOR_FILE, DATASIZE_FOR_FILE); TEST_OPERATION_RESULT( lstat(TEST_FILE, &st), &ret, ret==0&&st.st_nlink==1); REMOVE_EXISTING_FILEPATH(TEST_FILE); ////////////////////////////////////// //stat test TEST_OPERATION_RESULT( stat(TEST_FILE, &st), &ret, (ret==-1&&errno==ENOENT)); fprintf(stderr, "errno=%d\n", errno);fflush(0); CREATE_FILE(TEST_FILE, DATA_FOR_FILE, DATASIZE_FOR_FILE); TEST_OPERATION_RESULT( stat(TEST_FILE, &st), &ret, ret==0&&st.st_nlink==1&&S_ISREG(st.st_mode)); CREATE_EMPTY_DIR(DIR_NAME); TEST_OPERATION_RESULT( stat(DIR_NAME, &st), &ret, ret==0&&st.st_nlink==2&&S_ISDIR(st.st_mode)); CREATE_EMPTY_DIR(DIR_NAME "/" DIR_NAME2); TEST_OPERATION_RESULT( stat(DIR_NAME, &st), &ret, ret==0&&st.st_nlink==3&&S_ISDIR(st.st_mode)!=0); TEST_OPERATION_RESULT( rmdir(DIR_NAME "/" DIR_NAME2), &ret, ret==0 ); TEST_OPERATION_RESULT( stat(DIR_NAME, &st), &ret, ret==0&&st.st_nlink==2); REMOVE_EXISTING_FILEPATH(TEST_FILE); /*mapping && file mode tests*/ TEST_OPERATION_RESULT( stat("/dev/stdin", &st), &ret, ret==0&& ((st.st_mode&S_IWUSR)!=S_IWUSR)&& ((st.st_mode&S_IRUSR)==S_IRUSR)&& S_ISCHR(st.st_mode)); TEST_OPERATION_RESULT( stat("/dev/stdout", &st), &ret, ret==0&& ((st.st_mode&S_IWUSR)==S_IWUSR)&& ((st.st_mode&S_IRUSR)!=S_IRUSR)&& S_ISFIFO(st.st_mode)); TEST_OPERATION_RESULT( stat("/dev/stderr", &st), &ret, ret==0&& ((st.st_mode&S_IWUSR)==S_IWUSR)&& ((st.st_mode&S_IRUSR)!=S_IRUSR)&& S_ISREG(st.st_mode)); TEST_OPERATION_RESULT( stat("/dev/read-write", &st), &ret, ret==0&& ((st.st_mode&S_IWUSR)==S_IWUSR)&& ((st.st_mode&S_IRUSR)==S_IRUSR)&& S_ISBLK(st.st_mode)); return 0; }