2016-06-22 00:07:06 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <blockstore/implementations/versioncounting/KnownBlockVersions.h>
|
2016-06-22 00:56:29 +02:00
|
|
|
#include <cpp-utils/tempfile/TempFile.h>
|
2016-06-22 00:07:06 +02:00
|
|
|
|
|
|
|
using blockstore::versioncounting::KnownBlockVersions;
|
2016-06-22 00:56:29 +02:00
|
|
|
using cpputils::TempFile;
|
2016-06-22 00:07:06 +02:00
|
|
|
|
|
|
|
class KnownBlockVersionsTest : public ::testing::Test {
|
|
|
|
public:
|
2016-06-22 00:56:29 +02:00
|
|
|
KnownBlockVersionsTest() :stateFile(false), testobj(stateFile.path()) {}
|
|
|
|
|
2016-06-22 00:07:06 +02:00
|
|
|
blockstore::Key key = blockstore::Key::FromString("1491BB4932A389EE14BC7090AC772972");
|
2016-06-22 00:56:29 +02:00
|
|
|
blockstore::Key key2 = blockstore::Key::FromString("C772972491BB4932A1389EE14BC7090A");
|
2016-06-22 20:10:02 +02:00
|
|
|
uint32_t clientId = 0x12345678;
|
|
|
|
uint32_t clientId2 = 0x23456789;
|
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
|
|
|
|
|
|
|
void EXPECT_VERSION_IS(uint64_t version, KnownBlockVersions *testobj, blockstore::Key &key, uint32_t clientId) {
|
|
|
|
EXPECT_FALSE(testobj->checkAndUpdateVersion(clientId, key, version-1));
|
2016-06-23 01:27:35 +02:00
|
|
|
EXPECT_TRUE(testobj->checkAndUpdateVersion(clientId, key, version+1));
|
2016-06-22 20:10:02 +02:00
|
|
|
}
|
2016-06-22 00:07:06 +02:00
|
|
|
};
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, update_newEntry_lowversion) {
|
|
|
|
testobj.updateVersion(key, 1);
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, update_newEntry_highversion) {
|
2016-06-22 00:07:06 +02:00
|
|
|
testobj.updateVersion(key, 100);
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, update_existingEntry_equal_lowversion) {
|
|
|
|
testobj.updateVersion(key, 1);
|
|
|
|
testobj.updateVersion(key, 1);
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, update_existingEntry_equal_highversion) {
|
2016-06-22 00:07:06 +02:00
|
|
|
testobj.updateVersion(key, 100);
|
|
|
|
testobj.updateVersion(key, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, update_existingEntry_nonequal) {
|
|
|
|
testobj.updateVersion(key, 100);
|
|
|
|
testobj.updateVersion(key, 101);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, update_existingEntry_invalid) {
|
|
|
|
testobj.updateVersion(key, 100);
|
|
|
|
EXPECT_ANY_THROW(
|
|
|
|
testobj.updateVersion(key, 99);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-22 20:10:02 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, update_updatesOwnClientId) {
|
|
|
|
testobj.updateVersion(key, 100);
|
|
|
|
EXPECT_VERSION_IS(100, &testobj, key, testobj.myClientId());
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_newEntry_lowversion) {
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 1));
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_newEntry_highversion) {
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_existingEntry_equal_lowversion) {
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 1));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 1));
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_existingEntry_equal_highversion) {
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_existingEntry_nonequal) {
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 101));
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_existingEntry_invalid) {
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_FALSE(testobj.checkAndUpdateVersion(clientId, key, 99));
|
2016-06-22 00:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_existingEntry_invalidDoesntModifyEntry) {
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_FALSE(testobj.checkAndUpdateVersion(clientId, key, 99));
|
2016-06-22 00:07:06 +02:00
|
|
|
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_VERSION_IS(100, &testobj, key, clientId);
|
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
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key2, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 150));
|
|
|
|
|
|
|
|
// Checks
|
|
|
|
EXPECT_VERSION_IS(150, &testobj, key, clientId);
|
|
|
|
EXPECT_VERSION_IS(100, &testobj, key2, clientId);
|
|
|
|
}
|
2016-06-22 00:56:29 +02:00
|
|
|
|
2016-06-22 20:10:02 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_twoEntriesDontInfluenceEachOther_differentClientIds) {
|
|
|
|
// Setup
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId2, key, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 150));
|
2016-06-22 00:56:29 +02:00
|
|
|
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_VERSION_IS(150, &testobj, key, clientId);
|
|
|
|
EXPECT_VERSION_IS(100, &testobj, key, clientId2);
|
2016-06-22 00:56:29 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_allowsRollbackToSameClientWithSameVersionNumber) {
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, checkAndUpdate_doesntAllowRollbackToOldClientWithSameVersionNumber) {
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(testobj.checkAndUpdateVersion(clientId2, key, 10));
|
|
|
|
EXPECT_FALSE(testobj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
}
|
|
|
|
|
2016-06-22 00:56:29 +02:00
|
|
|
TEST_F(KnownBlockVersionsTest, saveAndLoad_empty) {
|
|
|
|
TempFile stateFile(false);
|
|
|
|
KnownBlockVersions(stateFile.path());
|
|
|
|
|
2016-06-23 01:27:35 +02:00
|
|
|
EXPECT_TRUE(KnownBlockVersions(stateFile.path()).checkAndUpdateVersion(clientId, key, 1));
|
2016-06-22 00:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, saveAndLoad_oneentry) {
|
|
|
|
TempFile stateFile(false);
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_TRUE(KnownBlockVersions(stateFile.path()).checkAndUpdateVersion(clientId, key, 100));
|
2016-06-22 00:56:29 +02:00
|
|
|
|
2016-06-22 20:10:02 +02:00
|
|
|
KnownBlockVersions obj(stateFile.path());
|
|
|
|
EXPECT_VERSION_IS(100, &obj, key, clientId);
|
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);
|
|
|
|
{
|
|
|
|
KnownBlockVersions obj(stateFile.path());
|
|
|
|
obj.updateVersion(key, 100);
|
|
|
|
obj.updateVersion(key2, 50);
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_TRUE(obj.checkAndUpdateVersion(clientId, key, 150));
|
2016-06-22 00:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KnownBlockVersions obj(stateFile.path());
|
2016-06-22 20:10:02 +02:00
|
|
|
EXPECT_VERSION_IS(100, &obj, key, obj.myClientId());
|
|
|
|
EXPECT_VERSION_IS(50, &obj, key2, obj.myClientId());
|
|
|
|
EXPECT_VERSION_IS(150, &obj, key, clientId);
|
2016-06-22 00:56:29 +02:00
|
|
|
}
|
2016-06-23 01:27:35 +02:00
|
|
|
|
|
|
|
TEST_F(KnownBlockVersionsTest, saveAndLoad_lastUpdateClientIdIsStored) {
|
|
|
|
{
|
|
|
|
KnownBlockVersions obj(stateFile.path());
|
|
|
|
EXPECT_TRUE(obj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(obj.checkAndUpdateVersion(clientId2, key, 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
KnownBlockVersions obj(stateFile.path());
|
|
|
|
EXPECT_FALSE(obj.checkAndUpdateVersion(clientId, key, 100));
|
|
|
|
EXPECT_TRUE(obj.checkAndUpdateVersion(clientId2, key, 10));
|
|
|
|
EXPECT_TRUE(obj.checkAndUpdateVersion(clientId, key, 101));
|
|
|
|
}
|