diff --git a/test/fuse/read/FuseReadReturnedDataTest.cpp b/test/fuse/read/FuseReadReturnedDataTest.cpp index 2b853f29..02c91827 100644 --- a/test/fuse/read/FuseReadReturnedDataTest.cpp +++ b/test/fuse/read/FuseReadReturnedDataTest.cpp @@ -1,4 +1,6 @@ -#include +#include +#include +#include "../../testutils/InMemoryFile.h" #include "testutils/FuseReadTest.h" #include "../../../fuse/FuseErrnoException.h" @@ -21,7 +23,8 @@ using std::get; using std::min; using std::unique_ptr; using std::make_unique; -using cpputils::DataBlockFixture; +using cpputils::Data; +using cpputils::DataFixture; using namespace fspp::fuse; @@ -45,20 +48,18 @@ struct TestData { // memory region and check methods to check for data equality of a region. class FuseReadReturnedDataTest: public FuseReadTest, public WithParamInterface> { public: - unique_ptr testFile; + std::unique_ptr testFile; TestData testData; - FuseReadReturnedDataTest() { - testData = GetParam(); - testFile = make_unique(testData.fileSize()); - + FuseReadReturnedDataTest(): testFile(nullptr), testData(GetParam()) { + testFile = make_unique(DataFixture::generate(testData.fileSize())); ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize()); OnOpenReturnFileDescriptor(FILENAME, 0); EXPECT_CALL(fsimpl, read(0, _, _, _)) .WillRepeatedly(ReadFromFile); } - // This read() mock implementation reads from the stored virtual file. + // This read() mock implementation reads from the stored virtual file (testFile). Action ReadFromFile = Invoke([this](int, void *buf, size_t count, off_t offset) { return testFile->read(buf, count, offset); }); @@ -67,8 +68,7 @@ INSTANTIATE_TEST_CASE_P(FuseReadReturnedDataTest, FuseReadReturnedDataTest, Comb TEST_P(FuseReadReturnedDataTest, ReturnedDataRangeIsCorrect) { - char *buf = new char[testData.count]; - ReadFile(FILENAME, buf, testData.count, testData.offset); - EXPECT_TRUE(testFile->fileContentEqual(buf, testData.count, testData.offset)); - delete[] buf; + Data buf(testData.count); + ReadFile(FILENAME, buf.data(), testData.count, testData.offset); + EXPECT_TRUE(testFile->fileContentEquals(buf, testData.offset)); } diff --git a/test/fuse/write/FuseWriteDataTest.cpp b/test/fuse/write/FuseWriteDataTest.cpp index 9ce0dd5e..2139e382 100644 --- a/test/fuse/write/FuseWriteDataTest.cpp +++ b/test/fuse/write/FuseWriteDataTest.cpp @@ -1,5 +1,6 @@ -#include +#include #include "testutils/FuseWriteTest.h" +#include "../../testutils/InMemoryFile.h" #include "../../../fuse/FuseErrnoException.h" @@ -21,8 +22,8 @@ using std::get; using std::min; using std::unique_ptr; using std::make_unique; -using cpputils::DataBlockFixture; -using cpputils::DataBlockFixtureWriteable; +using cpputils::Data; +using cpputils::DataFixture; using namespace fspp::fuse; @@ -45,13 +46,11 @@ struct TestData { // memory region and check methods to check for data equality of a region. class FuseWriteDataTest: public FuseWriteTest, public WithParamInterface> { public: - unique_ptr testFile; + std::unique_ptr testFile; TestData testData; - FuseWriteDataTest() { - testData = GetParam(); - testFile = make_unique(testData.fileSize(), 1); - + FuseWriteDataTest(): testFile(nullptr), testData(GetParam()) { + testFile = make_unique(DataFixture::generate(testData.fileSize(), 1)); ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize()); OnOpenReturnFileDescriptor(FILENAME, 0); EXPECT_CALL(fsimpl, write(0, _, _, _)) @@ -67,14 +66,14 @@ INSTANTIATE_TEST_CASE_P(FuseWriteDataTest, FuseWriteDataTest, Combine(Values(0,1 TEST_P(FuseWriteDataTest, DataWasCorrectlyWritten) { - DataBlockFixture randomWriteData(testData.count, 2); + Data randomWriteData = DataFixture::generate(testData.count, 2); WriteFile(FILENAME, randomWriteData.data(), testData.count, testData.offset); - EXPECT_TRUE(testFile->fileContentEqual(randomWriteData.data(), testData.count, testData.offset)); + EXPECT_TRUE(testFile->fileContentEquals(randomWriteData, testData.offset)); } TEST_P(FuseWriteDataTest, RestOfFileIsUnchanged) { - DataBlockFixture randomWriteData(testData.count, 2); + Data randomWriteData = DataFixture::generate(testData.count, 2); WriteFile(FILENAME, randomWriteData.data(), testData.count, testData.offset); EXPECT_TRUE(testFile->sizeUnchanged()); diff --git a/test/fuse/write/FuseWriteOverflowTest.cpp b/test/fuse/write/FuseWriteOverflowTest.cpp index a7b2f0dc..1076de57 100644 --- a/test/fuse/write/FuseWriteOverflowTest.cpp +++ b/test/fuse/write/FuseWriteOverflowTest.cpp @@ -1,5 +1,6 @@ -#include +#include #include "testutils/FuseWriteTest.h" +#include "../../testutils/InMemoryFile.h" #include "../../../fuse/FuseErrnoException.h" @@ -11,8 +12,8 @@ using ::testing::Invoke; using ::testing::Action; using std::min; -using cpputils::DataBlockFixture; -using cpputils::DataBlockFixtureWriteable; +using cpputils::DataFixture; +using cpputils::Data; using namespace fspp::fuse; @@ -22,11 +23,11 @@ public: size_t WRITESIZE; size_t OFFSET; - DataBlockFixtureWriteable testFile; - DataBlockFixture writeData; + WriteableInMemoryFile testFile; + Data writeData; FuseWriteOverflowTest(size_t filesize, size_t writesize, size_t offset) - : FILESIZE(filesize), WRITESIZE(writesize), OFFSET(offset), testFile(FILESIZE), writeData(WRITESIZE) { + : FILESIZE(filesize), WRITESIZE(writesize), OFFSET(offset), testFile(DataFixture::generate(FILESIZE)), writeData(DataFixture::generate(WRITESIZE)) { ReturnIsFileOnLstatWithSize(FILENAME, FILESIZE); OnOpenReturnFileDescriptor(FILENAME, 0); EXPECT_CALL(fsimpl, write(0, _, _, _)).WillRepeatedly(WriteToFile); @@ -48,7 +49,7 @@ TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromBeginning WriteFile(FILENAME, writeData.data(), WRITESIZE, 0); EXPECT_EQ(WRITESIZE, testFile.size()); - EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, 0)); + EXPECT_TRUE(testFile.fileContentEquals(writeData, 0)); } TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromMiddle) { @@ -56,7 +57,7 @@ TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromMiddle) { EXPECT_EQ(OFFSET + WRITESIZE, testFile.size()); EXPECT_TRUE(testFile.regionUnchanged(0, OFFSET)); - EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, OFFSET)); + EXPECT_TRUE(testFile.fileContentEquals(writeData, OFFSET)); } TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteAfterFileEnd) { @@ -64,7 +65,7 @@ TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteAfterFileEnd) { EXPECT_EQ(FILESIZE + OFFSET + WRITESIZE, testFile.size()); EXPECT_TRUE(testFile.regionUnchanged(0, FILESIZE)); - EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, FILESIZE + OFFSET)); + EXPECT_TRUE(testFile.fileContentEquals(writeData, FILESIZE + OFFSET)); } class FuseWriteOverflowTestWithEmptyFile: public FuseWriteOverflowTest { @@ -76,12 +77,12 @@ TEST_F(FuseWriteOverflowTestWithEmptyFile, WriteToBeginOfEmptyFile) { WriteFile(FILENAME, writeData.data(), WRITESIZE, 0); EXPECT_EQ(WRITESIZE, testFile.size()); - EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, 0)); + EXPECT_TRUE(testFile.fileContentEquals(writeData, 0)); } TEST_F(FuseWriteOverflowTestWithEmptyFile, WriteAfterFileEnd) { WriteFile(FILENAME, writeData.data(), WRITESIZE, OFFSET); EXPECT_EQ(OFFSET + WRITESIZE, testFile.size()); - EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, OFFSET)); + EXPECT_TRUE(testFile.fileContentEquals(writeData, OFFSET)); } diff --git a/test/testutils/InMemoryFile.cpp b/test/testutils/InMemoryFile.cpp new file mode 100644 index 00000000..65f40cd0 --- /dev/null +++ b/test/testutils/InMemoryFile.cpp @@ -0,0 +1,56 @@ +#include "InMemoryFile.h" + +using cpputils::Data; + +InMemoryFile::InMemoryFile(Data data): _data(std::move(data)) { +} + +InMemoryFile::~InMemoryFile() { +} + +int InMemoryFile::read(void *buf, size_t count, off_t offset) const { + size_t realCount = std::min(count, _data.size() - offset); + std::memcpy(buf, (uint8_t*)_data.data() + offset, realCount); + return realCount; +} + +const void *InMemoryFile::data() const { + return _data.data(); +} + +bool InMemoryFile::fileContentEquals(const Data &expected, off_t offset) const { + return 0 == std::memcmp((uint8_t*)expected.data(), (uint8_t*)_data.data() + offset, expected.size()); +} + +size_t InMemoryFile::size() const { + return _data.size(); +} + +WriteableInMemoryFile::WriteableInMemoryFile(Data data): InMemoryFile(std::move(data)), _originalData(_data.copy()) { +} + +void WriteableInMemoryFile::write(const void *buf, size_t count, off_t offset) { + _extendFileSizeIfNecessary(count + offset); + + std::memcpy((uint8_t*)_data.data() + offset, buf, count); +} + +void WriteableInMemoryFile::_extendFileSizeIfNecessary(size_t size) { + if (size > _data.size()) { + _extendFileSize(size); + } +} + +void WriteableInMemoryFile::_extendFileSize(size_t size) { + Data newfile(size); + std::memcpy(newfile.data(), _data.data(), _data.size()); + _data = std::move(newfile); +} + +bool WriteableInMemoryFile::sizeUnchanged() const { + return _data.size() == _originalData.size(); +} + +bool WriteableInMemoryFile::regionUnchanged(off_t offset, size_t count) const { + return 0 == std::memcmp((uint8_t*)_data.data() + offset, (uint8_t*)_originalData.data() + offset, count); +} diff --git a/test/testutils/InMemoryFile.h b/test/testutils/InMemoryFile.h new file mode 100644 index 00000000..00c64217 --- /dev/null +++ b/test/testutils/InMemoryFile.h @@ -0,0 +1,40 @@ +#pragma once +#ifndef BLOCKS_MESSMER_FSPP_TEST_TESTUTILS_INMEMORYFILE_H_ +#define BLOCKS_MESSMER_FSPP_TEST_TESTUTILS_INMEMORYFILE_H_ + +#include + +class InMemoryFile { +public: + InMemoryFile(cpputils::Data data); + virtual ~InMemoryFile(); + + int read(void *buf, size_t count, off_t offset) const; + + const void *data() const; + size_t size() const; + + bool fileContentEquals(const cpputils::Data &expected, off_t offset) const; + +protected: + cpputils::Data _data; +}; + +class WriteableInMemoryFile: public InMemoryFile { +public: + WriteableInMemoryFile(cpputils::Data data); + + void write(const void *buf, size_t count, off_t offset); + + bool sizeUnchanged() const; + bool regionUnchanged(off_t offset, size_t count) const; + +private: + void _extendFileSizeIfNecessary(size_t size); + void _extendFileSize(size_t size); + + cpputils::Data _originalData; +}; + + +#endif