Test cases for ftruncate

This commit is contained in:
Sebastian Messmer 2014-11-25 18:54:33 +01:00
parent 5438ab05bd
commit e4382ab2ed
6 changed files with 127 additions and 3 deletions

View File

@ -0,0 +1,30 @@
#include "testutils/FuseFTruncateTest.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 FuseFTruncateErrorTest: public FuseFTruncateTest, public WithParamInterface<int> {
};
INSTANTIATE_TEST_CASE_P(FuseFTruncateErrorTest, FuseFTruncateErrorTest, Values(EACCES, EFAULT, EFBIG, EINTR, EINVAL, EIO, EISDIR, ELOOP, ENAMETOOLONG, ENOENT, ENOTDIR, EPERM, EROFS, ETXTBSY, EBADF));
TEST_P(FuseFTruncateErrorTest, ReturnedErrorIsCorrect) {
ReturnIsFileOnLstat(FILENAME);
OnOpenReturnFileDescriptor(FILENAME, 0);
EXPECT_CALL(fsimpl, ftruncate(0, GetParam()))
.Times(1).WillOnce(Throw(FuseErrnoException(GetParam())));
//Needed to make ::ftruncate system call return successfully
ReturnIsFileOnFstat(0);
int retval = FTruncateFileAllowError(FILENAME, GetParam());
EXPECT_EQ(GetParam(), errno);
EXPECT_EQ(-1, retval);
}

View File

@ -0,0 +1,29 @@
#include "testutils/FuseFTruncateTest.h"
#include "fspp/impl/FuseErrnoException.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::WithParamInterface;
using ::testing::Values;
using ::testing::Eq;
using ::testing::Return;
using ::testing::Throw;
using namespace fspp;
class FuseFTruncateFileDescriptorTest: public FuseFTruncateTest, public WithParamInterface<int> {
};
INSTANTIATE_TEST_CASE_P(FuseFTruncateFileDescriptorTest, FuseFTruncateFileDescriptorTest, Values(0,1,10,1000,1024*1024*1024));
TEST_P(FuseFTruncateFileDescriptorTest, FileDescriptorIsCorrect) {
ReturnIsFileOnLstat(FILENAME);
OnOpenReturnFileDescriptor(FILENAME, GetParam());
EXPECT_CALL(fsimpl, ftruncate(Eq(GetParam()), _))
.Times(1).WillOnce(Return());
//Needed to make ::ftruncate system call return successfully
ReturnIsFileOnFstat(GetParam());
FTruncateFile(FILENAME, GetParam());
}

View File

@ -0,0 +1,27 @@
#include "testutils/FuseFTruncateTest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::_;
using ::testing::StrEq;
using ::testing::Eq;
using ::testing::Return;
using ::testing::WithParamInterface;
using ::testing::Values;
class FuseFTruncateSizeTest: public FuseFTruncateTest, public WithParamInterface<off_t> {
};
INSTANTIATE_TEST_CASE_P(FuseFTruncateSizeTest, FuseFTruncateSizeTest, Values(0, 1, 10, 1024, 1024*1024*1024));
TEST_P(FuseFTruncateSizeTest, FTruncateFile) {
ReturnIsFileOnLstat(FILENAME);
OnOpenReturnFileDescriptor(FILENAME, 0);
EXPECT_CALL(fsimpl, ftruncate(Eq(0), GetParam()))
.Times(1).WillOnce(Return());
//Needed to make ::ftruncate system call return successfully
ReturnIsFileOnFstat(0);
FTruncateFile(FILENAME, GetParam());
}

View File

@ -0,0 +1,20 @@
#include "FuseFTruncateTest.h"
void FuseFTruncateTest::FTruncateFile(const char *filename, off_t size) {
int retval = FTruncateFileAllowError(filename, size);
EXPECT_EQ(0, retval);
}
int FuseFTruncateTest::FTruncateFileAllowError(const char *filename, off_t size) {
auto fs = TestFS();
int fd = OpenFile(fs.get(), filename);
return ::ftruncate(fd, size);
}
int 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";
return fd;
}

View File

@ -0,0 +1,18 @@
#pragma once
#ifndef TEST_FSPP_FUSE_FTRUNCATE_TESTUTILS_FUSEFTRUNCATETEST_H_
#define TEST_FSPP_FUSE_FTRUNCATE_TESTUTILS_FUSEFTRUNCATETEST_H_
#include "test/testutils/FuseTest.h"
class FuseFTruncateTest: public FuseTest {
public:
const char *FILENAME = "/myfile";
void FTruncateFile(const char *filename, off_t size);
int FTruncateFileAllowError(const char *filename, off_t size);
private:
int OpenFile(const TempTestFS *fs, const char *filename);
};
#endif

View File

@ -16,12 +16,12 @@ class FuseTruncateErrorTest: public FuseTruncateTest, public WithParamInterface<
};
INSTANTIATE_TEST_CASE_P(FuseTruncateErrorTest, FuseTruncateErrorTest, Values(EACCES, EFAULT, EFBIG, EINTR, EINVAL, EIO, EISDIR, ELOOP, ENAMETOOLONG, ENOENT, ENOTDIR, EPERM, EROFS, ETXTBSY));
TEST_P(FuseTruncateErrorTest, TruncateFile) {
TEST_P(FuseTruncateErrorTest, ReturnedErrorIsCorrect) {
ReturnIsFileOnLstat(FILENAME);
EXPECT_CALL(fsimpl, truncate(StrEq(FILENAME), _))
.Times(1).WillOnce(Throw(FuseErrnoException(GetParam())));
int fd = TruncateFileAllowError(FILENAME, 0);
int retval = TruncateFileAllowError(FILENAME, 0);
EXPECT_EQ(GetParam(), errno);
EXPECT_EQ(-1, fd);
EXPECT_EQ(-1, retval);
}