Fix test cases for older libfuse versions

This commit is contained in:
Sebastian Messmer 2015-11-27 17:43:21 +01:00
parent e542529104
commit 1222cd8de6
5 changed files with 30 additions and 12 deletions

View File

@ -19,7 +19,7 @@ private:
int CreateAndOpenFile(const TempTestFS *fs, const char *filename) {
auto realpath = fs->mountDir() / filename;
#if __GNUC__ == 4 && __GNUC_MINOR__ == 8
int fd = ::open(realpath.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int fd = ::open(realpath.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
#else
int fd = ::open(realpath.c_str(), O_RDONLY | O_CREAT);
#endif
@ -27,8 +27,9 @@ private:
return fd;
}
void ReadFile(int fd) {
int retval = ::read(fd, nullptr, 0);
EXPECT_EQ(0, retval) << "Reading file failed";
uint8_t buf;
int retval = ::read(fd, &buf, 1);
EXPECT_EQ(1, retval) << "Reading file failed";
}
};
INSTANTIATE_TEST_CASE_P(FuseCreateAndOpenFileDescriptorTest, FuseCreateAndOpenFileDescriptorTest, Values(0, 2, 5, 1000, 1024*1024*1024));
@ -37,9 +38,9 @@ TEST_P(FuseCreateAndOpenFileDescriptorTest, TestReturnedFileDescriptor) {
ReturnDoesntExistOnLstat(FILENAME);
EXPECT_CALL(fsimpl, createAndOpenFile(StrEq(FILENAME), _, _, _))
.Times(1).WillOnce(Return(GetParam()));
EXPECT_CALL(fsimpl, read(GetParam(), _, _, _)).Times(1).WillOnce(Return(0));
EXPECT_CALL(fsimpl, read(GetParam(), _, _, _)).Times(1).WillOnce(Return(1));
//For the syscall to succeed, we also need to give an fstat implementation.
ReturnIsFileOnFstat(GetParam());
ReturnIsFileOnFstatWithSize(GetParam(), 1);
CreateAndOpenAndReadFile(FILENAME);
}

View File

@ -23,17 +23,19 @@ private:
return fd;
}
void ReadFile(int fd) {
int retval = ::read(fd, nullptr, 0);
EXPECT_EQ(0, retval) << "Reading file failed";
uint8_t buf;
int retval = ::read(fd, &buf, 1);
EXPECT_EQ(1, retval) << "Reading file failed";
}
};
INSTANTIATE_TEST_CASE_P(FuseOpenFileDescriptorTest, FuseOpenFileDescriptorTest, Values(0, 2, 5, 1000, 1024*1024*1024));
TEST_P(FuseOpenFileDescriptorTest, TestReturnedFileDescriptor) {
ReturnIsFileOnLstat(FILENAME);
ReturnIsFileOnLstatWithSize(FILENAME, 1);
EXPECT_CALL(fsimpl, openFile(StrEq(FILENAME), _))
.Times(1).WillOnce(Return(GetParam()));
EXPECT_CALL(fsimpl, read(GetParam(), _, _, _)).Times(1).WillOnce(Return(0));
EXPECT_CALL(fsimpl, read(GetParam(), _, _, _)).Times(1).WillOnce(Return(1));
OpenAndReadFile(FILENAME);
}

View File

@ -19,11 +19,11 @@ INSTANTIATE_TEST_CASE_P(FuseReadFileDescriptorTest, FuseReadFileDescriptorTest,
TEST_P(FuseReadFileDescriptorTest, FileDescriptorIsCorrect) {
ReturnIsFileOnLstat(FILENAME);
ReturnIsFileOnLstatWithSize(FILENAME, 1);
OnOpenReturnFileDescriptor(FILENAME, GetParam());
EXPECT_CALL(fsimpl, read(Eq(GetParam()), _, _, _))
.Times(1).WillOnce(ReturnSuccessfulRead);
char buf[1];
ReadFile(FILENAME, buf, 0, 0);
ReadFile(FILENAME, buf, 1, 0);
}

View File

@ -81,6 +81,15 @@ Action<void(int, struct ::stat*)> FuseTest::ReturnIsFileFstat =
result->st_nlink = 1;
});
Action<void(int, struct ::stat*)> FuseTest::ReturnIsFileFstatWithSize(size_t size) {
return Invoke([size](int, struct ::stat *result) {
result->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
result->st_nlink = 1;
result->st_size = size;
std::cout << "Return size: " << size <<std::endl;
});
}
Action<void(const char*, struct ::stat*)> FuseTest::ReturnIsDir =
Invoke([](const char*, struct ::stat* result) {
result->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH;
@ -112,3 +121,7 @@ void FuseTest::ReturnDoesntExistOnLstat(const bf::path &path) {
void FuseTest::ReturnIsFileOnFstat(int descriptor) {
EXPECT_CALL(fsimpl, fstat(descriptor, _)).WillRepeatedly(ReturnIsFileFstat);
}
void FuseTest::ReturnIsFileOnFstatWithSize(int descriptor, size_t size) {
EXPECT_CALL(fsimpl, fstat(descriptor, _)).WillRepeatedly(ReturnIsFileFstatWithSize(size));
}

View File

@ -104,11 +104,12 @@ public:
MockFilesystem fsimpl;
static ::testing::Action<void(const char*, struct ::stat*)> ReturnIsFileWithSize(size_t size);
//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
static ::testing::Action<void(const char*, struct ::stat*)> ReturnIsFile;
static ::testing::Action<void(const char*, struct ::stat*)> ReturnIsFileWithSize(size_t size);
static ::testing::Action<void(int, struct ::stat*)> ReturnIsFileFstat;
static ::testing::Action<void(int, struct ::stat*)> ReturnIsFileFstatWithSize(size_t size);
static ::testing::Action<void(const char*, struct ::stat*)> ReturnIsDir;
static ::testing::Action<void(const char*, struct ::stat*)> ReturnDoesntExist;
@ -118,6 +119,7 @@ public:
void ReturnDoesntExistOnLstat(const boost::filesystem::path &path);
void OnOpenReturnFileDescriptor(const char *filename, int descriptor);
void ReturnIsFileOnFstat(int descriptor);
void ReturnIsFileOnFstatWithSize(int descriptor, const size_t size);
};
#endif