TestMerge.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. /*
  2. * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestMerge.h"
  18. #include "TestGlobal.h"
  19. #include "mock/MockClock.h"
  20. #include "core/Merger.h"
  21. #include "core/Metadata.h"
  22. #include "crypto/Crypto.h"
  23. #include <QSignalSpy>
  24. QTEST_GUILESS_MAIN(TestMerge)
  25. namespace
  26. {
  27. TimeInfo modificationTime(TimeInfo timeInfo, int years, int months, int days)
  28. {
  29. const QDateTime time = timeInfo.lastModificationTime();
  30. timeInfo.setLastModificationTime(time.addYears(years).addMonths(months).addDays(days));
  31. return timeInfo;
  32. }
  33. MockClock* m_clock = nullptr;
  34. } // namespace
  35. void TestMerge::initTestCase()
  36. {
  37. qRegisterMetaType<Entry*>("Entry*");
  38. qRegisterMetaType<Group*>("Group*");
  39. QVERIFY(Crypto::init());
  40. }
  41. void TestMerge::init()
  42. {
  43. Q_ASSERT(m_clock == nullptr);
  44. m_clock = new MockClock(2010, 5, 5, 10, 30, 10);
  45. MockClock::setup(m_clock);
  46. }
  47. void TestMerge::cleanup()
  48. {
  49. MockClock::teardown();
  50. m_clock = nullptr;
  51. }
  52. /**
  53. * Merge an existing database into a new one.
  54. * All the entries of the existing should end
  55. * up in the new one.
  56. */
  57. void TestMerge::testMergeIntoNew()
  58. {
  59. QScopedPointer<Database> dbSource(createTestDatabase());
  60. QScopedPointer<Database> dbDestination(new Database());
  61. Merger merger(dbSource.data(), dbDestination.data());
  62. merger.merge();
  63. QCOMPARE(dbDestination->rootGroup()->children().size(), 2);
  64. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().size(), 2);
  65. // Test for retention of history
  66. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().at(0)->historyItems().isEmpty(), false);
  67. }
  68. /**
  69. * Merging when no changes occured should not
  70. * have any side effect.
  71. */
  72. void TestMerge::testMergeNoChanges()
  73. {
  74. QScopedPointer<Database> dbDestination(createTestDatabase());
  75. QScopedPointer<Database> dbSource(
  76. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  77. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  78. QCOMPARE(dbSource->rootGroup()->entriesRecursive().size(), 2);
  79. m_clock->advanceSecond(1);
  80. Merger merger1(dbSource.data(), dbDestination.data());
  81. merger1.merge();
  82. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  83. QCOMPARE(dbSource->rootGroup()->entriesRecursive().size(), 2);
  84. m_clock->advanceSecond(1);
  85. Merger merger2(dbSource.data(), dbDestination.data());
  86. merger2.merge();
  87. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  88. QCOMPARE(dbSource->rootGroup()->entriesRecursive().size(), 2);
  89. }
  90. /**
  91. * If the entry is updated in the source database, the update
  92. * should propagate in the destination database.
  93. */
  94. void TestMerge::testResolveConflictNewer()
  95. {
  96. QScopedPointer<Database> dbDestination(createTestDatabase());
  97. QScopedPointer<Database> dbSource(
  98. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  99. // sanity check
  100. QPointer<Group> groupSourceInitial = dbSource->rootGroup()->findChildByName("group1");
  101. QVERIFY(groupSourceInitial != nullptr);
  102. QCOMPARE(groupSourceInitial->entries().size(), 2);
  103. QPointer<Group> groupDestinationInitial = dbSource->rootGroup()->findChildByName("group1");
  104. QVERIFY(groupDestinationInitial != nullptr);
  105. QCOMPARE(groupDestinationInitial->entries().size(), 2);
  106. QPointer<Entry> entrySourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  107. QVERIFY(entrySourceInitial != nullptr);
  108. QVERIFY(entrySourceInitial->group() == groupSourceInitial);
  109. const TimeInfo entrySourceInitialTimeInfo = entrySourceInitial->timeInfo();
  110. const TimeInfo groupSourceInitialTimeInfo = groupSourceInitial->timeInfo();
  111. const TimeInfo groupDestinationInitialTimeInfo = groupDestinationInitial->timeInfo();
  112. // Make sure the two changes have a different timestamp.
  113. m_clock->advanceSecond(1);
  114. // make this entry newer than in destination db
  115. entrySourceInitial->beginUpdate();
  116. entrySourceInitial->setPassword("password");
  117. entrySourceInitial->endUpdate();
  118. const TimeInfo entrySourceUpdatedTimeInfo = entrySourceInitial->timeInfo();
  119. const TimeInfo groupSourceUpdatedTimeInfo = groupSourceInitial->timeInfo();
  120. QVERIFY(entrySourceInitialTimeInfo != entrySourceUpdatedTimeInfo);
  121. QVERIFY(groupSourceInitialTimeInfo == groupSourceUpdatedTimeInfo);
  122. QVERIFY(groupSourceInitialTimeInfo == groupDestinationInitialTimeInfo);
  123. // Make sure the merge changes have a different timestamp.
  124. m_clock->advanceSecond(1);
  125. Merger merger(dbSource.data(), dbDestination.data());
  126. merger.merge();
  127. // sanity check
  128. QPointer<Group> groupDestinationMerged = dbDestination->rootGroup()->findChildByName("group1");
  129. QVERIFY(groupDestinationMerged != nullptr);
  130. QCOMPARE(groupDestinationMerged->entries().size(), 2);
  131. QCOMPARE(groupDestinationMerged->timeInfo(), groupDestinationInitialTimeInfo);
  132. QPointer<Entry> entryDestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  133. QVERIFY(entryDestinationMerged != nullptr);
  134. QVERIFY(entryDestinationMerged->group() != nullptr);
  135. QCOMPARE(entryDestinationMerged->password(), QString("password"));
  136. QCOMPARE(entryDestinationMerged->timeInfo(), entrySourceUpdatedTimeInfo);
  137. // When updating an entry, it should not end up in the
  138. // deleted objects.
  139. for (DeletedObject deletedObject : dbDestination->deletedObjects()) {
  140. QVERIFY(deletedObject.uuid != entryDestinationMerged->uuid());
  141. }
  142. }
  143. /**
  144. * If the entry is updated in the source database, and the
  145. * destination database after, the entry should remain the
  146. * same.
  147. */
  148. void TestMerge::testResolveConflictExisting()
  149. {
  150. QScopedPointer<Database> dbDestination(createTestDatabase());
  151. QScopedPointer<Database> dbSource(
  152. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  153. // sanity check
  154. QPointer<Group> groupSourceInitial = dbSource->rootGroup()->findChildByName("group1");
  155. QVERIFY(groupSourceInitial != nullptr);
  156. QCOMPARE(groupSourceInitial->entries().size(), 2);
  157. QPointer<Group> groupDestinationInitial = dbDestination->rootGroup()->findChildByName("group1");
  158. QVERIFY(groupDestinationInitial != nullptr);
  159. QCOMPARE(groupSourceInitial->entries().size(), 2);
  160. QPointer<Entry> entrySourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  161. QVERIFY(entrySourceInitial != nullptr);
  162. QVERIFY(entrySourceInitial->group() == groupSourceInitial);
  163. const TimeInfo entrySourceInitialTimeInfo = entrySourceInitial->timeInfo();
  164. const TimeInfo groupSourceInitialTimeInfo = groupSourceInitial->timeInfo();
  165. const TimeInfo groupDestinationInitialTimeInfo = groupDestinationInitial->timeInfo();
  166. // Make sure the two changes have a different timestamp.
  167. m_clock->advanceSecond(1);
  168. // make this entry older than in destination db
  169. entrySourceInitial->beginUpdate();
  170. entrySourceInitial->setPassword("password1");
  171. entrySourceInitial->endUpdate();
  172. const TimeInfo entrySourceUpdatedOlderTimeInfo = entrySourceInitial->timeInfo();
  173. const TimeInfo groupSourceUpdatedOlderTimeInfo = groupSourceInitial->timeInfo();
  174. QPointer<Group> groupDestinationUpdated = dbDestination->rootGroup()->findChildByName("group1");
  175. QVERIFY(groupDestinationUpdated != nullptr);
  176. QCOMPARE(groupDestinationUpdated->entries().size(), 2);
  177. QPointer<Entry> entryDestinationUpdated = dbDestination->rootGroup()->findEntryByPath("entry1");
  178. QVERIFY(entryDestinationUpdated != nullptr);
  179. QVERIFY(entryDestinationUpdated->group() == groupDestinationUpdated);
  180. // Make sure the two changes have a different timestamp.
  181. m_clock->advanceSecond(1);
  182. // make this entry newer than in source db
  183. entryDestinationUpdated->beginUpdate();
  184. entryDestinationUpdated->setPassword("password2");
  185. entryDestinationUpdated->endUpdate();
  186. const TimeInfo entryDestinationUpdatedNewerTimeInfo = entryDestinationUpdated->timeInfo();
  187. const TimeInfo groupDestinationUpdatedNewerTimeInfo = groupDestinationUpdated->timeInfo();
  188. QVERIFY(entrySourceUpdatedOlderTimeInfo != entrySourceInitialTimeInfo);
  189. QVERIFY(entrySourceUpdatedOlderTimeInfo != entryDestinationUpdatedNewerTimeInfo);
  190. QVERIFY(groupSourceInitialTimeInfo == groupSourceUpdatedOlderTimeInfo);
  191. QVERIFY(groupDestinationInitialTimeInfo == groupDestinationUpdatedNewerTimeInfo);
  192. QVERIFY(groupSourceInitialTimeInfo == groupDestinationInitialTimeInfo);
  193. // Make sure the merge changes have a different timestamp.
  194. m_clock->advanceSecond(1);
  195. Merger merger(dbSource.data(), dbDestination.data());
  196. merger.merge();
  197. // sanity check
  198. QPointer<Group> groupDestinationMerged = dbDestination->rootGroup()->findChildByName("group1");
  199. QVERIFY(groupDestinationMerged != nullptr);
  200. QCOMPARE(groupDestinationMerged->entries().size(), 2);
  201. QCOMPARE(groupDestinationMerged->timeInfo(), groupDestinationUpdatedNewerTimeInfo);
  202. QPointer<Entry> entryDestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  203. QVERIFY(entryDestinationMerged != nullptr);
  204. QCOMPARE(entryDestinationMerged->password(), QString("password2"));
  205. QCOMPARE(entryDestinationMerged->timeInfo(), entryDestinationUpdatedNewerTimeInfo);
  206. // When updating an entry, it should not end up in the
  207. // deleted objects.
  208. for (DeletedObject deletedObject : dbDestination->deletedObjects()) {
  209. QVERIFY(deletedObject.uuid != entryDestinationMerged->uuid());
  210. }
  211. }
  212. /**
  213. * Tests the KeepBoth merge mode.
  214. */
  215. void TestMerge::testResolveConflictDuplicate()
  216. {
  217. QScopedPointer<Database> dbDestination(createTestDatabase());
  218. QScopedPointer<Database> dbSource(
  219. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneIncludeHistory, Group::CloneIncludeEntries));
  220. // sanity check
  221. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().size(), 2);
  222. // make this entry newer than in original db
  223. QPointer<Entry> updatedDestinationEntry = dbDestination->rootGroup()->children().at(0)->entries().at(0);
  224. const TimeInfo initialEntryTimeInfo = updatedDestinationEntry->timeInfo();
  225. const TimeInfo updatedEntryTimeInfo = modificationTime(initialEntryTimeInfo, 1, 0, 0);
  226. updatedDestinationEntry->setTimeInfo(updatedEntryTimeInfo);
  227. dbDestination->rootGroup()->setMergeMode(Group::MergeMode::Duplicate);
  228. // Make sure the merge changes have a different timestamp.
  229. m_clock->advanceSecond(1);
  230. Merger merger(dbSource.data(), dbDestination.data());
  231. merger.merge();
  232. // one entry is duplicated because of mode
  233. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().size(), 3);
  234. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().at(0)->historyItems().isEmpty(), false);
  235. // the older entry was merged from the other db as last in the group
  236. QPointer<Entry> newerEntry = dbDestination->rootGroup()->children().at(0)->entries().at(0);
  237. QPointer<Entry> olderEntry = dbDestination->rootGroup()->children().at(0)->entries().at(2);
  238. QVERIFY(newerEntry->title() == olderEntry->title());
  239. QVERIFY2(!newerEntry->attributes()->hasKey("merged"), "newer entry is not marked with an attribute \"merged\"");
  240. QVERIFY2(olderEntry->attributes()->hasKey("merged"), "older entry is marked with an attribute \"merged\"");
  241. QCOMPARE(olderEntry->historyItems().isEmpty(), false);
  242. QCOMPARE(newerEntry->timeInfo(), updatedEntryTimeInfo);
  243. // TODO HNH: this may be subject to discussions since the entry itself is newer but represents an older one
  244. // QCOMPARE(olderEntry->timeInfo(), initialEntryTimeInfo);
  245. QVERIFY2(olderEntry->uuidToHex() != updatedDestinationEntry->uuidToHex(),
  246. "KeepBoth should not reuse the UUIDs when cloning.");
  247. }
  248. void TestMerge::testResolveConflictTemplate(
  249. int mergeMode,
  250. std::function<void(Database*, const QMap<const char*, QDateTime>&)> verification)
  251. {
  252. QMap<const char*, QDateTime> timestamps;
  253. timestamps["initialTime"] = m_clock->currentDateTimeUtc();
  254. QScopedPointer<Database> dbDestination(createTestDatabase());
  255. Entry* deletedEntry1 = new Entry();
  256. deletedEntry1->setUuid(QUuid::createUuid());
  257. deletedEntry1->beginUpdate();
  258. deletedEntry1->setGroup(dbDestination->rootGroup());
  259. deletedEntry1->setTitle("deletedDestination");
  260. deletedEntry1->endUpdate();
  261. Entry* deletedEntry2 = new Entry();
  262. deletedEntry2->setUuid(QUuid::createUuid());
  263. deletedEntry2->beginUpdate();
  264. deletedEntry2->setGroup(dbDestination->rootGroup());
  265. deletedEntry2->setTitle("deletedSource");
  266. deletedEntry2->endUpdate();
  267. QScopedPointer<Database> dbSource(
  268. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneIncludeHistory, Group::CloneIncludeEntries));
  269. timestamps["oldestCommonHistoryTime"] = m_clock->currentDateTimeUtc();
  270. // sanity check
  271. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().size(), 2);
  272. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().at(0)->historyItems().count(), 1);
  273. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().at(1)->historyItems().count(), 1);
  274. QCOMPARE(dbSource->rootGroup()->children().at(0)->entries().size(), 2);
  275. QCOMPARE(dbSource->rootGroup()->children().at(0)->entries().at(0)->historyItems().count(), 1);
  276. QCOMPARE(dbSource->rootGroup()->children().at(0)->entries().at(1)->historyItems().count(), 1);
  277. // simulate some work in the dbs (manipulate the history)
  278. QPointer<Entry> destinationEntry1 = dbDestination->rootGroup()->children().at(0)->entries().at(0);
  279. QPointer<Entry> destinationEntry2 = dbDestination->rootGroup()->children().at(0)->entries().at(1);
  280. QPointer<Entry> sourceEntry1 = dbSource->rootGroup()->children().at(0)->entries().at(0);
  281. QPointer<Entry> sourceEntry2 = dbSource->rootGroup()->children().at(0)->entries().at(1);
  282. timestamps["newestCommonHistoryTime"] = m_clock->advanceMinute(1);
  283. destinationEntry1->beginUpdate();
  284. destinationEntry1->setNotes("1 Common");
  285. destinationEntry1->endUpdate();
  286. destinationEntry2->beginUpdate();
  287. destinationEntry2->setNotes("1 Common");
  288. destinationEntry2->endUpdate();
  289. sourceEntry1->beginUpdate();
  290. sourceEntry1->setNotes("1 Common");
  291. sourceEntry1->endUpdate();
  292. sourceEntry2->beginUpdate();
  293. sourceEntry2->setNotes("1 Common");
  294. sourceEntry2->endUpdate();
  295. timestamps["oldestDivergingHistoryTime"] = m_clock->advanceSecond(1);
  296. destinationEntry2->beginUpdate();
  297. destinationEntry2->setNotes("2 Destination");
  298. destinationEntry2->endUpdate();
  299. sourceEntry1->beginUpdate();
  300. sourceEntry1->setNotes("2 Source");
  301. sourceEntry1->endUpdate();
  302. timestamps["newestDivergingHistoryTime"] = m_clock->advanceHour(1);
  303. destinationEntry1->beginUpdate();
  304. destinationEntry1->setNotes("3 Destination");
  305. destinationEntry1->endUpdate();
  306. sourceEntry2->beginUpdate();
  307. sourceEntry2->setNotes("3 Source");
  308. sourceEntry2->endUpdate();
  309. // sanity check
  310. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().at(0)->historyItems().count(), 3);
  311. QCOMPARE(dbDestination->rootGroup()->children().at(0)->entries().at(1)->historyItems().count(), 3);
  312. QCOMPARE(dbSource->rootGroup()->children().at(0)->entries().at(0)->historyItems().count(), 3);
  313. QCOMPARE(dbSource->rootGroup()->children().at(0)->entries().at(1)->historyItems().count(), 3);
  314. m_clock->advanceMinute(1);
  315. QPointer<Entry> deletedEntryDestination = dbDestination->rootGroup()->findEntryByPath("deletedDestination");
  316. dbDestination->recycleEntry(deletedEntryDestination);
  317. QPointer<Entry> deletedEntrySource = dbSource->rootGroup()->findEntryByPath("deletedSource");
  318. dbSource->recycleEntry(deletedEntrySource);
  319. m_clock->advanceMinute(1);
  320. Entry* destinationEntrySingle = new Entry();
  321. destinationEntrySingle->setUuid(QUuid::createUuid());
  322. destinationEntrySingle->beginUpdate();
  323. destinationEntrySingle->setGroup(dbDestination->rootGroup()->children().at(1));
  324. destinationEntrySingle->setTitle("entryDestination");
  325. destinationEntrySingle->endUpdate();
  326. Entry* sourceEntrySingle = new Entry();
  327. sourceEntrySingle->setUuid(QUuid::createUuid());
  328. sourceEntrySingle->beginUpdate();
  329. sourceEntrySingle->setGroup(dbSource->rootGroup()->children().at(1));
  330. sourceEntrySingle->setTitle("entrySource");
  331. sourceEntrySingle->endUpdate();
  332. dbDestination->rootGroup()->setMergeMode(static_cast<Group::MergeMode>(mergeMode));
  333. // Make sure the merge changes have a different timestamp.
  334. timestamps["mergeTime"] = m_clock->advanceSecond(1);
  335. Merger merger(dbSource.data(), dbDestination.data());
  336. merger.merge();
  337. QPointer<Group> mergedRootGroup = dbDestination->rootGroup();
  338. QCOMPARE(mergedRootGroup->entries().size(), 0);
  339. // Both databases contain their own generated recycleBin - just one is considered a real recycleBin, the other
  340. // exists as normal group, therefore only one entry is considered deleted
  341. QCOMPARE(dbDestination->metadata()->recycleBin()->entries().size(), 1);
  342. QPointer<Group> mergedGroup1 = mergedRootGroup->children().at(0);
  343. QPointer<Group> mergedGroup2 = mergedRootGroup->children().at(1);
  344. QVERIFY(mergedGroup1);
  345. QVERIFY(mergedGroup2);
  346. QCOMPARE(mergedGroup2->entries().size(), 2);
  347. QVERIFY(mergedGroup1->entries().at(0));
  348. QVERIFY(mergedGroup1->entries().at(1));
  349. verification(dbDestination.data(), timestamps);
  350. QVERIFY(dbDestination->rootGroup()->findEntryByPath("entryDestination"));
  351. QVERIFY(dbDestination->rootGroup()->findEntryByPath("entrySource"));
  352. }
  353. void TestMerge::testDeletionConflictTemplate(int mergeMode,
  354. std::function<void(Database*, const QMap<QString, QUuid>&)> verification)
  355. {
  356. QMap<QString, QUuid> identifiers;
  357. m_clock->currentDateTimeUtc();
  358. QScopedPointer<Database> dbDestination(createTestDatabase());
  359. // scenarios:
  360. // entry directly deleted in source before updated in target
  361. // entry directly deleted in source after updated in target
  362. // entry directly deleted in target before updated in source
  363. // entry directly deleted in target after updated in source
  364. // entry indirectly deleted in source before updated in target
  365. // entry indirectly deleted in source after updated in target
  366. // entry indirectly deleted in target before updated in source
  367. // entry indirectly deleted in target after updated in source
  368. auto createGroup = [&](const char* name, Group* parent) {
  369. Group* group = new Group();
  370. group->setUuid(QUuid::createUuid());
  371. group->setName(name);
  372. group->setParent(parent, 0);
  373. identifiers[group->name()] = group->uuid();
  374. return group;
  375. };
  376. auto createEntry = [&](const char* title, Group* parent) {
  377. Entry* entry = new Entry();
  378. entry->setUuid(QUuid::createUuid());
  379. entry->setTitle(title);
  380. entry->setGroup(parent);
  381. identifiers[entry->title()] = entry->uuid();
  382. return entry;
  383. };
  384. auto changeEntry = [](Entry* entry) {
  385. entry->beginUpdate();
  386. entry->setNotes("Change");
  387. entry->endUpdate();
  388. };
  389. Group* directlyDeletedEntryGroup = createGroup("DirectlyDeletedEntries", dbDestination->rootGroup());
  390. createEntry("EntryDeletedInSourceBeforeChangedInTarget", directlyDeletedEntryGroup);
  391. createEntry("EntryDeletedInSourceAfterChangedInTarget", directlyDeletedEntryGroup);
  392. createEntry("EntryDeletedInTargetBeforeChangedInSource", directlyDeletedEntryGroup);
  393. createEntry("EntryDeletedInTargetAfterChangedInSource", directlyDeletedEntryGroup);
  394. Group* groupDeletedInSourceBeforeEntryUpdatedInTarget =
  395. createGroup("GroupDeletedInSourceBeforeEntryUpdatedInTarget", dbDestination->rootGroup());
  396. createEntry("EntryDeletedInSourceBeforeEntryUpdatedInTarget", groupDeletedInSourceBeforeEntryUpdatedInTarget);
  397. Group* groupDeletedInSourceAfterEntryUpdatedInTarget =
  398. createGroup("GroupDeletedInSourceAfterEntryUpdatedInTarget", dbDestination->rootGroup());
  399. createEntry("EntryDeletedInSourceAfterEntryUpdatedInTarget", groupDeletedInSourceAfterEntryUpdatedInTarget);
  400. Group* groupDeletedInTargetBeforeEntryUpdatedInSource =
  401. createGroup("GroupDeletedInTargetBeforeEntryUpdatedInSource", dbDestination->rootGroup());
  402. createEntry("EntryDeletedInTargetBeforeEntryUpdatedInSource", groupDeletedInTargetBeforeEntryUpdatedInSource);
  403. Group* groupDeletedInTargetAfterEntryUpdatedInSource =
  404. createGroup("GroupDeletedInTargetAfterEntryUpdatedInSource", dbDestination->rootGroup());
  405. createEntry("EntryDeletedInTargetAfterEntryUpdatedInSource", groupDeletedInTargetAfterEntryUpdatedInSource);
  406. QScopedPointer<Database> dbSource(
  407. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneIncludeHistory, Group::CloneIncludeEntries));
  408. QPointer<Entry> sourceEntryDeletedInSourceBeforeChangedInTarget =
  409. dbSource->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInSourceBeforeChangedInTarget"]);
  410. QPointer<Entry> targetEntryDeletedInSourceBeforeChangedInTarget =
  411. dbDestination->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInSourceBeforeChangedInTarget"]);
  412. QPointer<Entry> sourceEntryDeletedInSourceAfterChangedInTarget =
  413. dbSource->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInSourceAfterChangedInTarget"]);
  414. QPointer<Entry> targetEntryDeletedInSourceAfterChangedInTarget =
  415. dbDestination->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInSourceAfterChangedInTarget"]);
  416. QPointer<Entry> sourceEntryDeletedInTargetBeforeChangedInSource =
  417. dbSource->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInTargetBeforeChangedInSource"]);
  418. QPointer<Entry> targetEntryDeletedInTargetBeforeChangedInSource =
  419. dbDestination->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInTargetBeforeChangedInSource"]);
  420. QPointer<Entry> sourceEntryDeletedInTargetAfterChangedInSource =
  421. dbSource->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInTargetAfterChangedInSource"]);
  422. QPointer<Entry> targetEntryDeletedInTargetAfterChangedInSource =
  423. dbDestination->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInTargetAfterChangedInSource"]);
  424. QPointer<Group> sourceGroupDeletedInSourceBeforeEntryUpdatedInTarget =
  425. dbSource->rootGroup()->findGroupByUuid(identifiers["GroupDeletedInSourceBeforeEntryUpdatedInTarget"]);
  426. QPointer<Entry> targetEntryDeletedInSourceBeforeEntryUpdatedInTarget =
  427. dbDestination->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInSourceBeforeEntryUpdatedInTarget"]);
  428. QPointer<Group> sourceGroupDeletedInSourceAfterEntryUpdatedInTarget =
  429. dbSource->rootGroup()->findGroupByUuid(identifiers["GroupDeletedInSourceAfterEntryUpdatedInTarget"]);
  430. QPointer<Entry> targetEntryDeletedInSourceAfterEntryUpdatedInTarget =
  431. dbDestination->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInSourceAfterEntryUpdatedInTarget"]);
  432. QPointer<Group> targetGroupDeletedInTargetBeforeEntryUpdatedInSource =
  433. dbDestination->rootGroup()->findGroupByUuid(identifiers["GroupDeletedInTargetBeforeEntryUpdatedInSource"]);
  434. QPointer<Entry> sourceEntryDeletedInTargetBeforeEntryUpdatedInSource =
  435. dbSource->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInTargetBeforeEntryUpdatedInSource"]);
  436. QPointer<Group> targetGroupDeletedInTargetAfterEntryUpdatedInSource =
  437. dbDestination->rootGroup()->findGroupByUuid(identifiers["GroupDeletedInTargetAfterEntryUpdatedInSource"]);
  438. QPointer<Entry> sourceEntryDeletedInTargetAfterEntryUpdatedInSoruce =
  439. dbSource->rootGroup()->findEntryByUuid(identifiers["EntryDeletedInTargetAfterEntryUpdatedInSource"]);
  440. // simulate some work in the dbs (manipulate the history)
  441. m_clock->advanceMinute(1);
  442. delete sourceEntryDeletedInSourceBeforeChangedInTarget.data();
  443. changeEntry(targetEntryDeletedInSourceAfterChangedInTarget);
  444. delete targetEntryDeletedInTargetBeforeChangedInSource.data();
  445. changeEntry(sourceEntryDeletedInTargetAfterChangedInSource);
  446. delete sourceGroupDeletedInSourceBeforeEntryUpdatedInTarget.data();
  447. changeEntry(targetEntryDeletedInSourceAfterEntryUpdatedInTarget);
  448. delete targetGroupDeletedInTargetBeforeEntryUpdatedInSource.data();
  449. changeEntry(sourceEntryDeletedInTargetAfterEntryUpdatedInSoruce);
  450. m_clock->advanceMinute(1);
  451. changeEntry(targetEntryDeletedInSourceBeforeChangedInTarget);
  452. delete sourceEntryDeletedInSourceAfterChangedInTarget.data();
  453. changeEntry(sourceEntryDeletedInTargetBeforeChangedInSource);
  454. delete targetEntryDeletedInTargetAfterChangedInSource.data();
  455. changeEntry(targetEntryDeletedInSourceBeforeEntryUpdatedInTarget);
  456. delete sourceGroupDeletedInSourceAfterEntryUpdatedInTarget.data();
  457. changeEntry(sourceEntryDeletedInTargetBeforeEntryUpdatedInSource);
  458. delete targetGroupDeletedInTargetAfterEntryUpdatedInSource.data();
  459. m_clock->advanceMinute(1);
  460. dbDestination->rootGroup()->setMergeMode(static_cast<Group::MergeMode>(mergeMode));
  461. Merger merger(dbSource.data(), dbDestination.data());
  462. merger.merge();
  463. verification(dbDestination.data(), identifiers);
  464. }
  465. void TestMerge::assertDeletionNewerOnly(Database* db, const QMap<QString, QUuid>& identifiers)
  466. {
  467. QPointer<Group> mergedRootGroup = db->rootGroup();
  468. // newer change in target prevents deletion
  469. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceBeforeChangedInTarget"]));
  470. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInSourceBeforeChangedInTarget"]));
  471. // newer deletion in source forces deletion
  472. QVERIFY(!mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceAfterChangedInTarget"]));
  473. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInSourceAfterChangedInTarget"]));
  474. // newer change in source privents deletion
  475. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetBeforeChangedInSource"]));
  476. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInTargetBeforeChangedInSource"]));
  477. // newer deletion in target forces deletion
  478. QVERIFY(!mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetAfterChangedInSource"]));
  479. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInTargetAfterChangedInSource"]));
  480. // newer change in target prevents deletion
  481. QVERIFY(mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInSourceBeforeEntryUpdatedInTarget"]));
  482. QVERIFY(!db->containsDeletedObject(identifiers["GroupDeletedInSourceBeforeEntryUpdatedInTarget"]));
  483. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceBeforeEntryUpdatedInTarget"]));
  484. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInSourceBeforeEntryUpdatedInTarget"]));
  485. // newer deletion in source forces deletion
  486. QVERIFY(!mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInSourceAfterEntryUpdatedInTarget"]));
  487. QVERIFY(db->containsDeletedObject(identifiers["GroupDeletedInSourceAfterEntryUpdatedInTarget"]));
  488. QVERIFY(!mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceAfterEntryUpdatedInTarget"]));
  489. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInSourceAfterEntryUpdatedInTarget"]));
  490. // newer change in source privents deletion
  491. QVERIFY(mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInTargetBeforeEntryUpdatedInSource"]));
  492. QVERIFY(!db->containsDeletedObject(identifiers["GroupDeletedInTargetBeforeEntryUpdatedInSource"]));
  493. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetBeforeEntryUpdatedInSource"]));
  494. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInTargetBeforeEntryUpdatedInSource"]));
  495. // newer deletion in target forces deletion
  496. QVERIFY(!mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInTargetAfterEntryUpdatedInSource"]));
  497. QVERIFY(db->containsDeletedObject(identifiers["GroupDeletedInTargetAfterEntryUpdatedInSource"]));
  498. QVERIFY(!mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetAfterEntryUpdatedInSource"]));
  499. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInTargetAfterEntryUpdatedInSource"]));
  500. }
  501. void TestMerge::assertDeletionLocalOnly(Database* db, const QMap<QString, QUuid>& identifiers)
  502. {
  503. QPointer<Group> mergedRootGroup = db->rootGroup();
  504. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceBeforeChangedInTarget"]));
  505. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInSourceBeforeChangedInTarget"]));
  506. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceAfterChangedInTarget"]));
  507. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInSourceAfterChangedInTarget"]));
  508. // Uuids in db and deletedObjects is intended according to KeePass #1752
  509. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetBeforeChangedInSource"]));
  510. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInTargetBeforeChangedInSource"]));
  511. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetAfterChangedInSource"]));
  512. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInTargetAfterChangedInSource"]));
  513. QVERIFY(mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInSourceBeforeEntryUpdatedInTarget"]));
  514. QVERIFY(!db->containsDeletedObject(identifiers["GroupDeletedInSourceBeforeEntryUpdatedInTarget"]));
  515. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceBeforeEntryUpdatedInTarget"]));
  516. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInSourceBeforeEntryUpdatedInTarget"]));
  517. QVERIFY(mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInSourceAfterEntryUpdatedInTarget"]));
  518. QVERIFY(!db->containsDeletedObject(identifiers["GroupDeletedInSourceAfterEntryUpdatedInTarget"]));
  519. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInSourceAfterEntryUpdatedInTarget"]));
  520. QVERIFY(!db->containsDeletedObject(identifiers["EntryDeletedInSourceAfterEntryUpdatedInTarget"]));
  521. QVERIFY(mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInTargetBeforeEntryUpdatedInSource"]));
  522. QVERIFY(db->containsDeletedObject(identifiers["GroupDeletedInTargetBeforeEntryUpdatedInSource"]));
  523. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetBeforeEntryUpdatedInSource"]));
  524. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInTargetBeforeEntryUpdatedInSource"]));
  525. QVERIFY(mergedRootGroup->findGroupByUuid(identifiers["GroupDeletedInTargetAfterEntryUpdatedInSource"]));
  526. QVERIFY(db->containsDeletedObject(identifiers["GroupDeletedInTargetAfterEntryUpdatedInSource"]));
  527. QVERIFY(mergedRootGroup->findEntryByUuid(identifiers["EntryDeletedInTargetAfterEntryUpdatedInSource"]));
  528. QVERIFY(db->containsDeletedObject(identifiers["EntryDeletedInTargetAfterEntryUpdatedInSource"]));
  529. }
  530. void TestMerge::assertUpdateMergedEntry1(Entry* mergedEntry1, const QMap<const char*, QDateTime>& timestamps)
  531. {
  532. QCOMPARE(mergedEntry1->historyItems().count(), 4);
  533. QCOMPARE(mergedEntry1->historyItems().at(0)->notes(), QString(""));
  534. QCOMPARE(mergedEntry1->historyItems().at(0)->timeInfo().lastModificationTime(), timestamps["initialTime"]);
  535. QCOMPARE(mergedEntry1->historyItems().at(1)->notes(), QString(""));
  536. QCOMPARE(mergedEntry1->historyItems().at(1)->timeInfo().lastModificationTime(),
  537. timestamps["oldestCommonHistoryTime"]);
  538. QCOMPARE(mergedEntry1->historyItems().at(2)->notes(), QString("1 Common"));
  539. QCOMPARE(mergedEntry1->historyItems().at(2)->timeInfo().lastModificationTime(),
  540. timestamps["newestCommonHistoryTime"]);
  541. QCOMPARE(mergedEntry1->historyItems().at(3)->notes(), QString("2 Source"));
  542. QCOMPARE(mergedEntry1->historyItems().at(3)->timeInfo().lastModificationTime(),
  543. timestamps["oldestDivergingHistoryTime"]);
  544. QCOMPARE(mergedEntry1->notes(), QString("3 Destination"));
  545. QCOMPARE(mergedEntry1->timeInfo().lastModificationTime(), timestamps["newestDivergingHistoryTime"]);
  546. }
  547. void TestMerge::assertUpdateReappliedEntry2(Entry* mergedEntry2, const QMap<const char*, QDateTime>& timestamps)
  548. {
  549. QCOMPARE(mergedEntry2->historyItems().count(), 5);
  550. QCOMPARE(mergedEntry2->historyItems().at(0)->notes(), QString(""));
  551. QCOMPARE(mergedEntry2->historyItems().at(0)->timeInfo().lastModificationTime(), timestamps["initialTime"]);
  552. QCOMPARE(mergedEntry2->historyItems().at(1)->notes(), QString(""));
  553. QCOMPARE(mergedEntry2->historyItems().at(1)->timeInfo().lastModificationTime(),
  554. timestamps["oldestCommonHistoryTime"]);
  555. QCOMPARE(mergedEntry2->historyItems().at(2)->notes(), QString("1 Common"));
  556. QCOMPARE(mergedEntry2->historyItems().at(2)->timeInfo().lastModificationTime(),
  557. timestamps["newestCommonHistoryTime"]);
  558. QCOMPARE(mergedEntry2->historyItems().at(3)->notes(), QString("2 Destination"));
  559. QCOMPARE(mergedEntry2->historyItems().at(3)->timeInfo().lastModificationTime(),
  560. timestamps["oldestDivergingHistoryTime"]);
  561. QCOMPARE(mergedEntry2->historyItems().at(4)->notes(), QString("3 Source"));
  562. QCOMPARE(mergedEntry2->historyItems().at(4)->timeInfo().lastModificationTime(),
  563. timestamps["newestDivergingHistoryTime"]);
  564. QCOMPARE(mergedEntry2->notes(), QString("2 Destination"));
  565. QCOMPARE(mergedEntry2->timeInfo().lastModificationTime(), timestamps["mergeTime"]);
  566. }
  567. void TestMerge::assertUpdateReappliedEntry1(Entry* mergedEntry1, const QMap<const char*, QDateTime>& timestamps)
  568. {
  569. QCOMPARE(mergedEntry1->historyItems().count(), 5);
  570. QCOMPARE(mergedEntry1->historyItems().at(0)->notes(), QString(""));
  571. QCOMPARE(mergedEntry1->historyItems().at(0)->timeInfo().lastModificationTime(), timestamps["initialTime"]);
  572. QCOMPARE(mergedEntry1->historyItems().at(1)->notes(), QString(""));
  573. QCOMPARE(mergedEntry1->historyItems().at(1)->timeInfo().lastModificationTime(),
  574. timestamps["oldestCommonHistoryTime"]);
  575. QCOMPARE(mergedEntry1->historyItems().at(2)->notes(), QString("1 Common"));
  576. QCOMPARE(mergedEntry1->historyItems().at(2)->timeInfo().lastModificationTime(),
  577. timestamps["newestCommonHistoryTime"]);
  578. QCOMPARE(mergedEntry1->historyItems().at(3)->notes(), QString("2 Source"));
  579. QCOMPARE(mergedEntry1->historyItems().at(3)->timeInfo().lastModificationTime(),
  580. timestamps["oldestDivergingHistoryTime"]);
  581. QCOMPARE(mergedEntry1->historyItems().at(4)->notes(), QString("3 Destination"));
  582. QCOMPARE(mergedEntry1->historyItems().at(4)->timeInfo().lastModificationTime(),
  583. timestamps["newestDivergingHistoryTime"]);
  584. QCOMPARE(mergedEntry1->notes(), QString("2 Source"));
  585. QCOMPARE(mergedEntry1->timeInfo().lastModificationTime(), timestamps["mergeTime"]);
  586. }
  587. void TestMerge::assertUpdateMergedEntry2(Entry* mergedEntry2, const QMap<const char*, QDateTime>& timestamps)
  588. {
  589. QCOMPARE(mergedEntry2->historyItems().count(), 4);
  590. QCOMPARE(mergedEntry2->historyItems().at(0)->notes(), QString(""));
  591. QCOMPARE(mergedEntry2->historyItems().at(0)->timeInfo().lastModificationTime(), timestamps["initialTime"]);
  592. QCOMPARE(mergedEntry2->historyItems().at(1)->notes(), QString(""));
  593. QCOMPARE(mergedEntry2->historyItems().at(1)->timeInfo().lastModificationTime(),
  594. timestamps["oldestCommonHistoryTime"]);
  595. QCOMPARE(mergedEntry2->historyItems().at(2)->notes(), QString("1 Common"));
  596. QCOMPARE(mergedEntry2->historyItems().at(2)->timeInfo().lastModificationTime(),
  597. timestamps["newestCommonHistoryTime"]);
  598. QCOMPARE(mergedEntry2->historyItems().at(3)->notes(), QString("2 Destination"));
  599. QCOMPARE(mergedEntry2->historyItems().at(3)->timeInfo().lastModificationTime(),
  600. timestamps["oldestDivergingHistoryTime"]);
  601. QCOMPARE(mergedEntry2->notes(), QString("3 Source"));
  602. QCOMPARE(mergedEntry2->timeInfo().lastModificationTime(), timestamps["newestDivergingHistoryTime"]);
  603. }
  604. void TestMerge::testDeletionConflictEntry_Synchronized()
  605. {
  606. testDeletionConflictTemplate(Group::Synchronize, &TestMerge::assertDeletionNewerOnly);
  607. }
  608. void TestMerge::testDeletionConflictEntry_KeepLocal()
  609. {
  610. testDeletionConflictTemplate(Group::KeepLocal, &TestMerge::assertDeletionLocalOnly);
  611. }
  612. void TestMerge::testDeletionConflictEntry_KeepRemote()
  613. {
  614. testDeletionConflictTemplate(Group::KeepRemote, &TestMerge::assertDeletionLocalOnly);
  615. }
  616. void TestMerge::testDeletionConflictEntry_KeepNewer()
  617. {
  618. testDeletionConflictTemplate(Group::KeepNewer, &TestMerge::assertDeletionLocalOnly);
  619. }
  620. void TestMerge::testDeletionConflictEntry_Duplicate()
  621. {
  622. testDeletionConflictTemplate(Group::Duplicate, &TestMerge::assertDeletionLocalOnly);
  623. }
  624. /**
  625. * Tests the KeepNewer mode concerning history.
  626. */
  627. void TestMerge::testResolveConflictEntry_Synchronize()
  628. {
  629. testResolveConflictTemplate(Group::Synchronize, [](Database* db, const QMap<const char*, QDateTime>& timestamps) {
  630. QPointer<Group> mergedRootGroup = db->rootGroup();
  631. QPointer<Group> mergedGroup1 = mergedRootGroup->children().at(0);
  632. TestMerge::assertUpdateMergedEntry1(mergedGroup1->entries().at(0), timestamps);
  633. TestMerge::assertUpdateMergedEntry2(mergedGroup1->entries().at(1), timestamps);
  634. });
  635. }
  636. /**
  637. * Tests the KeepExisting mode concerning history.
  638. */
  639. void TestMerge::testResolveConflictEntry_KeepLocal()
  640. {
  641. testResolveConflictTemplate(Group::KeepLocal, [](Database* db, const QMap<const char*, QDateTime>& timestamps) {
  642. QPointer<Group> mergedRootGroup = db->rootGroup();
  643. QPointer<Group> mergedGroup1 = mergedRootGroup->children().at(0);
  644. TestMerge::assertUpdateMergedEntry1(mergedGroup1->entries().at(0), timestamps);
  645. TestMerge::assertUpdateReappliedEntry2(mergedGroup1->entries().at(1), timestamps);
  646. });
  647. }
  648. void TestMerge::testResolveConflictEntry_KeepRemote()
  649. {
  650. testResolveConflictTemplate(Group::KeepRemote, [](Database* db, const QMap<const char*, QDateTime>& timestamps) {
  651. QPointer<Group> mergedRootGroup = db->rootGroup();
  652. QPointer<Group> mergedGroup1 = mergedRootGroup->children().at(0);
  653. TestMerge::assertUpdateReappliedEntry1(mergedGroup1->entries().at(0), timestamps);
  654. TestMerge::assertUpdateMergedEntry2(mergedGroup1->entries().at(1), timestamps);
  655. });
  656. }
  657. void TestMerge::testResolveConflictEntry_KeepNewer()
  658. {
  659. testResolveConflictTemplate(Group::KeepNewer, [](Database* db, const QMap<const char*, QDateTime>& timestamps) {
  660. QPointer<Group> mergedRootGroup = db->rootGroup();
  661. QPointer<Group> mergedGroup1 = mergedRootGroup->children().at(0);
  662. TestMerge::assertUpdateMergedEntry1(mergedGroup1->entries().at(0), timestamps);
  663. TestMerge::assertUpdateMergedEntry2(mergedGroup1->entries().at(1), timestamps);
  664. });
  665. }
  666. /**
  667. * The location of an entry should be updated in the
  668. * destination database.
  669. */
  670. void TestMerge::testMoveEntry()
  671. {
  672. QScopedPointer<Database> dbDestination(createTestDatabase());
  673. QScopedPointer<Database> dbSource(
  674. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  675. QPointer<Entry> entrySourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  676. QVERIFY(entrySourceInitial != nullptr);
  677. QPointer<Group> groupSourceInitial = dbSource->rootGroup()->findChildByName("group2");
  678. QVERIFY(groupSourceInitial != nullptr);
  679. // Make sure the two changes have a different timestamp.
  680. m_clock->advanceSecond(1);
  681. entrySourceInitial->setGroup(groupSourceInitial);
  682. QCOMPARE(entrySourceInitial->group()->name(), QString("group2"));
  683. m_clock->advanceSecond(1);
  684. Merger merger(dbSource.data(), dbDestination.data());
  685. merger.merge();
  686. QPointer<Entry> entryDestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  687. QVERIFY(entryDestinationMerged != nullptr);
  688. QCOMPARE(entryDestinationMerged->group()->name(), QString("group2"));
  689. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  690. }
  691. /**
  692. * The location of an entry should be updated in the
  693. * destination database, but changes from the destination
  694. * database should be preserved.
  695. */
  696. void TestMerge::testMoveEntryPreserveChanges()
  697. {
  698. QScopedPointer<Database> dbDestination(createTestDatabase());
  699. QScopedPointer<Database> dbSource(
  700. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  701. QPointer<Entry> entrySourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  702. QVERIFY(entrySourceInitial != nullptr);
  703. QPointer<Group> group2Source = dbSource->rootGroup()->findChildByName("group2");
  704. QVERIFY(group2Source != nullptr);
  705. m_clock->advanceSecond(1);
  706. entrySourceInitial->setGroup(group2Source);
  707. QCOMPARE(entrySourceInitial->group()->name(), QString("group2"));
  708. QPointer<Entry> entryDestinationInitial = dbDestination->rootGroup()->findEntryByPath("entry1");
  709. QVERIFY(entryDestinationInitial != nullptr);
  710. m_clock->advanceSecond(1);
  711. entryDestinationInitial->beginUpdate();
  712. entryDestinationInitial->setPassword("password");
  713. entryDestinationInitial->endUpdate();
  714. m_clock->advanceSecond(1);
  715. Merger merger(dbSource.data(), dbDestination.data());
  716. merger.merge();
  717. QPointer<Entry> entryDestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  718. QVERIFY(entryDestinationMerged != nullptr);
  719. QCOMPARE(entryDestinationMerged->group()->name(), QString("group2"));
  720. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  721. QCOMPARE(entryDestinationMerged->password(), QString("password"));
  722. }
  723. void TestMerge::testCreateNewGroups()
  724. {
  725. QScopedPointer<Database> dbDestination(createTestDatabase());
  726. QScopedPointer<Database> dbSource(
  727. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  728. m_clock->advanceSecond(1);
  729. Group* groupSourceCreated = new Group();
  730. groupSourceCreated->setName("group3");
  731. groupSourceCreated->setUuid(QUuid::createUuid());
  732. groupSourceCreated->setParent(dbSource->rootGroup());
  733. m_clock->advanceSecond(1);
  734. Merger merger(dbSource.data(), dbDestination.data());
  735. merger.merge();
  736. QPointer<Group> groupDestinationMerged = dbDestination->rootGroup()->findChildByName("group3");
  737. QVERIFY(groupDestinationMerged != nullptr);
  738. QCOMPARE(groupDestinationMerged->name(), QString("group3"));
  739. }
  740. void TestMerge::testMoveEntryIntoNewGroup()
  741. {
  742. QScopedPointer<Database> dbDestination(createTestDatabase());
  743. QScopedPointer<Database> dbSource(
  744. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  745. m_clock->advanceSecond(1);
  746. Group* groupSourceCreated = new Group();
  747. groupSourceCreated->setName("group3");
  748. groupSourceCreated->setUuid(QUuid::createUuid());
  749. groupSourceCreated->setParent(dbSource->rootGroup());
  750. QPointer<Entry> entrySourceMoved = dbSource->rootGroup()->findEntryByPath("entry1");
  751. entrySourceMoved->setGroup(groupSourceCreated);
  752. m_clock->advanceSecond(1);
  753. Merger merger(dbSource.data(), dbDestination.data());
  754. merger.merge();
  755. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  756. QPointer<Group> groupDestinationMerged = dbDestination->rootGroup()->findChildByName("group3");
  757. QVERIFY(groupDestinationMerged != nullptr);
  758. QCOMPARE(groupDestinationMerged->name(), QString("group3"));
  759. QCOMPARE(groupDestinationMerged->entries().size(), 1);
  760. QPointer<Entry> entryDestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  761. QVERIFY(entryDestinationMerged != nullptr);
  762. QCOMPARE(entryDestinationMerged->group()->name(), QString("group3"));
  763. }
  764. /**
  765. * Even though the entries' locations are no longer
  766. * the same, we will keep associating them.
  767. */
  768. void TestMerge::testUpdateEntryDifferentLocation()
  769. {
  770. QScopedPointer<Database> dbDestination(createTestDatabase());
  771. QScopedPointer<Database> dbSource(
  772. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  773. Group* groupDestinationCreated = new Group();
  774. groupDestinationCreated->setName("group3");
  775. groupDestinationCreated->setUuid(QUuid::createUuid());
  776. groupDestinationCreated->setParent(dbDestination->rootGroup());
  777. m_clock->advanceSecond(1);
  778. QPointer<Entry> entryDestinationMoved = dbDestination->rootGroup()->findEntryByPath("entry1");
  779. QVERIFY(entryDestinationMoved != nullptr);
  780. entryDestinationMoved->setGroup(groupDestinationCreated);
  781. QUuid uuidBeforeSyncing = entryDestinationMoved->uuid();
  782. QDateTime destinationLocationChanged = entryDestinationMoved->timeInfo().locationChanged();
  783. // Change the entry in the source db.
  784. m_clock->advanceSecond(1);
  785. QPointer<Entry> entrySourceMoved = dbSource->rootGroup()->findEntryByPath("entry1");
  786. QVERIFY(entrySourceMoved != nullptr);
  787. entrySourceMoved->beginUpdate();
  788. entrySourceMoved->setUsername("username");
  789. entrySourceMoved->endUpdate();
  790. QDateTime sourceLocationChanged = entrySourceMoved->timeInfo().locationChanged();
  791. QVERIFY(destinationLocationChanged > sourceLocationChanged);
  792. m_clock->advanceSecond(1);
  793. Merger merger(dbSource.data(), dbDestination.data());
  794. merger.merge();
  795. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  796. QPointer<Entry> entryDestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  797. QVERIFY(entryDestinationMerged != nullptr);
  798. QVERIFY(entryDestinationMerged->group() != nullptr);
  799. QCOMPARE(entryDestinationMerged->username(), QString("username"));
  800. QCOMPARE(entryDestinationMerged->group()->name(), QString("group3"));
  801. QCOMPARE(uuidBeforeSyncing, entryDestinationMerged->uuid());
  802. // default merge strategie is KeepNewer - therefore the older location is used!
  803. QCOMPARE(entryDestinationMerged->timeInfo().locationChanged(), sourceLocationChanged);
  804. }
  805. /**
  806. * Groups should be updated using the uuids.
  807. */
  808. void TestMerge::testUpdateGroup()
  809. {
  810. QScopedPointer<Database> dbDestination(createTestDatabase());
  811. QScopedPointer<Database> dbSource(
  812. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  813. m_clock->advanceSecond(1);
  814. QPointer<Group> groupSourceInitial = dbSource->rootGroup()->findChildByName("group2");
  815. groupSourceInitial->setName("group2 renamed");
  816. groupSourceInitial->setNotes("updated notes");
  817. QUuid customIconId = QUuid::createUuid();
  818. QImage customIcon;
  819. dbSource->metadata()->addCustomIcon(customIconId, customIcon);
  820. groupSourceInitial->setIcon(customIconId);
  821. QPointer<Entry> entrySourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  822. QVERIFY(entrySourceInitial != nullptr);
  823. entrySourceInitial->setGroup(groupSourceInitial);
  824. entrySourceInitial->setTitle("entry1 renamed");
  825. QUuid uuidBeforeSyncing = entrySourceInitial->uuid();
  826. m_clock->advanceSecond(1);
  827. Merger merger(dbSource.data(), dbDestination.data());
  828. merger.merge();
  829. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  830. QPointer<Entry> entryDestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1 renamed");
  831. QVERIFY(entryDestinationMerged != nullptr);
  832. QVERIFY(entryDestinationMerged->group() != nullptr);
  833. QCOMPARE(entryDestinationMerged->group()->name(), QString("group2 renamed"));
  834. QCOMPARE(uuidBeforeSyncing, entryDestinationMerged->uuid());
  835. QPointer<Group> groupMerged = dbDestination->rootGroup()->findChildByName("group2 renamed");
  836. QCOMPARE(groupMerged->notes(), QString("updated notes"));
  837. QCOMPARE(groupMerged->iconUuid(), customIconId);
  838. }
  839. void TestMerge::testUpdateGroupLocation()
  840. {
  841. QScopedPointer<Database> dbDestination(createTestDatabase());
  842. Group* group3DestinationCreated = new Group();
  843. QUuid group3Uuid = QUuid::createUuid();
  844. group3DestinationCreated->setUuid(group3Uuid);
  845. group3DestinationCreated->setName("group3");
  846. group3DestinationCreated->setParent(dbDestination->rootGroup()->findChildByName("group1"));
  847. QScopedPointer<Database> dbSource(
  848. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  849. // Sanity check
  850. QPointer<Group> group3SourceInitial = dbSource->rootGroup()->findGroupByUuid(group3Uuid);
  851. QVERIFY(group3DestinationCreated != nullptr);
  852. QDateTime initialLocationChanged = group3SourceInitial->timeInfo().locationChanged();
  853. m_clock->advanceSecond(1);
  854. QPointer<Group> group3SourceMoved = dbSource->rootGroup()->findGroupByUuid(group3Uuid);
  855. QVERIFY(group3SourceMoved != nullptr);
  856. group3SourceMoved->setParent(dbSource->rootGroup()->findChildByName("group2"));
  857. QDateTime movedLocaltionChanged = group3SourceMoved->timeInfo().locationChanged();
  858. QVERIFY(initialLocationChanged < movedLocaltionChanged);
  859. m_clock->advanceSecond(1);
  860. Merger merger1(dbSource.data(), dbDestination.data());
  861. merger1.merge();
  862. QPointer<Group> group3DestinationMerged1 = dbDestination->rootGroup()->findGroupByUuid(group3Uuid);
  863. QVERIFY(group3DestinationMerged1 != nullptr);
  864. QCOMPARE(group3DestinationMerged1->parent(), dbDestination->rootGroup()->findChildByName("group2"));
  865. QCOMPARE(group3DestinationMerged1->timeInfo().locationChanged(), movedLocaltionChanged);
  866. m_clock->advanceSecond(1);
  867. Merger merger2(dbSource.data(), dbDestination.data());
  868. merger2.merge();
  869. QPointer<Group> group3DestinationMerged2 = dbDestination->rootGroup()->findGroupByUuid(group3Uuid);
  870. QVERIFY(group3DestinationMerged2 != nullptr);
  871. QCOMPARE(group3DestinationMerged2->parent(), dbDestination->rootGroup()->findChildByName("group2"));
  872. QCOMPARE(group3DestinationMerged1->timeInfo().locationChanged(), movedLocaltionChanged);
  873. }
  874. /**
  875. * The first merge should create new entries, the
  876. * second should only sync them, since they have
  877. * been created with the same UUIDs.
  878. */
  879. void TestMerge::testMergeAndSync()
  880. {
  881. QScopedPointer<Database> dbDestination(new Database());
  882. QScopedPointer<Database> dbSource(createTestDatabase());
  883. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 0);
  884. m_clock->advanceSecond(1);
  885. Merger merger1(dbSource.data(), dbDestination.data());
  886. merger1.merge();
  887. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  888. m_clock->advanceSecond(1);
  889. Merger merger2(dbSource.data(), dbDestination.data());
  890. merger2.merge();
  891. // Still only 2 entries, since now we detect which are already present.
  892. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  893. }
  894. /**
  895. * Custom icons should be brought over when merging.
  896. */
  897. void TestMerge::testMergeCustomIcons()
  898. {
  899. QScopedPointer<Database> dbDestination(new Database());
  900. QScopedPointer<Database> dbSource(createTestDatabase());
  901. m_clock->advanceSecond(1);
  902. QUuid customIconId = QUuid::createUuid();
  903. QImage customIcon;
  904. dbSource->metadata()->addCustomIcon(customIconId, customIcon);
  905. // Sanity check.
  906. QVERIFY(dbSource->metadata()->hasCustomIcon(customIconId));
  907. m_clock->advanceSecond(1);
  908. Merger merger(dbSource.data(), dbDestination.data());
  909. merger.merge();
  910. QVERIFY(dbDestination->metadata()->hasCustomIcon(customIconId));
  911. }
  912. /**
  913. * No duplicate icons should be created
  914. */
  915. void TestMerge::testMergeDuplicateCustomIcons()
  916. {
  917. QScopedPointer<Database> dbDestination(new Database());
  918. QScopedPointer<Database> dbSource(createTestDatabase());
  919. m_clock->advanceSecond(1);
  920. QUuid customIconId = QUuid::createUuid();
  921. QImage customIcon;
  922. dbSource->metadata()->addCustomIcon(customIconId, customIcon);
  923. dbDestination->metadata()->addCustomIcon(customIconId, customIcon);
  924. // Sanity check.
  925. QVERIFY(dbSource->metadata()->hasCustomIcon(customIconId));
  926. QVERIFY(dbDestination->metadata()->hasCustomIcon(customIconId));
  927. m_clock->advanceSecond(1);
  928. Merger merger(dbSource.data(), dbDestination.data());
  929. merger.merge();
  930. QVERIFY(dbDestination->metadata()->hasCustomIcon(customIconId));
  931. QCOMPARE(dbDestination->metadata()->customIconsOrder().count(), 1);
  932. }
  933. void TestMerge::testMetadata()
  934. {
  935. QSKIP("Sophisticated merging for Metadata not implemented");
  936. // TODO HNH: I think a merge of recycle bins would be nice since duplicating them
  937. // is not really a good solution - the one to use as final recycle bin
  938. // is determined by the merge method - if only one has a bin, this one
  939. // will be used - exception is the target has no recycle bin activated
  940. }
  941. void TestMerge::testCustomData()
  942. {
  943. QScopedPointer<Database> dbDestination(new Database());
  944. QScopedPointer<Database> dbSource(createTestDatabase());
  945. QScopedPointer<Database> dbDestination2(new Database());
  946. QScopedPointer<Database> dbSource2(createTestDatabase());
  947. m_clock->advanceSecond(1);
  948. dbDestination->metadata()->customData()->set("toBeDeleted", "value");
  949. dbDestination->metadata()->customData()->set("key3", "oldValue");
  950. dbSource2->metadata()->customData()->set("key1", "value1");
  951. dbSource2->metadata()->customData()->set("key2", "value2");
  952. dbSource2->metadata()->customData()->set("key3", "newValue");
  953. dbSource2->metadata()->customData()->set("Browser", "n'8=3W@L^6d->d.]St_>]");
  954. m_clock->advanceSecond(1);
  955. dbSource->metadata()->customData()->set("key1", "value1");
  956. dbSource->metadata()->customData()->set("key2", "value2");
  957. dbSource->metadata()->customData()->set("key3", "newValue");
  958. dbSource->metadata()->customData()->set("Browser", "n'8=3W@L^6d->d.]St_>]");
  959. dbDestination2->metadata()->customData()->set("notToBeDeleted", "value");
  960. dbDestination2->metadata()->customData()->set("key3", "oldValue");
  961. // Sanity check.
  962. QVERIFY(!dbSource->metadata()->customData()->isEmpty());
  963. QVERIFY(!dbSource2->metadata()->customData()->isEmpty());
  964. m_clock->advanceSecond(1);
  965. Merger merger(dbSource.data(), dbDestination.data());
  966. QStringList changes = merger.merge();
  967. QVERIFY(!changes.isEmpty());
  968. // Source is newer, data should be merged
  969. QVERIFY(!dbDestination->metadata()->customData()->isEmpty());
  970. QVERIFY(dbDestination->metadata()->customData()->contains("key1"));
  971. QVERIFY(dbDestination->metadata()->customData()->contains("key2"));
  972. QVERIFY(dbDestination->metadata()->customData()->contains("Browser"));
  973. QVERIFY(!dbDestination->metadata()->customData()->contains("toBeDeleted"));
  974. QCOMPARE(dbDestination->metadata()->customData()->value("key1"), QString("value1"));
  975. QCOMPARE(dbDestination->metadata()->customData()->value("key2"), QString("value2"));
  976. QCOMPARE(dbDestination->metadata()->customData()->value("Browser"), QString("n'8=3W@L^6d->d.]St_>]"));
  977. QCOMPARE(dbDestination->metadata()->customData()->value("key3"),
  978. QString("newValue")); // Old value should be replaced
  979. // Merging again should not do anything if the values are the same.
  980. m_clock->advanceSecond(1);
  981. dbSource->metadata()->customData()->set("key3", "oldValue");
  982. dbSource->metadata()->customData()->set("key3", "newValue");
  983. Merger merger2(dbSource.data(), dbDestination.data());
  984. QStringList changes2 = merger2.merge();
  985. QVERIFY(changes2.isEmpty());
  986. Merger merger3(dbSource2.data(), dbDestination2.data());
  987. merger3.merge();
  988. // Target is newer, no data is merged
  989. QVERIFY(!dbDestination2->metadata()->customData()->isEmpty());
  990. QVERIFY(!dbDestination2->metadata()->customData()->contains("key1"));
  991. QVERIFY(!dbDestination2->metadata()->customData()->contains("key2"));
  992. QVERIFY(!dbDestination2->metadata()->customData()->contains("Browser"));
  993. QVERIFY(dbDestination2->metadata()->customData()->contains("notToBeDeleted"));
  994. QCOMPARE(dbDestination2->metadata()->customData()->value("key3"),
  995. QString("oldValue")); // Old value should not be replaced
  996. }
  997. void TestMerge::testDeletedEntry()
  998. {
  999. QScopedPointer<Database> dbDestination(createTestDatabase());
  1000. QScopedPointer<Database> dbSource(
  1001. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  1002. m_clock->advanceSecond(1);
  1003. QPointer<Entry> entry1SourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  1004. QVERIFY(entry1SourceInitial != nullptr);
  1005. QUuid entry1Uuid = entry1SourceInitial->uuid();
  1006. delete entry1SourceInitial;
  1007. QVERIFY(dbSource->containsDeletedObject(entry1Uuid));
  1008. m_clock->advanceSecond(1);
  1009. QPointer<Entry> entry2DestinationInitial = dbDestination->rootGroup()->findEntryByPath("entry2");
  1010. QVERIFY(entry2DestinationInitial != nullptr);
  1011. QUuid entry2Uuid = entry2DestinationInitial->uuid();
  1012. delete entry2DestinationInitial;
  1013. QVERIFY(dbDestination->containsDeletedObject(entry2Uuid));
  1014. m_clock->advanceSecond(1);
  1015. Merger merger(dbSource.data(), dbDestination.data());
  1016. merger.merge();
  1017. QPointer<Entry> entry1DestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  1018. QVERIFY(entry1DestinationMerged);
  1019. QVERIFY(!dbDestination->containsDeletedObject(entry1Uuid));
  1020. QPointer<Entry> entry2DestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry2");
  1021. QVERIFY(entry2DestinationMerged);
  1022. // Uuid in db and deletedObjects is intended according to KeePass #1752
  1023. QVERIFY(dbDestination->containsDeletedObject(entry2Uuid));
  1024. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 2);
  1025. }
  1026. void TestMerge::testDeletedGroup()
  1027. {
  1028. QScopedPointer<Database> dbDestination(createTestDatabase());
  1029. QScopedPointer<Database> dbSource(
  1030. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  1031. m_clock->advanceSecond(1);
  1032. QPointer<Group> group2DestinationInitial = dbDestination->rootGroup()->findChildByName("group2");
  1033. QVERIFY(group2DestinationInitial != nullptr);
  1034. Entry* entry3DestinationCreated = new Entry();
  1035. entry3DestinationCreated->beginUpdate();
  1036. entry3DestinationCreated->setUuid(QUuid::createUuid());
  1037. entry3DestinationCreated->setGroup(group2DestinationInitial);
  1038. entry3DestinationCreated->setTitle("entry3");
  1039. entry3DestinationCreated->endUpdate();
  1040. m_clock->advanceSecond(1);
  1041. QPointer<Group> group1SourceInitial = dbSource->rootGroup()->findChildByName("group1");
  1042. QVERIFY(group1SourceInitial != nullptr);
  1043. QPointer<Entry> entry1SourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  1044. QVERIFY(entry1SourceInitial != nullptr);
  1045. QPointer<Entry> entry2SourceInitial = dbSource->rootGroup()->findEntryByPath("entry2");
  1046. QVERIFY(entry2SourceInitial != nullptr);
  1047. QUuid group1Uuid = group1SourceInitial->uuid();
  1048. QUuid entry1Uuid = entry1SourceInitial->uuid();
  1049. QUuid entry2Uuid = entry2SourceInitial->uuid();
  1050. delete group1SourceInitial;
  1051. QVERIFY(dbSource->containsDeletedObject(group1Uuid));
  1052. QVERIFY(dbSource->containsDeletedObject(entry1Uuid));
  1053. QVERIFY(dbSource->containsDeletedObject(entry2Uuid));
  1054. m_clock->advanceSecond(1);
  1055. QPointer<Group> group2SourceInitial = dbSource->rootGroup()->findChildByName("group2");
  1056. QVERIFY(group2SourceInitial != nullptr);
  1057. QUuid group2Uuid = group2SourceInitial->uuid();
  1058. delete group2SourceInitial;
  1059. QVERIFY(dbSource->containsDeletedObject(group2Uuid));
  1060. m_clock->advanceSecond(1);
  1061. Merger merger(dbSource.data(), dbDestination.data());
  1062. merger.merge();
  1063. QVERIFY(!dbDestination->containsDeletedObject(group1Uuid));
  1064. QVERIFY(!dbDestination->containsDeletedObject(entry1Uuid));
  1065. QVERIFY(!dbDestination->containsDeletedObject(entry2Uuid));
  1066. QVERIFY(!dbDestination->containsDeletedObject(group2Uuid));
  1067. QPointer<Entry> entry1DestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  1068. QVERIFY(entry1DestinationMerged);
  1069. QPointer<Entry> entry2DestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry2");
  1070. QVERIFY(entry2DestinationMerged);
  1071. QPointer<Entry> entry3DestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry3");
  1072. QVERIFY(entry3DestinationMerged);
  1073. QPointer<Group> group1DestinationMerged = dbDestination->rootGroup()->findChildByName("group1");
  1074. QVERIFY(group1DestinationMerged);
  1075. QPointer<Group> group2DestinationMerged = dbDestination->rootGroup()->findChildByName("group2");
  1076. QVERIFY(group2DestinationMerged);
  1077. QCOMPARE(dbDestination->rootGroup()->entriesRecursive().size(), 3);
  1078. }
  1079. void TestMerge::testDeletedRevertedEntry()
  1080. {
  1081. QScopedPointer<Database> dbDestination(createTestDatabase());
  1082. QScopedPointer<Database> dbSource(
  1083. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  1084. m_clock->advanceSecond(1);
  1085. QPointer<Entry> entry1DestinationInitial = dbDestination->rootGroup()->findEntryByPath("entry1");
  1086. QVERIFY(entry1DestinationInitial != nullptr);
  1087. QUuid entry1Uuid = entry1DestinationInitial->uuid();
  1088. delete entry1DestinationInitial;
  1089. QVERIFY(dbDestination->containsDeletedObject(entry1Uuid));
  1090. m_clock->advanceSecond(1);
  1091. QPointer<Entry> entry2SourceInitial = dbSource->rootGroup()->findEntryByPath("entry2");
  1092. QVERIFY(entry2SourceInitial != nullptr);
  1093. QUuid entry2Uuid = entry2SourceInitial->uuid();
  1094. delete entry2SourceInitial;
  1095. QVERIFY(dbSource->containsDeletedObject(entry2Uuid));
  1096. m_clock->advanceSecond(1);
  1097. QPointer<Entry> entry1SourceInitial = dbSource->rootGroup()->findEntryByPath("entry1");
  1098. QVERIFY(entry1SourceInitial != nullptr);
  1099. entry1SourceInitial->setNotes("Updated");
  1100. QPointer<Entry> entry2DestinationInitial = dbDestination->rootGroup()->findEntryByPath("entry2");
  1101. QVERIFY(entry2DestinationInitial != nullptr);
  1102. entry2DestinationInitial->setNotes("Updated");
  1103. Merger merger(dbSource.data(), dbDestination.data());
  1104. merger.merge();
  1105. // Uuid in db and deletedObjects is intended according to KeePass #1752
  1106. QVERIFY(dbDestination->containsDeletedObject(entry1Uuid));
  1107. QVERIFY(!dbDestination->containsDeletedObject(entry2Uuid));
  1108. QPointer<Entry> entry1DestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry1");
  1109. QVERIFY(entry1DestinationMerged);
  1110. QVERIFY(entry1DestinationMerged->notes() == "Updated");
  1111. QPointer<Entry> entry2DestinationMerged = dbDestination->rootGroup()->findEntryByPath("entry2");
  1112. QVERIFY(entry2DestinationMerged);
  1113. QVERIFY(entry2DestinationMerged->notes() == "Updated");
  1114. }
  1115. void TestMerge::testDeletedRevertedGroup()
  1116. {
  1117. QScopedPointer<Database> dbDestination(createTestDatabase());
  1118. QScopedPointer<Database> dbSource(
  1119. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  1120. m_clock->advanceSecond(1);
  1121. QPointer<Group> group2SourceInitial = dbSource->rootGroup()->findChildByName("group2");
  1122. QVERIFY(group2SourceInitial);
  1123. QUuid group2Uuid = group2SourceInitial->uuid();
  1124. delete group2SourceInitial;
  1125. QVERIFY(dbSource->containsDeletedObject(group2Uuid));
  1126. m_clock->advanceSecond(1);
  1127. QPointer<Group> group1DestinationInitial = dbDestination->rootGroup()->findChildByName("group1");
  1128. QVERIFY(group1DestinationInitial);
  1129. QUuid group1Uuid = group1DestinationInitial->uuid();
  1130. delete group1DestinationInitial;
  1131. QVERIFY(dbDestination->containsDeletedObject(group1Uuid));
  1132. m_clock->advanceSecond(1);
  1133. QPointer<Group> group1SourceInitial = dbSource->rootGroup()->findChildByName("group1");
  1134. QVERIFY(group1SourceInitial);
  1135. group1SourceInitial->setNotes("Updated");
  1136. m_clock->advanceSecond(1);
  1137. QPointer<Group> group2DestinationInitial = dbDestination->rootGroup()->findChildByName("group2");
  1138. QVERIFY(group2DestinationInitial);
  1139. group2DestinationInitial->setNotes("Updated");
  1140. m_clock->advanceSecond(1);
  1141. Merger merger(dbSource.data(), dbDestination.data());
  1142. merger.merge();
  1143. // Uuid in db and deletedObjects is intended according to KeePass #1752
  1144. QVERIFY(dbDestination->containsDeletedObject(group1Uuid));
  1145. QVERIFY(!dbDestination->containsDeletedObject(group2Uuid));
  1146. QPointer<Group> group1DestinationMerged = dbDestination->rootGroup()->findChildByName("group1");
  1147. QVERIFY(group1DestinationMerged);
  1148. QVERIFY(group1DestinationMerged->notes() == "Updated");
  1149. QPointer<Group> group2DestinationMerged = dbDestination->rootGroup()->findChildByName("group2");
  1150. QVERIFY(group2DestinationMerged);
  1151. QVERIFY(group2DestinationMerged->notes() == "Updated");
  1152. }
  1153. /**
  1154. * If the group is updated in the source database, and the
  1155. * destination database after, the group should remain the
  1156. * same.
  1157. */
  1158. void TestMerge::testResolveGroupConflictOlder()
  1159. {
  1160. QScopedPointer<Database> dbDestination(createTestDatabase());
  1161. QScopedPointer<Database> dbSource(
  1162. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  1163. // sanity check
  1164. QPointer<Group> groupSourceInitial = dbSource->rootGroup()->findChildByName("group1");
  1165. QVERIFY(groupSourceInitial != nullptr);
  1166. // Make sure the two changes have a different timestamp.
  1167. m_clock->advanceSecond(1);
  1168. groupSourceInitial->setName("group1 updated in source");
  1169. // Make sure the two changes have a different timestamp.
  1170. m_clock->advanceSecond(1);
  1171. QPointer<Group> groupDestinationUpdated = dbDestination->rootGroup()->findChildByName("group1");
  1172. groupDestinationUpdated->setName("group1 updated in destination");
  1173. m_clock->advanceSecond(1);
  1174. Merger merger(dbSource.data(), dbDestination.data());
  1175. merger.merge();
  1176. // sanity check
  1177. QPointer<Group> groupDestinationMerged =
  1178. dbDestination->rootGroup()->findChildByName("group1 updated in destination");
  1179. QVERIFY(groupDestinationMerged != nullptr);
  1180. }
  1181. void TestMerge::testMergeNotModified()
  1182. {
  1183. QScopedPointer<Database> dbDestination(createTestDatabase());
  1184. QScopedPointer<Database> dbSource(
  1185. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  1186. QSignalSpy modifiedSignalSpy(dbDestination.data(), SIGNAL(databaseModified()));
  1187. Merger merger(dbSource.data(), dbDestination.data());
  1188. merger.merge();
  1189. QTRY_VERIFY(modifiedSignalSpy.empty());
  1190. }
  1191. void TestMerge::testMergeModified()
  1192. {
  1193. QScopedPointer<Database> dbDestination(createTestDatabase());
  1194. QScopedPointer<Database> dbSource(
  1195. createTestDatabaseStructureClone(dbDestination.data(), Entry::CloneNoFlags, Group::CloneIncludeEntries));
  1196. QSignalSpy modifiedSignalSpy(dbDestination.data(), SIGNAL(databaseModified()));
  1197. // Make sure the two changes have a different timestamp.
  1198. QTest::qSleep(1);
  1199. Entry* entry = dbSource->rootGroup()->findEntryByPath("entry1");
  1200. entry->beginUpdate();
  1201. entry->setTitle("new title");
  1202. entry->endUpdate();
  1203. Merger merger(dbSource.data(), dbDestination.data());
  1204. merger.merge();
  1205. QTRY_VERIFY(!modifiedSignalSpy.empty());
  1206. }
  1207. Database* TestMerge::createTestDatabase()
  1208. {
  1209. Database* db = new Database();
  1210. Group* group1 = new Group();
  1211. group1->setName("group1");
  1212. group1->setUuid(QUuid::createUuid());
  1213. Group* group2 = new Group();
  1214. group2->setName("group2");
  1215. group2->setUuid(QUuid::createUuid());
  1216. Entry* entry1 = new Entry();
  1217. entry1->setUuid(QUuid::createUuid());
  1218. Entry* entry2 = new Entry();
  1219. entry2->setUuid(QUuid::createUuid());
  1220. m_clock->advanceYear(1);
  1221. // Give Entry 1 a history
  1222. entry1->beginUpdate();
  1223. entry1->setGroup(group1);
  1224. entry1->setTitle("entry1");
  1225. entry1->endUpdate();
  1226. // Give Entry 2 a history
  1227. entry2->beginUpdate();
  1228. entry2->setGroup(group1);
  1229. entry2->setTitle("entry2");
  1230. entry2->endUpdate();
  1231. group1->setParent(db->rootGroup());
  1232. group2->setParent(db->rootGroup());
  1233. return db;
  1234. }
  1235. Database* TestMerge::createTestDatabaseStructureClone(Database* source, int entryFlags, int groupFlags)
  1236. {
  1237. Database* db = new Database();
  1238. // the old root group is deleted by QObject::parent relationship
  1239. db->setRootGroup(source->rootGroup()->clone(static_cast<Entry::CloneFlag>(entryFlags),
  1240. static_cast<Group::CloneFlag>(groupFlags)));
  1241. return db;
  1242. }