libcryfs/test/fspp/fuse/write/FuseWriteOverflowTest.cpp

85 lines
3.1 KiB
C++
Raw Normal View History

2016-02-11 12:53:42 +01:00
#include <cpp-utils/data/DataFixture.h>
2014-11-27 16:40:22 +01:00
#include "testutils/FuseWriteTest.h"
#include "../../testutils/InMemoryFile.h"
2014-11-27 16:40:22 +01:00
#include "fspp/fs_interface/FuseErrnoException.h"
2014-11-27 16:40:22 +01:00
using ::testing::_;
using ::testing::Invoke;
using ::testing::Action;
using cpputils::DataFixture;
using cpputils::Data;
2014-11-27 16:40:22 +01:00
using namespace fspp::fuse;
2014-11-27 16:40:22 +01:00
class FuseWriteOverflowTest: public FuseWriteTest {
public:
2018-09-15 23:32:58 +02:00
fspp::num_bytes_t FILESIZE;
fspp::num_bytes_t WRITESIZE;
fspp::num_bytes_t OFFSET;
2014-11-27 16:40:22 +01:00
WriteableInMemoryFile testFile;
Data writeData;
2014-11-27 16:40:22 +01:00
2018-09-15 23:32:58 +02:00
FuseWriteOverflowTest(fspp::num_bytes_t filesize, fspp::num_bytes_t writesize, fspp::num_bytes_t offset)
: FILESIZE(filesize), WRITESIZE(writesize), OFFSET(offset), testFile(DataFixture::generate(FILESIZE.value())), writeData(DataFixture::generate(WRITESIZE.value())) {
2014-11-27 16:40:22 +01:00
ReturnIsFileOnLstatWithSize(FILENAME, FILESIZE);
OnOpenReturnFileDescriptor(FILENAME, 0);
EXPECT_CALL(*fsimpl, write(0, _, _, _)).WillRepeatedly(WriteToFile);
2014-11-27 16:40:22 +01:00
}
// This write() mock implementation writes to the stored virtual file.
2018-09-15 23:32:58 +02:00
Action<void(int, const void*, fspp::num_bytes_t, fspp::num_bytes_t)> WriteToFile =
Invoke([this](int, const void *buf, fspp::num_bytes_t count, fspp::num_bytes_t offset) {
2014-11-27 16:40:22 +01:00
testFile.write(buf, count, offset);
});
};
class FuseWriteOverflowTestWithNonemptyFile: public FuseWriteOverflowTest {
public:
2018-09-15 23:32:58 +02:00
FuseWriteOverflowTestWithNonemptyFile(): FuseWriteOverflowTest(fspp::num_bytes_t(1000), fspp::num_bytes_t(2000), fspp::num_bytes_t(500)) {}
2014-11-27 16:40:22 +01:00
};
TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromBeginning) {
2018-09-15 23:32:58 +02:00
WriteFile(FILENAME, writeData.data(), WRITESIZE, fspp::num_bytes_t(0));
2014-11-27 16:40:22 +01:00
EXPECT_EQ(WRITESIZE, testFile.size());
2018-09-15 23:32:58 +02:00
EXPECT_TRUE(testFile.fileContentEquals(writeData, fspp::num_bytes_t(0)));
2014-11-27 16:40:22 +01:00
}
TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromMiddle) {
WriteFile(FILENAME, writeData.data(), WRITESIZE, OFFSET);
EXPECT_EQ(OFFSET + WRITESIZE, testFile.size());
2018-09-15 23:32:58 +02:00
EXPECT_TRUE(testFile.regionUnchanged(fspp::num_bytes_t(0), OFFSET));
EXPECT_TRUE(testFile.fileContentEquals(writeData, OFFSET));
2014-11-27 16:40:22 +01:00
}
TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteAfterFileEnd) {
WriteFile(FILENAME, writeData.data(), WRITESIZE, FILESIZE + OFFSET);
EXPECT_EQ(FILESIZE + OFFSET + WRITESIZE, testFile.size());
2018-09-15 23:32:58 +02:00
EXPECT_TRUE(testFile.regionUnchanged(fspp::num_bytes_t(0), FILESIZE));
EXPECT_TRUE(testFile.fileContentEquals(writeData, FILESIZE + OFFSET));
2014-11-27 16:40:22 +01:00
}
class FuseWriteOverflowTestWithEmptyFile: public FuseWriteOverflowTest {
public:
2018-09-15 23:32:58 +02:00
FuseWriteOverflowTestWithEmptyFile(): FuseWriteOverflowTest(fspp::num_bytes_t(0), fspp::num_bytes_t(2000), fspp::num_bytes_t(500)) {}
2014-11-27 16:40:22 +01:00
};
TEST_F(FuseWriteOverflowTestWithEmptyFile, WriteToBeginOfEmptyFile) {
2018-09-15 23:32:58 +02:00
WriteFile(FILENAME, writeData.data(), WRITESIZE, fspp::num_bytes_t(0));
2014-11-27 16:40:22 +01:00
EXPECT_EQ(WRITESIZE, testFile.size());
2018-09-15 23:32:58 +02:00
EXPECT_TRUE(testFile.fileContentEquals(writeData, fspp::num_bytes_t(0)));
2014-11-27 16:40:22 +01:00
}
TEST_F(FuseWriteOverflowTestWithEmptyFile, WriteAfterFileEnd) {
WriteFile(FILENAME, writeData.data(), WRITESIZE, OFFSET);
EXPECT_EQ(OFFSET + WRITESIZE, testFile.size());
EXPECT_TRUE(testFile.fileContentEquals(writeData, OFFSET));
2014-11-27 16:40:22 +01:00
}