Test cases for ftruncate
This commit is contained in:
parent
5438ab05bd
commit
e4382ab2ed
30
src/test/fspp/fuse/ftruncate/FuseFTruncateErrorTest.cpp
Normal file
30
src/test/fspp/fuse/ftruncate/FuseFTruncateErrorTest.cpp
Normal 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);
|
||||
}
|
@ -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());
|
||||
}
|
27
src/test/fspp/fuse/ftruncate/FuseFTruncateSizeTest.cpp
Normal file
27
src/test/fspp/fuse/ftruncate/FuseFTruncateSizeTest.cpp
Normal 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());
|
||||
}
|
20
src/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.cpp
Normal file
20
src/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.cpp
Normal 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;
|
||||
}
|
18
src/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.h
Normal file
18
src/test/fspp/fuse/ftruncate/testutils/FuseFTruncateTest.h
Normal 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
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user