2012-05-08 16:03:59 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TestKeePass1Reader.h"
|
2018-01-24 07:22:20 -05:00
|
|
|
#include "TestGlobal.h"
|
2012-05-08 16:03:59 -04:00
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QBuffer>
|
2012-05-08 16:03:59 -04:00
|
|
|
|
|
|
|
#include "config-keepassx-tests.h"
|
2012-05-10 05:44:25 -04:00
|
|
|
#include "core/Metadata.h"
|
2012-05-08 16:03:59 -04:00
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
#include "format/KeePass1Reader.h"
|
2012-05-10 16:46:36 -04:00
|
|
|
#include "format/KeePass2Reader.h"
|
|
|
|
#include "format/KeePass2Writer.h"
|
|
|
|
#include "keys/FileKey.h"
|
|
|
|
#include "keys/PasswordKey.h"
|
2012-05-08 16:03:59 -04:00
|
|
|
|
2014-05-16 06:32:52 -04:00
|
|
|
QTEST_GUILESS_MAIN(TestKeePass1Reader)
|
|
|
|
|
2012-05-08 16:03:59 -04:00
|
|
|
void TestKeePass1Reader::initTestCase()
|
|
|
|
{
|
2014-06-15 05:17:40 -04:00
|
|
|
QVERIFY(Crypto::init());
|
2012-05-08 16:03:59 -04:00
|
|
|
|
|
|
|
QString filename = QString(KEEPASSX_TEST_DATA_DIR).append("/basic.kdb");
|
|
|
|
|
|
|
|
KeePass1Reader reader;
|
2012-05-10 16:46:36 -04:00
|
|
|
m_db = reader.readDatabase(filename, "masterpw", 0);
|
2012-05-10 05:44:25 -04:00
|
|
|
QVERIFY(m_db);
|
2012-05-08 16:03:59 -04:00
|
|
|
QVERIFY(!reader.hasError());
|
2012-05-10 05:44:25 -04:00
|
|
|
}
|
2012-05-08 16:03:59 -04:00
|
|
|
|
2012-05-10 05:44:25 -04:00
|
|
|
void TestKeePass1Reader::testBasic()
|
|
|
|
{
|
2012-05-10 16:54:26 -04:00
|
|
|
QVERIFY(m_db->deletedObjects().isEmpty());
|
|
|
|
|
2012-05-10 05:44:25 -04:00
|
|
|
QCOMPARE(m_db->rootGroup()->children().size(), 2);
|
2012-05-09 17:14:27 -04:00
|
|
|
|
2012-05-10 05:44:25 -04:00
|
|
|
Group* group1 = m_db->rootGroup()->children().at(0);
|
2012-05-10 15:35:26 -04:00
|
|
|
QVERIFY(!group1->uuid().isNull());
|
2012-05-09 17:14:27 -04:00
|
|
|
QCOMPARE(group1->name(), QString("Internet"));
|
2012-05-10 05:44:25 -04:00
|
|
|
QCOMPARE(group1->children().size(), 2);
|
2012-05-09 17:14:27 -04:00
|
|
|
QCOMPARE(group1->entries().size(), 2);
|
2012-05-10 05:44:25 -04:00
|
|
|
QCOMPARE(group1->iconNumber(), 1);
|
2012-05-09 17:14:27 -04:00
|
|
|
|
|
|
|
Entry* entry11 = group1->entries().at(0);
|
2012-05-10 15:35:26 -04:00
|
|
|
QVERIFY(!entry11->uuid().isNull());
|
2012-05-09 17:14:27 -04:00
|
|
|
QCOMPARE(entry11->title(), QString("Test entry"));
|
|
|
|
QCOMPARE(entry11->iconNumber(), 1);
|
|
|
|
QCOMPARE(entry11->username(), QString("I"));
|
|
|
|
QCOMPARE(entry11->url(), QString("http://example.com/"));
|
|
|
|
QCOMPARE(entry11->password(), QString("secretpassword"));
|
|
|
|
QCOMPARE(entry11->notes(), QString("Lorem ipsum\ndolor sit amet"));
|
|
|
|
QVERIFY(entry11->timeInfo().expires());
|
|
|
|
QCOMPARE(entry11->timeInfo().expiryTime(), genDT(2012, 5, 9, 10, 32));
|
|
|
|
QCOMPARE(entry11->attachments()->keys().size(), 1);
|
2015-09-25 15:33:55 -04:00
|
|
|
QCOMPARE(entry11->attachments()->keys().at(0), QString("attachment.txt"));
|
2012-05-09 17:14:27 -04:00
|
|
|
QCOMPARE(entry11->attachments()->value("attachment.txt"), QByteArray("hello world\n"));
|
|
|
|
|
|
|
|
Entry* entry12 = group1->entries().at(1);
|
|
|
|
QCOMPARE(entry12->title(), QString(""));
|
|
|
|
QCOMPARE(entry12->iconNumber(), 0);
|
|
|
|
QCOMPARE(entry12->username(), QString(""));
|
|
|
|
QCOMPARE(entry12->url(), QString(""));
|
|
|
|
QCOMPARE(entry12->password(), QString(""));
|
|
|
|
QCOMPARE(entry12->notes(), QString(""));
|
|
|
|
QVERIFY(!entry12->timeInfo().expires());
|
|
|
|
QCOMPARE(entry12->attachments()->keys().size(), 0);
|
|
|
|
|
2012-05-10 05:44:25 -04:00
|
|
|
Group* group11 = group1->children().at(0);
|
|
|
|
QCOMPARE(group11->name(), QString("Subgroup 1"));
|
|
|
|
QCOMPARE(group11->children().size(), 1);
|
|
|
|
|
|
|
|
Group* group111 = group11->children().at(0);
|
|
|
|
QCOMPARE(group111->name(), QString("Unexpanded"));
|
|
|
|
QCOMPARE(group111->children().size(), 1);
|
|
|
|
|
|
|
|
Group* group1111 = group111->children().at(0);
|
|
|
|
QCOMPARE(group1111->name(), QString("abc"));
|
|
|
|
QCOMPARE(group1111->children().size(), 0);
|
|
|
|
|
|
|
|
Group* group12 = group1->children().at(1);
|
|
|
|
QCOMPARE(group12->name(), QString("Subgroup 2"));
|
|
|
|
QCOMPARE(group12->children().size(), 0);
|
|
|
|
|
|
|
|
Group* group2 = m_db->rootGroup()->children().at(1);
|
2012-05-09 17:14:27 -04:00
|
|
|
QCOMPARE(group2->name(), QString("eMail"));
|
2012-05-10 05:44:25 -04:00
|
|
|
QCOMPARE(group2->entries().size(), 1);
|
2012-05-09 17:14:27 -04:00
|
|
|
QCOMPARE(group2->iconNumber(), 19);
|
2012-05-10 17:00:29 -04:00
|
|
|
|
|
|
|
reopenDatabase(m_db, "masterpw", QString());
|
2012-05-10 05:44:25 -04:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:46:36 -04:00
|
|
|
void TestKeePass1Reader::testMasterKey()
|
|
|
|
{
|
2020-03-04 09:37:13 -05:00
|
|
|
QVERIFY(m_db->isInitialized());
|
2018-01-12 20:47:02 -05:00
|
|
|
QCOMPARE(m_db->kdf()->rounds(), 713);
|
2012-05-10 16:46:36 -04:00
|
|
|
}
|
|
|
|
|
2012-05-10 05:44:25 -04:00
|
|
|
void TestKeePass1Reader::testCustomIcons()
|
|
|
|
{
|
2020-05-29 10:00:07 -04:00
|
|
|
QCOMPARE(m_db->metadata()->customIconsOrder().size(), 1);
|
2012-05-09 17:14:27 -04:00
|
|
|
|
2015-09-25 15:33:55 -04:00
|
|
|
Entry* entry = m_db->rootGroup()->children().at(1)->entries().at(0);
|
2012-05-10 05:44:25 -04:00
|
|
|
|
|
|
|
QCOMPARE(entry->icon().width(), 16);
|
|
|
|
QCOMPARE(entry->icon().height(), 16);
|
|
|
|
|
|
|
|
for (int x = 0; x < 16; x++) {
|
|
|
|
for (int y = 0; y < 16; y++) {
|
|
|
|
QRgb rgb = entry->icon().pixel(x, y);
|
|
|
|
QCOMPARE(qRed(rgb), 8);
|
|
|
|
QCOMPARE(qGreen(rgb), 160);
|
|
|
|
QCOMPARE(qBlue(rgb), 60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass1Reader::testGroupExpanded()
|
|
|
|
{
|
|
|
|
QCOMPARE(m_db->rootGroup()->children().at(0)->isExpanded(), true);
|
|
|
|
QCOMPARE(m_db->rootGroup()->children().at(0)->children().at(0)->isExpanded(), true);
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(m_db->rootGroup()->children().at(0)->children().at(0)->children().at(0)->isExpanded(), false);
|
2012-05-10 05:44:25 -04:00
|
|
|
}
|
|
|
|
|
2012-07-19 17:21:12 -04:00
|
|
|
void TestKeePass1Reader::testAutoType()
|
|
|
|
{
|
|
|
|
Group* group = m_db->rootGroup()->children().at(0)->children().at(0);
|
|
|
|
QCOMPARE(group->entries().size(), 2);
|
|
|
|
|
|
|
|
Entry* entry1 = group->entries().at(0);
|
|
|
|
QCOMPARE(entry1->notes(), QString("last line"));
|
|
|
|
QCOMPARE(entry1->defaultAutoTypeSequence(), QString("{USERNAME}{ENTER}"));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->size(), 5);
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(0).sequence, QString(""));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(0).window, QString("a window"));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(1).sequence, QString(""));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(1).window, QString("a second window"));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(2).sequence, QString("{PASSWORD}{ENTER}"));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(2).window, QString("Window Nr 1a"));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(3).sequence, QString("{PASSWORD}{ENTER}"));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(3).window, QString("Window Nr 1b"));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(4).sequence, QString(""));
|
|
|
|
QCOMPARE(entry1->autoTypeAssociations()->get(4).window, QString("Window 2"));
|
|
|
|
|
|
|
|
Entry* entry2 = group->entries().at(1);
|
|
|
|
QCOMPARE(entry2->notes(), QString("start line\nend line"));
|
|
|
|
QCOMPARE(entry2->defaultAutoTypeSequence(), QString(""));
|
|
|
|
QCOMPARE(entry2->autoTypeAssociations()->size(), 2);
|
|
|
|
QCOMPARE(entry2->autoTypeAssociations()->get(0).sequence, QString(""));
|
|
|
|
QCOMPARE(entry2->autoTypeAssociations()->get(0).window, QString("Main Window"));
|
|
|
|
QCOMPARE(entry2->autoTypeAssociations()->get(1).sequence, QString(""));
|
|
|
|
QCOMPARE(entry2->autoTypeAssociations()->get(1).window, QString("Test Window"));
|
|
|
|
}
|
|
|
|
|
2012-05-10 13:34:55 -04:00
|
|
|
void TestKeePass1Reader::testFileKey()
|
|
|
|
{
|
|
|
|
QFETCH(QString, type);
|
|
|
|
|
|
|
|
QString name = QString("FileKey").append(type);
|
|
|
|
|
|
|
|
KeePass1Reader reader;
|
|
|
|
|
|
|
|
QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
QString keyFilename = QString("%1/%2.key").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = reader.readDatabase(dbFilename, QString(), keyFilename);
|
2012-05-10 13:34:55 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
QCOMPARE(db->rootGroup()->children().size(), 1);
|
|
|
|
QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
|
|
|
|
|
2012-05-10 17:00:29 -04:00
|
|
|
reopenDatabase(db, QString(), keyFilename);
|
2012-05-10 13:34:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass1Reader::testFileKey_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("type");
|
|
|
|
QTest::newRow("Binary") << QString("Binary");
|
|
|
|
QTest::newRow("Hex") << QString("Hex");
|
|
|
|
QTest::newRow("Hashed") << QString("Hashed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass1Reader::testCompositeKey()
|
|
|
|
{
|
|
|
|
QString name = "CompositeKey";
|
|
|
|
|
|
|
|
KeePass1Reader reader;
|
|
|
|
|
|
|
|
QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
QString keyFilename = QString("%1/FileKeyHex.key").arg(QString(KEEPASSX_TEST_DATA_DIR));
|
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = reader.readDatabase(dbFilename, "mypassword", keyFilename);
|
2012-05-10 13:34:55 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
QCOMPARE(db->rootGroup()->children().size(), 1);
|
|
|
|
QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
|
|
|
|
|
2012-05-10 17:00:29 -04:00
|
|
|
reopenDatabase(db, "mypassword", keyFilename);
|
2012-05-10 13:34:55 -04:00
|
|
|
}
|
|
|
|
|
2012-05-10 15:06:33 -04:00
|
|
|
void TestKeePass1Reader::testTwofish()
|
|
|
|
{
|
|
|
|
QString name = "Twofish";
|
|
|
|
|
|
|
|
KeePass1Reader reader;
|
|
|
|
|
|
|
|
QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = reader.readDatabase(dbFilename, "masterpw", 0);
|
2012-05-10 15:06:33 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
QCOMPARE(db->rootGroup()->children().size(), 1);
|
|
|
|
QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
|
|
|
|
}
|
|
|
|
|
2012-05-10 15:29:10 -04:00
|
|
|
void TestKeePass1Reader::testCP1252Password()
|
|
|
|
{
|
|
|
|
QString name = "CP-1252";
|
|
|
|
|
|
|
|
KeePass1Reader reader;
|
|
|
|
|
|
|
|
QString dbFilename = QString("%1/%2.kdb").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
QString password = QString::fromUtf8("\xe2\x80\x9e\x70\x61\x73\x73\x77\x6f\x72\x64\xe2\x80\x9d");
|
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = reader.readDatabase(dbFilename, password, 0);
|
2012-05-10 15:29:10 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
QCOMPARE(db->rootGroup()->children().size(), 1);
|
|
|
|
QCOMPARE(db->rootGroup()->children().at(0)->name(), name);
|
|
|
|
}
|
|
|
|
|
2012-05-10 05:44:25 -04:00
|
|
|
void TestKeePass1Reader::cleanupTestCase()
|
|
|
|
{
|
2012-05-08 16:03:59 -04:00
|
|
|
}
|
|
|
|
|
2012-05-09 17:14:27 -04:00
|
|
|
QDateTime TestKeePass1Reader::genDT(int year, int month, int day, int hour, int min)
|
|
|
|
{
|
|
|
|
QDate date(year, month, day);
|
|
|
|
QTime time(hour, min, 0);
|
|
|
|
return QDateTime(date, time, Qt::UTC);
|
|
|
|
}
|
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
void TestKeePass1Reader::reopenDatabase(QSharedPointer<Database> db,
|
|
|
|
const QString& password,
|
|
|
|
const QString& keyfileName)
|
2012-05-10 17:00:29 -04:00
|
|
|
{
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QIODevice::ReadWrite);
|
|
|
|
|
|
|
|
KeePass2Writer writer;
|
2018-11-22 05:47:31 -05:00
|
|
|
writer.writeDatabase(&buffer, db.data());
|
2013-06-30 08:43:02 -04:00
|
|
|
QVERIFY(!writer.hasError());
|
2012-05-10 17:00:29 -04:00
|
|
|
QVERIFY(buffer.seek(0));
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
2018-11-01 22:38:55 -04:00
|
|
|
if (!password.isEmpty()) {
|
2018-05-13 17:21:43 -04:00
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create(password));
|
2012-05-10 17:00:29 -04:00
|
|
|
}
|
|
|
|
if (!keyfileName.isEmpty()) {
|
2018-05-13 17:21:43 -04:00
|
|
|
auto fileKey = QSharedPointer<FileKey>::create();
|
|
|
|
QVERIFY(fileKey->load(keyfileName));
|
|
|
|
key->addKey(fileKey);
|
2012-05-10 17:00:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto newDb = QSharedPointer<Database>::create();
|
|
|
|
QVERIFY(reader.readDatabase(&buffer, key, newDb.data()));
|
2012-05-10 17:00:29 -04:00
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
}
|