Use cpputils::DataFixture instead of DataBlockFixture

This commit is contained in:
Sebastian Messmer 2015-04-25 16:43:37 +02:00
parent 0b60370665
commit 65eb73e7d9
5 changed files with 130 additions and 34 deletions

View File

@ -1,4 +1,6 @@
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
#include <messmer/cpp-utils/data/Data.h>
#include "../../testutils/InMemoryFile.h"
#include "testutils/FuseReadTest.h" #include "testutils/FuseReadTest.h"
#include "../../../fuse/FuseErrnoException.h" #include "../../../fuse/FuseErrnoException.h"
@ -21,7 +23,8 @@ using std::get;
using std::min; using std::min;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using cpputils::DataBlockFixture; using cpputils::Data;
using cpputils::DataFixture;
using namespace fspp::fuse; using namespace fspp::fuse;
@ -45,20 +48,18 @@ struct TestData {
// memory region and check methods to check for data equality of a region. // memory region and check methods to check for data equality of a region.
class FuseReadReturnedDataTest: public FuseReadTest, public WithParamInterface<tuple<size_t, off_t, size_t>> { class FuseReadReturnedDataTest: public FuseReadTest, public WithParamInterface<tuple<size_t, off_t, size_t>> {
public: public:
unique_ptr<DataBlockFixture> testFile; std::unique_ptr<InMemoryFile> testFile;
TestData testData; TestData testData;
FuseReadReturnedDataTest() { FuseReadReturnedDataTest(): testFile(nullptr), testData(GetParam()) {
testData = GetParam(); testFile = make_unique<InMemoryFile>(DataFixture::generate(testData.fileSize()));
testFile = make_unique<DataBlockFixture>(testData.fileSize());
ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize()); ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize());
OnOpenReturnFileDescriptor(FILENAME, 0); OnOpenReturnFileDescriptor(FILENAME, 0);
EXPECT_CALL(fsimpl, read(0, _, _, _)) EXPECT_CALL(fsimpl, read(0, _, _, _))
.WillRepeatedly(ReadFromFile); .WillRepeatedly(ReadFromFile);
} }
// This read() mock implementation reads from the stored virtual file. // This read() mock implementation reads from the stored virtual file (testFile).
Action<int(int, void*, size_t, off_t)> ReadFromFile = Invoke([this](int, void *buf, size_t count, off_t offset) { Action<int(int, void*, size_t, off_t)> ReadFromFile = Invoke([this](int, void *buf, size_t count, off_t offset) {
return testFile->read(buf, count, offset); return testFile->read(buf, count, offset);
}); });
@ -67,8 +68,7 @@ INSTANTIATE_TEST_CASE_P(FuseReadReturnedDataTest, FuseReadReturnedDataTest, Comb
TEST_P(FuseReadReturnedDataTest, ReturnedDataRangeIsCorrect) { TEST_P(FuseReadReturnedDataTest, ReturnedDataRangeIsCorrect) {
char *buf = new char[testData.count]; Data buf(testData.count);
ReadFile(FILENAME, buf, testData.count, testData.offset); ReadFile(FILENAME, buf.data(), testData.count, testData.offset);
EXPECT_TRUE(testFile->fileContentEqual(buf, testData.count, testData.offset)); EXPECT_TRUE(testFile->fileContentEquals(buf, testData.offset));
delete[] buf;
} }

View File

@ -1,5 +1,6 @@
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
#include "testutils/FuseWriteTest.h" #include "testutils/FuseWriteTest.h"
#include "../../testutils/InMemoryFile.h"
#include "../../../fuse/FuseErrnoException.h" #include "../../../fuse/FuseErrnoException.h"
@ -21,8 +22,8 @@ using std::get;
using std::min; using std::min;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using cpputils::DataBlockFixture; using cpputils::Data;
using cpputils::DataBlockFixtureWriteable; using cpputils::DataFixture;
using namespace fspp::fuse; using namespace fspp::fuse;
@ -45,13 +46,11 @@ struct TestData {
// memory region and check methods to check for data equality of a region. // memory region and check methods to check for data equality of a region.
class FuseWriteDataTest: public FuseWriteTest, public WithParamInterface<tuple<size_t, off_t, size_t>> { class FuseWriteDataTest: public FuseWriteTest, public WithParamInterface<tuple<size_t, off_t, size_t>> {
public: public:
unique_ptr<DataBlockFixtureWriteable> testFile; std::unique_ptr<WriteableInMemoryFile> testFile;
TestData testData; TestData testData;
FuseWriteDataTest() { FuseWriteDataTest(): testFile(nullptr), testData(GetParam()) {
testData = GetParam(); testFile = make_unique<WriteableInMemoryFile>(DataFixture::generate(testData.fileSize(), 1));
testFile = make_unique<DataBlockFixtureWriteable>(testData.fileSize(), 1);
ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize()); ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize());
OnOpenReturnFileDescriptor(FILENAME, 0); OnOpenReturnFileDescriptor(FILENAME, 0);
EXPECT_CALL(fsimpl, write(0, _, _, _)) EXPECT_CALL(fsimpl, write(0, _, _, _))
@ -67,14 +66,14 @@ INSTANTIATE_TEST_CASE_P(FuseWriteDataTest, FuseWriteDataTest, Combine(Values(0,1
TEST_P(FuseWriteDataTest, DataWasCorrectlyWritten) { TEST_P(FuseWriteDataTest, DataWasCorrectlyWritten) {
DataBlockFixture randomWriteData(testData.count, 2); Data randomWriteData = DataFixture::generate(testData.count, 2);
WriteFile(FILENAME, randomWriteData.data(), testData.count, testData.offset); 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) { TEST_P(FuseWriteDataTest, RestOfFileIsUnchanged) {
DataBlockFixture randomWriteData(testData.count, 2); Data randomWriteData = DataFixture::generate(testData.count, 2);
WriteFile(FILENAME, randomWriteData.data(), testData.count, testData.offset); WriteFile(FILENAME, randomWriteData.data(), testData.count, testData.offset);
EXPECT_TRUE(testFile->sizeUnchanged()); EXPECT_TRUE(testFile->sizeUnchanged());

View File

@ -1,5 +1,6 @@
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
#include "testutils/FuseWriteTest.h" #include "testutils/FuseWriteTest.h"
#include "../../testutils/InMemoryFile.h"
#include "../../../fuse/FuseErrnoException.h" #include "../../../fuse/FuseErrnoException.h"
@ -11,8 +12,8 @@ using ::testing::Invoke;
using ::testing::Action; using ::testing::Action;
using std::min; using std::min;
using cpputils::DataBlockFixture; using cpputils::DataFixture;
using cpputils::DataBlockFixtureWriteable; using cpputils::Data;
using namespace fspp::fuse; using namespace fspp::fuse;
@ -22,11 +23,11 @@ public:
size_t WRITESIZE; size_t WRITESIZE;
size_t OFFSET; size_t OFFSET;
DataBlockFixtureWriteable testFile; WriteableInMemoryFile testFile;
DataBlockFixture writeData; Data writeData;
FuseWriteOverflowTest(size_t filesize, size_t writesize, size_t offset) 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); ReturnIsFileOnLstatWithSize(FILENAME, FILESIZE);
OnOpenReturnFileDescriptor(FILENAME, 0); OnOpenReturnFileDescriptor(FILENAME, 0);
EXPECT_CALL(fsimpl, write(0, _, _, _)).WillRepeatedly(WriteToFile); EXPECT_CALL(fsimpl, write(0, _, _, _)).WillRepeatedly(WriteToFile);
@ -48,7 +49,7 @@ TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromBeginning
WriteFile(FILENAME, writeData.data(), WRITESIZE, 0); WriteFile(FILENAME, writeData.data(), WRITESIZE, 0);
EXPECT_EQ(WRITESIZE, testFile.size()); EXPECT_EQ(WRITESIZE, testFile.size());
EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, 0)); EXPECT_TRUE(testFile.fileContentEquals(writeData, 0));
} }
TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromMiddle) { TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromMiddle) {
@ -56,7 +57,7 @@ TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteMoreThanFileSizeFromMiddle) {
EXPECT_EQ(OFFSET + WRITESIZE, testFile.size()); EXPECT_EQ(OFFSET + WRITESIZE, testFile.size());
EXPECT_TRUE(testFile.regionUnchanged(0, OFFSET)); EXPECT_TRUE(testFile.regionUnchanged(0, OFFSET));
EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, OFFSET)); EXPECT_TRUE(testFile.fileContentEquals(writeData, OFFSET));
} }
TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteAfterFileEnd) { TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteAfterFileEnd) {
@ -64,7 +65,7 @@ TEST_F(FuseWriteOverflowTestWithNonemptyFile, WriteAfterFileEnd) {
EXPECT_EQ(FILESIZE + OFFSET + WRITESIZE, testFile.size()); EXPECT_EQ(FILESIZE + OFFSET + WRITESIZE, testFile.size());
EXPECT_TRUE(testFile.regionUnchanged(0, FILESIZE)); 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 { class FuseWriteOverflowTestWithEmptyFile: public FuseWriteOverflowTest {
@ -76,12 +77,12 @@ TEST_F(FuseWriteOverflowTestWithEmptyFile, WriteToBeginOfEmptyFile) {
WriteFile(FILENAME, writeData.data(), WRITESIZE, 0); WriteFile(FILENAME, writeData.data(), WRITESIZE, 0);
EXPECT_EQ(WRITESIZE, testFile.size()); EXPECT_EQ(WRITESIZE, testFile.size());
EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, 0)); EXPECT_TRUE(testFile.fileContentEquals(writeData, 0));
} }
TEST_F(FuseWriteOverflowTestWithEmptyFile, WriteAfterFileEnd) { TEST_F(FuseWriteOverflowTestWithEmptyFile, WriteAfterFileEnd) {
WriteFile(FILENAME, writeData.data(), WRITESIZE, OFFSET); WriteFile(FILENAME, writeData.data(), WRITESIZE, OFFSET);
EXPECT_EQ(OFFSET + WRITESIZE, testFile.size()); EXPECT_EQ(OFFSET + WRITESIZE, testFile.size());
EXPECT_TRUE(testFile.fileContentEqual(writeData.data(), WRITESIZE, OFFSET)); EXPECT_TRUE(testFile.fileContentEquals(writeData, OFFSET));
} }

View File

@ -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);
}

View File

@ -0,0 +1,40 @@
#pragma once
#ifndef BLOCKS_MESSMER_FSPP_TEST_TESTUTILS_INMEMORYFILE_H_
#define BLOCKS_MESSMER_FSPP_TEST_TESTUTILS_INMEMORYFILE_H_
#include <messmer/cpp-utils/data/Data.h>
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