TestKdbx2.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2018 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 "TestKdbx2.h"
  18. #include "config-keepassx-tests.h"
  19. #include "core/Group.h"
  20. #include "core/Metadata.h"
  21. #include "crypto/Crypto.h"
  22. #include "format/KeePass2Reader.h"
  23. #include "format/KeePass2Writer.h"
  24. #include "keys/PasswordKey.h"
  25. #include <QBuffer>
  26. #include <QTest>
  27. QTEST_GUILESS_MAIN(TestKdbx2)
  28. void TestKdbx2::initTestCase()
  29. {
  30. QVERIFY(Crypto::init());
  31. }
  32. /**
  33. * Helper method for verifying contents of the sample KDBX 2 file.
  34. */
  35. void TestKdbx2::verifyKdbx2Db(QSharedPointer<Database> db)
  36. {
  37. QVERIFY(db);
  38. QCOMPARE(db->rootGroup()->name(), QString("Format200"));
  39. QVERIFY(!db->metadata()->protectTitle());
  40. QVERIFY(db->metadata()->protectUsername());
  41. QVERIFY(!db->metadata()->protectPassword());
  42. QVERIFY(db->metadata()->protectUrl());
  43. QVERIFY(!db->metadata()->protectNotes());
  44. QCOMPARE(db->rootGroup()->entries().size(), 1);
  45. auto entry = db->rootGroup()->entries().at(0);
  46. QCOMPARE(entry->title(), QString("Sample Entry"));
  47. QCOMPARE(entry->username(), QString("User Name"));
  48. QCOMPARE(entry->attachments()->keys().size(), 2);
  49. QCOMPARE(entry->attachments()->value("myattach.txt"), QByteArray("abcdefghijk"));
  50. QCOMPARE(entry->attachments()->value("test.txt"), QByteArray("this is a test"));
  51. QCOMPARE(entry->historyItems().size(), 2);
  52. QCOMPARE(entry->historyItems().at(0)->attachments()->keys().size(), 0);
  53. QCOMPARE(entry->historyItems().at(1)->attachments()->keys().size(), 1);
  54. QCOMPARE(entry->historyItems().at(1)->attachments()->value("myattach.txt"), QByteArray("abcdefghijk"));
  55. }
  56. void TestKdbx2::testFormat200()
  57. {
  58. QString filename = QString(KEEPASSX_TEST_DATA_DIR).append("/Format200.kdbx");
  59. auto key = QSharedPointer<CompositeKey>::create();
  60. key->addKey(QSharedPointer<PasswordKey>::create("a"));
  61. auto db = QSharedPointer<Database>::create();
  62. KeePass2Reader reader;
  63. QVERIFY(reader.readDatabase(filename, key, db.data()));
  64. QCOMPARE(reader.version(), KeePass2::FILE_VERSION_2 & KeePass2::FILE_VERSION_CRITICAL_MASK);
  65. QVERIFY2(!reader.hasError(), reader.errorString().toStdString().c_str());
  66. verifyKdbx2Db(db);
  67. }
  68. void TestKdbx2::testFormat200Upgrade()
  69. {
  70. QString filename = QString(KEEPASSX_TEST_DATA_DIR).append("/Format200.kdbx");
  71. auto key = QSharedPointer<CompositeKey>::create();
  72. key->addKey(QSharedPointer<PasswordKey>::create("a"));
  73. auto db = QSharedPointer<Database>::create();
  74. KeePass2Reader reader;
  75. reader.readDatabase(filename, key, db.data());
  76. QVERIFY2(!reader.hasError(), reader.errorString().toStdString().c_str());
  77. QVERIFY(!db.isNull());
  78. QCOMPARE(reader.version(), KeePass2::FILE_VERSION_2);
  79. QCOMPARE(db->kdf()->uuid(), KeePass2::KDF_AES_KDBX3);
  80. QBuffer buffer;
  81. buffer.open(QBuffer::ReadWrite);
  82. // write KDBX 3 to upgrade it
  83. KeePass2Writer writer;
  84. QVERIFY(writer.writeDatabase(&buffer, db.data()));
  85. if (writer.hasError()) {
  86. QFAIL(qPrintable(QString("Error while writing database: %1").arg(writer.errorString())));
  87. }
  88. // read buffer back
  89. buffer.seek(0);
  90. auto targetDb = QSharedPointer<Database>::create();
  91. QVERIFY(reader.readDatabase(&buffer, key, targetDb.data()));
  92. if (reader.hasError()) {
  93. QFAIL(qPrintable(QString("Error while reading database: %1").arg(reader.errorString())));
  94. }
  95. // database should now be upgraded to KDBX 3 without data loss
  96. verifyKdbx2Db(targetDb);
  97. QCOMPARE(reader.version(), KeePass2::FILE_VERSION_3_1);
  98. QCOMPARE(targetDb->kdf()->uuid(), KeePass2::KDF_AES_KDBX3);
  99. }