libcryfs/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp

140 lines
4.5 KiB
C++
Raw Normal View History

#include "../../../interface/helpers/BlockStoreWithRandomKeys.h"
2015-02-17 00:23:33 +01:00
#include "google/gtest/gtest.h"
#include "google/gmock/gmock.h"
#include <messmer/cpp-utils/data/DataFixture.h>
2014-12-09 17:19:59 +01:00
using ::testing::Test;
using ::testing::_;
using ::testing::Return;
using ::testing::Invoke;
using ::testing::Eq;
using ::testing::ByRef;
2014-12-09 17:19:59 +01:00
using std::string;
using cpputils::Data;
using cpputils::DataFixture;
using cpputils::unique_ref;
using boost::optional;
2014-12-09 17:19:59 +01:00
using namespace blockstore;
class BlockStoreWithRandomKeysMock: public BlockStoreWithRandomKeys {
public:
optional<unique_ref<Block>> tryCreate(const Key &key, Data data) {
return cpputils::nullcheck(std::unique_ptr<Block>(do_create(key, data)));
2014-12-09 17:19:59 +01:00
}
MOCK_METHOD2(do_create, Block*(const Key &, const Data &data));
optional<unique_ref<Block>> load(const Key &key) {
return cpputils::nullcheck(std::unique_ptr<Block>(do_load(key)));
2014-12-09 17:19:59 +01:00
}
MOCK_METHOD1(do_load, Block*(const Key &));
void remove(unique_ref<Block> block) {UNUSED(block);}
2015-02-23 21:07:07 +01:00
MOCK_CONST_METHOD0(numBlocks, uint64_t());
2014-12-09 17:19:59 +01:00
};
class BlockMock: public Block {
public:
BlockMock(): Block(Key::CreatePseudoRandom()) {}
2014-12-09 17:19:59 +01:00
MOCK_CONST_METHOD0(data, const void*());
MOCK_METHOD3(write, void(const void*, uint64_t, uint64_t));
2014-12-09 17:19:59 +01:00
MOCK_METHOD0(flush, void());
MOCK_CONST_METHOD0(size, size_t());
2015-01-24 22:08:41 +01:00
MOCK_CONST_METHOD0(key, const Key&());
2014-12-09 17:19:59 +01:00
};
class BlockStoreWithRandomKeysTest: public Test {
public:
BlockStoreWithRandomKeysMock blockStoreMock;
BlockStore &blockStore = blockStoreMock;
2014-12-09 20:57:10 +01:00
const blockstore::Key key = Key::FromString("1491BB4932A389EE14BC7090AC772972");
Data createDataWithSize(size_t size) {
Data fixture(DataFixture::generate(size));
Data data(size);
std::memcpy(data.data(), fixture.data(), size);
return data;
}
2014-12-09 17:19:59 +01:00
};
TEST_F(BlockStoreWithRandomKeysTest, DataIsPassedThrough0) {
Data data = createDataWithSize(0);
EXPECT_CALL(blockStoreMock, do_create(_, Eq(ByRef(data)))).WillOnce(Return(new BlockMock));
blockStore.create(data);
2014-12-09 17:19:59 +01:00
}
TEST_F(BlockStoreWithRandomKeysTest, DataIsPassedThrough1) {
Data data = createDataWithSize(1);
EXPECT_CALL(blockStoreMock, do_create(_, Eq(ByRef(data)))).WillOnce(Return(new BlockMock));
blockStore.create(data);
2014-12-09 17:19:59 +01:00
}
TEST_F(BlockStoreWithRandomKeysTest, DataIsPassedThrough1024) {
Data data = createDataWithSize(1024);
EXPECT_CALL(blockStoreMock, do_create(_, Eq(ByRef(data)))).WillOnce(Return(new BlockMock));
blockStore.create(data);
2014-12-09 17:19:59 +01:00
}
TEST_F(BlockStoreWithRandomKeysTest, KeyHasCorrectSize) {
EXPECT_CALL(blockStoreMock, do_create(_, _)).WillOnce(Invoke([](const Key &key, const Data &) {
2015-04-09 20:07:03 +02:00
EXPECT_EQ(Key::STRING_LENGTH, key.ToString().size());
return new BlockMock;
2014-12-09 17:19:59 +01:00
}));
blockStore.create(createDataWithSize(1024));
2014-12-09 17:19:59 +01:00
}
TEST_F(BlockStoreWithRandomKeysTest, TwoBlocksGetDifferentKeys) {
2014-12-09 20:57:10 +01:00
Key first_key = key;
2014-12-09 17:19:59 +01:00
EXPECT_CALL(blockStoreMock, do_create(_, _))
.WillOnce(Invoke([&first_key](const Key &key, const Data &) {
2014-12-09 17:19:59 +01:00
first_key = key;
return new BlockMock;
2014-12-09 17:19:59 +01:00
}))
.WillOnce(Invoke([&first_key](const Key &key, const Data &) {
2014-12-09 17:19:59 +01:00
EXPECT_NE(first_key, key);
return new BlockMock;
2014-12-09 17:19:59 +01:00
}));
Data data = createDataWithSize(1024);
blockStore.create(data);
blockStore.create(data);
2014-12-09 17:19:59 +01:00
}
TEST_F(BlockStoreWithRandomKeysTest, WillTryADifferentKeyIfKeyAlreadyExists) {
2014-12-09 20:57:10 +01:00
Key first_key = key;
Data data = createDataWithSize(1024);
EXPECT_CALL(blockStoreMock, do_create(_, Eq(ByRef(data))))
.WillOnce(Invoke([&first_key](const Key &key, const Data &) {
2014-12-09 17:19:59 +01:00
first_key = key;
return nullptr;
}))
//TODO Check that this test case fails when the second do_create call gets different data
.WillOnce(Invoke([&first_key](const Key &key, const Data &) {
2014-12-09 17:19:59 +01:00
EXPECT_NE(first_key, key);
return new BlockMock;
2014-12-09 17:19:59 +01:00
}));
blockStore.create(data);
2014-12-09 17:19:59 +01:00
}
TEST_F(BlockStoreWithRandomKeysTest, WillTryADifferentKeyIfKeyAlreadyExistsTwoTimes) {
2014-12-09 20:57:10 +01:00
Key first_key = key;
Data data = createDataWithSize(1024);
EXPECT_CALL(blockStoreMock, do_create(_, Eq(ByRef(data))))
.WillOnce(Invoke([&first_key](const Key &key, const Data &) {
2014-12-09 17:19:59 +01:00
first_key = key;
return nullptr;
}))
//TODO Check that this test case fails when the second/third do_create calls get different data
.WillOnce(Invoke([&first_key](const Key &key, const Data &) {
2014-12-09 17:19:59 +01:00
first_key = key;
return nullptr;
}))
.WillOnce(Invoke([&first_key](const Key &key, const Data &) {
2014-12-09 17:19:59 +01:00
EXPECT_NE(first_key, key);
return new BlockMock;
2014-12-09 17:19:59 +01:00
}));
blockStore.create(data);
2014-12-09 17:19:59 +01:00
}