TestImports.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2024 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 "TestImports.h"
  18. #include "config-keepassx-tests.h"
  19. #include "core/Group.h"
  20. #include "core/Metadata.h"
  21. #include "core/Totp.h"
  22. #include "crypto/Crypto.h"
  23. #include "format/BitwardenReader.h"
  24. #include "format/OPUXReader.h"
  25. #include "format/OpVaultReader.h"
  26. #include <QJsonObject>
  27. #include <QList>
  28. #include <QTest>
  29. QTEST_GUILESS_MAIN(TestImports)
  30. void TestImports::initTestCase()
  31. {
  32. QVERIFY(Crypto::init());
  33. }
  34. void TestImports::testOPUX()
  35. {
  36. auto opuxPath = QStringLiteral("%1/%2").arg(KEEPASSX_TEST_DATA_DIR, QStringLiteral("/1PasswordExport.1pux"));
  37. OPUXReader reader;
  38. auto db = reader.convert(opuxPath);
  39. QVERIFY2(!reader.hasError(), qPrintable(reader.errorString()));
  40. QVERIFY(db);
  41. // Confirm specific entry details are valid
  42. auto entry = db->rootGroup()->findEntryByPath("/Personal/Login");
  43. QVERIFY(entry);
  44. QCOMPARE(entry->title(), QStringLiteral("Login"));
  45. QCOMPARE(entry->username(), QStringLiteral("team@keepassxc.org"));
  46. QCOMPARE(entry->password(), QStringLiteral("password"));
  47. QCOMPARE(entry->url(), QStringLiteral("https://keepassxc.org"));
  48. QCOMPARE(entry->notes(), QStringLiteral("Note to self"));
  49. // Check extra URL's
  50. QCOMPARE(entry->attribute("KP2A_URL_1"), QStringLiteral("https://twitter.com"));
  51. // Check TOTP
  52. QVERIFY(entry->hasTotp());
  53. QVERIFY(!entry->attribute("otp_1").isEmpty());
  54. // Check tags
  55. QVERIFY(entry->tagList().contains("Favorite"));
  56. QVERIFY(entry->tagList().contains("website"));
  57. // Check attachments
  58. entry = db->rootGroup()->findEntryByPath("/Personal/KeePassXC Logo");
  59. auto attachments = entry->attachments();
  60. QCOMPARE(attachments->keys().count(), 1);
  61. QCOMPARE(attachments->keys()[0], QString("keepassxc.png"));
  62. // Confirm advanced attributes
  63. // NOTE: 1PUX does not support an explicit expiration field
  64. entry = db->rootGroup()->findEntryByPath("/Personal/Credit Card");
  65. QVERIFY(entry);
  66. auto tmpl = QString("Credit Card Fields_%1");
  67. auto attr = entry->attributes();
  68. QCOMPARE(attr->value(tmpl.arg("cardholder name")), QStringLiteral("KeePassXC"));
  69. QCOMPARE(attr->value(tmpl.arg("expiry date")), QStringLiteral("202206"));
  70. QCOMPARE(attr->value(tmpl.arg("verification number")), QStringLiteral("123"));
  71. QVERIFY(attr->isProtected(tmpl.arg("verification number")));
  72. // Confirm address fields
  73. entry = db->rootGroup()->findEntryByPath("/Personal/Identity");
  74. QVERIFY(entry);
  75. attr = entry->attributes();
  76. QCOMPARE(attr->value("Address_address"), QStringLiteral("123 Avenue Rd\nBoston, MA 12345\nus"));
  77. // Check archived entries
  78. entry = db->rootGroup()->findEntryByPath("/Personal/Login Archived");
  79. QVERIFY(entry);
  80. QVERIFY(entry->tagList().contains("Archived"));
  81. // Check vault to group structure
  82. entry = db->rootGroup()->findEntryByPath("/Shared/Bank Account");
  83. QVERIFY(entry);
  84. // Check custom group icon
  85. QVERIFY(!entry->group()->iconUuid().isNull());
  86. // Check Category UUID 05 Passwords
  87. entry = db->rootGroup()->findEntryByPath("/Personal/UUID 005 Password");
  88. QVERIFY(entry);
  89. QCOMPARE(entry->password(), QStringLiteral("uuid005password"));
  90. }
  91. void TestImports::testOPVault()
  92. {
  93. auto opVaultPath = QStringLiteral("%1/%2").arg(KEEPASSX_TEST_DATA_DIR, QStringLiteral("/keepassxc.opvault"));
  94. auto categories = QStringList({QStringLiteral("Login"),
  95. QStringLiteral("Credit Card"),
  96. QStringLiteral("Secure Note"),
  97. QStringLiteral("Identity"),
  98. QStringLiteral("Password"),
  99. QStringLiteral("Tombstone"),
  100. QStringLiteral("Software License"),
  101. QStringLiteral("Bank Account"),
  102. QStringLiteral("Database"),
  103. QStringLiteral("Driver License"),
  104. QStringLiteral("Outdoor License"),
  105. QStringLiteral("Membership"),
  106. QStringLiteral("Passport"),
  107. QStringLiteral("Rewards"),
  108. QStringLiteral("SSN"),
  109. QStringLiteral("Router"),
  110. QStringLiteral("Server"),
  111. QStringLiteral("Email")});
  112. QDir opVaultDir(opVaultPath);
  113. OpVaultReader reader;
  114. auto db = reader.convert(opVaultDir, "a");
  115. QVERIFY2(!reader.hasError(), qPrintable(reader.errorString()));
  116. QVERIFY(db);
  117. // Confirm specific entry details are valid
  118. auto entry = db->rootGroup()->findEntryByPath("/Login/KeePassXC");
  119. QVERIFY(entry);
  120. QCOMPARE(entry->title(), QStringLiteral("KeePassXC"));
  121. QCOMPARE(entry->username(), QStringLiteral("keepassxc"));
  122. QCOMPARE(entry->password(), QStringLiteral("opvault"));
  123. QCOMPARE(entry->url(), QStringLiteral("https://www.keepassxc.org"));
  124. QCOMPARE(entry->notes(), QStringLiteral("KeePassXC Account"));
  125. // Check extra URL's
  126. QCOMPARE(entry->attribute("KP2A_URL_1"), QStringLiteral("https://snapshot.keepassxc.org"));
  127. // Check TOTP
  128. QVERIFY(entry->hasTotp());
  129. // Check attachments
  130. auto attachments = entry->attachments();
  131. QCOMPARE(attachments->keys().count(), 1);
  132. QCOMPARE(*attachments->values().begin(), QByteArray("attachment"));
  133. // Confirm expired entries
  134. entry = db->rootGroup()->findEntryByPath("/Login/Expired Login");
  135. QVERIFY(entry->isExpired());
  136. // Confirm advanced attributes
  137. entry = db->rootGroup()->findEntryByPath("/Credit Card/My Credit Card");
  138. QVERIFY(entry);
  139. auto attr = entry->attributes();
  140. QCOMPARE(attr->value("cardholder name"), QStringLiteral("Team KeePassXC"));
  141. QVERIFY(!attr->value("valid from").isEmpty());
  142. QCOMPARE(attr->value("Additional Details_PIN"), QStringLiteral("1234"));
  143. QVERIFY(attr->isProtected("Additional Details_PIN"));
  144. // Confirm address fields
  145. entry = db->rootGroup()->findEntryByPath("/Identity/Team KeePassXC");
  146. QVERIFY(entry);
  147. attr = entry->attributes();
  148. QCOMPARE(attr->value("address_street"), QStringLiteral("123 Password Lane"));
  149. // Confirm complex passwords
  150. entry = db->rootGroup()->findEntryByPath("/Password/Complex Password");
  151. QVERIFY(entry);
  152. QCOMPARE(entry->password(), QStringLiteral("HfgcHjEL}iO}^3N!?*cv~O:9GJZQ0>oC"));
  153. QVERIFY(entry->hasTotp());
  154. auto totpSettings = entry->totpSettings();
  155. QCOMPARE(totpSettings->digits, static_cast<unsigned int>(8));
  156. QCOMPARE(totpSettings->step, static_cast<unsigned int>(45));
  157. // Add another OTP to this entry to confirm it doesn't overwrite the existing one
  158. auto field = QJsonObject::fromVariantMap({{"n", "TOTP_SETTINGS"}, {"v", "otpauth://test.url?digits=6"}});
  159. reader.fillFromSectionField(entry, "", field);
  160. QVERIFY(entry->hasTotp());
  161. totpSettings = entry->totpSettings();
  162. QCOMPARE(totpSettings->digits, static_cast<unsigned int>(8));
  163. QCOMPARE(totpSettings->step, static_cast<unsigned int>(45));
  164. QVERIFY(entry->attributes()->contains("otp_1"));
  165. // Confirm trashed entries are sent to the recycle bin
  166. auto recycleBin = db->metadata()->recycleBin();
  167. QVERIFY(recycleBin);
  168. QVERIFY(!recycleBin->isEmpty());
  169. QVERIFY(recycleBin->findEntryByPath("Trashed Password"));
  170. // Confirm created groups align with category names
  171. for (const auto group : db->rootGroup()->children()) {
  172. if (group == recycleBin) {
  173. continue;
  174. }
  175. QVERIFY2(categories.contains(group->name()),
  176. qPrintable(QStringLiteral("Invalid group name: %1").arg(group->name())));
  177. // Confirm each group is not empty
  178. QVERIFY2(!group->isEmpty(), qPrintable(QStringLiteral("Group %1 is empty").arg(group->name())));
  179. }
  180. }
  181. void TestImports::testBitwarden()
  182. {
  183. auto bitwardenPath = QStringLiteral("%1/%2").arg(KEEPASSX_TEST_DATA_DIR, QStringLiteral("/bitwarden_export.json"));
  184. BitwardenReader reader;
  185. auto db = reader.convert(bitwardenPath);
  186. QVERIFY2(!reader.hasError(), qPrintable(reader.errorString()));
  187. QVERIFY(db);
  188. // Confirm Login fields
  189. auto entry = db->rootGroup()->findEntryByPath("/My Folder/Login Name");
  190. QVERIFY(entry);
  191. QCOMPARE(entry->title(), QStringLiteral("Login Name"));
  192. QCOMPARE(entry->username(), QStringLiteral("myusername@gmail.com"));
  193. QCOMPARE(entry->password(), QStringLiteral("mypassword"));
  194. QCOMPARE(entry->url(), QStringLiteral("https://mail.google.com"));
  195. QCOMPARE(entry->notes(), QStringLiteral("1st line of note text\n2nd Line of note text"));
  196. // Check extra URL's
  197. QCOMPARE(entry->attribute("KP2A_URL_1"), QStringLiteral("https://google.com"));
  198. QCOMPARE(entry->attribute("KP2A_URL_2"), QStringLiteral("https://gmail.com"));
  199. // Check TOTP
  200. QVERIFY(entry->hasTotp());
  201. // NOTE: Bitwarden does not export attachments
  202. // NOTE: Bitwarden does not export expiration dates
  203. // Confirm Identity fields
  204. entry = db->rootGroup()->findEntryByPath("/My Folder/My Identity");
  205. QVERIFY(entry);
  206. auto attr = entry->attributes();
  207. // NOTE: The extra spaces are deliberate to test unmodified ingest of data
  208. QCOMPARE(attr->value("identity_address"),
  209. QStringLiteral(" 1 North Calle Cesar Chavez \nSanta Barbara, CA 93103\nUnited States "));
  210. QCOMPARE(attr->value("identity_name"), QStringLiteral("Mrs Jane A Doe"));
  211. QCOMPARE(attr->value("identity_ssn"), QStringLiteral("123-12-1234"));
  212. QVERIFY(attr->isProtected("identity_ssn"));
  213. // Confirm Secure Note
  214. entry = db->rootGroup()->findEntryByPath("/My Folder/My Secure Note");
  215. QVERIFY(entry);
  216. QCOMPARE(entry->notes(),
  217. QStringLiteral("1st line of secure note\n2nd line of secure note\n3rd line of secure note"));
  218. // Confirm Credit Card
  219. entry = db->rootGroup()->findEntryByPath("/Second Folder/Card Name");
  220. QVERIFY(entry);
  221. attr = entry->attributes();
  222. QCOMPARE(attr->value("card_cardholderName"), QStringLiteral("Jane Doe"));
  223. QCOMPARE(attr->value("card_number"), QStringLiteral("1234567891011121"));
  224. QCOMPARE(attr->value("card_code"), QStringLiteral("123"));
  225. QVERIFY(attr->isProtected("card_code"));
  226. }
  227. void TestImports::testBitwardenEncrypted()
  228. {
  229. // We already tested the parser so just test that decryption works properly
  230. // First test PBKDF2 password stretching (KDF Type 0)
  231. auto bitwardenPath =
  232. QStringLiteral("%1/%2").arg(KEEPASSX_TEST_DATA_DIR, QStringLiteral("/bitwarden_encrypted_export.json"));
  233. BitwardenReader reader;
  234. auto db = reader.convert(bitwardenPath, "a");
  235. if (reader.hasError()) {
  236. QFAIL(qPrintable(reader.errorString()));
  237. }
  238. QVERIFY(db);
  239. // Now test Argon2id password stretching (KDF Type 1)
  240. bitwardenPath = QStringLiteral("%1/%2").arg(KEEPASSX_TEST_DATA_DIR,
  241. QStringLiteral("/bitwarden_encrypted_argon2id_export.json"));
  242. db = reader.convert(bitwardenPath, "a");
  243. if (reader.hasError()) {
  244. QFAIL(qPrintable(reader.errorString()));
  245. }
  246. QVERIFY(db);
  247. }
  248. void TestImports::testBitwardenPasskey()
  249. {
  250. auto bitwardenPath =
  251. QStringLiteral("%1/%2").arg(KEEPASSX_TEST_DATA_DIR, QStringLiteral("/bitwarden_passkey_export.json"));
  252. BitwardenReader reader;
  253. auto db = reader.convert(bitwardenPath);
  254. QVERIFY2(!reader.hasError(), qPrintable(reader.errorString()));
  255. QVERIFY(db);
  256. // Confirm Login fields
  257. auto entry = db->rootGroup()->findEntryByPath("/webauthn.io");
  258. QVERIFY(entry);
  259. QCOMPARE(entry->title(), QStringLiteral("webauthn.io"));
  260. QCOMPARE(entry->username(), QStringLiteral("KPXC_BITWARDEN"));
  261. QCOMPARE(entry->url(), QStringLiteral("https://webauthn.io/"));
  262. // Confirm passkey attributes
  263. auto attr = entry->attributes();
  264. QCOMPARE(attr->value(EntryAttributes::KPEX_PASSKEY_CREDENTIAL_ID), QStringLiteral("o-FfiyfBQq6Qz6YVrYeFTw"));
  265. QCOMPARE(
  266. attr->value(EntryAttributes::KPEX_PASSKEY_PRIVATE_KEY_PEM),
  267. QStringLiteral(
  268. "-----BEGIN PRIVATE "
  269. "KEY-----"
  270. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgmr4GQQjerojFuf0ZouOuUllMvAwxZSZAfB6gwDYcLiehRANCAAT0WR5zVS"
  271. "p6ieusvjkLkzaGc7fjGBmwpiuLPxR/d+ZjqMI9L2DKh+takp6wGt2x0n4jzr1KA352NZg0vjZX9CHh-----END PRIVATE KEY-----"));
  272. QCOMPARE(attr->value(EntryAttributes::KPEX_PASSKEY_USERNAME), QStringLiteral("KPXC_BITWARDEN"));
  273. QCOMPARE(attr->value(EntryAttributes::KPEX_PASSKEY_RELYING_PARTY), QStringLiteral("webauthn.io"));
  274. QCOMPARE(attr->value(EntryAttributes::KPEX_PASSKEY_USER_HANDLE),
  275. QStringLiteral("aTFtdmFnOHYtS2dxVEJ0by1rSFpLWGg0enlTVC1iUVJReDZ5czJXa3c2aw"));
  276. }