2014-11-19 16:16:43 +01:00
# include "FuseTest.h"
2019-10-13 17:49:57 +02:00
using : : testing : : Eq ;
2014-11-25 18:43:50 +01:00
using : : testing : : Return ;
2014-11-27 16:51:15 +01:00
using : : testing : : Throw ;
using : : testing : : Action ;
using : : testing : : Invoke ;
2018-12-03 07:57:21 +01:00
using std : : make_shared ;
using std : : shared_ptr ;
2014-11-27 16:51:15 +01:00
2015-06-18 19:30:52 +02:00
using cpputils : : unique_ref ;
using cpputils : : make_unique_ref ;
2014-11-27 16:51:15 +01:00
namespace bf = boost : : filesystem ;
2014-11-28 14:46:45 +01:00
using namespace fspp : : fuse ;
2014-11-25 15:01:33 +01:00
MockFilesystem : : MockFilesystem ( ) { }
MockFilesystem : : ~ MockFilesystem ( ) { }
2014-11-25 18:43:50 +01:00
2019-10-13 13:29:59 +02:00
FuseTest : : FuseTest ( ) : fsimpl ( make_shared < MockFilesystem > ( ) ) , _context ( boost : : none ) {
2021-04-21 08:09:00 +02:00
using : : testing : : _ ;
2014-11-27 16:51:15 +01:00
auto defaultAction = Throw ( FuseErrnoException ( EIO ) ) ;
2018-12-03 07:57:21 +01:00
ON_CALL ( * fsimpl , openFile ( _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , closeFile ( _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , lstat ( _ , _ ) ) . WillByDefault ( Throw ( FuseErrnoException ( ENOENT ) ) ) ;
ON_CALL ( * fsimpl , fstat ( _ , _ ) ) . WillByDefault ( Throw ( FuseErrnoException ( ENOENT ) ) ) ;
ON_CALL ( * fsimpl , truncate ( _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , ftruncate ( _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , read ( _ , _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , write ( _ , _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , fsync ( _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , fdatasync ( _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , access ( _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , createAndOpenFile ( _ , _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , mkdir ( _ , _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , rmdir ( _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , unlink ( _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , rename ( _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , readDir ( _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , utimens ( _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , statfs ( _ ) ) . WillByDefault ( Invoke ( [ ] ( struct statvfs * result ) {
2016-02-14 01:01:59 +01:00
: : statvfs ( " / " , result ) ; // As dummy value take the values from the root filesystem
} ) ) ;
2018-12-03 07:57:21 +01:00
ON_CALL ( * fsimpl , chmod ( _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , chown ( _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , createSymlink ( _ , _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
ON_CALL ( * fsimpl , readSymlink ( _ , _ , _ ) ) . WillByDefault ( defaultAction ) ;
2016-02-14 02:23:54 +01:00
2018-12-03 07:57:21 +01:00
EXPECT_CALL ( * fsimpl , access ( _ , _ ) ) . WillRepeatedly ( Return ( ) ) ;
2019-10-13 13:29:59 +02:00
ON_CALL ( * fsimpl , setContext ( _ ) ) . WillByDefault ( Invoke ( [ this ] ( fspp : : Context context ) {
_context = std : : move ( context ) ;
} ) ) ;
2016-02-14 01:54:36 +01:00
ReturnIsDirOnLstat ( " / " ) ;
2014-11-27 16:51:15 +01:00
}
2019-10-13 13:29:59 +02:00
unique_ref < FuseTest : : TempTestFS > FuseTest : : TestFS ( const std : : vector < std : : string > & fuseOptions ) {
return make_unique_ref < TempTestFS > ( fsimpl , fuseOptions ) ;
2014-11-27 16:51:15 +01:00
}
2019-10-13 13:29:59 +02:00
FuseTest : : TempTestFS : : TempTestFS ( shared_ptr < MockFilesystem > fsimpl , const std : : vector < std : : string > & fuseOptions )
2018-12-03 07:57:21 +01:00
: _mountDir ( ) ,
2018-12-09 18:27:53 +01:00
_fuse ( [ fsimpl ] ( Fuse * ) { return fsimpl ; } , [ ] { } , " fusetest " , boost : : none ) , _fuse_thread ( & _fuse ) {
2018-12-03 07:57:21 +01:00
2019-10-13 13:29:59 +02:00
_fuse_thread . start ( _mountDir . path ( ) , fuseOptions ) ;
2014-11-27 16:51:15 +01:00
}
FuseTest : : TempTestFS : : ~ TempTestFS ( ) {
_fuse_thread . stop ( ) ;
}
const bf : : path & FuseTest : : TempTestFS : : mountDir ( ) const {
return _mountDir . path ( ) ;
}
2019-10-13 17:49:57 +02:00
Action < void ( const boost : : filesystem : : path & , fspp : : fuse : : STAT * ) > FuseTest : : ReturnIsFileWithSize ( fspp : : num_bytes_t size ) {
return Invoke ( [ size ] ( const boost : : filesystem : : path & , fspp : : fuse : : STAT * result ) {
2014-11-27 16:51:15 +01:00
result - > st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH ;
result - > st_nlink = 1 ;
2018-09-15 23:32:58 +02:00
result - > st_size = size . value ( ) ;
2014-11-27 16:51:15 +01:00
} ) ;
}
//TODO Combine ReturnIsFile and ReturnIsFileFstat. This should be possible in gmock by either (a) using ::testing::Undefined as parameter type or (b) using action macros
2019-10-13 17:49:57 +02:00
Action < void ( const boost : : filesystem : : path & , fspp : : fuse : : STAT * ) > FuseTest : : ReturnIsFile = ReturnIsFileWithSize ( fspp : : num_bytes_t ( 0 ) ) ;
2014-11-27 16:51:15 +01:00
2018-09-16 03:02:03 +02:00
Action < void ( int , fspp : : fuse : : STAT * ) > FuseTest : : ReturnIsFileFstat =
Invoke ( [ ] ( int , fspp : : fuse : : STAT * result ) {
2014-11-27 16:51:15 +01:00
result - > st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH ;
result - > st_nlink = 1 ;
} ) ;
2018-09-16 03:02:03 +02:00
Action < void ( int , fspp : : fuse : : STAT * ) > FuseTest : : ReturnIsFileFstatWithSize ( fspp : : num_bytes_t size ) {
2015-11-27 17:43:21 +01:00
return Invoke ( [ size ] ( int , struct : : stat * result ) {
result - > st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH ;
result - > st_nlink = 1 ;
2018-09-15 23:32:58 +02:00
result - > st_size = size . value ( ) ;
2015-11-27 17:43:21 +01:00
} ) ;
}
2019-10-13 17:49:57 +02:00
Action < void ( const boost : : filesystem : : path & , fspp : : fuse : : STAT * ) > FuseTest : : ReturnIsDir =
Invoke ( [ ] ( const boost : : filesystem : : path & , fspp : : fuse : : STAT * result ) {
2014-11-27 16:51:15 +01:00
result - > st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH ;
result - > st_nlink = 1 ;
} ) ;
2019-10-13 17:49:57 +02:00
Action < void ( const boost : : filesystem : : path & , fspp : : fuse : : STAT * ) > FuseTest : : ReturnDoesntExist = Throw ( fspp : : fuse : : FuseErrnoException ( ENOENT ) ) ;
2014-11-27 16:51:15 +01:00
2014-11-25 18:43:50 +01:00
void FuseTest : : OnOpenReturnFileDescriptor ( const char * filename , int descriptor ) {
2021-04-21 08:09:00 +02:00
EXPECT_CALL ( * fsimpl , openFile ( Eq ( filename ) , testing : : _ ) ) . Times ( 1 ) . WillOnce ( Return ( descriptor ) ) ;
2014-11-25 18:43:50 +01:00
}
void FuseTest : : ReturnIsFileOnLstat ( const bf : : path & path ) {
2019-10-13 17:49:57 +02:00
EXPECT_CALL ( * fsimpl , lstat ( Eq ( path ) , : : testing : : _ ) ) . WillRepeatedly ( ReturnIsFile ) ;
2014-11-25 18:43:50 +01:00
}
2018-09-15 23:32:58 +02:00
void FuseTest : : ReturnIsFileOnLstatWithSize ( const bf : : path & path , const fspp : : num_bytes_t size ) {
2019-10-13 17:49:57 +02:00
EXPECT_CALL ( * fsimpl , lstat ( Eq ( path ) , : : testing : : _ ) ) . WillRepeatedly ( ReturnIsFileWithSize ( size ) ) ;
2014-11-27 03:48:38 +01:00
}
2014-11-25 18:43:50 +01:00
void FuseTest : : ReturnIsDirOnLstat ( const bf : : path & path ) {
2019-10-13 17:49:57 +02:00
EXPECT_CALL ( * fsimpl , lstat ( Eq ( path ) , : : testing : : _ ) ) . WillRepeatedly ( ReturnIsDir ) ;
2014-11-25 18:43:50 +01:00
}
void FuseTest : : ReturnDoesntExistOnLstat ( const bf : : path & path ) {
2019-10-13 17:49:57 +02:00
EXPECT_CALL ( * fsimpl , lstat ( Eq ( path ) , : : testing : : _ ) ) . WillRepeatedly ( ReturnDoesntExist ) ;
2014-11-25 18:43:50 +01:00
}
void FuseTest : : ReturnIsFileOnFstat ( int descriptor ) {
2021-04-21 08:09:00 +02:00
EXPECT_CALL ( * fsimpl , fstat ( descriptor , testing : : _ ) ) . WillRepeatedly ( ReturnIsFileFstat ) ;
2014-11-25 18:43:50 +01:00
}
2015-11-27 17:43:21 +01:00
2018-09-15 23:32:58 +02:00
void FuseTest : : ReturnIsFileOnFstatWithSize ( int descriptor , fspp : : num_bytes_t size ) {
2021-04-21 08:09:00 +02:00
EXPECT_CALL ( * fsimpl , fstat ( descriptor , testing : : _ ) ) . WillRepeatedly ( ReturnIsFileFstatWithSize ( size ) ) ;
2019-10-13 14:16:06 +02:00
}