diff --git a/src/test/fspp/fuse/fsync/FuseFsyncErrorTest.cpp b/src/test/fspp/fuse/fsync/FuseFsyncErrorTest.cpp new file mode 100644 index 00000000..5901a0a7 --- /dev/null +++ b/src/test/fspp/fuse/fsync/FuseFsyncErrorTest.cpp @@ -0,0 +1,28 @@ +#include "testutils/FuseFsyncTest.h" +#include "gtest/gtest.h" +#include "gmock/gmock.h" + +#include "fspp/impl/FuseErrnoException.h" + +using ::testing::_; +using ::testing::StrEq; +using ::testing::Throw; +using ::testing::WithParamInterface; +using ::testing::Values; + +using namespace fspp; + +class FuseFsyncErrorTest: public FuseFsyncTest, public WithParamInterface { +}; +INSTANTIATE_TEST_CASE_P(FuseFsyncErrorTest, FuseFsyncErrorTest, Values(EBADF, EIO, EROFS, EINVAL)); + +TEST_P(FuseFsyncErrorTest, ReturnedErrorIsCorrect) { + ReturnIsFileOnLstat(FILENAME); + OnOpenReturnFileDescriptor(FILENAME, 0); + EXPECT_CALL(fsimpl, fsync(0)) + .Times(1).WillOnce(Throw(FuseErrnoException(GetParam()))); + + int retval = FsyncFileAllowError(FILENAME); + EXPECT_EQ(GetParam(), errno); + EXPECT_EQ(-1, retval); +} diff --git a/src/test/fspp/fuse/fsync/FuseFsyncFileDescriptorTest.cpp b/src/test/fspp/fuse/fsync/FuseFsyncFileDescriptorTest.cpp new file mode 100644 index 00000000..38eec350 --- /dev/null +++ b/src/test/fspp/fuse/fsync/FuseFsyncFileDescriptorTest.cpp @@ -0,0 +1,26 @@ +#include "testutils/FuseFsyncTest.h" + +#include "fspp/impl/FuseErrnoException.h" + +using ::testing::_; +using ::testing::StrEq; +using ::testing::WithParamInterface; +using ::testing::Values; +using ::testing::Eq; +using ::testing::Return; + +using namespace fspp; + +class FuseFsyncFileDescriptorTest: public FuseFsyncTest, public WithParamInterface { +}; +INSTANTIATE_TEST_CASE_P(FuseFsyncFileDescriptorTest, FuseFsyncFileDescriptorTest, Values(0,1,10,1000,1024*1024*1024)); + + +TEST_P(FuseFsyncFileDescriptorTest, FileDescriptorIsCorrect) { + ReturnIsFileOnLstat(FILENAME); + OnOpenReturnFileDescriptor(FILENAME, GetParam()); + EXPECT_CALL(fsimpl, fsync(Eq(GetParam()))) + .Times(1).WillOnce(Return()); + + FsyncFile(FILENAME); +} diff --git a/src/test/fspp/fuse/fsync/testutils/FuseFsyncTest.cpp b/src/test/fspp/fuse/fsync/testutils/FuseFsyncTest.cpp new file mode 100644 index 00000000..ba62cca9 --- /dev/null +++ b/src/test/fspp/fuse/fsync/testutils/FuseFsyncTest.cpp @@ -0,0 +1,20 @@ +#include "FuseFsyncTest.h" + +void FuseFsyncTest::FsyncFile(const char *filename) { + int retval = FsyncFileAllowError(filename); + EXPECT_EQ(0, retval); +} + +int FuseFsyncTest::FsyncFileAllowError(const char *filename) { + auto fs = TestFS(); + + int fd = OpenFile(fs.get(), filename); + return ::fsync(fd); +} + +int 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"; + return fd; +} diff --git a/src/test/fspp/fuse/fsync/testutils/FuseFsyncTest.h b/src/test/fspp/fuse/fsync/testutils/FuseFsyncTest.h new file mode 100644 index 00000000..104f1bda --- /dev/null +++ b/src/test/fspp/fuse/fsync/testutils/FuseFsyncTest.h @@ -0,0 +1,18 @@ +#pragma once +#ifndef TEST_FSPP_FUSE_FSYNC_TESTUTILS_FUSEFSYNCTEST_H_ +#define TEST_FSPP_FUSE_FSYNC_TESTUTILS_FUSEFSYNCTEST_H_ + +#include "test/testutils/FuseTest.h" + +class FuseFsyncTest: public FuseTest { +public: + const char *FILENAME = "/myfile"; + + void FsyncFile(const char *filename); + int FsyncFileAllowError(const char *filename); + +private: + int OpenFile(const TempTestFS *fs, const char *filename); +}; + +#endif