Added testcases for open()

This commit is contained in:
Sebastian Messmer 2014-11-20 21:23:35 +01:00
parent e7181c3c8b
commit 6e05240cb6
8 changed files with 164 additions and 28 deletions

View File

@ -1,24 +0,0 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "test/testutils/FuseTest.h"
using namespace fspp;
using namespace fspp::fuse;
using ::testing::_;
using ::testing::StrEq;
typedef FuseTest FuseOpenTest;
TEST_F(FuseOpenTest, openFile) {
const char *filename = "/myfile";
ReturnIsFileOnLstat(filename);
EXPECT_CALL(fsimpl, openFile(StrEq(filename), OpenFlagsEq(O_RDWR)))
.Times(1);
auto fs = TestFS();
auto realpath = fs->mountDir() / filename;
::open(realpath.c_str(), O_RDWR);
}

View File

@ -0,0 +1,33 @@
#include "testutils/FuseOpenTest.h"
#include "fspp/impl/FuseErrnoException.h"
using ::testing::WithParamInterface;
using ::testing::Values;
using ::testing::Return;
using ::testing::Throw;
using ::testing::StrEq;
using ::testing::_;
using namespace fspp;
class FuseOpenErrorTest: public FuseOpenTest, public WithParamInterface<int> {
};
INSTANTIATE_TEST_CASE_P(OpenErrorCodes, FuseOpenErrorTest, Values(EACCES, EDQUOT, EEXIST, EFAULT, EFBIG, EINTR, EOVERFLOW, EINVAL, EISDIR, ELOOP, EMFILE, ENAMETOOLONG, ENFILE, ENODEV, ENOENT, ENOMEM, ENOSPC, ENOTDIR, ENXIO, EOPNOTSUPP, EPERM, EROFS, ETXTBSY, EWOULDBLOCK, EBADF, ENOTDIR));
TEST_F(FuseOpenErrorTest, ReturnNoError) {
ReturnIsFileOnLstat(FILENAME);
EXPECT_CALL(fsimpl, openFile(StrEq(FILENAME), _)).Times(1).WillOnce(Return(1));
errno = 0;
int retval = OpenFileAllowError(FILENAME, O_RDONLY);
EXPECT_EQ(errno, 0);
EXPECT_GE(retval, 0);
}
TEST_P(FuseOpenErrorTest, ReturnError) {
ReturnIsFileOnLstat(FILENAME);
EXPECT_CALL(fsimpl, openFile(StrEq(FILENAME), _)).Times(1).WillOnce(Throw(FuseErrnoException(GetParam())));
int retval = OpenFileAllowError(FILENAME, O_RDONLY);
EXPECT_EQ(retval, -1);
EXPECT_EQ(GetParam(), errno);
}

View File

@ -0,0 +1,42 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "testutils/FuseOpenTest.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::WithParamInterface;
using ::testing::Values;
using ::testing::Return;
class FuseOpenFileDescriptorTest: public FuseOpenTest, public WithParamInterface<int> {
public:
void OpenAndReadFile(const char *filename) {
auto fs = TestFS();
int fd = OpenFile(fs.get(), filename);
ReadFile(fd);
}
private:
int 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";
return fd;
}
void ReadFile(int fd) {
int retval = ::read(fd, nullptr, 0);
EXPECT_EQ(0, retval) << "Reading file failed";
}
};
INSTANTIATE_TEST_CASE_P(FuseOpenFileDescriptorTest, FuseOpenFileDescriptorTest, Values(0, 2, 5, 1000, 1024*1024*1024));
TEST_P(FuseOpenFileDescriptorTest, TestReturnedFileDescriptor) {
ReturnIsFileOnLstat(FILENAME);
EXPECT_CALL(fsimpl, openFile(StrEq(FILENAME), _))
.Times(1).WillOnce(Return(GetParam()));
EXPECT_CALL(fsimpl, read(GetParam(), _, _, _)).Times(1);
OpenAndReadFile(FILENAME);
}

View File

@ -0,0 +1,38 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "testutils/FuseOpenTest.h"
using ::testing::_;
using ::testing::StrEq;
class FuseOpenFilenameTest: public FuseOpenTest {
public:
};
TEST_F(FuseOpenFilenameTest, OpenFile) {
ReturnIsFileOnLstat("/myfile");
EXPECT_CALL(fsimpl, openFile(StrEq("/myfile"), _))
.Times(1);
OpenFile("/myfile", O_RDONLY);
}
TEST_F(FuseOpenFilenameTest, OpenFileNested) {
ReturnIsDirOnLstat("/mydir");
ReturnIsFileOnLstat("/mydir/myfile");
EXPECT_CALL(fsimpl, openFile(StrEq("/mydir/myfile"), _))
.Times(1);
OpenFile("/mydir/myfile", O_RDONLY);
}
TEST_F(FuseOpenFilenameTest, OpenFileNested2) {
ReturnIsDirOnLstat("/mydir");
ReturnIsDirOnLstat("/mydir/mydir2");
ReturnIsFileOnLstat("/mydir/mydir2/myfile");
EXPECT_CALL(fsimpl, openFile(StrEq("/mydir/mydir2/myfile"), _))
.Times(1);
OpenFile("/mydir/mydir2/myfile", O_RDONLY);
}

View File

@ -0,0 +1,18 @@
#include "testutils/FuseOpenTest.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::WithParamInterface;
using ::testing::Values;
class FuseOpenFlagsTest: public FuseOpenTest, public WithParamInterface<int> {
};
INSTANTIATE_TEST_CASE_P(FuseOpenFlagsTest, FuseOpenFlagsTest, Values(O_RDWR, O_RDONLY, O_WRONLY));
TEST_P(FuseOpenFlagsTest, testFlags) {
ReturnIsFileOnLstat(FILENAME);
EXPECT_CALL(fsimpl, openFile(StrEq(FILENAME), OpenFlagsEq(GetParam())))
.Times(1);
OpenFile(FILENAME, GetParam());
}

View File

@ -0,0 +1,14 @@
#include "FuseOpenTest.h"
int FuseOpenTest::OpenFile(const char *filename, int flags) {
int fd = OpenFileAllowError(filename, flags);
EXPECT_GE(fd, 0);
return fd;
}
int FuseOpenTest::OpenFileAllowError(const char *filename, int flags) {
auto fs = TestFS();
auto realpath = fs->mountDir() / filename;
return ::open(realpath.c_str(), flags);
}

View File

@ -0,0 +1,19 @@
#pragma once
#ifndef TEST_FSPP_FUSE_OPENFILE_TESTUTILS_FUSEOPENTEST_H_
#define TEST_FSPP_FUSE_OPENFILE_TESTUTILS_FUSEOPENTEST_H_
#include "test/testutils/FuseTest.h"
class FuseOpenTest: public FuseTest {
public:
const char *FILENAME = "/myfile";
int OpenFile(const char *FILENAME, int flags);
int OpenFileAllowError(const char *FILENAME, int flags);
};
MATCHER_P(OpenFlagsEq, expectedFlags, "") {
return expectedFlags == (O_ACCMODE & arg);
}
#endif

View File

@ -142,8 +142,4 @@ public:
} }
}; };
MATCHER_P(OpenFlagsEq, expectedFlags, "") {
return expectedFlags == (O_ACCMODE & arg);
}
#endif #endif