Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan White <droidmonkey@users.noreply.github.com>2016-11-08 06:37:42 +0300
committerGitHub <noreply@github.com>2016-11-08 06:37:42 +0300
commite25cd9ba48859322527aee78c4bcd60325487b76 (patch)
tree4dd44239231ff9a383e9a47dec4d37d764d5fee7 /tests/TestGroup.cpp
parente00c6f9c77519acc5821309cbfa68bfee9a9efcb (diff)
Add Merge database utility function (#47)
Thank you to @TheZ3ro and @monomon for there major contributions to this PR!
Diffstat (limited to 'tests/TestGroup.cpp')
-rw-r--r--tests/TestGroup.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp
index e271abfc0..e87e6cedc 100644
--- a/tests/TestGroup.cpp
+++ b/tests/TestGroup.cpp
@@ -19,6 +19,7 @@
#include <QPointer>
#include <QSignalSpy>
+#include <QDebug>
#include <QTest>
#include "core/Database.h"
@@ -449,3 +450,120 @@ void TestGroup::testCopyCustomIcons()
delete dbTarget;
delete dbSource;
}
+
+void TestGroup::testMerge()
+{
+ Group* group1 = new Group();
+ group1->setName("group 1");
+ Group* group2 = new Group();
+ group2->setName("group 2");
+
+ Entry* entry1 = new Entry();
+ Entry* entry2 = new Entry();
+
+ entry1->setGroup(group1);
+ entry1->setUuid(Uuid::random());
+ entry2->setGroup(group1);
+ entry2->setUuid(Uuid::random());
+
+ group2->merge(group1);
+
+ QCOMPARE(group1->entries().size(), 2);
+ QCOMPARE(group2->entries().size(), 2);
+}
+
+void TestGroup::testMergeDatabase()
+{
+ Database* dbSource = createMergeTestDatabase();
+ Database* dbDest = new Database();
+
+ dbDest->merge(dbSource);
+
+ QCOMPARE(dbDest->rootGroup()->children().size(), 2);
+ QCOMPARE(dbDest->rootGroup()->children().at(0)->entries().size(), 2);
+
+ delete dbDest;
+ delete dbSource;
+}
+
+void TestGroup::testMergeConflict()
+{
+ Database* dbSource = createMergeTestDatabase();
+
+ // test merging updated entries
+ // falls back to KeepBoth mode
+ Database* dbCopy = new Database();
+ dbCopy->setRootGroup(dbSource->rootGroup()->clone(Entry::CloneNoFlags));
+
+ // sanity check
+ QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 2);
+
+ // make this entry newer than in original db
+ Entry* updatedEntry = dbCopy->rootGroup()->children().at(0)->entries().at(0);
+ TimeInfo updatedTimeInfo = updatedEntry->timeInfo();
+ updatedTimeInfo.setLastModificationTime(updatedTimeInfo.lastModificationTime().addYears(1));
+ updatedEntry->setTimeInfo(updatedTimeInfo);
+
+ dbCopy->merge(dbSource);
+
+ // one entry is duplicated because of mode
+ QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 2);
+
+ delete dbSource;
+ delete dbCopy;
+}
+
+void TestGroup::testMergeConflictKeepBoth()
+{
+ Database* dbSource = createMergeTestDatabase();
+
+ // test merging updated entries
+ // falls back to KeepBoth mode
+ Database* dbCopy = new Database();
+ dbCopy->setRootGroup(dbSource->rootGroup()->clone(Entry::CloneNoFlags));
+
+ // sanity check
+ QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 2);
+
+ // make this entry newer than in original db
+ Entry* updatedEntry = dbCopy->rootGroup()->children().at(0)->entries().at(0);
+ TimeInfo updatedTimeInfo = updatedEntry->timeInfo();
+ updatedTimeInfo.setLastModificationTime(updatedTimeInfo.lastModificationTime().addYears(1));
+ updatedEntry->setTimeInfo(updatedTimeInfo);
+
+ dbCopy->rootGroup()->setMergeMode(Group::MergeMode::KeepBoth);
+
+ dbCopy->merge(dbSource);
+
+ // one entry is duplicated because of mode
+ QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 3);
+ // the older entry was merged from the other db as last in the group
+ Entry* olderEntry = dbCopy->rootGroup()->children().at(0)->entries().at(2);
+ QVERIFY2(olderEntry->attributes()->hasKey("merged"), "older entry is marked with an attribute \"merged\"");
+
+ delete dbSource;
+ delete dbCopy;
+}
+
+Database* TestGroup::createMergeTestDatabase()
+{
+ Database* db = new Database();
+
+ Group* group1 = new Group();
+ group1->setName("group 1");
+ Group* group2 = new Group();
+ group2->setName("group 2");
+
+ Entry* entry1 = new Entry();
+ Entry* entry2 = new Entry();
+
+ entry1->setGroup(group1);
+ entry1->setUuid(Uuid::random());
+ entry2->setGroup(group1);
+ entry2->setUuid(Uuid::random());
+
+ group1->setParent(db->rootGroup());
+ group2->setParent(db->rootGroup());
+
+ return db;
+}