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"
|
2015-04-25 16:43:37 +02:00
|
|
|
#include "../../testutils/InMemoryFile.h"
|
2014-11-27 16:40:22 +01:00
|
|
|
|
2018-05-21 08:18:34 +02:00
|
|
|
#include "fspp/fs_interface/FuseErrnoException.h"
|
2014-11-27 16:40:22 +01:00
|
|
|
|
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::Invoke;
|
|
|
|
using ::testing::Action;
|
|
|
|
|
2015-04-25 16:43:37 +02:00
|
|
|
using cpputils::DataFixture;
|
|
|
|
using cpputils::Data;
|
2014-11-27 16:40:22 +01:00
|
|
|
|
2014-11-28 14:46:45 +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
|
|
|
|
2015-04-25 16:43:37 +02: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);
|
2018-12-03 07:57:21 +01:00
|
|
|
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));
|
2015-04-25 16:43:37 +02:00
|
|
|
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));
|
2015-04-25 16:43:37 +02:00
|
|
|
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());
|
2015-04-25 16:43:37 +02:00
|
|
|
EXPECT_TRUE(testFile.fileContentEquals(writeData, OFFSET));
|
2014-11-27 16:40:22 +01:00
|
|
|
}
|