2016-06-22 00:07:06 +02:00
# include <gtest/gtest.h>
2017-09-16 01:18:58 +02:00
# include <blockstore/implementations/integrity/KnownBlockVersions.h>
2016-06-22 00:56:29 +02:00
# include <cpp-utils/tempfile/TempFile.h>
2016-06-22 00:07:06 +02:00
2017-09-16 01:18:58 +02:00
using blockstore : : integrity : : KnownBlockVersions ;
2017-09-17 03:07:27 +02:00
using blockstore : : BlockId ;
2016-06-22 00:56:29 +02:00
using cpputils : : TempFile ;
2016-06-26 01:36:41 +02:00
using std : : unordered_set ;
2016-06-22 00:07:06 +02:00
class KnownBlockVersionsTest : public : : testing : : Test {
public :
2016-06-26 23:35:52 +02:00
KnownBlockVersionsTest ( ) : stateFile ( false ) , testobj ( stateFile . path ( ) , myClientId ) { }
2016-06-22 00:56:29 +02:00
2017-09-17 03:07:27 +02:00
blockstore : : BlockId blockId = blockstore : : BlockId : : FromString ( " 1491BB4932A389EE14BC7090AC772972 " ) ;
blockstore : : BlockId blockId2 = blockstore : : BlockId : : FromString ( " C772972491BB4932A1389EE14BC7090A " ) ;
2016-06-26 23:35:52 +02:00
static constexpr uint32_t myClientId = 0x12345678 ;
static constexpr uint32_t clientId = 0x23456789 ;
static constexpr uint32_t clientId2 = 0x34567890 ;
2016-06-22 00:07:06 +02:00
2016-06-22 00:56:29 +02:00
TempFile stateFile ;
2016-06-22 00:07:06 +02:00
KnownBlockVersions testobj ;
2016-06-22 20:10:02 +02:00
2017-09-17 03:07:27 +02:00
void setVersion ( KnownBlockVersions * testobj , uint32_t clientId , const blockstore : : BlockId & blockId , uint64_t version ) {
if ( ! testobj - > checkAndUpdateVersion ( clientId , blockId , version ) ) {
2016-06-24 00:33:47 +02:00
throw std : : runtime_error ( " Couldn't increase version " ) ;
}
}
2017-09-17 03:07:27 +02:00
void EXPECT_VERSION_IS ( uint64_t version , KnownBlockVersions * testobj , blockstore : : BlockId & blockId , uint32_t clientId ) {
EXPECT_FALSE ( testobj - > checkAndUpdateVersion ( clientId , blockId , version - 1 ) ) ;
EXPECT_TRUE ( testobj - > checkAndUpdateVersion ( clientId , blockId , version + 1 ) ) ;
2016-06-22 20:10:02 +02:00
}
2016-06-22 00:07:06 +02:00
} ;
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , setandget ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-22 00:07:06 +02:00
}
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , setandget_isPerClientId ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId2 , blockId , 3 ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 3u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-22 00:07:06 +02:00
}
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , setandget_isPerBlock ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId , blockId2 , 3 ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 3u , testobj . getBlockVersion ( clientId , blockId2 ) ) ;
2016-06-22 00:07:06 +02:00
}
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , setandget_allowsIncreasing ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId , blockId , 6 ) ;
EXPECT_EQ ( 6u , testobj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-22 00:07:06 +02:00
}
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , setandget_doesntAllowDecreasing ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
2016-06-22 00:07:06 +02:00
EXPECT_ANY_THROW (
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 4 ) ;
2016-06-22 00:07:06 +02:00
) ;
}
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , myClientId_isConsistent ) {
EXPECT_EQ ( testobj . myClientId ( ) , testobj . myClientId ( ) ) ;
2016-06-22 20:10:02 +02:00
}
2016-07-16 11:42:06 +02:00
TEST_F ( KnownBlockVersionsTest , incrementVersion_newentry ) {
2017-09-17 03:07:27 +02:00
auto version = testobj . incrementVersion ( blockId ) ;
2016-06-23 23:47:27 +02:00
EXPECT_EQ ( 1u , version ) ;
2017-09-17 03:07:27 +02:00
EXPECT_EQ ( 1u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
2016-06-22 00:07:06 +02:00
}
2016-07-16 11:42:06 +02:00
TEST_F ( KnownBlockVersionsTest , incrementVersion_oldentry ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , testobj . myClientId ( ) , blockId , 5 ) ;
auto version = testobj . incrementVersion ( blockId ) ;
2016-06-23 23:47:27 +02:00
EXPECT_EQ ( 6u , version ) ;
2017-09-17 03:07:27 +02:00
EXPECT_EQ ( 6u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
2016-06-22 00:07:06 +02:00
}
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_newentry ) {
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 5 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_sameClientSameVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 5 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_sameClientLowerVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 4 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_sameClientNewerVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 6 ) ) ;
EXPECT_EQ ( 6u , testobj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_differentClientSameVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 5 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_differentClientLowerVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 3 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 3u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_differentClientHigherVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 7 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientLowerVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 7 ) ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 3 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientSameVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 7 ) ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 5 ) ) ; // Don't allow rollback to old client's newest block, if it was superseded by another client
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientHigherVersion ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 7 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 6 ) ) ;
EXPECT_EQ ( 6u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientLowerVersion_oldClientIsSelf ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , testobj . myClientId ( ) , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 7 ) ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( testobj . myClientId ( ) , blockId , 3 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientSameVersion_oldClientIsSelf ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , testobj . myClientId ( ) , blockId , 5 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 7 ) ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( testobj . myClientId ( ) , blockId , 5 ) ) ; // Don't allow rollback to old client's newest block, if it was superseded by another client
EXPECT_EQ ( 5u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientHigherVersion_oldClientIsSelf ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , testobj . myClientId ( ) , blockId , 4 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 7 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( testobj . myClientId ( ) , blockId , 6 ) ) ;
EXPECT_EQ ( 6u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( clientId2 , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientLowerVersion_newClientIsSelf ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , testobj . myClientId ( ) , blockId , 7 ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 3 ) ) ;
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientSameVersion_newClientIsSelf ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , testobj . myClientId ( ) , blockId , 7 ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 5 ) ) ; // Don't allow rollback to old client's newest block, if it was superseded by another client
EXPECT_EQ ( 5u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
2016-06-23 23:47:27 +02:00
}
2016-06-22 00:07:06 +02:00
2016-06-23 23:47:27 +02:00
TEST_F ( KnownBlockVersionsTest , checkAndUpdateVersion_oldentry_oldClientHigherVersion_newClientIsSelf ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , testobj . myClientId ( ) , blockId , 7 ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 6 ) ) ;
EXPECT_EQ ( 6u , testobj . getBlockVersion ( clientId , blockId ) ) ;
EXPECT_EQ ( 7u , testobj . getBlockVersion ( testobj . myClientId ( ) , blockId ) ) ;
2016-06-22 00:07:06 +02:00
}
2016-06-22 00:56:29 +02:00
2016-06-22 20:10:02 +02:00
TEST_F ( KnownBlockVersionsTest , checkAndUpdate_twoEntriesDontInfluenceEachOther_differentKeys ) {
// Setup
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId2 , 100 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 150 ) ) ;
2016-06-22 20:10:02 +02:00
// Checks
2017-09-17 03:07:27 +02:00
EXPECT_VERSION_IS ( 150 , & testobj , blockId , clientId ) ;
EXPECT_VERSION_IS ( 100 , & testobj , blockId2 , clientId ) ;
2016-06-22 20:10:02 +02:00
}
2016-06-22 00:56:29 +02:00
2016-06-22 20:10:02 +02:00
TEST_F ( KnownBlockVersionsTest , checkAndUpdate_twoEntriesDontInfluenceEachOther_differentClientIds ) {
// Setup
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 100 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 150 ) ) ;
2016-06-22 00:56:29 +02:00
2017-09-17 03:07:27 +02:00
EXPECT_VERSION_IS ( 150 , & testobj , blockId , clientId ) ;
EXPECT_VERSION_IS ( 100 , & testobj , blockId , clientId2 ) ;
2016-06-22 00:56:29 +02:00
}
2016-06-23 01:27:35 +02:00
TEST_F ( KnownBlockVersionsTest , checkAndUpdate_allowsRollbackToSameClientWithSameVersionNumber ) {
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
2016-06-23 01:27:35 +02:00
}
TEST_F ( KnownBlockVersionsTest , checkAndUpdate_doesntAllowRollbackToOldClientWithSameVersionNumber ) {
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
EXPECT_TRUE ( testobj . checkAndUpdateVersion ( clientId2 , blockId , 10 ) ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
2016-06-23 01:27:35 +02:00
}
2016-06-22 00:56:29 +02:00
TEST_F ( KnownBlockVersionsTest , saveAndLoad_empty ) {
TempFile stateFile ( false ) ;
2018-10-14 22:26:30 +02:00
{
KnownBlockVersions _1 ( stateFile . path ( ) , myClientId ) ;
}
2016-06-22 00:56:29 +02:00
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( KnownBlockVersions ( stateFile . path ( ) , myClientId ) . checkAndUpdateVersion ( clientId , blockId , 1 ) ) ;
2016-06-22 00:56:29 +02:00
}
TEST_F ( KnownBlockVersionsTest , saveAndLoad_oneentry ) {
TempFile stateFile ( false ) ;
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( KnownBlockVersions ( stateFile . path ( ) , myClientId ) . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
2016-06-22 00:56:29 +02:00
2016-06-26 23:35:52 +02:00
KnownBlockVersions obj ( stateFile . path ( ) , myClientId ) ;
2017-09-17 03:07:27 +02:00
EXPECT_EQ ( 100u , obj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-22 00:56:29 +02:00
}
2016-06-22 20:10:02 +02:00
TEST_F ( KnownBlockVersionsTest , saveAndLoad_threeentries ) {
2016-06-22 00:56:29 +02:00
TempFile stateFile ( false ) ;
{
2016-06-26 23:35:52 +02:00
KnownBlockVersions obj ( stateFile . path ( ) , myClientId ) ;
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( obj . checkAndUpdateVersion ( obj . myClientId ( ) , blockId , 100 ) ) ;
EXPECT_TRUE ( obj . checkAndUpdateVersion ( obj . myClientId ( ) , blockId2 , 50 ) ) ;
EXPECT_TRUE ( obj . checkAndUpdateVersion ( clientId , blockId , 150 ) ) ;
2016-06-22 00:56:29 +02:00
}
2016-06-26 23:35:52 +02:00
KnownBlockVersions obj ( stateFile . path ( ) , myClientId ) ;
2017-09-17 03:07:27 +02:00
EXPECT_EQ ( 100u , obj . getBlockVersion ( obj . myClientId ( ) , blockId ) ) ;
EXPECT_EQ ( 50u , obj . getBlockVersion ( obj . myClientId ( ) , blockId2 ) ) ;
EXPECT_EQ ( 150u , obj . getBlockVersion ( clientId , blockId ) ) ;
2016-06-22 00:56:29 +02:00
}
2016-06-23 01:27:35 +02:00
TEST_F ( KnownBlockVersionsTest , saveAndLoad_lastUpdateClientIdIsStored ) {
{
2016-06-26 23:35:52 +02:00
KnownBlockVersions obj ( stateFile . path ( ) , myClientId ) ;
2017-09-17 03:07:27 +02:00
EXPECT_TRUE ( obj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
EXPECT_TRUE ( obj . checkAndUpdateVersion ( clientId2 , blockId , 10 ) ) ;
2016-06-23 01:27:35 +02:00
}
2016-06-26 23:35:52 +02:00
KnownBlockVersions obj ( stateFile . path ( ) , myClientId ) ;
2017-09-17 03:07:27 +02:00
EXPECT_FALSE ( obj . checkAndUpdateVersion ( clientId , blockId , 100 ) ) ;
EXPECT_TRUE ( obj . checkAndUpdateVersion ( clientId2 , blockId , 10 ) ) ;
EXPECT_TRUE ( obj . checkAndUpdateVersion ( clientId , blockId , 101 ) ) ;
2016-06-23 01:27:35 +02:00
}
2016-06-26 00:41:22 +02:00
TEST_F ( KnownBlockVersionsTest , markAsDeleted_doesntAllowReIntroducing_sameClientId ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
testobj . markBlockAsDeleted ( blockId ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 5 ) ) ;
2016-06-26 00:41:22 +02:00
}
TEST_F ( KnownBlockVersionsTest , markAsDeleted_doesntAllowReIntroducing_oldClientId ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId2 , blockId , 5 ) ;
testobj . markBlockAsDeleted ( blockId ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 5 ) ) ;
2016-06-26 00:41:22 +02:00
}
TEST_F ( KnownBlockVersionsTest , markAsDeleted_checkAndUpdateDoesntDestroyState ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId2 , blockId , 5 ) ;
testobj . markBlockAsDeleted ( blockId ) ;
EXPECT_FALSE ( testobj . checkAndUpdateVersion ( clientId , blockId , 5 ) ) ;
2016-06-26 00:41:22 +02:00
// Check block is still deleted
2017-09-17 03:07:27 +02:00
EXPECT_FALSE ( testobj . blockShouldExist ( blockId ) ) ;
2016-06-26 00:41:22 +02:00
}
TEST_F ( KnownBlockVersionsTest , blockShouldExist_unknownBlock ) {
2017-09-17 03:07:27 +02:00
EXPECT_FALSE ( testobj . blockShouldExist ( blockId ) ) ;
2016-06-26 00:41:22 +02:00
}
TEST_F ( KnownBlockVersionsTest , blockShouldExist_knownBlock ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_TRUE ( testobj . blockShouldExist ( blockId ) ) ;
2016-06-26 00:41:22 +02:00
}
TEST_F ( KnownBlockVersionsTest , blockShouldExist_deletedBlock ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
testobj . markBlockAsDeleted ( blockId ) ;
EXPECT_FALSE ( testobj . blockShouldExist ( blockId ) ) ;
2016-06-26 00:41:22 +02:00
}
2016-06-26 01:16:26 +02:00
TEST_F ( KnownBlockVersionsTest , path ) {
2016-06-26 23:35:52 +02:00
KnownBlockVersions obj ( stateFile . path ( ) , myClientId ) ;
2016-06-26 01:16:26 +02:00
EXPECT_EQ ( stateFile . path ( ) , obj . path ( ) ) ;
2016-06-26 01:36:41 +02:00
}
TEST_F ( KnownBlockVersionsTest , existingBlocks_empty ) {
2017-09-17 03:07:27 +02:00
EXPECT_EQ ( unordered_set < BlockId > ( { } ) , testobj . existingBlocks ( ) ) ;
2016-06-26 01:36:41 +02:00
}
TEST_F ( KnownBlockVersionsTest , existingBlocks_oneentry ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
EXPECT_EQ ( unordered_set < BlockId > ( { blockId } ) , testobj . existingBlocks ( ) ) ;
2016-06-26 01:36:41 +02:00
}
TEST_F ( KnownBlockVersionsTest , existingBlocks_twoentries ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId2 , blockId2 , 5 ) ;
EXPECT_EQ ( unordered_set < BlockId > ( { blockId , blockId2 } ) , testobj . existingBlocks ( ) ) ;
2016-06-26 01:36:41 +02:00
}
TEST_F ( KnownBlockVersionsTest , existingBlocks_twoentries_sameKey ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId2 , blockId , 5 ) ;
EXPECT_EQ ( unordered_set < BlockId > ( { blockId } ) , testobj . existingBlocks ( ) ) ;
2016-06-26 01:36:41 +02:00
}
TEST_F ( KnownBlockVersionsTest , existingBlocks_deletedEntry ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId2 , blockId2 , 5 ) ;
testobj . markBlockAsDeleted ( blockId2 ) ;
EXPECT_EQ ( unordered_set < BlockId > ( { blockId } ) , testobj . existingBlocks ( ) ) ;
2016-06-26 01:36:41 +02:00
}
TEST_F ( KnownBlockVersionsTest , existingBlocks_deletedEntries ) {
2017-09-17 03:07:27 +02:00
setVersion ( & testobj , clientId , blockId , 5 ) ;
setVersion ( & testobj , clientId2 , blockId2 , 5 ) ;
testobj . markBlockAsDeleted ( blockId ) ;
testobj . markBlockAsDeleted ( blockId2 ) ;
EXPECT_EQ ( unordered_set < BlockId > ( { } ) , testobj . existingBlocks ( ) ) ;
2016-06-26 01:36:41 +02:00
}