diff options
author | Joel Sherrill <joel.sherrill@OARcorp.com> | 2011-08-01 21:54:19 +0000 |
---|---|---|
committer | Joel Sherrill <joel.sherrill@OARcorp.com> | 2011-08-01 21:54:19 +0000 |
commit | a7c39d3d3fb4a529b29d432b81769617093965d8 (patch) | |
tree | d63d59fe676604f9095b3b742a1759c28d220f68 /testsuites/fstests/fsrdwr | |
parent | 2011-08-01 Xiang Cui <medivhc@gmail.com> (diff) | |
download | rtems-a7c39d3d3fb4a529b29d432b81769617093965d8.tar.bz2 |
2011-08-01 Xiang Cui <medivhc@gmail.com>
* imfs_fslink/Makefile.am, imfs_fssymlink/Makefile.am,
mimfs_fslink/Makefile.am, mimfs_fssymlink/Makefile.am,
mrfs_fslink/Makefile.am, mrfs_fssymlink/Makefile.am,
mrfs_support/fs_config.h: Correcting from previous commit of
incorrect tarball.
* fserror/fserror.doc, fserror/test.c, fspatheval/patheval.doc,
fspatheval/test.c, fspermission/fspermission.doc,
fspermission/test.c, fsrdwr/fsrdwr.doc, fsrdwr/init.c,
fstime/fstime.doc, fstime/test.c, imfs_fserror/.cvsignore,
imfs_fserror/Makefile.am, imfs_fslink/.cvsignore,
imfs_fspatheval/.cvsignore, imfs_fspatheval/Makefile.am,
imfs_fspermission/.cvsignore, imfs_fspermission/Makefile.am,
imfs_fsrdwr/.cvsignore, imfs_fsrdwr/Makefile.am,
imfs_fssymlink/.cvsignore, imfs_fstime/.cvsignore,
imfs_fstime/Makefile.am, imfs_support/fs_supprot.h,
mdosfs_fserror/.cvsignore, mdosfs_fserror/Makefile.am,
mdosfs_fspatheval/.cvsignore, mdosfs_fspatheval/Makefile.am,
mdosfs_fsrdwr/.cvsignore, mdosfs_fsrdwr/Makefile.am,
mdosfs_fstime/.cvsignore, mdosfs_fstime/Makefile.am,
mimfs_fserror/.cvsignore, mimfs_fserror/Makefile.am,
mimfs_fslink/.cvsignore, mimfs_fspatheval/.cvsignore,
mimfs_fspatheval/Makefile.am, mimfs_fspermission/.cvsignore,
mimfs_fspermission/Makefile.am, mimfs_fsrdwr/.cvsignore,
mimfs_fsrdwr/Makefile.am, mimfs_fssymlink/.cvsignore,
mimfs_fstime/.cvsignore, mimfs_fstime/Makefile.am,
mrfs_fserror/.cvsignore, mrfs_fserror/Makefile.am,
mrfs_fslink/.cvsignore, mrfs_fspatheval/.cvsignore,
mrfs_fspatheval/Makefile.am, mrfs_fspermission/.cvsignore,
mrfs_fspermission/Makefile.am, mrfs_fsrdwr/.cvsignore,
mrfs_fsrdwr/Makefile.am, mrfs_fssymlink/.cvsignore,
mrfs_fstime/.cvsignore, mrfs_fstime/Makefile.am: New files.
Diffstat (limited to 'testsuites/fstests/fsrdwr')
-rw-r--r-- | testsuites/fstests/fsrdwr/fsrdwr.doc | 25 | ||||
-rw-r--r-- | testsuites/fstests/fsrdwr/init.c | 519 |
2 files changed, 544 insertions, 0 deletions
diff --git a/testsuites/fstests/fsrdwr/fsrdwr.doc b/testsuites/fstests/fsrdwr/fsrdwr.doc new file mode 100644 index 0000000000..7b0338f629 --- /dev/null +++ b/testsuites/fstests/fsrdwr/fsrdwr.doc @@ -0,0 +1,25 @@ +# +# $Id$ +# +# COPYRIGHT (c) 1989-2011. +# On-Line Applications Research Corporation (OAR). +# +# The license and distribution terms for this file may be +# found in the file LICENSE in this distribution or at +# http://www.rtems.com/license/LICENSE. +# + +This file describes the directives and concepts tested by this test set. + +test set name: fsrdwr + +directives: + ++ open ++ read ++ write ++ lseek + +concepts: + ++ Simlpe read and write test. diff --git a/testsuites/fstests/fsrdwr/init.c b/testsuites/fstests/fsrdwr/init.c new file mode 100644 index 0000000000..6565eb9415 --- /dev/null +++ b/testsuites/fstests/fsrdwr/init.c @@ -0,0 +1,519 @@ +/* + * COPYRIGHT (c) 1989-2011. + * On-Line Applications Research Corporation (OAR). + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id Exp $ + */ +#include <sys/stat.h> +#include <string.h> +#include <stdlib.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <memory.h> +#include <unistd.h> + +#include "fstest.h" + +const char *databuf = + "Happy days are here again. Happy days are here again.1Happy " + "days are here again.2Happy days are here again.3Happy days are here again." + "4Happy days are here again.5Happy days are here again.6Happy days are here " + "again.7Happy days are here again."; + +void read_write_test (void) +{ + + int fd; + int status; + char *name01 = "name01"; + char *name02 = "name02"; + struct stat statbuf; + char *readbuf; + size_t len = strlen (databuf); + off_t pos = 0; + + int n; + mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; + + + const char* wd=__func__; + + /* + * Create a new directory and change the current directory to this + */ + status=mkdir(wd,mode); + rtems_test_assert(status==0); + status=chdir(wd); + rtems_test_assert(status==0); + /* + * Create an empty file + */ + fd = open (name01, O_CREAT | O_WRONLY, mode); + status = close (fd); + rtems_test_assert (status == 0); + /* + * Verify the empty file + */ + status = stat (name01, &statbuf); + rtems_test_assert (status == 0); + + rtems_test_assert (S_ISREG (statbuf.st_mode)); + rtems_test_assert (statbuf.st_size == 0); + + /* + * Write data to the empty file + */ + fd = open (name01, O_WRONLY); + rtems_test_assert (fd != -1); + + n = write (fd, databuf, len); + rtems_test_assert (n == len); + status = close (fd); + rtems_test_assert (status == 0); + + status = stat (name01, &statbuf); + rtems_test_assert (status == 0); + rtems_test_assert (S_ISREG (statbuf.st_mode)); + rtems_test_assert (statbuf.st_size == len); + + /* + * Read the data from the file + */ + readbuf = (char *) malloc (len + 1); + rtems_test_assert (readbuf); + + fd = open (name01, O_RDONLY); + rtems_test_assert (fd != -1); + n = read (fd, readbuf, len); + rtems_test_assert (n == len); + rtems_test_assert (!strncmp (databuf, readbuf, len)); + status = close (fd); + rtems_test_assert (status == 0); + + /* + * Open the file using O_APPEND and write the data + */ + memset (readbuf, 0, len + 1); + fd = open (name01, O_WRONLY | O_APPEND); + n = write (fd, databuf, len); + rtems_test_assert (n == len); + pos = lseek (fd, 0, SEEK_CUR); + rtems_test_assert (pos == 2 * len); + status = close (fd); + rtems_test_assert (status == 0); + + /* + * Read the data and verify it + */ + fd = open (name01, O_RDONLY); + rtems_test_assert (fd != -1); + n = read (fd, readbuf, len); + rtems_test_assert (n == len); + rtems_test_assert (!strncmp (databuf, readbuf, len)); + + n = read (fd, readbuf, len); + rtems_test_assert (n == len); + rtems_test_assert (!strncmp (databuf, readbuf, len)); + status = close (fd); + rtems_test_assert (status == 0); + + /* + * Open the file using O_RDWR + */ + memset (readbuf, 0, len + 1); + + fd = open (name01, O_RDWR); + n = write (fd, databuf, len); + rtems_test_assert (n == len); + pos = lseek (fd, 0, SEEK_CUR); + rtems_test_assert (pos == len); + n = read (fd, readbuf, len); + rtems_test_assert (n == len); + rtems_test_assert (!strncmp (databuf, readbuf, len)); + pos = lseek (fd, 0, SEEK_CUR); + rtems_test_assert (pos == 2 * len); + status = close (fd); + rtems_test_assert (status == 0); + + /* + * Open the file using O_TRUNC + */ + + fd = open (name01, O_WRONLY | O_TRUNC); + status = close (fd); + rtems_test_assert (status == 0); + + /* + * Verify if the length is zero + */ + status = stat (name01, &statbuf); + rtems_test_assert (status == 0); + rtems_test_assert (S_ISREG (statbuf.st_mode)); + rtems_test_assert (statbuf.st_size == 0); + + /* + * Open a directory + */ + status = mkdir (name02, mode); + rtems_test_assert (status == 0); + fd = open (name02, O_RDONLY); + rtems_test_assert (fd != -1); + + status = close (fd); + rtems_test_assert (status == 0); + + free (readbuf); + + /* + * Go back to parent directory + */ + status=chdir(".."); + rtems_test_assert(status==0); +} + +void truncate_test03(void ) +{ + + int fd; + int status; + char *name01="name01"; + struct stat statbuf; + + char data; + int n; + int i; + + size_t len = strlen(databuf); + + char *readbuf; + off_t good_size=100; + mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO; + + + const char* wd=__func__; + + /* + * Create a new directory and change the current directory to this + */ + status=mkdir(wd,mode); + rtems_test_assert(status==0); + status=chdir(wd); + rtems_test_assert(status==0); + + /* + * Create an empty file + */ + fd=creat(name01,mode); + status=close(fd); + rtems_test_assert(status==0); + + + /* + * Truncate it to a valid size + */ + status=truncate(name01,good_size); + rtems_test_assert(status==0); + /* + * Verify the size and the data + */ + status=stat(name01,&statbuf); + rtems_test_assert(status==0); + rtems_test_assert(good_size==statbuf.st_size); + + fd=open(name01,O_RDONLY); + while ((n=read(fd,&data,1))>0) { + rtems_test_assert(data==0); + } + + status=close(fd); + rtems_test_assert(status==0); + + /* + * Fill a file with data + */ + fd=open(name01,O_WRONLY); + rtems_test_assert(fd!=-1); + n=write(fd,databuf,len); + rtems_test_assert(n==len); + + /* + * Truncate it to the half size + */ + + status=truncate(name01,len/2); + status=truncate(name01,len); + + /* + * verify the data + */ + readbuf = (char *)malloc(len/2); + rtems_test_assert( readbuf ); + fd=open(name01,O_RDONLY); + rtems_test_assert(fd!=-1); + n=read(fd,readbuf,len/2); + rtems_test_assert(n==len/2); + rtems_test_assert(!strncmp(databuf,readbuf,len/2)); + n=read(fd,readbuf,len/2); + rtems_test_assert(n==len/2); + for (i=0;i<len/2;i++){ + rtems_test_assert(readbuf[i]==0); + } + status=close(fd); + rtems_test_assert( status==0 ); + + /* + * Go back to parent directory + */ + status=chdir(".."); + rtems_test_assert(status==0); +} + +void lseek_test(void) +{ + int fd; + int status; + char *name01="test_name01"; + struct stat statbuf; + + int n; + int i; + + size_t len = strlen(databuf); + off_t pos; + int total_written=0; + + char *readbuf; + mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO; + + + + const char* wd=__func__; + + /* + * Create a new directory and change the current directory to this + */ + status=mkdir(wd,mode); + rtems_test_assert(status==0); + status=chdir(wd); + rtems_test_assert(status==0); + + /* + * Create a file and fill with the data. + */ + puts("Create a new file"); + fd=creat(name01,mode); + rtems_test_assert(fd!=-1); + + pos=lseek(fd,0,SEEK_CUR); + rtems_test_assert(pos==0); + + pos=lseek(fd,0,SEEK_END); + rtems_test_assert(pos==0); + + pos=lseek(fd,0,SEEK_SET); + rtems_test_assert(pos==0); + + + printf( "Writing %d bytes to file\n", len * 10 ); + for (i=0; i<10; i++) { + n = write(fd, databuf, len); + rtems_test_assert( n != -1 ); + total_written += n; + } + printf("Successfully wrote %d\n", total_written); + + /* + * Check the current position + */ + puts("Check the current position"); + pos=lseek(fd,0,SEEK_CUR); + rtems_test_assert(pos==total_written); + + pos=lseek(fd,0,SEEK_END); + rtems_test_assert(pos==total_written); + + /* + * ftruncate shall not change the posistion + */ + status=ftruncate(fd,total_written+1); + rtems_test_assert(status==0); + + pos=lseek(fd,0,SEEK_CUR); + rtems_test_assert(pos==total_written); + + pos=lseek(fd,0,SEEK_END); + printf("%jd\n",(intmax_t)pos); + rtems_test_assert(pos==total_written+1); + + status=ftruncate(fd,total_written); + rtems_test_assert(status==0); + + pos=lseek(fd,0,SEEK_CUR); + rtems_test_assert(pos==total_written+1); + + + status=close(fd); + rtems_test_assert(status==0); + + /* + * Check the file size + */ + status=stat(name01,&statbuf); + rtems_test_assert(statbuf.st_size==total_written); + + /* + * Open the file with O_RDONLY and check the lseek + */ + readbuf=(char*)malloc(len); + fd=open(name01,O_RDONLY); + pos=lseek(fd,len,SEEK_CUR); + rtems_test_assert(pos==len); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + pos=lseek(fd,len,SEEK_CUR); + rtems_test_assert(pos==3*len); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + pos=lseek(fd,-len,SEEK_CUR); + rtems_test_assert(pos==3*len); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + pos=lseek(fd,4*len,SEEK_SET); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + + pos=lseek(fd,10,SEEK_SET); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(strncmp(databuf,readbuf,len)!=0); + + pos=lseek(fd,-len,SEEK_END); + n=read(fd,readbuf,2*len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + status=close(fd); + rtems_test_assert(status==0); + + /* + * Open the file withe O_RDWR and check the lseek + */ + + fd=open(name01,O_RDWR); + + pos=lseek(fd,len,SEEK_CUR); + rtems_test_assert(pos==len); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + pos=lseek(fd,len,SEEK_CUR); + rtems_test_assert(pos==3*len); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + pos=lseek(fd,-len,SEEK_CUR); + rtems_test_assert(pos==3*len); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + pos=lseek(fd,4*len,SEEK_SET); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + /* + * Go to the wrong position, so the data is not the same + */ + pos=lseek(fd,10,SEEK_SET); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(strncmp(databuf,readbuf,len)!=0); + + /* + * Use SEEK_END + */ + pos=lseek(fd,-len,SEEK_END); + n=read(fd,readbuf,2*len); + rtems_test_assert(n==len); + rtems_test_assert(!strncmp(databuf,readbuf,len)); + + memset(readbuf,0,len); + + /* + * Write the zero to the end of file. + */ + pos=lseek(fd,-len,SEEK_END); + rtems_test_assert(pos==total_written-len); + n=write(fd,readbuf,len); + rtems_test_assert(n==len); + /* + * Verify it + */ + pos=lseek(fd,total_written-len,SEEK_SET); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + for (i=0;i<n;i++){ + rtems_test_assert(readbuf[i]==0); + } + + /* + * Write the zero to the beginning of file. + */ + pos=lseek(fd,-total_written,SEEK_END); + rtems_test_assert(pos==0); + n=write(fd,readbuf,len); + rtems_test_assert(n==len); + + /* + * Verify it + */ + + pos=lseek(fd,0,SEEK_SET); + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + for (i=0;i<n;i++){ + rtems_test_assert(readbuf[i]==0); + } + + n=read(fd,readbuf,len); + rtems_test_assert(n==len); + rtems_test_assert(strncmp(databuf,readbuf,len)==0); + /* + * Call ftruncate to decrease the file and the position not change + */ + status=ftruncate(fd,len); + rtems_test_assert(status==0); + pos=lseek(fd,0,SEEK_CUR); + rtems_test_assert(pos==len*2); + + status=close(fd); + rtems_test_assert(status==0); + /* + * Go back to parent directory + */ + status=chdir(".."); + rtems_test_assert(status==0); + +} +void test(void ) +{ + read_write_test(); + lseek_test(); +} |