libcryfs/test/blockstore/interface/helpers/BlockStoreWithRandomKeysTest.cpp

146 lines
4.8 KiB
C++
Raw Normal View History

#include "blockstore/interface/helpers/BlockStoreWithRandomKeys.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <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());
2016-02-15 18:47:45 +01:00
MOCK_CONST_METHOD0(estimateNumFreeBytes, uint64_t());
MOCK_CONST_METHOD1(blockSizeFromPhysicalBlockSize, uint64_t(uint64_t));
2014-12-09 17:19:59 +01:00
};
class BlockMock: public Block {
public:
BlockMock(): Block(cpputils::Random::PseudoRandom().getFixedSize<Key::BINARY_LENGTH>()) {}
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-12-14 17:16:54 +01:00
MOCK_METHOD1(resize, void(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:
2015-10-17 21:10:26 +02:00
BlockStoreWithRandomKeysTest() :blockStoreMock(), blockStore(blockStoreMock),
key(Key::FromString("1491BB4932A389EE14BC7090AC772972")) {}
2014-12-09 17:19:59 +01:00
BlockStoreWithRandomKeysMock blockStoreMock;
2015-10-17 21:10:26 +02:00
BlockStore &blockStore;
const blockstore::Key key;
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
}