Fix EncryptedBlock::resize() and add test cases for it

This commit is contained in:
Sebastian Messmer 2016-06-23 21:20:36 -07:00
parent fcbca9ddea
commit acedbbd0c4
2 changed files with 68 additions and 1 deletions

View File

@ -187,6 +187,9 @@ template<class Cipher>
void EncryptedBlock<Cipher>::_encryptToBaseBlock() { void EncryptedBlock<Cipher>::_encryptToBaseBlock() {
if (_dataChanged) { if (_dataChanged) {
cpputils::Data encrypted = Cipher::encrypt((byte*)_plaintextWithHeader.data(), _plaintextWithHeader.size(), _encKey); cpputils::Data encrypted = Cipher::encrypt((byte*)_plaintextWithHeader.data(), _plaintextWithHeader.size(), _encKey);
if (_baseBlock->size() != sizeof(FORMAT_VERSION_HEADER) + encrypted.size()) {
_baseBlock->resize(sizeof(FORMAT_VERSION_HEADER) + encrypted.size());
}
_baseBlock->write(&FORMAT_VERSION_HEADER, 0, sizeof(FORMAT_VERSION_HEADER)); _baseBlock->write(&FORMAT_VERSION_HEADER, 0, sizeof(FORMAT_VERSION_HEADER));
_baseBlock->write(encrypted.data(), sizeof(FORMAT_VERSION_HEADER), encrypted.size()); _baseBlock->write(encrypted.data(), sizeof(FORMAT_VERSION_HEADER), encrypted.size());
_dataChanged = false; _dataChanged = false;

View File

@ -111,6 +111,62 @@ TYPED_TEST_P(BlockStoreTest, CanRemoveModifiedBlock) {
EXPECT_EQ(0u, blockStore->numBlocks()); EXPECT_EQ(0u, blockStore->numBlocks());
} }
TYPED_TEST_P(BlockStoreTest, Resize_Larger_FromZero) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(0));
block->resize(10);
EXPECT_EQ(10u, block->size());
}
TYPED_TEST_P(BlockStoreTest, Resize_Larger_FromZero_BlockIsStillUsable) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(0));
block->resize(10);
this->TestBlockIsUsable(std::move(block), blockStore.get());
}
TYPED_TEST_P(BlockStoreTest, Resize_Larger) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(10));
block->resize(20);
EXPECT_EQ(20u, block->size());
}
TYPED_TEST_P(BlockStoreTest, Resize_Larger_BlockIsStillUsable) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(10));
block->resize(20);
this->TestBlockIsUsable(std::move(block), blockStore.get());
}
TYPED_TEST_P(BlockStoreTest, Resize_Smaller) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(10));
block->resize(5);
EXPECT_EQ(5u, block->size());
}
TYPED_TEST_P(BlockStoreTest, Resize_Smaller_BlockIsStillUsable) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(10));
block->resize(5);
this->TestBlockIsUsable(std::move(block), blockStore.get());
}
TYPED_TEST_P(BlockStoreTest, Resize_Smaller_ToZero) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(10));
block->resize(0);
EXPECT_EQ(0u, block->size());
}
TYPED_TEST_P(BlockStoreTest, Resize_Smaller_ToZero_BlockIsStillUsable) {
auto blockStore = this->fixture.createBlockStore();
auto block = blockStore->create(cpputils::Data(10));
block->resize(0);
this->TestBlockIsUsable(std::move(block), blockStore.get());
}
#include "BlockStoreTest_Size.h" #include "BlockStoreTest_Size.h"
#include "BlockStoreTest_Data.h" #include "BlockStoreTest_Data.h"
@ -141,7 +197,15 @@ REGISTER_TYPED_TEST_CASE_P(BlockStoreTest,
WriteAndReadImmediately, WriteAndReadImmediately,
WriteAndReadAfterLoading, WriteAndReadAfterLoading,
OverwriteAndRead, OverwriteAndRead,
CanRemoveModifiedBlock CanRemoveModifiedBlock,
Resize_Larger_FromZero,
Resize_Larger_FromZero_BlockIsStillUsable,
Resize_Larger,
Resize_Larger_BlockIsStillUsable,
Resize_Smaller,
Resize_Smaller_BlockIsStillUsable,
Resize_Smaller_ToZero,
Resize_Smaller_ToZero_BlockIsStillUsable
); );