From bc46bc88e9222dd80073a50e85e0efc286a2cbd5 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 25 Aug 2017 00:14:16 +0100 Subject: [PATCH] Fix some issues with fspp-test on Mac OS X --- test/fspp/CMakeLists.txt | 2 +- test/fspp/fuse/closeFile/FuseCloseTest.cpp | 18 +++++--- .../FuseCreateAndOpenFileDescriptorTest.cpp | 12 ++--- .../testutils/FuseCreateAndOpenTest.cpp | 31 ++++--------- .../testutils/FuseCreateAndOpenTest.h | 5 +-- .../fdatasync/testutils/FuseFdatasyncTest.cpp | 13 +++--- .../fdatasync/testutils/FuseFdatasyncTest.h | 3 +- test/fspp/fuse/flush/FuseFlushErrorTest.cpp | 5 ++- .../fuse/flush/testutils/FuseFlushTest.cpp | 14 +++--- .../fspp/fuse/flush/testutils/FuseFlushTest.h | 3 +- test/fspp/fuse/fstat/FuseFstatErrorTest.cpp | 9 ++-- .../fuse/fstat/testutils/FuseFstatTest.cpp | 27 +++++------ .../fspp/fuse/fstat/testutils/FuseFstatTest.h | 5 ++- .../fuse/fsync/testutils/FuseFsyncTest.cpp | 13 +++--- .../fspp/fuse/fsync/testutils/FuseFsyncTest.h | 3 +- .../ftruncate/testutils/FuseFTruncateTest.cpp | 13 +++--- .../ftruncate/testutils/FuseFTruncateTest.h | 3 +- .../openFile/FuseOpenFileDescriptorTest.cpp | 12 ++--- .../fuse/openFile/testutils/FuseOpenTest.cpp | 35 ++++++--------- .../fuse/openFile/testutils/FuseOpenTest.h | 5 ++- .../fspp/fuse/read/testutils/FuseReadTest.cpp | 13 +++--- test/fspp/fuse/read/testutils/FuseReadTest.h | 3 +- .../fuse/write/testutils/FuseWriteTest.cpp | 13 +++--- .../fspp/fuse/write/testutils/FuseWriteTest.h | 3 +- test/fspp/testutils/OpenFileHandle.cpp | 1 + test/fspp/testutils/OpenFileHandle.h | 45 +++++++++++++++++++ 26 files changed, 184 insertions(+), 125 deletions(-) create mode 100644 test/fspp/testutils/OpenFileHandle.cpp create mode 100644 test/fspp/testutils/OpenFileHandle.h diff --git a/test/fspp/CMakeLists.txt b/test/fspp/CMakeLists.txt index 3df535d7..aec783db 100644 --- a/test/fspp/CMakeLists.txt +++ b/test/fspp/CMakeLists.txt @@ -100,7 +100,7 @@ set(SOURCES fs_interface/DirTest.cpp fs_interface/DeviceTest.cpp fs_interface/OpenFileTest.cpp -) + testutils/OpenFileHandle.cpp testutils/OpenFileHandle.h) add_executable(${PROJECT_NAME} ${SOURCES}) target_link_libraries(${PROJECT_NAME} googletest fspp) diff --git a/test/fspp/fuse/closeFile/FuseCloseTest.cpp b/test/fspp/fuse/closeFile/FuseCloseTest.cpp index acfc2121..fe66a1e4 100644 --- a/test/fspp/fuse/closeFile/FuseCloseTest.cpp +++ b/test/fspp/fuse/closeFile/FuseCloseTest.cpp @@ -1,4 +1,5 @@ #include "../../testutils/FuseTest.h" +#include "../../testutils/OpenFileHandle.h" #include using ::testing::_; @@ -17,6 +18,8 @@ using std::unique_lock; using std::condition_variable; using std::chrono::duration; using std::chrono::seconds; +using cpputils::unique_ref; +using cpputils::make_unique_ref; // The fuse behaviour is: For each open(), there will be exactly one call to release(). // Directly before this call to release(), flush() will be called. After flush() returns, @@ -58,20 +61,21 @@ public: void OpenAndCloseFile(const string &filename) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); - CloseFile(fd); + auto fd = OpenFile(fs.get(), filename); + CloseFile(std::move(fd)); } - int OpenFile(const TempTestFS *fs, const string &filename) { + unique_ref OpenFile(const TempTestFS *fs, const string &filename) { auto real_path = fs->mountDir() / filename; - int fd = ::open(real_path.c_str(), O_RDONLY); - EXPECT_GE(fd, 0) << "Opening file failed"; + auto fd = make_unique_ref(real_path.c_str(), O_RDONLY); + EXPECT_GE(fd->fd(), 0) << "Opening file failed"; return fd; } - void CloseFile(int fd) { - int retval = ::close(fd); + void CloseFile(unique_ref fd) { + int retval = ::close(fd->fd()); EXPECT_EQ(0, retval); + fd->release(); // don't try closing it again } }; INSTANTIATE_TEST_CASE_P(FuseCloseTest, FuseCloseTest, Values(0, 1, 2, 100, 1024*1024*1024)); diff --git a/test/fspp/fuse/createAndOpenFile/FuseCreateAndOpenFileDescriptorTest.cpp b/test/fspp/fuse/createAndOpenFile/FuseCreateAndOpenFileDescriptorTest.cpp index 22a71a8c..474a43a6 100644 --- a/test/fspp/fuse/createAndOpenFile/FuseCreateAndOpenFileDescriptorTest.cpp +++ b/test/fspp/fuse/createAndOpenFile/FuseCreateAndOpenFileDescriptorTest.cpp @@ -5,21 +5,23 @@ using ::testing::StrEq; using ::testing::WithParamInterface; using ::testing::Values; using ::testing::Return; +using cpputils::unique_ref; +using cpputils::make_unique_ref; class FuseCreateAndOpenFileDescriptorTest: public FuseCreateAndOpenTest, public WithParamInterface { public: void CreateAndOpenAndReadFile(const char *filename) { auto fs = TestFS(); - int fd = CreateAndOpenFile(fs.get(), filename); - ReadFile(fd); + auto fd = CreateAndOpenFile(fs.get(), filename); + ReadFile(fd->fd()); } private: - int CreateAndOpenFile(const TempTestFS *fs, const char *filename) { + unique_ref CreateAndOpenFile(const TempTestFS *fs, const char *filename) { auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH); - EXPECT_GE(fd, 0) << "Creating file failed"; + auto fd = make_unique_ref(realpath.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH); + EXPECT_GE(fd->fd(), 0) << "Creating file failed"; return fd; } void ReadFile(int fd) { diff --git a/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.cpp b/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.cpp index 6b851088..6a59c592 100644 --- a/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.cpp +++ b/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.cpp @@ -2,29 +2,16 @@ using ::testing::_; -int FuseCreateAndOpenTest::CreateAndOpenFile(const char *filename, int flags) { - int fd = CreateAndOpenFileAllowError(filename, flags); - EXPECT_GE(fd, 0); - return fd; -} - -int FuseCreateAndOpenTest::CreateAndOpenFileReturnError(const char *filename, int flags) { - int fd = CreateAndOpenFileAllowError(filename, flags); - if (fd >= 0) { - return 0; - } else { - return -fd; - } -} - -int FuseCreateAndOpenTest::CreateAndOpenFileAllowError(const char *filename, int flags) { +void FuseCreateAndOpenTest::CreateAndOpenFile(const char *filename, int flags) { auto fs = TestFS(); auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), flags | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (fd >= 0) { - return fd; - } else { - return -errno; - } + OpenFileHandle fd(realpath.c_str(), flags | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + + EXPECT_GE(fd.fd(), 0); +} + +int FuseCreateAndOpenTest::CreateAndOpenFileReturnError(const char *filename, int flags) { + OpenFileHandle fd(filename, flags); + return fd.errorcode(); } diff --git a/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.h b/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.h index c19b2f4c..2591936f 100644 --- a/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.h +++ b/test/fspp/fuse/createAndOpenFile/testutils/FuseCreateAndOpenTest.h @@ -3,15 +3,14 @@ #define MESSMER_FSPP_TEST_FUSE_CREATEANDOPENFILE_TESTUTILS_FUSECREATEANDOPENTEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseCreateAndOpenTest: public FuseTest { public: const char *FILENAME = "/myfile"; - int CreateAndOpenFile(const char *FILENAME, int flags); + void CreateAndOpenFile(const char *FILENAME, int flags); int CreateAndOpenFileReturnError(const char *FILENAME, int flags); -private: - int CreateAndOpenFileAllowError(const char *FILENAME, int flags); }; MATCHER_P(OpenFlagsEq, expectedFlags, "") { diff --git a/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.cpp b/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.cpp index 8ca0f583..245ab895 100644 --- a/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.cpp +++ b/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.cpp @@ -1,6 +1,9 @@ #include "FuseFdatasyncTest.h" #include +using cpputils::unique_ref; +using cpputils::make_unique_ref; + void FuseFdatasyncTest::FdatasyncFile(const char *filename) { int error = FdatasyncFileReturnError(filename); EXPECT_EQ(0, error); @@ -9,10 +12,10 @@ void FuseFdatasyncTest::FdatasyncFile(const char *filename) { int FuseFdatasyncTest::FdatasyncFileReturnError(const char *filename) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); + auto fd = OpenFile(fs.get(), filename); #ifdef F_FULLFSYNC // This is MacOSX, which doesn't know fdatasync - int retval = fcntl(fd, F_FULLFSYNC); + int retval = fcntl(fd->fd(), F_FULLFSYNC); #else int retval = ::fdatasync(fd); #endif @@ -23,9 +26,9 @@ int FuseFdatasyncTest::FdatasyncFileReturnError(const char *filename) { } } -int FuseFdatasyncTest::OpenFile(const TempTestFS *fs, const char *filename) { +unique_ref FuseFdatasyncTest::OpenFile(const TempTestFS *fs, const char *filename) { auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), O_RDWR); - EXPECT_GE(fd, 0) << "Error opening file"; + auto fd = make_unique_ref(realpath.c_str(), O_RDWR); + EXPECT_GE(fd->fd(), 0) << "Error opening file"; return fd; } diff --git a/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.h b/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.h index 8c73cce3..42eda5dd 100644 --- a/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.h +++ b/test/fspp/fuse/fdatasync/testutils/FuseFdatasyncTest.h @@ -3,6 +3,7 @@ #define MESSMER_FSPP_TEST_FUSE_FDATASYNC_TESTUTILS_FUSEFDATASYNCTEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseFdatasyncTest: public FuseTest { public: @@ -12,7 +13,7 @@ public: int FdatasyncFileReturnError(const char *filename); private: - int OpenFile(const TempTestFS *fs, const char *filename); + cpputils::unique_ref OpenFile(const TempTestFS *fs, const char *filename); }; #endif diff --git a/test/fspp/fuse/flush/FuseFlushErrorTest.cpp b/test/fspp/fuse/flush/FuseFlushErrorTest.cpp index 54be24c6..504064d2 100644 --- a/test/fspp/fuse/flush/FuseFlushErrorTest.cpp +++ b/test/fspp/fuse/flush/FuseFlushErrorTest.cpp @@ -24,9 +24,10 @@ TEST_P(FuseFlushErrorTest, ReturnErrorFromFlush) { EXPECT_CALL(fsimpl, flush(Eq(GetParam()))).Times(1).WillOnce(Throw(FuseErrnoException(GetParam()))); auto fs = TestFS(); - int fd = OpenFile(fs.get(), FILENAME); + auto fd = OpenFile(fs.get(), FILENAME); - int close_result = ::close(fd); + int close_result = ::close(fd->fd()); EXPECT_EQ(GetParam(), errno); EXPECT_EQ(-1, close_result); + fd->release(); // don't close it again } diff --git a/test/fspp/fuse/flush/testutils/FuseFlushTest.cpp b/test/fspp/fuse/flush/testutils/FuseFlushTest.cpp index 1000ca56..fc2f9424 100644 --- a/test/fspp/fuse/flush/testutils/FuseFlushTest.cpp +++ b/test/fspp/fuse/flush/testutils/FuseFlushTest.cpp @@ -1,15 +1,19 @@ #include "FuseFlushTest.h" +using cpputils::unique_ref; +using cpputils::make_unique_ref; + void FuseFlushTest::OpenAndCloseFile(const std::string &filename) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); - CloseFile(fd); + auto fd = OpenFile(fs.get(), filename); + CloseFile(fd->fd()); + fd->release(); // don't try to close it again } -int FuseFlushTest::OpenFile(const TempTestFS *fs, const std::string &filename) { +unique_ref FuseFlushTest::OpenFile(const TempTestFS *fs, const std::string &filename) { auto real_path = fs->mountDir() / filename; - int fd = ::open(real_path.c_str(), O_RDONLY); - EXPECT_GE(fd, 0) << "Opening file failed"; + auto fd = make_unique_ref(real_path.c_str(), O_RDONLY); + EXPECT_GE(fd->fd(), 0) << "Opening file failed"; return fd; } diff --git a/test/fspp/fuse/flush/testutils/FuseFlushTest.h b/test/fspp/fuse/flush/testutils/FuseFlushTest.h index d90ff8fb..6c172bb8 100644 --- a/test/fspp/fuse/flush/testutils/FuseFlushTest.h +++ b/test/fspp/fuse/flush/testutils/FuseFlushTest.h @@ -3,13 +3,14 @@ #define MESSMER_FSPP_TEST_FUSE_FLUSH_TESTUTILS_FUSEFLUSHTEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseFlushTest: public FuseTest { public: const std::string FILENAME = "/myfile"; void OpenAndCloseFile(const std::string &filename); - int OpenFile(const TempTestFS *fs, const std::string &filename); + cpputils::unique_ref OpenFile(const TempTestFS *fs, const std::string &filename); void CloseFile(int fd); }; diff --git a/test/fspp/fuse/fstat/FuseFstatErrorTest.cpp b/test/fspp/fuse/fstat/FuseFstatErrorTest.cpp index f9765832..cf655d95 100644 --- a/test/fspp/fuse/fstat/FuseFstatErrorTest.cpp +++ b/test/fspp/fuse/fstat/FuseFstatErrorTest.cpp @@ -10,6 +10,9 @@ using ::testing::Eq; using ::testing::Return; using ::testing::Throw; +using cpputils::unique_ref; +using cpputils::make_unique_ref; + using namespace fspp::fuse; // Cite from FUSE documentation on the fgetattr function: @@ -19,10 +22,10 @@ using namespace fspp::fuse; class FuseFstatErrorTest: public FuseFstatTest, public WithParamInterface { public: - int CreateFileAllowErrors(const TempTestFS *fs, const std::string &filename) { + /*unique_ref CreateFileAllowErrors(const TempTestFS *fs, const std::string &filename) { auto real_path = fs->mountDir() / filename; - return ::open(real_path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - } + return make_unique_ref(real_path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + }*/ }; INSTANTIATE_TEST_CASE_P(FuseFstatErrorTest, FuseFstatErrorTest, Values(EACCES, EBADF, EFAULT, ELOOP, ENAMETOOLONG, ENOENT, ENOMEM, ENOTDIR, EOVERFLOW)); diff --git a/test/fspp/fuse/fstat/testutils/FuseFstatTest.cpp b/test/fspp/fuse/fstat/testutils/FuseFstatTest.cpp index 0b3cfa27..9a8df826 100644 --- a/test/fspp/fuse/fstat/testutils/FuseFstatTest.cpp +++ b/test/fspp/fuse/fstat/testutils/FuseFstatTest.cpp @@ -3,30 +3,25 @@ using ::testing::StrEq; using ::testing::_; using ::testing::Return; +using cpputils::unique_ref; +using cpputils::make_unique_ref; -int FuseFstatTest::CreateFile(const TempTestFS *fs, const std::string &filename) { - int fd = CreateFileAllowErrors(fs, filename); - EXPECT_GE(fd, 0) << "Opening file failed"; + +unique_ref FuseFstatTest::CreateFile(const TempTestFS *fs, const std::string &filename) { + auto fd = CreateFileAllowErrors(fs, filename); + EXPECT_GE(fd->fd(), 0) << "Opening file failed"; return fd; } int FuseFstatTest::CreateFileReturnError(const TempTestFS *fs, const std::string &filename) { - int fd = CreateFileAllowErrors(fs, filename); - if (fd >= 0) { - return 0; - } else { - return -fd; - } + auto fd = CreateFileAllowErrors(fs, filename); + return fd->errorcode(); } -int FuseFstatTest::CreateFileAllowErrors(const TempTestFS *fs, const std::string &filename) { +unique_ref FuseFstatTest::CreateFileAllowErrors(const TempTestFS *fs, const std::string &filename) { auto real_path = fs->mountDir() / filename; - int fd = ::open(real_path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (fd >= 0) { - return fd; - } else { - return -errno; - } + auto fd = make_unique_ref(real_path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + return fd; } void FuseFstatTest::OnCreateAndOpenReturnFileDescriptor(const char *filename, int descriptor) { diff --git a/test/fspp/fuse/fstat/testutils/FuseFstatTest.h b/test/fspp/fuse/fstat/testutils/FuseFstatTest.h index 761a82a1..b15760c1 100644 --- a/test/fspp/fuse/fstat/testutils/FuseFstatTest.h +++ b/test/fspp/fuse/fstat/testutils/FuseFstatTest.h @@ -3,14 +3,15 @@ #define MESSMER_FSPP_TEST_FUSE_FSTAT_TESTUTILS_FUSEFSTATTEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseFstatTest: public FuseTest { public: - int CreateFile(const TempTestFS *fs, const std::string &filename); + cpputils::unique_ref CreateFile(const TempTestFS *fs, const std::string &filename); int CreateFileReturnError(const TempTestFS *fs, const std::string &filename); void OnCreateAndOpenReturnFileDescriptor(const char *filename, int descriptor); private: - int CreateFileAllowErrors(const TempTestFS *fs, const std::string &filename); + cpputils::unique_ref CreateFileAllowErrors(const TempTestFS *fs, const std::string &filename); }; diff --git a/test/fspp/fuse/fsync/testutils/FuseFsyncTest.cpp b/test/fspp/fuse/fsync/testutils/FuseFsyncTest.cpp index 97de440c..372abc15 100644 --- a/test/fspp/fuse/fsync/testutils/FuseFsyncTest.cpp +++ b/test/fspp/fuse/fsync/testutils/FuseFsyncTest.cpp @@ -1,5 +1,8 @@ #include "FuseFsyncTest.h" +using cpputils::unique_ref; +using cpputils::make_unique_ref; + void FuseFsyncTest::FsyncFile(const char *filename) { int error = FsyncFileReturnError(filename); EXPECT_EQ(0, error); @@ -8,8 +11,8 @@ void FuseFsyncTest::FsyncFile(const char *filename) { int FuseFsyncTest::FsyncFileReturnError(const char *filename) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); - int retval = ::fsync(fd); + auto fd = OpenFile(fs.get(), filename); + int retval = ::fsync(fd->fd()); if (retval == 0) { return 0; } else { @@ -17,9 +20,9 @@ int FuseFsyncTest::FsyncFileReturnError(const char *filename) { } } -int FuseFsyncTest::OpenFile(const TempTestFS *fs, const char *filename) { +unique_ref FuseFsyncTest::OpenFile(const TempTestFS *fs, const char *filename) { auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), O_RDWR); - EXPECT_GE(fd, 0) << "Error opening file"; + auto fd = make_unique_ref(realpath.c_str(), O_RDWR); + EXPECT_GE(fd->fd(), 0) << "Error opening file"; return fd; } diff --git a/test/fspp/fuse/fsync/testutils/FuseFsyncTest.h b/test/fspp/fuse/fsync/testutils/FuseFsyncTest.h index 1ab04c96..25106c98 100644 --- a/test/fspp/fuse/fsync/testutils/FuseFsyncTest.h +++ b/test/fspp/fuse/fsync/testutils/FuseFsyncTest.h @@ -3,6 +3,7 @@ #define MESSMER_FSPP_TEST_FUSE_FSYNC_TESTUTILS_FUSEFSYNCTEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseFsyncTest: public FuseTest { public: @@ -12,7 +13,7 @@ public: int FsyncFileReturnError(const char *filename); private: - int OpenFile(const TempTestFS *fs, const char *filename); + cpputils::unique_ref OpenFile(const TempTestFS *fs, const char *filename); }; #endif diff --git a/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.cpp b/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.cpp index 7adcfa2b..38290280 100644 --- a/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.cpp +++ b/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.cpp @@ -1,5 +1,8 @@ #include "FuseFTruncateTest.h" +using cpputils::unique_ref; +using cpputils::make_unique_ref; + void FuseFTruncateTest::FTruncateFile(const char *filename, off_t size) { int error = FTruncateFileReturnError(filename, size); EXPECT_EQ(0, error); @@ -8,8 +11,8 @@ void FuseFTruncateTest::FTruncateFile(const char *filename, off_t size) { int FuseFTruncateTest::FTruncateFileReturnError(const char *filename, off_t size) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); - int retval = ::ftruncate(fd, size); + auto fd = OpenFile(fs.get(), filename); + int retval = ::ftruncate(fd->fd(), size); if (0 == retval) { return 0; } else { @@ -17,9 +20,9 @@ int FuseFTruncateTest::FTruncateFileReturnError(const char *filename, off_t size } } -int FuseFTruncateTest::OpenFile(const TempTestFS *fs, const char *filename) { +unique_ref FuseFTruncateTest::OpenFile(const TempTestFS *fs, const char *filename) { auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), O_RDWR); - EXPECT_GE(fd, 0) << "Error opening file"; + auto fd = make_unique_ref(realpath.c_str(), O_RDWR); + EXPECT_GE(fd->fd(), 0) << "Error opening file"; return fd; } diff --git a/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.h b/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.h index d77dff07..6cda00ef 100644 --- a/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.h +++ b/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.h @@ -3,6 +3,7 @@ #define MESSMER_FSPP_TEST_FUSE_FTRUNCATE_TESTUTILS_FUSEFTRUNCATETEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseFTruncateTest: public FuseTest { public: @@ -12,7 +13,7 @@ public: int FTruncateFileReturnError(const char *filename, off_t size); private: - int OpenFile(const TempTestFS *fs, const char *filename); + cpputils::unique_ref OpenFile(const TempTestFS *fs, const char *filename); }; #endif diff --git a/test/fspp/fuse/openFile/FuseOpenFileDescriptorTest.cpp b/test/fspp/fuse/openFile/FuseOpenFileDescriptorTest.cpp index 18212aba..45e8904e 100644 --- a/test/fspp/fuse/openFile/FuseOpenFileDescriptorTest.cpp +++ b/test/fspp/fuse/openFile/FuseOpenFileDescriptorTest.cpp @@ -5,21 +5,23 @@ using ::testing::StrEq; using ::testing::WithParamInterface; using ::testing::Values; using ::testing::Return; +using cpputils::unique_ref; +using cpputils::make_unique_ref; class FuseOpenFileDescriptorTest: public FuseOpenTest, public WithParamInterface { public: void OpenAndReadFile(const char *filename) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); - ReadFile(fd); + auto fd = OpenFile(fs.get(), filename); + ReadFile(fd->fd()); } private: - int OpenFile(const TempTestFS *fs, const char *filename) { + unique_ref OpenFile(const TempTestFS *fs, const char *filename) { auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), O_RDONLY); - EXPECT_GE(fd, 0) << "Opening file failed"; + auto fd = make_unique_ref(realpath.c_str(), O_RDONLY); + EXPECT_GE(fd->fd(), 0) << "Opening file failed"; return fd; } void ReadFile(int fd) { diff --git a/test/fspp/fuse/openFile/testutils/FuseOpenTest.cpp b/test/fspp/fuse/openFile/testutils/FuseOpenTest.cpp index 6a307b7f..ee06afa0 100644 --- a/test/fspp/fuse/openFile/testutils/FuseOpenTest.cpp +++ b/test/fspp/fuse/openFile/testutils/FuseOpenTest.cpp @@ -1,28 +1,21 @@ #include "FuseOpenTest.h" -int FuseOpenTest::OpenFile(const char *filename, int flags) { - int fd = OpenFileAllowError(filename, flags); - EXPECT_GE(fd, 0); - return fd; +using cpputils::unique_ref; +using cpputils::make_unique_ref; + +void FuseOpenTest::OpenFile(const char *filename, int flags) { + auto fs = TestFS(); + auto fd = OpenFileAllowError(fs.get(), filename, flags); + EXPECT_GE(fd->fd(), 0); } int FuseOpenTest::OpenFileReturnError(const char *filename, int flags) { - int fd = OpenFileAllowError(filename, flags); - if (fd >= 0) { - return 0; - } else { - return -fd; - } -} - -int FuseOpenTest::OpenFileAllowError(const char *filename, int flags) { auto fs = TestFS(); - - auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), flags); - if (fd >= 0) { - return fd; - } else { - return -errno; - } + auto fd = OpenFileAllowError(fs.get(), filename, flags); + return fd->errorcode(); +} + +unique_ref FuseOpenTest::OpenFileAllowError(const TempTestFS *fs, const char *filename, int flags) { + auto realpath = fs->mountDir() / filename; + return make_unique_ref(realpath.c_str(), flags); } diff --git a/test/fspp/fuse/openFile/testutils/FuseOpenTest.h b/test/fspp/fuse/openFile/testutils/FuseOpenTest.h index edb07ca0..2c709524 100644 --- a/test/fspp/fuse/openFile/testutils/FuseOpenTest.h +++ b/test/fspp/fuse/openFile/testutils/FuseOpenTest.h @@ -3,15 +3,16 @@ #define MESSMER_FSPP_TEST_FUSE_OPENFILE_TESTUTILS_FUSEOPENTEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseOpenTest: public FuseTest { public: const char *FILENAME = "/myfile"; - int OpenFile(const char *FILENAME, int flags); + void OpenFile(const char *FILENAME, int flags); int OpenFileReturnError(const char *FILENAME, int flags); private: - int OpenFileAllowError(const char *FILENAME, int flags); + cpputils::unique_ref OpenFileAllowError(const TempTestFS *fs, const char *FILENAME, int flags); }; MATCHER_P(OpenFlagsEq, expectedFlags, "") { diff --git a/test/fspp/fuse/read/testutils/FuseReadTest.cpp b/test/fspp/fuse/read/testutils/FuseReadTest.cpp index 20345393..dee3b68a 100644 --- a/test/fspp/fuse/read/testutils/FuseReadTest.cpp +++ b/test/fspp/fuse/read/testutils/FuseReadTest.cpp @@ -1,5 +1,8 @@ #include "FuseReadTest.h" +using cpputils::make_unique_ref; +using cpputils::unique_ref; + void FuseReadTest::ReadFile(const char *filename, void *buf, size_t count, off_t offset) { auto retval = ReadFileReturnError(filename, buf, count, offset); EXPECT_EQ(0, retval.error); @@ -9,18 +12,18 @@ void FuseReadTest::ReadFile(const char *filename, void *buf, size_t count, off_t FuseReadTest::ReadError FuseReadTest::ReadFileReturnError(const char *filename, void *buf, size_t count, off_t offset) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); + auto fd = OpenFile(fs.get(), filename); ReadError result; errno = 0; - result.read_bytes = ::pread(fd, buf, count, offset); + result.read_bytes = ::pread(fd->fd(), buf, count, offset); result.error = errno; return result; } -int FuseReadTest::OpenFile(const TempTestFS *fs, const char *filename) { +unique_ref FuseReadTest::OpenFile(const TempTestFS *fs, const char *filename) { auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), O_RDONLY); - EXPECT_GE(fd, 0) << "Error opening file"; + auto fd = make_unique_ref(realpath.c_str(), O_RDONLY); + EXPECT_GE(fd->fd(), 0) << "Error opening file"; return fd; } diff --git a/test/fspp/fuse/read/testutils/FuseReadTest.h b/test/fspp/fuse/read/testutils/FuseReadTest.h index f74bb474..0afc2a9b 100644 --- a/test/fspp/fuse/read/testutils/FuseReadTest.h +++ b/test/fspp/fuse/read/testutils/FuseReadTest.h @@ -3,6 +3,7 @@ #define MESSMER_FSPP_TEST_FUSE_READ_TESTUTILS_FUSEREADTEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseReadTest: public FuseTest { public: @@ -29,7 +30,7 @@ public: } private: - int OpenFile(const TempTestFS *fs, const char *filename); + cpputils::unique_ref OpenFile(const TempTestFS *fs, const char *filename); }; #endif diff --git a/test/fspp/fuse/write/testutils/FuseWriteTest.cpp b/test/fspp/fuse/write/testutils/FuseWriteTest.cpp index 9f464fdb..af96de03 100644 --- a/test/fspp/fuse/write/testutils/FuseWriteTest.cpp +++ b/test/fspp/fuse/write/testutils/FuseWriteTest.cpp @@ -1,5 +1,8 @@ #include "FuseWriteTest.h" +using cpputils::unique_ref; +using cpputils::make_unique_ref; + void FuseWriteTest::WriteFile(const char *filename, const void *buf, size_t count, off_t offset) { auto retval = WriteFileReturnError(filename, buf, count, offset); EXPECT_EQ(0, retval.error); @@ -9,18 +12,18 @@ void FuseWriteTest::WriteFile(const char *filename, const void *buf, size_t coun FuseWriteTest::WriteError FuseWriteTest::WriteFileReturnError(const char *filename, const void *buf, size_t count, off_t offset) { auto fs = TestFS(); - int fd = OpenFile(fs.get(), filename); + auto fd = OpenFile(fs.get(), filename); WriteError result; errno = 0; - result.written_bytes = ::pwrite(fd, buf, count, offset); + result.written_bytes = ::pwrite(fd->fd(), buf, count, offset); result.error = errno; return result; } -int FuseWriteTest::OpenFile(const TempTestFS *fs, const char *filename) { +unique_ref FuseWriteTest::OpenFile(const TempTestFS *fs, const char *filename) { auto realpath = fs->mountDir() / filename; - int fd = ::open(realpath.c_str(), O_WRONLY); - EXPECT_GE(fd, 0) << "Error opening file"; + auto fd = make_unique_ref(realpath.c_str(), O_WRONLY); + EXPECT_GE(fd->fd(), 0) << "Error opening file"; return fd; } diff --git a/test/fspp/fuse/write/testutils/FuseWriteTest.h b/test/fspp/fuse/write/testutils/FuseWriteTest.h index 8da26a67..c21006a7 100644 --- a/test/fspp/fuse/write/testutils/FuseWriteTest.h +++ b/test/fspp/fuse/write/testutils/FuseWriteTest.h @@ -3,6 +3,7 @@ #define MESSMER_FSPP_TEST_FUSE_WRITE_TESTUTILS_FUSEWRITETEST_H_ #include "../../../testutils/FuseTest.h" +#include "../../../testutils/OpenFileHandle.h" class FuseWriteTest: public FuseTest { public: @@ -17,7 +18,7 @@ public: WriteError WriteFileReturnError(const char *filename, const void *buf, size_t count, off_t offset); private: - int OpenFile(const TempTestFS *fs, const char *filename); + cpputils::unique_ref OpenFile(const TempTestFS *fs, const char *filename); }; #endif diff --git a/test/fspp/testutils/OpenFileHandle.cpp b/test/fspp/testutils/OpenFileHandle.cpp new file mode 100644 index 00000000..875f0446 --- /dev/null +++ b/test/fspp/testutils/OpenFileHandle.cpp @@ -0,0 +1 @@ +#include "OpenFileHandle.h" diff --git a/test/fspp/testutils/OpenFileHandle.h b/test/fspp/testutils/OpenFileHandle.h new file mode 100644 index 00000000..8ef83a4b --- /dev/null +++ b/test/fspp/testutils/OpenFileHandle.h @@ -0,0 +1,45 @@ +#pragma once +#ifndef MESSMER_FSPP_TEST_TESTUTILS_OPENFILEHANDLE_H_ +#define MESSMER_FSPP_TEST_TESTUTILS_OPENFILEHANDLE_H_ + +#include +#include +#include +#include +#include +#include + +class OpenFileHandle final { +public: + OpenFileHandle(const char *path, int flags): fd_(::open(path, flags)), errno_(fd_ >= 0 ? 0 : errno) { + } + + OpenFileHandle(const char *path, int flags, int flags2): fd_(::open(path, flags, flags2)), errno_(fd_ >= 0 ? 0 : errno) { + } + + ~OpenFileHandle() { + if (fd_ >= 0) { + ::close(fd_); +#ifdef __APPLE__ + // On Mac OS X, we might have to give it some time to free up the file + std::this_thread::sleep_for(std::chrono::milliseconds(50)); +#endif + } + } + + int fd() { return fd_; } + int errorcode() { return errno_; } + + void release() { + fd_ = -1; // don't close anymore + } + +private: + int fd_; + const int errno_; + + DISALLOW_COPY_AND_ASSIGN(OpenFileHandle); +}; + + +#endif