TestImports.cpp 16 KB

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