1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
#include <gtest/gtest.h>
#include "blockstore/implementations/integrity/IntegrityBlockStore2.h"
#include "blockstore/implementations/inmemory/InMemoryBlockStore2.h"
#include "blockstore/utils/BlockStoreUtils.h"
#include <cpp-utils/data/DataFixture.h>
#include <cpp-utils/tempfile/TempFile.h>
#include "../../testutils/gtest_printers.h"
#include <cstddef>
using ::testing::Test;
using cpputils::DataFixture;
using cpputils::Data;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using cpputils::TempFile;
using cpputils::serialize;
using cpputils::deserialize;
using std::unique_ptr;
using blockstore::inmemory::InMemoryBlockStore2;
using namespace blockstore::integrity;
namespace {
class FakeCallback final {
public:
FakeCallback(): wasCalled_(false) {}
bool wasCalled() const {
return wasCalled_;
}
std::function<void ()> callback() {
return [this] () {
wasCalled_ = true;
};
}
private:
bool wasCalled_;
};
}
template<bool AllowIntegrityViolations, bool MissingBlockIsIntegrityViolation>
class IntegrityBlockStoreTest: public Test {
public:
static constexpr unsigned int BLOCKSIZE = 1024;
IntegrityBlockStoreTest():
stateFile(false),
onIntegrityViolation(),
baseBlockStore(new InMemoryBlockStore2),
blockStore(make_unique_ref<IntegrityBlockStore2>(std::move(cpputils::nullcheck(std::unique_ptr<InMemoryBlockStore2>(baseBlockStore)).value()), stateFile.path(), myClientId, AllowIntegrityViolations, MissingBlockIsIntegrityViolation, onIntegrityViolation.callback())),
data(DataFixture::generate(BLOCKSIZE)) {
}
static constexpr uint32_t myClientId = 0x12345678;
TempFile stateFile;
FakeCallback onIntegrityViolation;
InMemoryBlockStore2 *baseBlockStore;
unique_ref<IntegrityBlockStore2> blockStore;
Data data;
blockstore::BlockId CreateBlockReturnKey() {
return CreateBlockReturnKey(data);
}
blockstore::BlockId CreateBlockReturnKey(const Data &initData) {
return blockStore->create(initData.copy());
}
Data loadBaseBlock(const blockstore::BlockId &blockId) {
return baseBlockStore->load(blockId).value();
}
Data loadBlock(const blockstore::BlockId &blockId) {
return blockStore->load(blockId).value();
}
void modifyBlock(const blockstore::BlockId &blockId) {
auto block = blockStore->load(blockId).value();
CryptoPP::byte* first_byte = static_cast<CryptoPP::byte*>(block.data());
*first_byte = *first_byte + 1;
blockStore->store(blockId, block);
}
void rollbackBaseBlock(const blockstore::BlockId &blockId, const Data &data) {
baseBlockStore->store(blockId, data);
}
void decreaseVersionNumber(const blockstore::BlockId &blockId) {
auto baseBlock = baseBlockStore->load(blockId).value();
void* versionPtr = static_cast<uint8_t*>(baseBlock.data()) + IntegrityBlockStore2::VERSION_HEADER_OFFSET;
const uint64_t version = deserialize<uint64_t>(versionPtr);
ASSERT(version > 1, "Can't decrease the lowest allowed version number");
serialize<uint64_t>(versionPtr, version-1);
baseBlockStore->store(blockId, baseBlock);
}
void increaseVersionNumber(const blockstore::BlockId &blockId) {
auto baseBlock = baseBlockStore->load(blockId).value();
void* versionPtr = static_cast<uint8_t*>(baseBlock.data()) + IntegrityBlockStore2::VERSION_HEADER_OFFSET;
const uint64_t version = deserialize<uint64_t>(versionPtr);
serialize<uint64_t>(versionPtr, version+1);
baseBlockStore->store(blockId, baseBlock);
}
void changeClientId(const blockstore::BlockId &blockId) {
auto baseBlock = baseBlockStore->load(blockId).value();
void* clientIdPtr = static_cast<uint8_t*>(baseBlock.data()) + IntegrityBlockStore2::CLIENTID_HEADER_OFFSET;
const uint64_t clientId = deserialize<uint64_t>(clientIdPtr);
serialize<uint64_t>(clientIdPtr, clientId+1);
baseBlockStore->store(blockId, baseBlock);
}
void deleteBlock(const blockstore::BlockId &blockId) {
blockStore->remove(blockId);
}
void insertBaseBlock(const blockstore::BlockId &blockId, Data data) {
EXPECT_TRUE(baseBlockStore->tryCreate(blockId, data));
}
private:
DISALLOW_COPY_AND_ASSIGN(IntegrityBlockStoreTest);
};
using IntegrityBlockStoreTest_Default = IntegrityBlockStoreTest<false, false>;
using IntegrityBlockStoreTest_MissingBlockIsIntegrityViolation = IntegrityBlockStoreTest<false, true>;
using IntegrityBlockStoreTest_AllowIntegrityViolations = IntegrityBlockStoreTest<true, false>;
using IntegrityBlockStoreTest_AllowIntegrityViolations_MissingBlockIsIntegrityViolation = IntegrityBlockStoreTest<true, true>;
template<bool AllowIntegrityViolations, bool MissingBlockIsIntegrityViolation>
constexpr uint32_t IntegrityBlockStoreTest<AllowIntegrityViolations, MissingBlockIsIntegrityViolation>::myClientId;
// Test that a decreasing version number is not allowed
TEST_F(IntegrityBlockStoreTest_Default, RollbackPrevention_DoesntAllowDecreasingVersionNumberForSameClient_1) {
auto blockId = CreateBlockReturnKey();
const Data oldBaseBlock = loadBaseBlock(blockId);
modifyBlock(blockId);
rollbackBaseBlock(blockId, oldBaseBlock);
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_TRUE(onIntegrityViolation.wasCalled());
}
// Test that a decreasing version number is allowed if allowIntegrityViolations is set.
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, RollbackPrevention_AllowsDecreasingVersionNumberForSameClient_1) {
auto blockId = CreateBlockReturnKey();
const Data oldBaseBlock = loadBaseBlock(blockId);
modifyBlock(blockId);
rollbackBaseBlock(blockId, oldBaseBlock);
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_Default, RollbackPrevention_DoesntAllowDecreasingVersionNumberForSameClient_2) {
auto blockId = CreateBlockReturnKey();
// Increase the version number
modifyBlock(blockId);
// Decrease the version number again
decreaseVersionNumber(blockId);
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_TRUE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, RollbackPrevention_AllowsDecreasingVersionNumberForSameClient_2) {
auto blockId = CreateBlockReturnKey();
// Increase the version number
modifyBlock(blockId);
// Decrease the version number again
decreaseVersionNumber(blockId);
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// Test that a different client doesn't need to have a higher version number (i.e. version numbers are per client).
TEST_F(IntegrityBlockStoreTest_Default, RollbackPrevention_DoesAllowDecreasingVersionNumberForDifferentClient) {
auto blockId = CreateBlockReturnKey();
// Increase the version number
modifyBlock(blockId);
// Fake a modification by a different client with lower version numbers
changeClientId(blockId);
decreaseVersionNumber(blockId);
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, RollbackPrevention_DoesAllowDecreasingVersionNumberForDifferentClient) {
auto blockId = CreateBlockReturnKey();
// Increase the version number
modifyBlock(blockId);
// Fake a modification by a different client with lower version numbers
changeClientId(blockId);
decreaseVersionNumber(blockId);
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// Test that it doesn't allow a rollback to the "newest" block of a client, when this block was superseded by a version of a different client
TEST_F(IntegrityBlockStoreTest_Default, RollbackPrevention_DoesntAllowSameVersionNumberForOldClient) {
auto blockId = CreateBlockReturnKey();
// Increase the version number
modifyBlock(blockId);
const Data oldBaseBlock = loadBaseBlock(blockId);
// Fake a modification by a different client with lower version numbers
changeClientId(blockId);
loadBlock(blockId); // make the block store know about this other client's modification
// Rollback to old client
rollbackBaseBlock(blockId, oldBaseBlock);
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_TRUE(onIntegrityViolation.wasCalled());
}
// Test that it does allow a rollback to the "newest" block of a client, when this block was superseded by a version of a different client, but integrity violations are allowed
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, RollbackPrevention_AllowsSameVersionNumberForOldClient) {
auto blockId = CreateBlockReturnKey();
// Increase the version number
modifyBlock(blockId);
const Data oldBaseBlock = loadBaseBlock(blockId);
// Fake a modification by a different client with lower version numbers
changeClientId(blockId);
loadBlock(blockId); // make the block store know about this other client's modification
// Rollback to old client
rollbackBaseBlock(blockId, oldBaseBlock);
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// Test that deleted blocks cannot be re-introduced
TEST_F(IntegrityBlockStoreTest_Default, RollbackPrevention_DoesntAllowReintroducingDeletedBlocks) {
auto blockId = CreateBlockReturnKey();
Data oldBaseBlock = loadBaseBlock(blockId);
deleteBlock(blockId);
insertBaseBlock(blockId, std::move(oldBaseBlock));
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_TRUE(onIntegrityViolation.wasCalled());
}
// Test that deleted blocks can be re-introduced if integrity violations are allowed
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, RollbackPrevention_AllowsReintroducingDeletedBlocks) {
auto blockId = CreateBlockReturnKey();
Data oldBaseBlock = loadBaseBlock(blockId);
deleteBlock(blockId);
insertBaseBlock(blockId, std::move(oldBaseBlock));
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// This can happen if a client synchronization is delayed. Another client might have won the conflict and pushed a new version for the deleted block.
TEST_F(IntegrityBlockStoreTest_Default, RollbackPrevention_AllowsReintroducingDeletedBlocksWithNewVersionNumber) {
auto blockId = CreateBlockReturnKey();
Data oldBaseBlock = loadBaseBlock(blockId);
deleteBlock(blockId);
insertBaseBlock(blockId, std::move(oldBaseBlock));
increaseVersionNumber(blockId);
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, RollbackPrevention_AllowsReintroducingDeletedBlocksWithNewVersionNumber) {
auto blockId = CreateBlockReturnKey();
Data oldBaseBlock = loadBaseBlock(blockId);
deleteBlock(blockId);
insertBaseBlock(blockId, std::move(oldBaseBlock));
increaseVersionNumber(blockId);
EXPECT_NE(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// Check that in a multi-client scenario, missing blocks are not integrity errors, because another client might have deleted them.
TEST_F(IntegrityBlockStoreTest_Default, DeletionPrevention_AllowsDeletingBlocksWhenDeactivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, DeletionPrevention_AllowsDeletingBlocksWhenDeactivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// Check that in a single-client scenario, missing blocks are integrity errors.
TEST_F(IntegrityBlockStoreTest_MissingBlockIsIntegrityViolation, DeletionPrevention_DoesntAllowDeletingBlocksWhenActivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_TRUE(onIntegrityViolation.wasCalled());
}
// Check that in a single-client scenario, missing blocks don't throw if integrity violations are allowed.
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations_MissingBlockIsIntegrityViolation, DeletionPrevention_AllowsDeletingBlocksWhenActivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
EXPECT_EQ(boost::none, blockStore->load(blockId));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// Check that in a multi-client scenario, missing blocks are not integrity errors, because another client might have deleted them.
TEST_F(IntegrityBlockStoreTest_Default, DeletionPrevention_InForEachBlock_AllowsDeletingBlocksWhenDeactivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
int count = 0;
blockStore->forEachBlock([&count] (const blockstore::BlockId &) {
++count;
});
EXPECT_EQ(0, count);
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, DeletionPrevention_InForEachBlock_AllowsDeletingBlocksWhenDeactivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
int count = 0;
blockStore->forEachBlock([&count] (const blockstore::BlockId &) {
++count;
});
EXPECT_EQ(0, count);
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// Check that in a single-client scenario, missing blocks are integrity errors.
TEST_F(IntegrityBlockStoreTest_MissingBlockIsIntegrityViolation, DeletionPrevention_InForEachBlock_DoesntAllowDeletingBlocksWhenActivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
blockStore->forEachBlock([] (const blockstore::BlockId &) {});
EXPECT_TRUE(onIntegrityViolation.wasCalled());
}
// Check that in a single-client scenario, missing blocks don't throw if integrity violations are allowed.
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations_MissingBlockIsIntegrityViolation, DeletionPrevention_InForEachBlock_AllowsDeletingBlocksWhenActivated) {
auto blockId = blockStore->create(Data(0));
baseBlockStore->remove(blockId);
blockStore->forEachBlock([] (const blockstore::BlockId &) {});
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_Default, LoadingWithDifferentBlockIdFails) {
auto blockId = CreateBlockReturnKey();
const blockstore::BlockId key2 = blockstore::BlockId::FromString("1491BB4932A389EE14BC7090AC772972");
baseBlockStore->store(key2, baseBlockStore->load(blockId).value());
EXPECT_EQ(boost::none, blockStore->load(key2));
EXPECT_TRUE(onIntegrityViolation.wasCalled());
}
TEST_F(IntegrityBlockStoreTest_AllowIntegrityViolations, LoadingWithDifferentBlockIdDoesntFail) {
auto blockId = CreateBlockReturnKey();
const blockstore::BlockId key2 = blockstore::BlockId::FromString("1491BB4932A389EE14BC7090AC772972");
baseBlockStore->store(key2, baseBlockStore->load(blockId).value());
EXPECT_NE(boost::none, blockStore->load(key2));
EXPECT_FALSE(onIntegrityViolation.wasCalled());
}
// TODO Test more integrity cases:
// - RollbackPrevention_DoesntAllowReintroducingDeletedBlocks with different client id (i.e. trying to re-introduce the newest block of a different client)
// - RollbackPrevention_AllowsReintroducingDeletedBlocksWithNewVersionNumber with different client id
// - Think about more...
// TODO Test that disabling integrity checks allows all these cases
TEST_F(IntegrityBlockStoreTest_Default, PhysicalBlockSize_zerophysical) {
EXPECT_EQ(0u, blockStore->blockSizeFromPhysicalBlockSize(0));
}
TEST_F(IntegrityBlockStoreTest_Default, PhysicalBlockSize_zerovirtual) {
auto blockId = CreateBlockReturnKey(Data(0));
auto base = baseBlockStore->load(blockId).value();
EXPECT_EQ(0u, blockStore->blockSizeFromPhysicalBlockSize(base.size()));
}
TEST_F(IntegrityBlockStoreTest_Default, PhysicalBlockSize_negativeboundaries) {
// This tests that a potential if/else in blockSizeFromPhysicalBlockSize that catches negative values has the
// correct boundary set. We test the highest value that is negative and the smallest value that is positive.
auto physicalSizeForVirtualSizeZero = baseBlockStore->load(CreateBlockReturnKey(Data(0))).value().size();
if (physicalSizeForVirtualSizeZero > 0) {
EXPECT_EQ(0u, blockStore->blockSizeFromPhysicalBlockSize(physicalSizeForVirtualSizeZero - 1));
}
EXPECT_EQ(0u, blockStore->blockSizeFromPhysicalBlockSize(physicalSizeForVirtualSizeZero));
EXPECT_EQ(1u, blockStore->blockSizeFromPhysicalBlockSize(physicalSizeForVirtualSizeZero + 1));
}
TEST_F(IntegrityBlockStoreTest_Default, PhysicalBlockSize_positive) {
auto blockId = CreateBlockReturnKey(Data(10*1024));
auto base = baseBlockStore->load(blockId).value();
EXPECT_EQ(10*1024u, blockStore->blockSizeFromPhysicalBlockSize(base.size()));
}
|