TestKeePass1Reader.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2010 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 "TestKeePass1Reader.h"
  18. #include <QBuffer>
  19. #include <QTest>
  20. #include "config-keepassx-tests.h"
  21. #include "core/Group.h"
  22. #include "core/Metadata.h"
  23. #include "crypto/Crypto.h"
  24. #include "format/KeePass1Reader.h"
  25. #include "format/KeePass2Reader.h"
  26. #include "format/KeePass2Writer.h"
  27. #include "keys/FileKey.h"
  28. #include "keys/PasswordKey.h"
  29. QTEST_GUILESS_MAIN(TestKeePass1Reader)
  30. void TestKeePass1Reader::initTestCase()
  31. {
  32. QVERIFY(Crypto::init());
  33. QString filename = QString(KEEPASSX_TEST_DATA_DIR).append("/basic.kdb");
  34. KeePass1Reader reader;
  35. m_db = reader.readDatabase(filename, "masterpw", nullptr);
  36. QVERIFY(m_db);
  37. QVERIFY(!reader.hasError());
  38. }
  39. void TestKeePass1Reader::testBasic()
  40. {
  41. QVERIFY(m_db->deletedObjects().isEmpty());
  42. QCOMPARE(m_db->rootGroup()->children().size(), 2);
  43. Group* group1 = m_db->rootGroup()->children().at(0);
  44. QVERIFY(!group1->uuid().isNull());
  45. QCOMPARE(group1->name(), QString("Internet"));
  46. QCOMPARE(group1->children().size(), 2);
  47. QCOMPARE(group1->entries().size(), 2);
  48. QCOMPARE(group1->iconNumber(), 1);
  49. Entry* entry11 = group1->entries().at(0);
  50. QVERIFY(!entry11->uuid().isNull());
  51. QCOMPARE(entry11->title(), QString("Test entry"));
  52. QCOMPARE(entry11->iconNumber(), 1);
  53. QCOMPARE(entry11->username(), QString("I"));
  54. QCOMPARE(entry11->url(), QString("http://example.com/"));
  55. QCOMPARE(entry11->password(), QString("secretpassword"));
  56. QCOMPARE(entry11->notes(), QString("Lorem ipsum\ndolor sit amet"));
  57. QVERIFY(entry11->timeInfo().expires());
  58. QCOMPARE(entry11->timeInfo().expiryTime(), genDT(2012, 5, 9, 10, 32));
  59. QCOMPARE(entry11->attachments()->keys().size(), 1);
  60. QCOMPARE(entry11->attachments()->keys().at(0), QString("attachment.txt"));
  61. QCOMPARE(entry11->attachments()->value("attachment.txt"), QByteArray("hello world\n"));
  62. Entry* entry12 = group1->entries().at(1);
  63. QCOMPARE(entry12->title(), QString(""));
  64. QCOMPARE(entry12->iconNumber(), 0);
  65. QCOMPARE(entry12->username(), QString(""));
  66. QCOMPARE(entry12->url(), QString(""));
  67. QCOMPARE(entry12->password(), QString(""));
  68. QCOMPARE(entry12->notes(), QString(""));
  69. QVERIFY(!entry12->timeInfo().expires());
  70. QCOMPARE(entry12->attachments()->keys().size(), 0);
  71. Group* group11 = group1->children().at(0);
  72. QCOMPARE(group11->name(), QString("Subgroup 1"));
  73. QCOMPARE(group11->children().size(), 1);
  74. Group* group111 = group11->children().at(0);
  75. QCOMPARE(group111->name(), QString("Unexpanded"));
  76. QCOMPARE(group111->children().size(), 1);
  77. Group* group1111 = group111->children().at(0);
  78. QCOMPARE(group1111->name(), QString("abc"));
  79. QCOMPARE(group1111->children().size(), 0);
  80. Group* group12 = group1->children().at(1);
  81. QCOMPARE(group12->name(), QString("Subgroup 2"));
  82. QCOMPARE(group12->children().size(), 0);
  83. Group* group2 = m_db->rootGroup()->children().at(1);
  84. QCOMPARE(group2->name(), QString("eMail"));
  85. QCOMPARE(group2->entries().size(), 1);
  86. QCOMPARE(group2->iconNumber(), 19);
  87. reopenDatabase(m_db, "masterpw", QString());
  88. }
  89. void TestKeePass1Reader::testMasterKey()
  90. {
  91. QVERIFY(m_db->isInitialized());
  92. QCOMPARE(m_db->kdf()->rounds(), 713);
  93. }
  94. void TestKeePass1Reader::testCustomIcons()
  95. {
  96. QCOMPARE(m_db->metadata()->customIconsOrder().size(), 1);
  97. QUuid uuid = m_db->metadata()->customIconsOrder().at(0);
  98. QVERIFY(m_db->metadata()->hasCustomIcon(uuid));
  99. QByteArray icon = m_db->metadata()->customIcon(uuid).data;
  100. QVERIFY(icon.startsWith(
  101. "\x89PNG\r\n\x1A\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\b\x06\x00\x00\x00\x1F\xF3\xFF"));
  102. }
  103. void TestKeePass1Reader::testGroupExpanded()
  104. {
  105. QCOMPARE(m_db->rootGroup()->children().at(0)->isExpanded(), true);
  106. QCOMPARE(m_db->rootGroup()->children().at(0)->children().at(0)->isExpanded(), true);
  107. QCOMPARE(m_db->rootGroup()->children().at(0)->children().at(0)->children().at(0)->isExpanded(), false);
  108. }
  109. void TestKeePass1Reader::testAutoType()
  110. {
  111. Group* group = m_db->rootGroup()->children().at(0)->children().at(0);
  112. QCOMPARE(group->entries().size(), 2);
  113. Entry* entry1 = group->entries().at(0);
  114. QCOMPARE(entry1->notes(), QString("last line"));
  115. QCOMPARE(entry1->defaultAutoTypeSequence(), QString("{USERNAME}{ENTER}"));
  116. QCOMPARE(entry1->autoTypeAssociations()->size(), 5);
  117. QCOMPARE(entry1->autoTypeAssociations()->get(0).sequence, QString(""));
  118. QCOMPARE(entry1->autoTypeAssociations()->get(0).window, QString("a window"));
  119. QCOMPARE(entry1->autoTypeAssociations()->get(1).sequence, QString(""));
  120. QCOMPARE(entry1->autoTypeAssociations()->get(1).window, QString("a second window"));
  121. QCOMPARE(entry1->autoTypeAssociations()->get(2).sequence, QString("{PASSWORD}{ENTER}"));
  122. QCOMPARE(entry1->autoTypeAssociations()->get(2).window, QString("Window Nr 1a"));
  123. QCOMPARE(entry1->autoTypeAssociations()->get(3).sequence, QString("{PASSWORD}{ENTER}"));
  124. QCOMPARE(entry1->autoTypeAssociations()->get(3).window, QString("Window Nr 1b"));
  125. QCOMPARE(entry1->autoTypeAssociations()->get(4).sequence, QString(""));
  126. QCOMPARE(entry1->autoTypeAssociations()->get(4).window, QString("Window 2"));
  127. Entry* entry2 = group->entries().at(1);
  128. QCOMPARE(entry2->notes(), QString("start line\nend line"));
  129. QCOMPARE(entry2->defaultAutoTypeSequence(), QString(""));
  130. QCOMPARE(entry2->autoTypeAssociations()->size(), 2);
  131. QCOMPARE(entry2->autoTypeAssociations()->get(0).sequence, QString(""));
  132. QCOMPARE(entry2->autoTypeAssociations()->get(0).window, QString("Main Window"));
  133. QCOMPARE(entry2->autoTypeAssociations()->get(1).sequence, QString(""));
  134. QCOMPARE(entry2->autoTypeAssociations()->get(1).window, QString("Test Window"));
  135. }
  136. void TestKeePass1Reader::testFileKey()
  137. {
  138. QFETCH(QString, type);
  139. QString name = QString("FileKey").append(type);
  140. KeePass1Reader reader;
  141. QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
  142. QString keyFilename = QString("%1/%2.key").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
  143. auto db = reader.readDatabase(dbFilename, QString(), keyFilename);
  144. QVERIFY(db);
  145. QVERIFY(!reader.hasError());
  146. QCOMPARE(db->rootGroup()->children().size(), 1);
  147. QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
  148. reopenDatabase(db, QString(), keyFilename);
  149. }
  150. void TestKeePass1Reader::testFileKey_data()
  151. {
  152. QTest::addColumn<QString>("type");
  153. QTest::newRow("Binary") << QString("Binary");
  154. QTest::newRow("Hex") << QString("Hex");
  155. QTest::newRow("Hashed") << QString("Hashed");
  156. }
  157. void TestKeePass1Reader::testCompositeKey()
  158. {
  159. QString name = "CompositeKey";
  160. KeePass1Reader reader;
  161. QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
  162. QString keyFilename = QString("%1/FileKeyHex.key").arg(QString(KEEPASSX_TEST_DATA_DIR));
  163. auto db = reader.readDatabase(dbFilename, "mypassword", keyFilename);
  164. QVERIFY(db);
  165. QVERIFY(!reader.hasError());
  166. QCOMPARE(db->rootGroup()->children().size(), 1);
  167. QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
  168. reopenDatabase(db, "mypassword", keyFilename);
  169. }
  170. void TestKeePass1Reader::testTwofish()
  171. {
  172. QString name = "Twofish";
  173. KeePass1Reader reader;
  174. QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
  175. auto db = reader.readDatabase(dbFilename, "masterpw", nullptr);
  176. QVERIFY(db);
  177. QVERIFY(!reader.hasError());
  178. QCOMPARE(db->rootGroup()->children().size(), 1);
  179. QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
  180. }
  181. void TestKeePass1Reader::testCP1252Password()
  182. {
  183. QString name = "CP-1252";
  184. KeePass1Reader reader;
  185. QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
  186. QString password = QString::fromUtf8("\xe2\x80\x9e\x70\x61\x73\x73\x77\x6f\x72\x64\xe2\x80\x9d");
  187. auto db = reader.readDatabase(dbFilename, password, nullptr);
  188. QVERIFY(db);
  189. QVERIFY(!reader.hasError());
  190. QCOMPARE(db->rootGroup()->children().size(), 1);
  191. QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
  192. }
  193. void TestKeePass1Reader::cleanupTestCase()
  194. {
  195. }
  196. QDateTime TestKeePass1Reader::genDT(int year, int month, int day, int hour, int min)
  197. {
  198. QDate date(year, month, day);
  199. QTime time(hour, min, 0);
  200. return {date, time, Qt::UTC};
  201. }
  202. void TestKeePass1Reader::reopenDatabase(QSharedPointer<Database> db,
  203. const QString& password,
  204. const QString& keyfileName)
  205. {
  206. QBuffer buffer;
  207. buffer.open(QIODevice::ReadWrite);
  208. KeePass2Writer writer;
  209. writer.writeDatabase(&buffer, db.data());
  210. QVERIFY(!writer.hasError());
  211. QVERIFY(buffer.seek(0));
  212. auto key = QSharedPointer<CompositeKey>::create();
  213. if (!password.isEmpty()) {
  214. key->addKey(QSharedPointer<PasswordKey>::create(password));
  215. }
  216. if (!keyfileName.isEmpty()) {
  217. auto fileKey = QSharedPointer<FileKey>::create();
  218. QVERIFY(fileKey->load(keyfileName));
  219. key->addKey(fileKey);
  220. }
  221. KeePass2Reader reader;
  222. auto newDb = QSharedPointer<Database>::create();
  223. QVERIFY(reader.readDatabase(&buffer, key, newDb.data()));
  224. QVERIFY(!reader.hasError());
  225. }