diff --git a/tests/TestKdbx4.cpp b/tests/TestKdbx4.cpp index 26937540a..8fb0f219b 100644 --- a/tests/TestKdbx4.cpp +++ b/tests/TestKdbx4.cpp @@ -188,73 +188,3 @@ void TestKdbx4::testFormat400Upgrade_data() QTest::newRow("AES-KDF + Twofish") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_TWOFISH << kdbx4; QTest::newRow("AES-KDF (legacy) + Twofish") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_TWOFISH << kdbx3; } - -/** - * Test for catching mapping errors with duplicate attachments. - */ -void TestKdbx4::testDuplicateAttachments() -{ - QScopedPointer db(new Database()); - db->setKey(CompositeKey()); - db->setKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2)); - - const QByteArray attachment1("abc"); - const QByteArray attachment2("def"); - const QByteArray attachment3("ghi"); - - auto entry1 = new Entry(); - entry1->setGroup(db->rootGroup()); - entry1->setUuid(Uuid("aaaaaaaaaaaaaaaa")); - entry1->attachments()->set("a", attachment1); - - auto entry2 = new Entry(); - entry2->setGroup(db->rootGroup()); - entry2->setUuid(Uuid("bbbbbbbbbbbbbbbb")); - entry2->attachments()->set("b1", attachment1); - entry2->beginUpdate(); - entry2->attachments()->set("b2", attachment1); - entry2->endUpdate(); - entry2->beginUpdate(); - entry2->attachments()->set("b3", attachment2); - entry2->endUpdate(); - entry2->beginUpdate(); - entry2->attachments()->set("b4", attachment2); - entry2->endUpdate(); - - auto entry3 = new Entry(); - entry3->setGroup(db->rootGroup()); - entry3->setUuid(Uuid("cccccccccccccccc")); - entry3->attachments()->set("c1", attachment2); - entry3->attachments()->set("c2", attachment2); - entry3->attachments()->set("c3", attachment3); - - QBuffer buffer; - buffer.open(QBuffer::ReadWrite); - - KeePass2Writer writer; - writer.writeDatabase(&buffer, db.data()); - if (writer.hasError()) { - QFAIL(qPrintable(QString("Error while writing database: %1").arg(writer.errorString()))); - } - - buffer.seek(0); - KeePass2Reader reader; - db.reset(reader.readDatabase(&buffer, CompositeKey())); - if (reader.hasError()) { - QFAIL(qPrintable(QString("Error while reading database: %1").arg(reader.errorString()))); - } - - QCOMPARE(db->rootGroup()->entries()[0]->attachments()->value("a"), attachment1); - - QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b1"), attachment1); - QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b2"), attachment1); - QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b3"), attachment2); - QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b4"), attachment2); - QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[0]->attachments()->value("b1"), attachment1); - QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[1]->attachments()->value("b2"), attachment1); - QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[2]->attachments()->value("b3"), attachment2); - - QCOMPARE(db->rootGroup()->entries()[2]->attachments()->value("c1"), attachment2); - QCOMPARE(db->rootGroup()->entries()[2]->attachments()->value("c2"), attachment2); - QCOMPARE(db->rootGroup()->entries()[2]->attachments()->value("c3"), attachment3); -} diff --git a/tests/TestKdbx4.h b/tests/TestKdbx4.h index ceceefcb7..59864b70e 100644 --- a/tests/TestKdbx4.h +++ b/tests/TestKdbx4.h @@ -28,7 +28,6 @@ private slots: void testFormat400(); void testFormat400Upgrade(); void testFormat400Upgrade_data(); - void testDuplicateAttachments(); protected: void initTestCaseImpl() override; diff --git a/tests/TestKeePass2Format.cpp b/tests/TestKeePass2Format.cpp index 34daea879..790f0d802 100644 --- a/tests/TestKeePass2Format.cpp +++ b/tests/TestKeePass2Format.cpp @@ -565,3 +565,75 @@ void TestKeePass2Format::testKdbxDeviceFailure() QVERIFY(hasError); QCOMPARE(errorString, QString("FAILDEVICE")); } + +/** + * Test for catching mapping errors with duplicate attachments. + */ +void TestKeePass2Format::testDuplicateAttachments() +{ + QScopedPointer db(new Database()); + db->setKey(CompositeKey()); + + const QByteArray attachment1("abc"); + const QByteArray attachment2("def"); + const QByteArray attachment3("ghi"); + + auto entry1 = new Entry(); + entry1->setGroup(db->rootGroup()); + entry1->setUuid(Uuid("aaaaaaaaaaaaaaaa")); + entry1->attachments()->set("a", attachment1); + + auto entry2 = new Entry(); + entry2->setGroup(db->rootGroup()); + entry2->setUuid(Uuid("bbbbbbbbbbbbbbbb")); + entry2->attachments()->set("b1", attachment1); + entry2->beginUpdate(); + entry2->attachments()->set("b2", attachment1); + entry2->endUpdate(); + entry2->beginUpdate(); + entry2->attachments()->set("b3", attachment2); + entry2->endUpdate(); + entry2->beginUpdate(); + entry2->attachments()->set("b4", attachment2); + entry2->endUpdate(); + + auto entry3 = new Entry(); + entry3->setGroup(db->rootGroup()); + entry3->setUuid(Uuid("cccccccccccccccc")); + entry3->attachments()->set("c1", attachment2); + entry3->attachments()->set("c2", attachment2); + entry3->attachments()->set("c3", attachment3); + + QBuffer buffer; + buffer.open(QBuffer::ReadWrite); + + bool hasError = false; + QString errorString; + writeKdbx(&buffer, db.data(), hasError, errorString); + if (hasError) { + QFAIL(qPrintable(QString("Error while writing database: %1").arg(errorString))); + } + + buffer.seek(0); + readKdbx(&buffer, CompositeKey(), db, hasError, errorString); + if (hasError) { + QFAIL(qPrintable(QString("Error while reading database: %1").arg(errorString))); + } + + QCOMPARE(db->rootGroup()->entries()[0]->attachments()->value("a"), attachment1); + + QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b1"), attachment1); + QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b2"), attachment1); + QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b3"), attachment2); + QCOMPARE(db->rootGroup()->entries()[1]->attachments()->value("b4"), attachment2); + QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[0]->attachments()->value("b1"), attachment1); + QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[1]->attachments()->value("b1"), attachment1); + QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[1]->attachments()->value("b2"), attachment1); + QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[2]->attachments()->value("b1"), attachment1); + QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[2]->attachments()->value("b2"), attachment1); + QCOMPARE(db->rootGroup()->entries()[1]->historyItems()[2]->attachments()->value("b3"), attachment2); + + QCOMPARE(db->rootGroup()->entries()[2]->attachments()->value("c1"), attachment2); + QCOMPARE(db->rootGroup()->entries()[2]->attachments()->value("c2"), attachment2); + QCOMPARE(db->rootGroup()->entries()[2]->attachments()->value("c3"), attachment3); +} diff --git a/tests/TestKeePass2Format.h b/tests/TestKeePass2Format.h index a2bdd4f49..2a30d92b2 100644 --- a/tests/TestKeePass2Format.h +++ b/tests/TestKeePass2Format.h @@ -63,6 +63,7 @@ private slots: void testKdbxAttachments(); void testKdbxNonAsciiPasswords(); void testKdbxDeviceFailure(); + void testDuplicateAttachments(); protected: virtual void initTestCaseImpl() = 0;