2014-11-19 00:25:57 +01:00
# pragma once
2015-10-15 13:04:57 +02:00
# ifndef MESSMER_FSPP_TEST_TESTUTILS_FUSETEST_H_
# define MESSMER_FSPP_TEST_TESTUTILS_FUSETEST_H_
2014-11-19 00:25:57 +01:00
2016-02-11 12:53:42 +01:00
# include <gtest/gtest.h>
# include <gmock/gmock.h>
2014-11-19 00:25:57 +01:00
2016-02-11 12:53:42 +01:00
# include "fspp/fuse/Filesystem.h"
2018-05-21 08:18:34 +02:00
# include "fspp/fs_interface/FuseErrnoException.h"
2016-02-11 12:53:42 +01:00
# include "fspp/fuse/Fuse.h"
# include "fspp/fs_interface/Dir.h"
2014-11-19 00:25:57 +01:00
# include <boost/filesystem.hpp>
2016-02-11 12:53:42 +01:00
# include <cpp-utils/tempfile/TempDir.h>
2014-11-19 00:40:46 +01:00
# include "FuseThread.h"
2014-11-19 00:25:57 +01:00
# define MOCK_PATH_METHOD1(NAME, RETURNTYPE) \
2014-11-27 16:51:15 +01:00
RETURNTYPE NAME ( const boost : : filesystem : : path & path ) override { \
2018-05-21 01:20:38 +02:00
return NAME ( path . string ( ) . c_str ( ) ) ; \
2014-11-19 00:25:57 +01:00
} \
2017-12-03 20:01:41 +01:00
MOCK_METHOD1 ( NAME , RETURNTYPE ( const char * ) ) \
2014-11-19 00:25:57 +01:00
2014-11-27 16:51:15 +01:00
# define MOCK_PATH_METHOD2(NAME, RETURNTYPE, PARAM1) \
2014-11-19 00:25:57 +01:00
RETURNTYPE NAME ( const boost : : filesystem : : path & path , PARAM1 param1 ) override { \
2018-05-21 01:20:38 +02:00
return NAME ( path . string ( ) . c_str ( ) , param1 ) ; \
2014-11-27 16:51:15 +01:00
} \
2017-12-03 20:01:41 +01:00
MOCK_METHOD2 ( NAME , RETURNTYPE ( const char * , PARAM1 ) ) \
2014-11-19 00:25:57 +01:00
2015-04-25 03:26:59 +02:00
# define MOCK_PATH_METHOD3(NAME, RETURNTYPE, PARAM1, PARAM2) \
RETURNTYPE NAME ( const boost : : filesystem : : path & path , PARAM1 p1 , PARAM2 p2 ) override { \
2018-05-21 01:20:38 +02:00
return NAME ( path . string ( ) . c_str ( ) , p1 , p2 ) ; \
2015-04-25 03:26:59 +02:00
} \
2017-12-03 20:01:41 +01:00
MOCK_METHOD3 ( NAME , RETURNTYPE ( const char * , PARAM1 , PARAM2 ) ) \
2015-04-25 03:26:59 +02:00
2014-11-27 16:51:15 +01:00
# define MOCK_PATH_METHOD4(NAME, RETURNTYPE, PARAM1, PARAM2, PARAM3) \
2014-11-19 00:25:57 +01:00
RETURNTYPE NAME ( const boost : : filesystem : : path & path , PARAM1 p1 , PARAM2 p2 , PARAM3 p3 ) override { \
2018-05-21 01:20:38 +02:00
return NAME ( path . string ( ) . c_str ( ) , p1 , p2 , p3 ) ; \
2014-11-27 16:51:15 +01:00
} \
2017-12-03 20:01:41 +01:00
MOCK_METHOD4 ( NAME , RETURNTYPE ( const char * , PARAM1 , PARAM2 , PARAM3 ) ) \
2014-11-19 00:25:57 +01:00
2014-11-28 14:46:45 +01:00
class MockFilesystem : public fspp : : fuse : : Filesystem {
2014-11-19 00:25:57 +01:00
public :
2014-11-25 15:01:33 +01:00
MockFilesystem ( ) ;
virtual ~ MockFilesystem ( ) ;
2014-11-19 00:25:57 +01:00
MOCK_PATH_METHOD2 ( openFile , int , int ) ;
MOCK_METHOD1 ( closeFile , void ( int ) ) ;
2018-09-16 03:02:03 +02:00
MOCK_PATH_METHOD2 ( lstat , void , fspp : : fuse : : STAT * ) ;
MOCK_METHOD2 ( fstat , void ( int , fspp : : fuse : : STAT * ) ) ;
2018-09-15 23:32:58 +02:00
MOCK_PATH_METHOD2 ( truncate , void , fspp : : num_bytes_t ) ;
MOCK_METHOD2 ( ftruncate , void ( int , fspp : : num_bytes_t ) ) ;
MOCK_METHOD4 ( read , fspp : : num_bytes_t ( int , void * , fspp : : num_bytes_t , fspp : : num_bytes_t ) ) ;
MOCK_METHOD4 ( write , void ( int , const void * , fspp : : num_bytes_t , fspp : : num_bytes_t ) ) ;
2014-11-21 01:11:24 +01:00
MOCK_METHOD1 ( flush , void ( int ) ) ;
2014-11-19 00:25:57 +01:00
MOCK_METHOD1 ( fsync , void ( int ) ) ;
MOCK_METHOD1 ( fdatasync , void ( int ) ) ;
MOCK_PATH_METHOD2 ( access , void , int ) ;
2015-04-25 03:26:59 +02:00
MOCK_PATH_METHOD4 ( createAndOpenFile , int , mode_t , uid_t , gid_t ) ;
MOCK_PATH_METHOD4 ( mkdir , void , mode_t , uid_t , gid_t ) ;
2014-11-19 00:25:57 +01:00
MOCK_PATH_METHOD1 ( rmdir , void ) ;
MOCK_PATH_METHOD1 ( unlink , void ) ;
void rename ( const boost : : filesystem : : path & from , const boost : : filesystem : : path & to ) override {
2018-05-21 01:20:38 +02:00
return rename ( from . string ( ) . c_str ( ) , to . string ( ) . c_str ( ) ) ;
2014-11-19 00:25:57 +01:00
}
MOCK_METHOD2 ( rename , void ( const char * , const char * ) ) ;
2015-11-28 16:50:25 +01:00
cpputils : : unique_ref < std : : vector < fspp : : Dir : : Entry > > readDir ( const boost : : filesystem : : path & path ) override {
2018-05-21 01:20:38 +02:00
return cpputils : : nullcheck ( std : : unique_ptr < std : : vector < fspp : : Dir : : Entry > > ( readDir ( path . string ( ) . c_str ( ) ) ) ) . value ( ) ;
2014-11-19 00:25:57 +01:00
}
2015-03-10 21:51:12 +01:00
MOCK_METHOD1 ( readDir , std : : vector < fspp : : Dir : : Entry > * ( const char * ) ) ;
2016-02-09 09:36:07 +01:00
void utimens ( const boost : : filesystem : : path & path , timespec lastAccessTime , timespec lastModificationTime ) override {
2018-05-21 01:20:38 +02:00
return utimens ( path . string ( ) . c_str ( ) , lastAccessTime , lastModificationTime ) ;
2014-11-19 00:25:57 +01:00
}
2016-02-09 09:36:07 +01:00
MOCK_METHOD3 ( utimens , void ( const char * , timespec , timespec ) ) ;
2018-09-22 23:24:31 +02:00
MOCK_METHOD1 ( statfs , void ( struct statvfs * ) ) ;
2015-04-25 03:26:59 +02:00
void createSymlink ( const boost : : filesystem : : path & to , const boost : : filesystem : : path & from , uid_t uid , gid_t gid ) override {
2018-05-21 01:20:38 +02:00
return createSymlink ( to . string ( ) . c_str ( ) , from . string ( ) . c_str ( ) , uid , gid ) ;
2015-04-25 03:26:59 +02:00
}
MOCK_PATH_METHOD2 ( chmod , void , mode_t ) ;
MOCK_PATH_METHOD3 ( chown , void , uid_t , gid_t ) ;
MOCK_METHOD4 ( createSymlink , void ( const char * , const char * , uid_t , gid_t ) ) ;
2018-09-15 23:32:58 +02:00
MOCK_PATH_METHOD3 ( readSymlink , void , char * , fspp : : num_bytes_t ) ;
2014-11-19 00:25:57 +01:00
} ;
class FuseTest : public : : testing : : Test {
public :
2015-10-17 20:35:17 +02:00
static constexpr const char * FILENAME = " /myfile " ;
2014-11-25 14:29:44 +01:00
2014-11-27 16:51:15 +01:00
FuseTest ( ) ;
2014-11-19 00:25:57 +01:00
class TempTestFS {
public :
2018-12-03 07:57:21 +01:00
TempTestFS ( std : : shared_ptr < MockFilesystem > fsimpl ) ;
2014-11-27 16:51:15 +01:00
virtual ~ TempTestFS ( ) ;
2014-11-19 00:25:57 +01:00
public :
2014-11-27 16:51:15 +01:00
const boost : : filesystem : : path & mountDir ( ) const ;
2014-11-19 00:25:57 +01:00
private :
2015-04-25 03:47:30 +02:00
cpputils : : TempDir _mountDir ;
2014-11-19 00:25:57 +01:00
fspp : : fuse : : Fuse _fuse ;
FuseThread _fuse_thread ;
} ;
2015-06-18 19:30:52 +02:00
cpputils : : unique_ref < TempTestFS > TestFS ( ) ;
2014-11-19 00:25:57 +01:00
2018-12-03 07:57:21 +01:00
std : : shared_ptr < MockFilesystem > fsimpl ;
2014-11-19 00:38:41 +01:00
2014-11-25 14:29:44 +01:00
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
2018-09-16 03:02:03 +02:00
static : : testing : : Action < void ( const char * , fspp : : fuse : : STAT * ) > ReturnIsFile ;
static : : testing : : Action < void ( const char * , fspp : : fuse : : STAT * ) > ReturnIsFileWithSize ( fspp : : num_bytes_t size ) ;
static : : testing : : Action < void ( int , fspp : : fuse : : STAT * ) > ReturnIsFileFstat ;
static : : testing : : Action < void ( int , fspp : : fuse : : STAT * ) > ReturnIsFileFstatWithSize ( fspp : : num_bytes_t size ) ;
static : : testing : : Action < void ( const char * , fspp : : fuse : : STAT * ) > ReturnIsDir ;
static : : testing : : Action < void ( const char * , fspp : : fuse : : STAT * ) > ReturnDoesntExist ;
2014-11-25 14:29:44 +01:00
2015-02-17 00:48:49 +01:00
void ReturnIsFileOnLstat ( const boost : : filesystem : : path & path ) ;
2018-09-15 23:32:58 +02:00
void ReturnIsFileOnLstatWithSize ( const boost : : filesystem : : path & path , fspp : : num_bytes_t size ) ;
2015-02-17 00:48:49 +01:00
void ReturnIsDirOnLstat ( const boost : : filesystem : : path & path ) ;
void ReturnDoesntExistOnLstat ( const boost : : filesystem : : path & path ) ;
2014-11-25 18:43:50 +01:00
void OnOpenReturnFileDescriptor ( const char * filename , int descriptor ) ;
void ReturnIsFileOnFstat ( int descriptor ) ;
2018-09-15 23:32:58 +02:00
void ReturnIsFileOnFstatWithSize ( int descriptor , fspp : : num_bytes_t size ) ;
2014-11-19 00:25:57 +01:00
} ;
# endif