diff --git a/implementations/encrypted/EncryptedBlockStore.h b/implementations/encrypted/EncryptedBlockStore.h index 09c35723..6845d3e4 100644 --- a/implementations/encrypted/EncryptedBlockStore.h +++ b/implementations/encrypted/EncryptedBlockStore.h @@ -22,6 +22,9 @@ public: void remove(std::unique_ptr block) override; uint64_t numBlocks() const override; + //This function should only be used by test cases + void __setKey(const typename Cipher::EncryptionKey &encKey); + private: std::unique_ptr _baseBlockStore; typename Cipher::EncryptionKey _encKey; @@ -66,6 +69,11 @@ uint64_t EncryptedBlockStore::numBlocks() const { return _baseBlockStore->numBlocks(); } +template +void EncryptedBlockStore::__setKey(const typename Cipher::EncryptionKey &encKey) { + _encKey = encKey; +} + } } diff --git a/test/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp b/test/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp index 072a028a..0a6ca336 100644 --- a/test/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp +++ b/test/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp @@ -2,34 +2,112 @@ #include "testutils/FakeAuthenticatedCipher.h" #include "../../../implementations/encrypted/EncryptedBlockStore.h" #include "../../../implementations/testfake/FakeBlockStore.h" +#include "../../../utils/BlockStoreUtils.h" +#include using ::testing::Test; using std::unique_ptr; using std::make_unique; +using cpputils::DataFixture; +using cpputils::Data; + using blockstore::testfake::FakeBlockStore; using namespace blockstore::encrypted; class EncryptedBlockStoreTest: public Test { public: - EncryptedBlockStoreTest(): blockStore(make_unique>(make_unique(), FakeAuthenticatedCipher::Key1())) {} + static constexpr unsigned int BLOCKSIZE = 1024; + EncryptedBlockStoreTest(): + _baseBlockStore(make_unique()), + baseBlockStore(_baseBlockStore.get()), + blockStore(make_unique>(std::move(_baseBlockStore), FakeAuthenticatedCipher::Key1())), + data(DataFixture::generate(BLOCKSIZE)) { + } + unique_ptr _baseBlockStore; + FakeBlockStore *baseBlockStore; unique_ptr> blockStore; + Data data; + + blockstore::Key CreateBlockDirectlyWithFixtureAndReturnKey() { + return blockStore->create(data)->key(); + } + + blockstore::Key CreateBlockWriteFixtureToItAndReturnKey() { + auto block = blockStore->create(Data(data.size())); + block->write(data.data(), 0, data.size()); + return block->key(); + } + + void ModifyBaseBlock(const blockstore::Key &key) { + auto block = baseBlockStore->load(key); + uint8_t middle_byte = ((byte*)block->data())[10]; + uint8_t new_middle_byte = middle_byte + 1; + block->write(&new_middle_byte, 10, 1); + } + + blockstore::Key CopyBaseBlock(const blockstore::Key &key) { + auto source = baseBlockStore->load(key); + return blockstore::utils::copyToNewBlock(baseBlockStore, *source)->key(); + } }; -TEST_F(EncryptedBlockStoreTest, LoadingWithSameKeyWorks) { - //TODO implement +TEST_F(EncryptedBlockStoreTest, LoadingWithSameKeyWorks_WriteOnCreate) { + auto key = CreateBlockDirectlyWithFixtureAndReturnKey(); + auto loaded = blockStore->load(key); + EXPECT_NE(nullptr, loaded.get()); + EXPECT_EQ(data.size(), loaded->size()); + EXPECT_EQ(0, std::memcmp(data.data(), loaded->data(), data.size())); } -TEST_F(EncryptedBlockStoreTest, LoadingWithDifferentKeyDoesntWork) { - //TODO Implement +TEST_F(EncryptedBlockStoreTest, LoadingWithSameKeyWorks_WriteSeparately) { + auto key = CreateBlockWriteFixtureToItAndReturnKey(); + auto loaded = blockStore->load(key); + EXPECT_NE(nullptr, loaded.get()); + EXPECT_EQ(data.size(), loaded->size()); + EXPECT_EQ(0, std::memcmp(data.data(), loaded->data(), data.size())); } -TEST_F(EncryptedBlockStoreTest, LoadingModifiedBlockFails) { - //TODO Implement +TEST_F(EncryptedBlockStoreTest, LoadingWithDifferentKeyDoesntWork_WriteOnCreate) { + auto key = CreateBlockDirectlyWithFixtureAndReturnKey(); + blockStore->__setKey(FakeAuthenticatedCipher::Key2()); + auto loaded = blockStore->load(key); + EXPECT_EQ(nullptr, loaded.get()); } -TEST_F(EncryptedBlockStoreTest, LoadingWithDifferentBlockIdFails) { - //TODO loading it with a different blockstore::Key will fail (because it stores its key in a header) +TEST_F(EncryptedBlockStoreTest, LoadingWithDifferentKeyDoesntWork_WriteSeparately) { + auto key = CreateBlockWriteFixtureToItAndReturnKey(); + blockStore->__setKey(FakeAuthenticatedCipher::Key2()); + auto loaded = blockStore->load(key); + EXPECT_EQ(nullptr, loaded.get()); +} + +TEST_F(EncryptedBlockStoreTest, LoadingModifiedBlockFails_WriteOnCreate) { + auto key = CreateBlockDirectlyWithFixtureAndReturnKey(); + ModifyBaseBlock(key); + auto loaded = blockStore->load(key); + EXPECT_EQ(nullptr, loaded.get()); +} + +TEST_F(EncryptedBlockStoreTest, LoadingModifiedBlockFails_WriteSeparately) { + auto key = CreateBlockWriteFixtureToItAndReturnKey(); + ModifyBaseBlock(key); + auto loaded = blockStore->load(key); + EXPECT_EQ(nullptr, loaded.get()); +} + +TEST_F(EncryptedBlockStoreTest, LoadingWithDifferentBlockIdFails_WriteOnCreate) { + auto key = CreateBlockDirectlyWithFixtureAndReturnKey(); + auto key2 = CopyBaseBlock(key); + auto loaded = blockStore->load(key2); + EXPECT_EQ(nullptr, loaded.get()); +} + +TEST_F(EncryptedBlockStoreTest, LoadingWithDifferentBlockIdFails_WriteSeparately) { + auto key = CreateBlockWriteFixtureToItAndReturnKey(); + auto key2 = CopyBaseBlock(key); + auto loaded = blockStore->load(key2); + EXPECT_EQ(nullptr, loaded.get()); }