TestDeletedObjects.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
  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 "TestDeletedObjects.h"
  18. #include "config-keepassx-tests.h"
  19. #include "core/Group.h"
  20. #include "crypto/Crypto.h"
  21. #include "format/KdbxXmlReader.h"
  22. #include "format/KeePass2.h"
  23. #include <QTest>
  24. QTEST_GUILESS_MAIN(TestDeletedObjects)
  25. void TestDeletedObjects::initTestCase()
  26. {
  27. QVERIFY(Crypto::init());
  28. }
  29. void TestDeletedObjects::createAndDelete(QSharedPointer<Database> db, int delObjectsSize)
  30. {
  31. QCOMPARE(db->deletedObjects().size(), delObjectsSize);
  32. Group* root = db->rootGroup();
  33. int rootChildrenCount = root->children().size();
  34. auto g = new Group();
  35. g->setParent(root);
  36. QUuid gUuid = QUuid::createUuid();
  37. g->setUuid(gUuid);
  38. delete g;
  39. QCOMPARE(db->deletedObjects().size(), ++delObjectsSize);
  40. QCOMPARE(db->deletedObjects().at(delObjectsSize - 1).uuid, gUuid);
  41. QCOMPARE(rootChildrenCount, root->children().size());
  42. auto g1 = new Group();
  43. g1->setParent(root);
  44. QUuid g1Uuid = QUuid::createUuid();
  45. g1->setUuid(g1Uuid);
  46. auto e1 = new Entry();
  47. e1->setGroup(g1);
  48. QUuid e1Uuid = QUuid::createUuid();
  49. e1->setUuid(e1Uuid);
  50. auto g2 = new Group();
  51. g2->setParent(g1);
  52. QUuid g2Uuid = QUuid::createUuid();
  53. g2->setUuid(g2Uuid);
  54. auto e2 = new Entry();
  55. e2->setGroup(g2);
  56. QUuid e2Uuid = QUuid::createUuid();
  57. e2->setUuid(e2Uuid);
  58. delete g1;
  59. delObjectsSize += 4;
  60. QCOMPARE(db->deletedObjects().size(), delObjectsSize);
  61. QCOMPARE(db->deletedObjects().at(delObjectsSize - 4).uuid, e1Uuid);
  62. QCOMPARE(db->deletedObjects().at(delObjectsSize - 3).uuid, e2Uuid);
  63. QCOMPARE(db->deletedObjects().at(delObjectsSize - 2).uuid, g2Uuid);
  64. QCOMPARE(db->deletedObjects().at(delObjectsSize - 1).uuid, g1Uuid);
  65. QCOMPARE(rootChildrenCount, root->children().size());
  66. auto e3 = new Entry();
  67. e3->setGroup(root);
  68. QUuid e3Uuid = QUuid::createUuid();
  69. e3->setUuid(e3Uuid);
  70. delete e3;
  71. QCOMPARE(db->deletedObjects().size(), ++delObjectsSize);
  72. QCOMPARE(db->deletedObjects().at(delObjectsSize - 1).uuid, e3Uuid);
  73. QCOMPARE(rootChildrenCount, root->children().size());
  74. }
  75. void TestDeletedObjects::testDeletedObjectsFromFile()
  76. {
  77. KdbxXmlReader reader(KeePass2::FILE_VERSION_3_1);
  78. reader.setStrictMode(true);
  79. QString xmlFile = QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml");
  80. auto db = reader.readDatabase(xmlFile);
  81. createAndDelete(db, 2);
  82. }
  83. void TestDeletedObjects::testDeletedObjectsFromNewDb()
  84. {
  85. auto db = QSharedPointer<Database>::create();
  86. createAndDelete(db, 0);
  87. }
  88. void TestDeletedObjects::testDatabaseChange()
  89. {
  90. auto db = QSharedPointer<Database>::create();
  91. Group* root = db->rootGroup();
  92. int delObjectsSize = 0;
  93. auto db2 = QSharedPointer<Database>::create();
  94. Group* root2 = db2->rootGroup();
  95. int delObjectsSize2 = 0;
  96. auto* e = new Entry();
  97. e->setGroup(root);
  98. QCOMPARE(db->deletedObjects().size(), delObjectsSize);
  99. QCOMPARE(db2->deletedObjects().size(), delObjectsSize2);
  100. e->setGroup(root2);
  101. QCOMPARE(db->deletedObjects().size(), ++delObjectsSize);
  102. QCOMPARE(db2->deletedObjects().size(), delObjectsSize2);
  103. delete e;
  104. QCOMPARE(db->deletedObjects().size(), delObjectsSize);
  105. QCOMPARE(db2->deletedObjects().size(), ++delObjectsSize2);
  106. auto* g1 = new Group();
  107. g1->setParent(root);
  108. QUuid g1Uuid = QUuid::createUuid();
  109. g1->setUuid(g1Uuid);
  110. auto* e1 = new Entry();
  111. e1->setGroup(g1);
  112. QUuid e1Uuid = QUuid::createUuid();
  113. e1->setUuid(e1Uuid);
  114. g1->setParent(root2);
  115. delObjectsSize += 2;
  116. QCOMPARE(db->deletedObjects().size(), delObjectsSize);
  117. QCOMPARE(db2->deletedObjects().size(), delObjectsSize2);
  118. QCOMPARE(db->deletedObjects().at(delObjectsSize - 2).uuid, e1Uuid);
  119. QCOMPARE(db->deletedObjects().at(delObjectsSize - 1).uuid, g1Uuid);
  120. auto* group = new Group();
  121. auto* entry = new Entry();
  122. entry->setGroup(group);
  123. entry->setGroup(root);
  124. QCOMPARE(db->deletedObjects().size(), delObjectsSize);
  125. QCOMPARE(db2->deletedObjects().size(), delObjectsSize2);
  126. delete group;
  127. }
  128. void TestDeletedObjects::testCustomIconDeletion()
  129. {
  130. Database db;
  131. QCOMPARE(db.deletedObjects().size(), 0);
  132. QUuid uuid = QUuid::createUuid();
  133. db.metadata()->addCustomIcon(uuid, QByteArray());
  134. db.metadata()->removeCustomIcon(uuid);
  135. QCOMPARE(db.deletedObjects().size(), 1);
  136. QCOMPARE(db.deletedObjects().at(0).uuid, uuid);
  137. }