Use cpputils::DataFixture instead of DataBlockFixture

This commit is contained in:
Sebastian Messmer 2015-04-25 16:43:52 +02:00
parent 3e12c771a1
commit a2c89ed73a
7 changed files with 42 additions and 62 deletions

View File

@ -3,14 +3,14 @@
#include "../../../implementations/encrypted/ciphers/AES256_GCM.h" #include "../../../implementations/encrypted/ciphers/AES256_GCM.h"
#include "../../../implementations/encrypted/ciphers/Cipher.h" #include "../../../implementations/encrypted/ciphers/Cipher.h"
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
#include <messmer/cpp-utils/data/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <boost/optional/optional_io.hpp> #include <boost/optional/optional_io.hpp>
using namespace blockstore::encrypted; using namespace blockstore::encrypted;
using cpputils::Data; using cpputils::Data;
using cpputils::DataBlockFixture; using cpputils::DataFixture;
template<class Cipher> template<class Cipher>
class CipherTest: public ::testing::Test { class CipherTest: public ::testing::Test {
@ -19,7 +19,7 @@ public:
typename Cipher::EncryptionKey encKey = createRandomKey(); typename Cipher::EncryptionKey encKey = createRandomKey();
static typename Cipher::EncryptionKey createRandomKey(int seed = 0) { static typename Cipher::EncryptionKey createRandomKey(int seed = 0) {
DataBlockFixture data(Cipher::EncryptionKey::BINARY_LENGTH, seed); Data data = DataFixture::generate(Cipher::EncryptionKey::BINARY_LENGTH, seed);
return Cipher::EncryptionKey::FromBinary(data.data()); return Cipher::EncryptionKey::FromBinary(data.data());
} }
@ -50,16 +50,11 @@ public:
} }
static Data CreateZeroes(unsigned int size) { static Data CreateZeroes(unsigned int size) {
Data zeroes(size); return std::move(Data(size).FillWithZeroes());
zeroes.FillWithZeroes();
return zeroes;
} }
static Data CreateData(unsigned int size, unsigned int seed = 0) { static Data CreateData(unsigned int size, unsigned int seed = 0) {
DataBlockFixture data(size, seed); return DataFixture::generate(size, seed);
Data result(size);
std::memcpy(result.data(), data.data(), size);
return result;
} }
}; };

View File

@ -1,6 +1,6 @@
#include "../../../../implementations/ondisk/FileAlreadyExistsException.h" #include "../../../../implementations/ondisk/FileAlreadyExistsException.h"
#include "../../../../implementations/ondisk/OnDiskBlock.h" #include "../../../../implementations/ondisk/OnDiskBlock.h"
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
#include "google/gtest/gtest.h" #include "google/gtest/gtest.h"
#include <messmer/cpp-utils/tempfile/TempFile.h> #include <messmer/cpp-utils/tempfile/TempFile.h>
@ -12,7 +12,7 @@ using ::testing::Values;
using std::unique_ptr; using std::unique_ptr;
using cpputils::Data; using cpputils::Data;
using cpputils::DataBlockFixture; using cpputils::DataFixture;
using cpputils::TempFile; using cpputils::TempFile;
using cpputils::TempDir; using cpputils::TempDir;
@ -28,13 +28,13 @@ public:
: dir(), : dir(),
key(Key::FromString("1491BB4932A389EE14BC7090AC772972")), key(Key::FromString("1491BB4932A389EE14BC7090AC772972")),
file(dir.path() / key.ToString(), false), file(dir.path() / key.ToString(), false),
randomData(GetParam()) { randomData(DataFixture::generate(GetParam())) {
} }
TempDir dir; TempDir dir;
Key key; Key key;
TempFile file; TempFile file;
DataBlockFixture randomData; Data randomData;
unique_ptr<OnDiskBlock> CreateBlockAndLoadItFromDisk() { unique_ptr<OnDiskBlock> CreateBlockAndLoadItFromDisk() {
{ {

View File

@ -1,5 +1,5 @@
#include "../../../../implementations/ondisk/OnDiskBlock.h" #include "../../../../implementations/ondisk/OnDiskBlock.h"
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
#include "../../../../utils/FileDoesntExistException.h" #include "../../../../utils/FileDoesntExistException.h"
#include "google/gtest/gtest.h" #include "google/gtest/gtest.h"
@ -16,7 +16,7 @@ using std::ofstream;
using std::unique_ptr; using std::unique_ptr;
using std::ios; using std::ios;
using cpputils::Data; using cpputils::Data;
using cpputils::DataBlockFixture; using cpputils::DataFixture;
using cpputils::TempFile; using cpputils::TempFile;
using cpputils::TempDir; using cpputils::TempDir;
@ -41,18 +41,15 @@ public:
data.StoreToFile(file.path()); data.StoreToFile(file.path());
} }
void StoreData(const DataBlockFixture &data) { void StoreData(const Data &data) {
//TODO Implement data.StoreToFile(filepath) instead data.StoreToFile(file.path());
Data dataobj(data.size());
std::memcpy(dataobj.data(), data.data(), data.size());
dataobj.StoreToFile(file.path());
} }
unique_ptr<OnDiskBlock> LoadBlock() { unique_ptr<OnDiskBlock> LoadBlock() {
return OnDiskBlock::LoadFromDisk(dir.path(), key); return OnDiskBlock::LoadFromDisk(dir.path(), key);
} }
void EXPECT_BLOCK_DATA_EQ(const DataBlockFixture &expected, const OnDiskBlock &actual) { void EXPECT_BLOCK_DATA_EQ(const Data &expected, const OnDiskBlock &actual) {
EXPECT_EQ(expected.size(), actual.size()); EXPECT_EQ(expected.size(), actual.size());
EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), expected.size())); EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), expected.size()));
} }
@ -68,7 +65,7 @@ TEST_P(OnDiskBlockLoadTest, FileSizeIsCorrect) {
} }
TEST_P(OnDiskBlockLoadTest, LoadedDataIsCorrect) { TEST_P(OnDiskBlockLoadTest, LoadedDataIsCorrect) {
DataBlockFixture randomData(GetParam()); Data randomData = DataFixture::generate(GetParam());
StoreData(randomData); StoreData(randomData);
auto block = LoadBlock(); auto block = LoadBlock();

View File

@ -1,7 +1,7 @@
#include "../../../interface/helpers/BlockStoreWithRandomKeys.h" #include "../../../interface/helpers/BlockStoreWithRandomKeys.h"
#include "google/gtest/gtest.h" #include "google/gtest/gtest.h"
#include "google/gmock/gmock.h" #include "google/gmock/gmock.h"
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
using ::testing::Test; using ::testing::Test;
using ::testing::_; using ::testing::_;
@ -14,7 +14,7 @@ using std::string;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using cpputils::Data; using cpputils::Data;
using cpputils::DataBlockFixture; using cpputils::DataFixture;
using namespace blockstore; using namespace blockstore;
@ -49,7 +49,7 @@ public:
const blockstore::Key key = Key::FromString("1491BB4932A389EE14BC7090AC772972"); const blockstore::Key key = Key::FromString("1491BB4932A389EE14BC7090AC772972");
Data createDataWithSize(size_t size) { Data createDataWithSize(size_t size) {
DataBlockFixture fixture(size); Data fixture(DataFixture::generate(size));
Data data(size); Data data(size);
std::memcpy(data.data(), fixture.data(), size); std::memcpy(data.data(), fixture.data(), size);
return data; return data;

View File

@ -12,11 +12,8 @@ public:
BlockStoreDataParametrizedTest(std::unique_ptr<blockstore::BlockStore> blockStore_, const DataRange &testData_) BlockStoreDataParametrizedTest(std::unique_ptr<blockstore::BlockStore> blockStore_, const DataRange &testData_)
: blockStore(std::move(blockStore_)), : blockStore(std::move(blockStore_)),
testData(testData_), testData(testData_),
foregroundData(testData.count), backgroundData(testData.blocksize) { foregroundData(cpputils::DataFixture::generate(testData.count, 0)),
cpputils::DataBlockFixture _foregroundData(testData.count); backgroundData(cpputils::DataFixture::generate(testData.blocksize, 1)) {
cpputils::DataBlockFixture _backgroundData(testData.blocksize);
std::memcpy(foregroundData.data(), _foregroundData.data(), foregroundData.size());
std::memcpy(backgroundData.data(), _backgroundData.data(), backgroundData.size());
} }
void TestWriteAndReadImmediately() { void TestWriteAndReadImmediately() {

View File

@ -1,7 +1,7 @@
// This file is meant to be included by BlockStoreTest.h only // This file is meant to be included by BlockStoreTest.h only
#include <messmer/cpp-utils/data/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
class BlockStoreSizeParameterizedTest { class BlockStoreSizeParameterizedTest {
public: public:
@ -19,39 +19,34 @@ public:
} }
void TestCreatedBlockData() { void TestCreatedBlockData() {
cpputils::DataBlockFixture dataFixture(size); cpputils::Data data = cpputils::DataFixture::generate(size);
cpputils::Data data(size); auto block = blockStore->create(data);
std::memcpy(data.data(), dataFixture.data(), size); EXPECT_EQ(0, std::memcmp(data.data(), block->data(), size));
auto block = blockStore->create(data);
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block->data(), size));
} }
void TestLoadingUnchangedBlockData() { void TestLoadingUnchangedBlockData() {
cpputils::DataBlockFixture dataFixture(size); cpputils::Data data = cpputils::DataFixture::generate(size);
cpputils::Data data(size); blockstore::Key key = blockStore->create(data)->key();
std::memcpy(data.data(), dataFixture.data(), size);
blockstore::Key key = blockStore->create(data)->key();
auto loaded_block = blockStore->load(key); auto loaded_block = blockStore->load(key);
EXPECT_EQ(0, std::memcmp(dataFixture.data(), loaded_block->data(), size)); EXPECT_EQ(0, std::memcmp(data.data(), loaded_block->data(), size));
} }
void TestLoadedBlockIsCorrect() { void TestLoadedBlockIsCorrect() {
cpputils::DataBlockFixture randomData(size); cpputils::Data randomData = cpputils::DataFixture::generate(size);
auto loaded_block = StoreDataToBlockAndLoadIt(randomData); auto loaded_block = StoreDataToBlockAndLoadIt(randomData);
EXPECT_EQ(size, loaded_block->size()); EXPECT_EQ(size, loaded_block->size());
EXPECT_EQ(0, std::memcmp(randomData.data(), loaded_block->data(), size)); EXPECT_EQ(0, std::memcmp(randomData.data(), loaded_block->data(), size));
} }
void TestLoadedBlockIsCorrectWhenLoadedDirectlyAfterFlushing() { void TestLoadedBlockIsCorrectWhenLoadedDirectlyAfterFlushing() {
cpputils::DataBlockFixture randomData(size); cpputils::Data randomData = cpputils::DataFixture::generate(size);
auto loaded_block = StoreDataToBlockAndLoadItDirectlyAfterFlushing(randomData); auto loaded_block = StoreDataToBlockAndLoadItDirectlyAfterFlushing(randomData);
EXPECT_EQ(size, loaded_block->size()); EXPECT_EQ(size, loaded_block->size());
EXPECT_EQ(0, std::memcmp(randomData.data(), loaded_block->data(), size)); EXPECT_EQ(0, std::memcmp(randomData.data(), loaded_block->data(), size));
} }
void TestAfterCreate_FlushingDoesntChangeBlock() { void TestAfterCreate_FlushingDoesntChangeBlock() {
cpputils::DataBlockFixture randomData(size); cpputils::Data randomData = cpputils::DataFixture::generate(size);
auto block = CreateBlock(); auto block = CreateBlock();
WriteDataToBlock(block.get(), randomData); WriteDataToBlock(block.get(), randomData);
block->flush(); block->flush();
@ -60,7 +55,7 @@ public:
} }
void TestAfterLoad_FlushingDoesntChangeBlock() { void TestAfterLoad_FlushingDoesntChangeBlock() {
cpputils::DataBlockFixture randomData(size); cpputils::Data randomData = cpputils::DataFixture::generate(size);
auto block = CreateBlockAndLoadIt(); auto block = CreateBlockAndLoadIt();
WriteDataToBlock(block.get(), randomData); WriteDataToBlock(block.get(), randomData);
block->flush(); block->flush();
@ -69,7 +64,7 @@ public:
} }
void TestAfterCreate_FlushesWhenDestructed() { void TestAfterCreate_FlushesWhenDestructed() {
cpputils::DataBlockFixture randomData(size); cpputils::Data randomData = cpputils::DataFixture::generate(size);
blockstore::Key key = key; blockstore::Key key = key;
{ {
auto block = blockStore->create(cpputils::Data(size)); auto block = blockStore->create(cpputils::Data(size));
@ -81,7 +76,7 @@ public:
} }
void TestAfterLoad_FlushesWhenDestructed() { void TestAfterLoad_FlushesWhenDestructed() {
cpputils::DataBlockFixture randomData(size); cpputils::Data randomData = cpputils::DataFixture::generate(size);
blockstore::Key key = key; blockstore::Key key = key;
{ {
key = CreateBlock()->key(); key = CreateBlock()->key();
@ -109,20 +104,16 @@ private:
return ZEROES; return ZEROES;
} }
std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadIt(const cpputils::DataBlockFixture &data) { std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadIt(const cpputils::Data &data) {
blockstore::Key key = StoreDataToBlockAndGetKey(data); blockstore::Key key = StoreDataToBlockAndGetKey(data);
return blockStore->load(key); return blockStore->load(key);
} }
blockstore::Key StoreDataToBlockAndGetKey(const cpputils::DataBlockFixture &dataFixture) { blockstore::Key StoreDataToBlockAndGetKey(const cpputils::Data &data) {
cpputils::Data data(dataFixture.size());
std::memcpy(data.data(), dataFixture.data(), dataFixture.size());
return blockStore->create(data)->key(); return blockStore->create(data)->key();
} }
std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadItDirectlyAfterFlushing(const cpputils::DataBlockFixture &dataFixture) { std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadItDirectlyAfterFlushing(const cpputils::Data &data) {
cpputils::Data data(dataFixture.size());
std::memcpy(data.data(), dataFixture.data(), dataFixture.size());
auto block = blockStore->create(data); auto block = blockStore->create(data);
block->flush(); block->flush();
return blockStore->load(block->key()); return blockStore->load(block->key());
@ -137,11 +128,11 @@ private:
return blockStore->create(cpputils::Data(size)); return blockStore->create(cpputils::Data(size));
} }
void WriteDataToBlock(blockstore::Block *block, const cpputils::DataBlockFixture &randomData) { void WriteDataToBlock(blockstore::Block *block, const cpputils::Data &randomData) {
block->write(randomData.data(), 0, randomData.size()); block->write(randomData.data(), 0, randomData.size());
} }
void EXPECT_BLOCK_DATA_CORRECT(const blockstore::Block &block, const cpputils::DataBlockFixture &randomData) { void EXPECT_BLOCK_DATA_CORRECT(const blockstore::Block &block, const cpputils::Data &randomData) {
EXPECT_EQ(randomData.size(), block.size()); EXPECT_EQ(randomData.size(), block.size());
EXPECT_EQ(0, std::memcmp(randomData.data(), block.data(), randomData.size())); EXPECT_EQ(0, std::memcmp(randomData.data(), block.data(), randomData.size()));
} }

View File

@ -1,5 +1,5 @@
#include "../../implementations/testfake/FakeBlockStore.h" #include "../../implementations/testfake/FakeBlockStore.h"
#include <messmer/cpp-utils/data/DataBlockFixture.h> #include <messmer/cpp-utils/data/DataFixture.h>
#include "../../utils/BlockStoreUtils.h" #include "../../utils/BlockStoreUtils.h"
#include "google/gtest/gtest.h" #include "google/gtest/gtest.h"
@ -12,7 +12,7 @@ using ::testing::Values;
using std::make_unique; using std::make_unique;
using std::unique_ptr; using std::unique_ptr;
using cpputils::Data; using cpputils::Data;
using cpputils::DataBlockFixture; using cpputils::DataFixture;
using namespace blockstore; using namespace blockstore;
using namespace blockstore::utils; using namespace blockstore::utils;
@ -24,13 +24,13 @@ public:
unsigned int SIZE = 1024 * 1024; unsigned int SIZE = 1024 * 1024;
BlockStoreUtilsTest(): BlockStoreUtilsTest():
ZEROES(SIZE), ZEROES(SIZE),
dataFixture(SIZE), dataFixture(DataFixture::generate(SIZE)),
blockStore(make_unique<FakeBlockStore>()) { blockStore(make_unique<FakeBlockStore>()) {
ZEROES.FillWithZeroes(); ZEROES.FillWithZeroes();
} }
Data ZEROES; Data ZEROES;
DataBlockFixture dataFixture; Data dataFixture;
unique_ptr<BlockStore> blockStore; unique_ptr<BlockStore> blockStore;
}; };