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

83 lines
3.5 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
#include <tuple>
#include <cstdlib>
using ::testing::_;
using ::testing::WithParamInterface;
using ::testing::Values;
using ::testing::Combine;
using ::testing::Invoke;
using ::testing::Action;
using std::tuple;
using std::get;
using cpputils::Data;
using cpputils::DataFixture;
2014-11-27 16:40:22 +01:00
using namespace fspp::fuse;
2014-11-27 16:40:22 +01:00
// We can't test the count or size parameter directly, because fuse doesn't pass them 1:1.
// But we can test that the data passed to the ::write syscall is correctly written.
struct TestData {
TestData(): count(0), offset(0), additional_bytes_at_end_of_file(0) {}
2018-09-15 23:32:58 +02:00
TestData(const tuple<fspp::num_bytes_t, fspp::num_bytes_t, fspp::num_bytes_t> &data): count(get<0>(data)), offset(get<1>(data)), additional_bytes_at_end_of_file(get<2>(data)) {}
fspp::num_bytes_t count;
fspp::num_bytes_t offset;
2014-11-27 16:40:22 +01:00
//How many more bytes does the file have after the read block?
2018-09-15 23:32:58 +02:00
fspp::num_bytes_t additional_bytes_at_end_of_file;
fspp::num_bytes_t fileSize() {
2014-11-27 16:40:22 +01:00
return count + offset + additional_bytes_at_end_of_file;
}
};
// The testcase creates random data in memory, offers a mock write() implementation to write to this
// memory region and check methods to check for data equality of a region.
2018-09-15 23:32:58 +02:00
class FuseWriteDataTest: public FuseWriteTest, public WithParamInterface<tuple<fspp::num_bytes_t, fspp::num_bytes_t, fspp::num_bytes_t>> {
2014-11-27 16:40:22 +01:00
public:
std::unique_ptr<WriteableInMemoryFile> testFile;
2014-11-27 16:40:22 +01:00
TestData testData;
FuseWriteDataTest()
: testFile(nullptr),
testData(GetParam()) {
2018-09-15 23:32:58 +02:00
testFile = std::make_unique<WriteableInMemoryFile>(DataFixture::generate(testData.fileSize().value(), 1));
2014-11-27 16:40:22 +01:00
ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize());
OnOpenReturnFileDescriptor(FILENAME, 0);
EXPECT_CALL(*fsimpl, write(0, _, _, _))
2014-11-27 16:40:22 +01:00
.WillRepeatedly(WriteToFile);
}
// 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);
});
};
2019-10-13 14:16:06 +02:00
INSTANTIATE_TEST_SUITE_P(FuseWriteDataTest, FuseWriteDataTest, Combine(
2018-09-15 23:32:58 +02:00
Values(fspp::num_bytes_t(0), fspp::num_bytes_t(1), fspp::num_bytes_t(10), fspp::num_bytes_t(1000), fspp::num_bytes_t(1024), fspp::num_bytes_t(10*1024*1024)),
Values(fspp::num_bytes_t(0), fspp::num_bytes_t(1), fspp::num_bytes_t(10), fspp::num_bytes_t(1024), fspp::num_bytes_t(10*1024*1024)),
Values(fspp::num_bytes_t(0), fspp::num_bytes_t(1), fspp::num_bytes_t(10), fspp::num_bytes_t(1024), fspp::num_bytes_t(10*1024*1024))
));
2014-11-27 16:40:22 +01:00
TEST_P(FuseWriteDataTest, DataWasCorrectlyWritten) {
2018-09-15 23:32:58 +02:00
Data randomWriteData = DataFixture::generate(testData.count.value(), 2);
2014-11-27 16:40:22 +01:00
WriteFile(FILENAME, randomWriteData.data(), testData.count, testData.offset);
EXPECT_TRUE(testFile->fileContentEquals(randomWriteData, testData.offset));
2014-11-27 16:40:22 +01:00
}
TEST_P(FuseWriteDataTest, RestOfFileIsUnchanged) {
2018-09-15 23:32:58 +02:00
Data randomWriteData = DataFixture::generate(testData.count.value(), 2);
2014-11-27 16:40:22 +01:00
WriteFile(FILENAME, randomWriteData.data(), testData.count, testData.offset);
EXPECT_TRUE(testFile->sizeUnchanged());
2018-09-15 23:32:58 +02:00
EXPECT_TRUE(testFile->regionUnchanged(fspp::num_bytes_t(0), testData.offset));
2014-11-27 16:40:22 +01:00
EXPECT_TRUE(testFile->regionUnchanged(testData.offset + testData.count, testData.additional_bytes_at_end_of_file));
}