From 96ed429c6ab22bd1f38f286a8ef58c16751067e8 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 7 May 2015 10:01:10 +0200 Subject: added a test for open flag testing (O_TRUNC and O_EXCL so far) --- tests/Makefile | 8 ++++- tests/testopen.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 tests/testopen.c (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index f44570f..2e9f39b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -6,7 +6,7 @@ BLOCKSIZE = 4096 CFLAGS += -I.. -test: testfsync testpgsql testtypes testbigfile +test: testfsync testpgsql testtypes testbigfile testopen psql < clean.sql psql < ../schema.sql test -d mnt || mkdir mnt @@ -64,6 +64,8 @@ test: testfsync testpgsql testtypes testbigfile # the more human readable output of statvfs -df -h mnt -df -i mnt + # test open/creat and flags + -./testopen # END: unmount FUSE file system fusermount -u mnt @@ -72,6 +74,7 @@ clean: rm -f testpgsql testpgsql.o rm -f testtypes testtypes.o rm -f testbigfile testbigfile.o + rm -f testopen testopen.o testfsync: testfsync.o $(CC) -o testfsync testfsync.o @@ -96,3 +99,6 @@ testbigfile: testbigfile.o testbigfile.o: testbigfile.c $(CC) -c $(CFLAGS) -o testbigfile.o testbigfile.c + +testbigfile.o: testopen.c + $(CC) -c $(CFLAGS) -o testopen.o testopen.c diff --git a/tests/testopen.c b/tests/testopen.c new file mode 100644 index 0000000..c67a755 --- /dev/null +++ b/tests/testopen.c @@ -0,0 +1,101 @@ +/* + Copyright (C) 2012 Andreas Baumann + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include + +int main( void ) +{ + char buf[4096]; + int fd; + int res; + ssize_t wres; + struct stat s; + + /* Make sure no file is around from last invocation */ + (void)unlink( "./mnt/testopen" ); + + /* This should succeed */ + fd = open( "./mnt/testopen", O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR ); + if( fd < 0 ) { + perror( "Unable to create testopen" ); + return 1; + } + + /* write some data into it so we know if truncate really works */ + memset( buf, 0, 4096 ); + + wres = write( fd, buf, 4096 ); + if( wres != 4096 ) { + perror( "Error writing" ); + (void)close( fd ); + return 1; + } + + (void)close( fd ); + + /* check if the file has size 4096 */ + res = stat( "./mnt/testopen", &s ); + if( res < 0 ) { + perror( "Error stating testopen" ); + return 1; + } + if( s.st_size == 4096 ) { + // OK + } else { + fprintf( stderr, "Size of file should be 4096, but is %jd\n", s.st_size ); + return 1; + } + + /* Second one should fail with EEXIST */ + fd = open( "./mnt/testopen", O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR ); + if( fd < 0 && errno == EEXIST ) { + // OK + } else { + fprintf( stderr, "Second create succedded and should not" ); + return 1; + } + + /* Third one should success and truncate the file */ + fd = open( "./mnt/testopen", O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR ); + (void)close( fd ); + + /* check if the file has size zero */ + res = stat( "./mnt/testopen", &s ); + if( res < 0 ) { + perror( "Error stating testopen" ); + return 1; + } + if( s.st_size == 0 ) { + // OK + } else { + fprintf( stderr, "Size of file should be 0, but is %jd\n", s.st_size ); + return 1; + } + + res = unlink( "./mnt/testopen" ); + if( res < 0 ) { + perror( "Error removing testopen" ); + return 1; + } + + return 0; +} -- cgit v1.2.3-54-g00ecf