2010-08-13 12:08:06 -04:00
|
|
|
/*
|
2018-01-17 14:52:29 -05:00
|
|
|
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
|
2010-08-13 12:08:06 -04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
#include "TestKeePass2Format.h"
|
2018-01-24 07:22:20 -05:00
|
|
|
#include "TestGlobal.h"
|
2018-10-19 14:22:28 -04:00
|
|
|
#include "mock/MockClock.h"
|
2010-11-21 17:06:30 -05:00
|
|
|
|
2010-08-13 12:08:06 -04:00
|
|
|
#include "core/Metadata.h"
|
2018-01-17 14:52:29 -05:00
|
|
|
#include "crypto/Crypto.h"
|
2018-01-06 22:08:32 -05:00
|
|
|
#include "format/KdbxXmlReader.h"
|
2020-01-09 20:11:43 -05:00
|
|
|
#include "keys/FileKey.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "keys/PasswordKey.h"
|
2020-01-09 20:11:43 -05:00
|
|
|
#include "mock/MockChallengeResponseKey.h"
|
2018-01-17 14:52:29 -05:00
|
|
|
|
|
|
|
#include "FailDevice.h"
|
2010-08-13 12:08:06 -04:00
|
|
|
#include "config-keepassx-tests.h"
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::initTestCase()
|
2018-01-17 12:15:37 -05:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
QVERIFY(Crypto::init());
|
2011-07-07 06:45:14 -04:00
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
// read raw XML database
|
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
2018-11-22 05:47:31 -05:00
|
|
|
m_xmlDb = readXml(QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml"), true, hasError, errorString);
|
2018-01-17 14:52:29 -05:00
|
|
|
if (hasError) {
|
|
|
|
QFAIL(qPrintable(QString("Error while reading XML: ").append(errorString)));
|
2011-07-07 06:45:14 -04:00
|
|
|
}
|
2018-01-17 14:52:29 -05:00
|
|
|
QVERIFY(m_xmlDb.data());
|
|
|
|
|
|
|
|
// construct and write KDBX to buffer
|
2018-05-13 17:21:43 -04:00
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create("test"));
|
2018-01-17 14:52:29 -05:00
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
m_kdbxSourceDb = QSharedPointer<Database>::create();
|
2018-01-17 14:52:29 -05:00
|
|
|
m_kdbxSourceDb->setKey(key);
|
|
|
|
m_kdbxSourceDb->metadata()->setName("TESTDB");
|
|
|
|
Group* group = m_kdbxSourceDb->rootGroup();
|
2018-03-22 17:56:05 -04:00
|
|
|
group->setUuid(QUuid::createUuid());
|
2018-01-17 14:52:29 -05:00
|
|
|
group->setNotes("I'm a note!");
|
|
|
|
auto entry = new Entry();
|
|
|
|
entry->setPassword(QString::fromUtf8("\xc3\xa4\xa3\xb6\xc3\xbc\xe9\x9b\xbb\xe7\xb4\x85"));
|
2018-03-22 17:56:05 -04:00
|
|
|
entry->setUuid(QUuid::createUuid());
|
2018-01-17 14:52:29 -05:00
|
|
|
entry->attributes()->set("test", "protectedTest", true);
|
|
|
|
QVERIFY(entry->attributes()->isProtected("test"));
|
|
|
|
entry->attachments()->set("myattach.txt", QByteArray("this is an attachment"));
|
|
|
|
entry->attachments()->set("aaa.txt", QByteArray("also an attachment"));
|
|
|
|
entry->setGroup(group);
|
|
|
|
auto groupNew = new Group();
|
2018-03-22 17:56:05 -04:00
|
|
|
groupNew->setUuid(QUuid::createUuid());
|
2018-01-17 14:52:29 -05:00
|
|
|
groupNew->setName("TESTGROUP");
|
|
|
|
groupNew->setNotes("I'm a sub group note!");
|
|
|
|
groupNew->setParent(group);
|
|
|
|
|
|
|
|
m_kdbxTargetBuffer.open(QBuffer::ReadWrite);
|
|
|
|
writeKdbx(&m_kdbxTargetBuffer, m_kdbxSourceDb.data(), hasError, errorString);
|
|
|
|
if (hasError) {
|
|
|
|
QFAIL(qPrintable(QString("Error while writing database: ").append(errorString)));
|
2016-02-01 18:38:58 -05:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
// call sub class init method
|
|
|
|
initTestCaseImpl();
|
2016-02-01 18:38:58 -05:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlMetadata()
|
2010-08-13 12:08:06 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->generator(), QString("KeePass"));
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->name(), QString("ANAME"));
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->nameChanged(), MockClock::datetimeUtc(2010, 8, 8, 17, 24, 53));
|
2018-01-17 14:52:29 -05:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->description(), QString("ADESC"));
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->descriptionChanged(), MockClock::datetimeUtc(2010, 8, 8, 17, 27, 12));
|
2018-01-17 14:52:29 -05:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->defaultUserName(), QString("DEFUSERNAME"));
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->defaultUserNameChanged(), MockClock::datetimeUtc(2010, 8, 8, 17, 27, 45));
|
2018-01-17 14:52:29 -05:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->maintenanceHistoryDays(), 127);
|
2020-01-26 21:38:43 -05:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->color(), QString("#FFEF00"));
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->masterKeyChanged(), MockClock::datetimeUtc(2012, 4, 5, 17, 9, 34));
|
2018-01-17 14:52:29 -05:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->masterKeyChangeRec(), 101);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->masterKeyChangeForce(), -1);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->protectTitle(), false);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->protectUsername(), true);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->protectPassword(), false);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->protectUrl(), true);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->protectNotes(), false);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->recycleBinEnabled(), true);
|
|
|
|
QVERIFY(m_xmlDb->metadata()->recycleBin() != nullptr);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->recycleBin()->name(), QString("Recycle Bin"));
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->recycleBinChanged(), MockClock::datetimeUtc(2010, 8, 25, 16, 12, 57));
|
2018-01-17 14:52:29 -05:00
|
|
|
QVERIFY(m_xmlDb->metadata()->entryTemplatesGroup() == nullptr);
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->entryTemplatesGroupChanged(), MockClock::datetimeUtc(2010, 8, 8, 17, 24, 19));
|
2018-01-17 14:52:29 -05:00
|
|
|
QVERIFY(m_xmlDb->metadata()->lastSelectedGroup() != nullptr);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->lastSelectedGroup()->name(), QString("NewDatabase"));
|
|
|
|
QVERIFY(m_xmlDb->metadata()->lastTopVisibleGroup() == m_xmlDb->metadata()->lastSelectedGroup());
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->historyMaxItems(), -1);
|
|
|
|
QCOMPARE(m_xmlDb->metadata()->historyMaxSize(), 5242880);
|
2010-08-13 12:08:06 -04:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlCustomIcons()
|
2010-08-25 15:14:41 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
QCOMPARE(m_xmlDb->metadata()->customIcons().size(), 1);
|
2018-03-22 17:56:05 -04:00
|
|
|
QUuid uuid = QUuid::fromRfc4122(QByteArray::fromBase64("++vyI+daLk6omox4a6kQGA=="));
|
2018-01-17 14:52:29 -05:00
|
|
|
QVERIFY(m_xmlDb->metadata()->customIcons().contains(uuid));
|
|
|
|
QImage icon = m_xmlDb->metadata()->customIcon(uuid);
|
2012-01-01 15:52:54 -05:00
|
|
|
QCOMPARE(icon.width(), 16);
|
|
|
|
QCOMPARE(icon.height(), 16);
|
|
|
|
|
2012-04-18 16:08:22 -04:00
|
|
|
for (int x = 0; x < 16; x++) {
|
|
|
|
for (int y = 0; y < 16; y++) {
|
2012-01-01 15:52:54 -05:00
|
|
|
QRgb rgb = icon.pixel(x, y);
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(qRed(rgb), 128);
|
|
|
|
QCOMPARE(qGreen(rgb), 0);
|
|
|
|
QCOMPARE(qBlue(rgb), 128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlGroupRoot()
|
2010-08-13 12:08:06 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
const Group* group = m_xmlDb->rootGroup();
|
2010-08-25 15:14:41 -04:00
|
|
|
QVERIFY(group);
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(group->uuid(), QUuid::fromRfc4122(QByteArray::fromBase64("lmU+9n0aeESKZvcEze+bRg==")));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(group->name(), QString("NewDatabase"));
|
|
|
|
QCOMPARE(group->notes(), QString(""));
|
|
|
|
QCOMPARE(group->iconNumber(), 49);
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(group->iconUuid(), QUuid());
|
2010-08-25 15:14:41 -04:00
|
|
|
QVERIFY(group->isExpanded());
|
|
|
|
TimeInfo ti = group->timeInfo();
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(ti.lastModificationTime(), MockClock::datetimeUtc(2010, 8, 8, 17, 24, 27));
|
|
|
|
QCOMPARE(ti.creationTime(), MockClock::datetimeUtc(2010, 8, 7, 17, 24, 27));
|
|
|
|
QCOMPARE(ti.lastAccessTime(), MockClock::datetimeUtc(2010, 8, 9, 9, 9, 44));
|
|
|
|
QCOMPARE(ti.expiryTime(), MockClock::datetimeUtc(2010, 8, 8, 17, 24, 17));
|
2010-08-18 05:05:46 -04:00
|
|
|
QVERIFY(!ti.expires());
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(ti.usageCount(), 52);
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(ti.locationChanged(), MockClock::datetimeUtc(2010, 8, 8, 17, 24, 27));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(group->defaultAutoTypeSequence(), QString(""));
|
2011-07-07 06:45:14 -04:00
|
|
|
QCOMPARE(group->autoTypeEnabled(), Group::Inherit);
|
2012-05-13 07:33:55 -04:00
|
|
|
QCOMPARE(group->searchingEnabled(), Group::Inherit);
|
2018-10-31 23:27:38 -04:00
|
|
|
QCOMPARE(group->lastTopVisibleEntry()->uuid(),
|
|
|
|
QUuid::fromRfc4122(QByteArray::fromBase64("+wSUOv6qf0OzW8/ZHAs2sA==")));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(group->children().size(), 3);
|
2018-01-17 14:52:29 -05:00
|
|
|
QVERIFY(m_xmlDb->metadata()->recycleBin() == m_xmlDb->rootGroup()->children().at(2));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
|
|
|
QCOMPARE(group->entries().size(), 2);
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlGroup1()
|
2010-08-25 15:14:41 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
const Group* group = m_xmlDb->rootGroup()->children().at(0);
|
2010-08-25 15:14:41 -04:00
|
|
|
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(group->uuid(), QUuid::fromRfc4122(QByteArray::fromBase64("AaUYVdXsI02h4T1RiAlgtg==")));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(group->name(), QString("General"));
|
|
|
|
QCOMPARE(group->notes(), QString("Group Notez"));
|
|
|
|
QCOMPARE(group->iconNumber(), 48);
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(group->iconUuid(), QUuid());
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(group->isExpanded(), true);
|
|
|
|
QCOMPARE(group->defaultAutoTypeSequence(), QString("{Password}{ENTER}"));
|
2011-07-07 06:45:14 -04:00
|
|
|
QCOMPARE(group->autoTypeEnabled(), Group::Enable);
|
2012-05-13 07:33:55 -04:00
|
|
|
QCOMPARE(group->searchingEnabled(), Group::Disable);
|
2010-08-25 15:14:41 -04:00
|
|
|
QVERIFY(!group->lastTopVisibleEntry());
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlGroup2()
|
2010-08-25 15:14:41 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
const Group* group = m_xmlDb->rootGroup()->children().at(1);
|
2010-08-25 15:14:41 -04:00
|
|
|
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(group->uuid(), QUuid::fromRfc4122(QByteArray::fromBase64("1h4NtL5DK0yVyvaEnN//4A==")));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(group->name(), QString("Windows"));
|
|
|
|
QCOMPARE(group->isExpanded(), false);
|
|
|
|
|
|
|
|
QCOMPARE(group->children().size(), 1);
|
|
|
|
const Group* child = group->children().first();
|
|
|
|
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(child->uuid(), QUuid::fromRfc4122(QByteArray::fromBase64("HoYE/BjLfUSW257pCHJ/eA==")));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(child->name(), QString("Subsub"));
|
|
|
|
QCOMPARE(child->entries().size(), 1);
|
|
|
|
|
|
|
|
const Entry* entry = child->entries().first();
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(entry->uuid(), QUuid::fromRfc4122(QByteArray::fromBase64("GZpdQvGXOU2kaKRL/IVAGg==")));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->title(), QString("Subsub Entry"));
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlEntry1()
|
2010-08-25 15:14:41 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
const Entry* entry = m_xmlDb->rootGroup()->entries().at(0);
|
2010-08-25 15:14:41 -04:00
|
|
|
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(entry->uuid(), QUuid::fromRfc4122(QByteArray::fromBase64("+wSUOv6qf0OzW8/ZHAs2sA==")));
|
2012-04-21 10:45:46 -04:00
|
|
|
QCOMPARE(entry->historyItems().size(), 2);
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->iconNumber(), 0);
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(entry->iconUuid(), QUuid());
|
2020-01-26 21:38:43 -05:00
|
|
|
QVERIFY(entry->foregroundColor().isEmpty());
|
|
|
|
QVERIFY(entry->backgroundColor().isEmpty());
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->overrideUrl(), QString(""));
|
2012-04-21 10:45:46 -04:00
|
|
|
QCOMPARE(entry->tags(), QString("a b c"));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
|
|
|
const TimeInfo ti = entry->timeInfo();
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(ti.lastModificationTime(), MockClock::datetimeUtc(2010, 8, 25, 16, 19, 25));
|
|
|
|
QCOMPARE(ti.creationTime(), MockClock::datetimeUtc(2010, 8, 25, 16, 13, 54));
|
|
|
|
QCOMPARE(ti.lastAccessTime(), MockClock::datetimeUtc(2010, 8, 25, 16, 19, 25));
|
|
|
|
QCOMPARE(ti.expiryTime(), MockClock::datetimeUtc(2010, 8, 25, 16, 12, 57));
|
2010-08-25 15:14:41 -04:00
|
|
|
QVERIFY(!ti.expires());
|
|
|
|
QCOMPARE(ti.usageCount(), 8);
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(ti.locationChanged(), MockClock::datetimeUtc(2010, 8, 25, 16, 13, 54));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
QList<QString> attrs = entry->attributes()->keys();
|
|
|
|
QCOMPARE(entry->attributes()->value("Notes"), QString("Notes"));
|
2012-04-21 10:45:46 -04:00
|
|
|
QVERIFY(!entry->attributes()->isProtected("Notes"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("Notes"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("Password"), QString("Password"));
|
2012-04-21 10:45:46 -04:00
|
|
|
QVERIFY(!entry->attributes()->isProtected("Password"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("Password"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("Title"), QString("Sample Entry 1"));
|
2012-04-21 10:45:46 -04:00
|
|
|
QVERIFY(!entry->attributes()->isProtected("Title"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("Title"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("URL"), QString(""));
|
2012-04-21 10:45:46 -04:00
|
|
|
QVERIFY(entry->attributes()->isProtected("URL"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("URL"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("UserName"), QString("User Name"));
|
2012-04-21 10:45:46 -04:00
|
|
|
QVERIFY(entry->attributes()->isProtected("UserName"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("UserName"));
|
2010-08-25 15:14:41 -04:00
|
|
|
QVERIFY(attrs.isEmpty());
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->title(), entry->attributes()->value("Title"));
|
|
|
|
QCOMPARE(entry->url(), entry->attributes()->value("URL"));
|
|
|
|
QCOMPARE(entry->username(), entry->attributes()->value("UserName"));
|
|
|
|
QCOMPARE(entry->password(), entry->attributes()->value("Password"));
|
|
|
|
QCOMPARE(entry->notes(), entry->attributes()->value("Notes"));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
QCOMPARE(entry->attachments()->keys().size(), 1);
|
|
|
|
QCOMPARE(entry->attachments()->value("myattach.txt"), QByteArray("abcdefghijk"));
|
|
|
|
QCOMPARE(entry->historyItems().at(0)->attachments()->keys().size(), 1);
|
|
|
|
QCOMPARE(entry->historyItems().at(0)->attachments()->value("myattach.txt"), QByteArray("0123456789"));
|
|
|
|
QCOMPARE(entry->historyItems().at(1)->attachments()->keys().size(), 1);
|
|
|
|
QCOMPARE(entry->historyItems().at(1)->attachments()->value("myattach.txt"), QByteArray("abcdefghijk"));
|
|
|
|
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->autoTypeEnabled(), false);
|
|
|
|
QCOMPARE(entry->autoTypeObfuscation(), 0);
|
|
|
|
QCOMPARE(entry->defaultAutoTypeSequence(), QString(""));
|
2012-07-16 03:54:04 -04:00
|
|
|
QCOMPARE(entry->autoTypeAssociations()->size(), 1);
|
|
|
|
const AutoTypeAssociations::Association assoc1 = entry->autoTypeAssociations()->get(0);
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(assoc1.window, QString("Target Window"));
|
|
|
|
QCOMPARE(assoc1.sequence, QString(""));
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlEntry2()
|
2010-08-25 15:14:41 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
const Entry* entry = m_xmlDb->rootGroup()->entries().at(1);
|
2010-08-25 15:14:41 -04:00
|
|
|
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(entry->uuid(), QUuid::fromRfc4122(QByteArray::fromBase64("4jbADG37hkiLh2O0qUdaOQ==")));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->iconNumber(), 0);
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(entry->iconUuid(), QUuid::fromRfc4122(QByteArray::fromBase64("++vyI+daLk6omox4a6kQGA==")));
|
2012-05-11 06:39:06 -04:00
|
|
|
// TODO: test entry->icon()
|
2020-01-26 21:38:43 -05:00
|
|
|
QCOMPARE(entry->foregroundColor(), QString("#FF0000"));
|
|
|
|
QCOMPARE(entry->backgroundColor(), QString("#FFFF00"));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->overrideUrl(), QString("http://override.net/"));
|
2012-04-21 10:45:46 -04:00
|
|
|
QCOMPARE(entry->tags(), QString(""));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
|
|
|
const TimeInfo ti = entry->timeInfo();
|
|
|
|
QCOMPARE(ti.usageCount(), 7);
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
QList<QString> attrs = entry->attributes()->keys();
|
|
|
|
QCOMPARE(entry->attributes()->value("CustomString"), QString("isavalue"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("CustomString"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("Notes"), QString(""));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("Notes"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("Password"), QString("Jer60Hz8o9XHvxBGcRqT"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("Password"));
|
2012-05-11 06:39:06 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("Protected String"), QString("y")); // TODO: should have a protection attribute
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("Protected String"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("Title"), QString("Sample Entry 2"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("Title"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("URL"), QString("http://www.keepassx.org/"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("URL"));
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("UserName"), QString("notDEFUSERNAME"));
|
2012-04-06 13:33:29 -04:00
|
|
|
QVERIFY(attrs.removeOne("UserName"));
|
2010-08-25 15:14:41 -04:00
|
|
|
QVERIFY(attrs.isEmpty());
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attachments()->keys().size(), 1);
|
2013-11-24 15:19:20 -05:00
|
|
|
QCOMPARE(QString::fromLatin1(entry->attachments()->value("myattach.txt")), QString("abcdefghijk"));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
|
|
|
QCOMPARE(entry->autoTypeEnabled(), true);
|
|
|
|
QCOMPARE(entry->autoTypeObfuscation(), 1);
|
|
|
|
QCOMPARE(entry->defaultAutoTypeSequence(), QString("{USERNAME}{TAB}{PASSWORD}{ENTER}"));
|
2012-07-16 03:54:04 -04:00
|
|
|
QCOMPARE(entry->autoTypeAssociations()->size(), 2);
|
|
|
|
const AutoTypeAssociations::Association assoc1 = entry->autoTypeAssociations()->get(0);
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(assoc1.window, QString("Target Window"));
|
|
|
|
QCOMPARE(assoc1.sequence, QString("{Title}{UserName}"));
|
2012-07-16 03:54:04 -04:00
|
|
|
const AutoTypeAssociations::Association assoc2 = entry->autoTypeAssociations()->get(1);
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(assoc2.window, QString("Target Window 2"));
|
|
|
|
QCOMPARE(assoc2.sequence, QString("{Title}{UserName} test"));
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlEntryHistory()
|
2010-08-25 15:14:41 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
const Entry* entryMain = m_xmlDb->rootGroup()->entries().at(0);
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entryMain->historyItems().size(), 2);
|
|
|
|
|
|
|
|
{
|
|
|
|
const Entry* entry = entryMain->historyItems().at(0);
|
|
|
|
QCOMPARE(entry->uuid(), entryMain->uuid());
|
|
|
|
QVERIFY(!entry->parent());
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(entry->timeInfo().lastModificationTime(), MockClock::datetimeUtc(2010, 8, 25, 16, 13, 54));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->timeInfo().usageCount(), 3);
|
|
|
|
QCOMPARE(entry->title(), QString("Sample Entry"));
|
|
|
|
QCOMPARE(entry->url(), QString("http://www.somesite.com/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const Entry* entry = entryMain->historyItems().at(1);
|
|
|
|
QCOMPARE(entry->uuid(), entryMain->uuid());
|
|
|
|
QVERIFY(!entry->parent());
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(entry->timeInfo().lastModificationTime(), MockClock::datetimeUtc(2010, 8, 25, 16, 15, 43));
|
2010-08-25 15:14:41 -04:00
|
|
|
QCOMPARE(entry->timeInfo().usageCount(), 7);
|
|
|
|
QCOMPARE(entry->title(), QString("Sample Entry 1"));
|
|
|
|
QCOMPARE(entry->url(), QString("http://www.somesite.com/"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlDeletedObjects()
|
2010-08-25 15:14:41 -04:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
QList<DeletedObject> objList = m_xmlDb->deletedObjects();
|
2010-08-25 15:14:41 -04:00
|
|
|
DeletedObject delObj;
|
|
|
|
|
|
|
|
delObj = objList.takeFirst();
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(delObj.uuid, QUuid::fromRfc4122(QByteArray::fromBase64("5K/bzWCSmkCv5OZxYl4N/w==")));
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(delObj.deletionTime, MockClock::datetimeUtc(2010, 8, 25, 16, 14, 12));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
|
|
|
delObj = objList.takeFirst();
|
2018-03-22 17:56:05 -04:00
|
|
|
QCOMPARE(delObj.uuid, QUuid::fromRfc4122(QByteArray::fromBase64("80h8uSNWgkKhKCp1TgXF7g==")));
|
2018-10-19 14:22:28 -04:00
|
|
|
QCOMPARE(delObj.deletionTime, MockClock::datetimeUtc(2010, 8, 25, 16, 14, 14));
|
2010-08-25 15:14:41 -04:00
|
|
|
|
|
|
|
QVERIFY(objList.isEmpty());
|
2010-08-13 12:08:06 -04:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlBroken()
|
2013-04-20 13:03:27 -04:00
|
|
|
{
|
|
|
|
QFETCH(QString, baseName);
|
2014-12-01 15:52:51 -05:00
|
|
|
QFETCH(bool, strictMode);
|
|
|
|
QFETCH(bool, expectError);
|
2013-04-20 13:03:27 -04:00
|
|
|
|
|
|
|
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, baseName);
|
|
|
|
QVERIFY(QFile::exists(xmlFile));
|
2017-11-12 13:55:03 -05:00
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = readXml(xmlFile, strictMode, hasError, errorString);
|
2017-11-12 13:55:03 -05:00
|
|
|
if (hasError) {
|
|
|
|
qWarning("Reader error: %s", qPrintable(errorString));
|
|
|
|
}
|
|
|
|
QCOMPARE(hasError, expectError);
|
2013-04-20 13:03:27 -04:00
|
|
|
}
|
|
|
|
|
2018-02-21 21:27:55 -05:00
|
|
|
// clang-format off
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlBroken_data()
|
2013-04-20 13:03:27 -04:00
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("baseName");
|
2014-12-01 15:52:51 -05:00
|
|
|
QTest::addColumn<bool>("strictMode");
|
|
|
|
QTest::addColumn<bool>("expectError");
|
|
|
|
|
2018-01-17 12:15:37 -05:00
|
|
|
// testfile strict? error?
|
|
|
|
QTest::newRow("BrokenNoGroupUuid (strict)") << "BrokenNoGroupUuid" << true << true;
|
|
|
|
QTest::newRow("BrokenNoGroupUuid (not strict)") << "BrokenNoGroupUuid" << false << false;
|
|
|
|
QTest::newRow("BrokenNoEntryUuid (strict)") << "BrokenNoEntryUuid" << true << true;
|
|
|
|
QTest::newRow("BrokenNoEntryUuid (not strict)") << "BrokenNoEntryUuid" << false << false;
|
|
|
|
QTest::newRow("BrokenNoRootGroup (strict)") << "BrokenNoRootGroup" << true << true;
|
|
|
|
QTest::newRow("BrokenNoRootGroup (not strict)") << "BrokenNoRootGroup" << false << true;
|
|
|
|
QTest::newRow("BrokenTwoRoots (strict)") << "BrokenTwoRoots" << true << true;
|
|
|
|
QTest::newRow("BrokenTwoRoots (not strict)") << "BrokenTwoRoots" << false << true;
|
|
|
|
QTest::newRow("BrokenTwoRootGroups (strict)") << "BrokenTwoRootGroups" << true << true;
|
|
|
|
QTest::newRow("BrokenTwoRootGroups (not strict)") << "BrokenTwoRootGroups" << false << true;
|
|
|
|
QTest::newRow("BrokenGroupReference (strict)") << "BrokenGroupReference" << true << false;
|
|
|
|
QTest::newRow("BrokenGroupReference (not strict)") << "BrokenGroupReference" << false << false;
|
|
|
|
QTest::newRow("BrokenDeletedObjects (strict)") << "BrokenDeletedObjects" << true << true;
|
|
|
|
QTest::newRow("BrokenDeletedObjects (not strict)") << "BrokenDeletedObjects" << false << false;
|
|
|
|
QTest::newRow("BrokenDifferentEntryHistoryUuid (strict)") << "BrokenDifferentEntryHistoryUuid" << true << true;
|
2016-08-02 18:00:56 -04:00
|
|
|
QTest::newRow("BrokenDifferentEntryHistoryUuid (not strict)") << "BrokenDifferentEntryHistoryUuid" << false << false;
|
2013-04-20 13:03:27 -04:00
|
|
|
}
|
2018-02-21 21:27:55 -05:00
|
|
|
// clang-format on
|
2013-04-20 13:03:27 -04:00
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlEmptyUuids()
|
2015-07-13 15:00:12 -04:00
|
|
|
{
|
2017-11-12 13:55:03 -05:00
|
|
|
|
2015-07-13 15:00:12 -04:00
|
|
|
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "EmptyUuids");
|
|
|
|
QVERIFY(QFile::exists(xmlFile));
|
2017-11-12 13:55:03 -05:00
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = readXml(xmlFile, true, hasError, errorString);
|
2017-11-12 13:55:03 -05:00
|
|
|
if (hasError) {
|
|
|
|
qWarning("Reader error: %s", qPrintable(errorString));
|
|
|
|
}
|
|
|
|
QVERIFY(!hasError);
|
2015-07-13 15:00:12 -04:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlInvalidXmlChars()
|
2016-01-24 11:20:16 -05:00
|
|
|
{
|
|
|
|
QScopedPointer<Database> dbWrite(new Database());
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
QString strPlainInvalid =
|
|
|
|
QString().append(QChar(0x02)).append(QChar(0x19)).append(QChar(0xFFFE)).append(QChar(0xFFFF));
|
|
|
|
QString strPlainValid = QString()
|
|
|
|
.append(QChar(0x09))
|
|
|
|
.append(QChar(0x0A))
|
|
|
|
.append(QChar(0x20))
|
|
|
|
.append(QChar(0xD7FF))
|
|
|
|
.append(QChar(0xE000))
|
|
|
|
.append(QChar(0xFFFD));
|
2016-02-01 18:38:58 -05:00
|
|
|
// U+10437 in UTF-16: D801 DC37
|
|
|
|
// high low surrogate
|
|
|
|
QString strSingleHighSurrogate1 = QString().append(QChar(0xD801));
|
|
|
|
QString strSingleHighSurrogate2 = QString().append(QChar(0x31)).append(QChar(0xD801)).append(QChar(0x32));
|
|
|
|
QString strHighHighSurrogate = QString().append(QChar(0xD801)).append(QChar(0xD801));
|
|
|
|
QString strSingleLowSurrogate1 = QString().append(QChar(0xDC37));
|
|
|
|
QString strSingleLowSurrogate2 = QString().append(QChar((0x31))).append(QChar(0xDC37)).append(QChar(0x32));
|
|
|
|
QString strLowLowSurrogate = QString().append(QChar(0xDC37)).append(QChar(0xDC37));
|
|
|
|
QString strSurrogateValid1 = QString().append(QChar(0xD801)).append(QChar(0xDC37));
|
2018-03-31 16:01:30 -04:00
|
|
|
QString strSurrogateValid2 =
|
|
|
|
QString().append(QChar(0x31)).append(QChar(0xD801)).append(QChar(0xDC37)).append(QChar(0x32));
|
2016-02-01 18:38:58 -05:00
|
|
|
|
2018-01-17 12:15:37 -05:00
|
|
|
auto entry = new Entry();
|
2018-03-22 17:56:05 -04:00
|
|
|
entry->setUuid(QUuid::createUuid());
|
2016-01-24 11:20:16 -05:00
|
|
|
entry->setGroup(dbWrite->rootGroup());
|
2016-02-01 18:38:58 -05:00
|
|
|
entry->attributes()->set("PlainInvalid", strPlainInvalid);
|
|
|
|
entry->attributes()->set("PlainValid", strPlainValid);
|
|
|
|
entry->attributes()->set("SingleHighSurrogate1", strSingleHighSurrogate1);
|
|
|
|
entry->attributes()->set("SingleHighSurrogate2", strSingleHighSurrogate2);
|
|
|
|
entry->attributes()->set("HighHighSurrogate", strHighHighSurrogate);
|
|
|
|
entry->attributes()->set("SingleLowSurrogate1", strSingleLowSurrogate1);
|
|
|
|
entry->attributes()->set("SingleLowSurrogate2", strSingleLowSurrogate2);
|
|
|
|
entry->attributes()->set("LowLowSurrogate", strLowLowSurrogate);
|
|
|
|
entry->attributes()->set("SurrogateValid1", strSurrogateValid1);
|
|
|
|
entry->attributes()->set("SurrogateValid2", strSurrogateValid2);
|
2016-01-24 11:20:16 -05:00
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QIODevice::ReadWrite);
|
2017-11-12 13:55:03 -05:00
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
2018-01-17 14:52:29 -05:00
|
|
|
writeXml(&buffer, dbWrite.data(), hasError, errorString);
|
2017-11-12 13:55:03 -05:00
|
|
|
QVERIFY(!hasError);
|
2016-01-24 11:20:16 -05:00
|
|
|
buffer.seek(0);
|
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
auto dbRead = readXml(&buffer, true, hasError, errorString);
|
2017-11-12 13:55:03 -05:00
|
|
|
if (hasError) {
|
|
|
|
qWarning("Database read error: %s", qPrintable(errorString));
|
2016-01-24 11:20:16 -05:00
|
|
|
}
|
2017-11-12 13:55:03 -05:00
|
|
|
QVERIFY(!hasError);
|
2018-01-17 12:15:37 -05:00
|
|
|
QVERIFY(dbRead.data());
|
2016-01-24 11:20:16 -05:00
|
|
|
QCOMPARE(dbRead->rootGroup()->entries().size(), 1);
|
2016-02-01 18:38:58 -05:00
|
|
|
Entry* entryRead = dbRead->rootGroup()->entries().at(0);
|
|
|
|
EntryAttributes* attrRead = entryRead->attributes();
|
|
|
|
|
2018-01-24 07:22:20 -05:00
|
|
|
QCOMPARE(attrRead->value("PlainInvalid"), QString());
|
|
|
|
QCOMPARE(attrRead->value("PlainValid"), strPlainValid);
|
|
|
|
QCOMPARE(attrRead->value("SingleHighSurrogate1"), QString());
|
|
|
|
QCOMPARE(attrRead->value("SingleHighSurrogate2"), QString("12"));
|
|
|
|
QCOMPARE(attrRead->value("HighHighSurrogate"), QString());
|
|
|
|
QCOMPARE(attrRead->value("SingleLowSurrogate1"), QString());
|
|
|
|
QCOMPARE(attrRead->value("SingleLowSurrogate2"), QString("12"));
|
|
|
|
QCOMPARE(attrRead->value("LowLowSurrogate"), QString());
|
|
|
|
QCOMPARE(attrRead->value("SurrogateValid1"), strSurrogateValid1);
|
|
|
|
QCOMPARE(attrRead->value("SurrogateValid2"), strSurrogateValid2);
|
2016-01-24 11:20:16 -05:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
void TestKeePass2Format::testXmlRepairUuidHistoryItem()
|
2016-08-02 18:00:56 -04:00
|
|
|
{
|
|
|
|
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "BrokenDifferentEntryHistoryUuid");
|
|
|
|
QVERIFY(QFile::exists(xmlFile));
|
2017-11-12 13:55:03 -05:00
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = readXml(xmlFile, false, hasError, errorString);
|
2017-11-12 13:55:03 -05:00
|
|
|
if (hasError) {
|
|
|
|
qWarning("Database read error: %s", qPrintable(errorString));
|
2016-08-02 18:00:56 -04:00
|
|
|
}
|
2017-11-12 13:55:03 -05:00
|
|
|
QVERIFY(!hasError);
|
2016-08-02 18:00:56 -04:00
|
|
|
|
2017-11-12 13:55:03 -05:00
|
|
|
QList<Entry*> entries = db->rootGroup()->entries();
|
2016-08-02 18:00:56 -04:00
|
|
|
QCOMPARE(entries.size(), 1);
|
|
|
|
Entry* entry = entries.at(0);
|
|
|
|
|
|
|
|
QList<Entry*> historyItems = entry->historyItems();
|
|
|
|
QCOMPARE(historyItems.size(), 1);
|
|
|
|
Entry* historyItem = historyItems.at(0);
|
|
|
|
|
|
|
|
QVERIFY(!entry->uuid().isNull());
|
|
|
|
QVERIFY(!historyItem->uuid().isNull());
|
|
|
|
QCOMPARE(historyItem->uuid(), entry->uuid());
|
2012-04-22 17:09:52 -04:00
|
|
|
}
|
2018-01-17 14:52:29 -05:00
|
|
|
|
|
|
|
void TestKeePass2Format::testReadBackTargetDb()
|
|
|
|
{
|
|
|
|
// read back previously constructed KDBX
|
2018-05-13 17:21:43 -04:00
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create("test"));
|
2018-01-17 14:52:29 -05:00
|
|
|
|
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
|
|
|
|
|
|
|
m_kdbxTargetBuffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
m_kdbxTargetDb = QSharedPointer<Database>::create();
|
2018-01-17 14:52:29 -05:00
|
|
|
readKdbx(&m_kdbxTargetBuffer, key, m_kdbxTargetDb, hasError, errorString);
|
|
|
|
if (hasError) {
|
|
|
|
QFAIL(qPrintable(QString("Error while reading database: ").append(errorString)));
|
|
|
|
}
|
|
|
|
QVERIFY(m_kdbxTargetDb.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Format::testKdbxBasic()
|
|
|
|
{
|
|
|
|
QCOMPARE(m_kdbxTargetDb->metadata()->name(), m_kdbxSourceDb->metadata()->name());
|
|
|
|
QVERIFY(m_kdbxTargetDb->rootGroup());
|
|
|
|
QCOMPARE(m_kdbxTargetDb->rootGroup()->children()[0]->name(), m_kdbxSourceDb->rootGroup()->children()[0]->name());
|
|
|
|
QCOMPARE(m_kdbxTargetDb->rootGroup()->notes(), m_kdbxSourceDb->rootGroup()->notes());
|
|
|
|
QCOMPARE(m_kdbxTargetDb->rootGroup()->children()[0]->notes(), m_kdbxSourceDb->rootGroup()->children()[0]->notes());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Format::testKdbxProtectedAttributes()
|
|
|
|
{
|
|
|
|
QCOMPARE(m_kdbxTargetDb->rootGroup()->entries().size(), 1);
|
|
|
|
Entry* entry = m_kdbxTargetDb->rootGroup()->entries().at(0);
|
|
|
|
QCOMPARE(entry->attributes()->value("test"), QString("protectedTest"));
|
|
|
|
QCOMPARE(entry->attributes()->isProtected("test"), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Format::testKdbxAttachments()
|
|
|
|
{
|
|
|
|
Entry* entry = m_kdbxTargetDb->rootGroup()->entries().at(0);
|
|
|
|
QCOMPARE(entry->attachments()->keys().size(), 2);
|
|
|
|
QCOMPARE(entry->attachments()->value("myattach.txt"), QByteArray("this is an attachment"));
|
|
|
|
QCOMPARE(entry->attachments()->value("aaa.txt"), QByteArray("also an attachment"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Format::testKdbxNonAsciiPasswords()
|
|
|
|
{
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(m_kdbxTargetDb->rootGroup()->entries()[0]->password(),
|
|
|
|
m_kdbxSourceDb->rootGroup()->entries()[0]->password());
|
2018-01-17 14:52:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Format::testKdbxDeviceFailure()
|
|
|
|
{
|
2018-05-13 17:21:43 -04:00
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create("test"));
|
2018-01-17 14:52:29 -05:00
|
|
|
QScopedPointer<Database> db(new Database());
|
|
|
|
db->setKey(key);
|
|
|
|
// Disable compression so we write a predictable number of bytes.
|
2018-11-22 05:47:31 -05:00
|
|
|
db->setCompressionAlgorithm(Database::CompressionNone);
|
2018-01-17 14:52:29 -05:00
|
|
|
|
|
|
|
auto entry = new Entry();
|
|
|
|
entry->setParent(db->rootGroup());
|
|
|
|
QByteArray attachment(4096, 'Z');
|
|
|
|
entry->attachments()->set("test", attachment);
|
|
|
|
|
|
|
|
FailDevice failDevice(512);
|
|
|
|
QVERIFY(failDevice.open(QIODevice::WriteOnly));
|
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
|
|
|
writeKdbx(&failDevice, db.data(), hasError, errorString);
|
|
|
|
QVERIFY(hasError);
|
|
|
|
QCOMPARE(errorString, QString("FAILDEVICE"));
|
|
|
|
}
|
2018-01-24 20:16:18 -05:00
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
Q_DECLARE_METATYPE(QSharedPointer<CompositeKey>)
|
|
|
|
|
|
|
|
void TestKeePass2Format::testKdbxKeyChange()
|
|
|
|
{
|
|
|
|
QFETCH(QSharedPointer<CompositeKey>, key1);
|
|
|
|
QFETCH(QSharedPointer<CompositeKey>, key2);
|
|
|
|
|
|
|
|
bool hasError;
|
|
|
|
QString errorString;
|
|
|
|
|
|
|
|
// write new database
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QBuffer::ReadWrite);
|
|
|
|
buffer.seek(0);
|
|
|
|
QSharedPointer<Database> db(new Database());
|
|
|
|
db->changeKdf(fastKdf(KeePass2::uuidToKdf(m_kdbxSourceDb->kdf()->uuid())));
|
|
|
|
db->setRootGroup(m_kdbxSourceDb->rootGroup()->clone(Entry::CloneNoFlags, Group::CloneIncludeEntries));
|
|
|
|
|
|
|
|
db->setKey(key1);
|
|
|
|
writeKdbx(&buffer, db.data(), hasError, errorString);
|
|
|
|
QVERIFY(!hasError);
|
|
|
|
|
|
|
|
// read database
|
|
|
|
db = QSharedPointer<Database>::create();
|
|
|
|
buffer.seek(0);
|
|
|
|
readKdbx(&buffer, key1, db, hasError, errorString);
|
|
|
|
if (hasError) {
|
|
|
|
QFAIL(qPrintable(QStringLiteral("Error while reading database: ").append(errorString)));
|
|
|
|
}
|
|
|
|
QVERIFY(db.data());
|
|
|
|
|
|
|
|
// change key
|
|
|
|
db->setKey(key2);
|
|
|
|
|
|
|
|
// write database
|
|
|
|
buffer.seek(0);
|
|
|
|
writeKdbx(&buffer, db.data(), hasError, errorString);
|
|
|
|
QVERIFY(!hasError);
|
|
|
|
|
|
|
|
// read database
|
|
|
|
db = QSharedPointer<Database>::create();
|
|
|
|
buffer.seek(0);
|
|
|
|
readKdbx(&buffer, key2, db, hasError, errorString);
|
|
|
|
if (hasError) {
|
|
|
|
QFAIL(qPrintable(QStringLiteral("Error while reading database: ").append(errorString)));
|
|
|
|
}
|
|
|
|
QVERIFY(db.data());
|
|
|
|
QVERIFY(db->rootGroup() != m_kdbxSourceDb->rootGroup());
|
|
|
|
QVERIFY(db->rootGroup()->uuid() == m_kdbxSourceDb->rootGroup()->uuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Format::testKdbxKeyChange_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QSharedPointer<CompositeKey>>("key1");
|
|
|
|
QTest::addColumn<QSharedPointer<CompositeKey>>("key2");
|
|
|
|
|
|
|
|
auto passwordKey1 = QSharedPointer<PasswordKey>::create("abc");
|
|
|
|
auto passwordKey2 = QSharedPointer<PasswordKey>::create("def");
|
|
|
|
|
|
|
|
QByteArray fileKeyBytes1("uvw");
|
|
|
|
QBuffer fileKeyBuffer1(&fileKeyBytes1);
|
|
|
|
fileKeyBuffer1.open(QBuffer::ReadOnly);
|
|
|
|
auto fileKey1 = QSharedPointer<FileKey>::create();
|
|
|
|
fileKey1->load(&fileKeyBuffer1);
|
|
|
|
|
|
|
|
QByteArray fileKeyBytes2("xzy");
|
|
|
|
QBuffer fileKeyBuffer2(&fileKeyBytes1);
|
|
|
|
fileKeyBuffer2.open(QBuffer::ReadOnly);
|
|
|
|
auto fileKey2 = QSharedPointer<FileKey>::create();
|
|
|
|
fileKey2->load(&fileKeyBuffer2);
|
|
|
|
|
|
|
|
auto crKey1 = QSharedPointer<MockChallengeResponseKey>::create(QByteArray("123"));
|
|
|
|
auto crKey2 = QSharedPointer<MockChallengeResponseKey>::create(QByteArray("456"));
|
|
|
|
|
|
|
|
// empty key
|
|
|
|
auto compositeKey0 = QSharedPointer<CompositeKey>::create();
|
|
|
|
|
|
|
|
// all in
|
|
|
|
auto compositeKey1_1 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey1_1->addKey(passwordKey1);
|
|
|
|
compositeKey1_1->addKey(fileKey1);
|
|
|
|
compositeKey1_1->addChallengeResponseKey(crKey1);
|
|
|
|
auto compositeKey1_2 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey1_2->addKey(passwordKey2);
|
|
|
|
compositeKey1_2->addKey(fileKey2);
|
|
|
|
compositeKey1_2->addChallengeResponseKey(crKey2);
|
|
|
|
|
|
|
|
QTest::newRow("Change: Empty Key -> Full Key") << compositeKey0 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: Full Key -> Empty Key") << compositeKey1_1 << compositeKey0;
|
|
|
|
QTest::newRow("Change: Full Key 1 -> Full Key 2") << compositeKey1_1 << compositeKey1_2;
|
|
|
|
|
|
|
|
// only password
|
|
|
|
auto compositeKey2_1 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey2_1->addKey(passwordKey1);
|
|
|
|
auto compositeKey2_2 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey2_2->addKey(passwordKey2);
|
|
|
|
|
|
|
|
QTest::newRow("Change: Password -> Empty Key") << compositeKey2_1 << compositeKey0;
|
|
|
|
QTest::newRow("Change: Empty Key -> Password") << compositeKey0 << compositeKey2_1;
|
|
|
|
QTest::newRow("Change: Full Key -> Password 1") << compositeKey1_1 << compositeKey2_1;
|
|
|
|
QTest::newRow("Change: Full Key -> Password 2") << compositeKey1_1 << compositeKey2_2;
|
|
|
|
QTest::newRow("Change: Password 1 -> Full Key") << compositeKey2_1 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: Password 2 -> Full Key") << compositeKey2_2 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: Password 1 -> Password 2") << compositeKey2_1 << compositeKey2_2;
|
|
|
|
|
|
|
|
// only key file
|
|
|
|
auto compositeKey3_1 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey3_1->addKey(fileKey1);
|
|
|
|
auto compositeKey3_2 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey3_2->addKey(fileKey2);
|
|
|
|
|
|
|
|
QTest::newRow("Change: Key File -> Empty Key") << compositeKey3_1 << compositeKey0;
|
|
|
|
QTest::newRow("Change: Empty Key -> Key File") << compositeKey0 << compositeKey3_1;
|
|
|
|
QTest::newRow("Change: Full Key -> Key File 1") << compositeKey1_1 << compositeKey3_1;
|
|
|
|
QTest::newRow("Change: Full Key -> Key File 2") << compositeKey1_1 << compositeKey3_2;
|
|
|
|
QTest::newRow("Change: Key File 1 -> Full Key") << compositeKey3_1 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: Key File 2 -> Full Key") << compositeKey3_2 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: Key File 1 -> Key File 2") << compositeKey3_1 << compositeKey3_2;
|
|
|
|
|
|
|
|
// only cr key
|
|
|
|
auto compositeKey4_1 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey4_1->addChallengeResponseKey(crKey1);
|
|
|
|
auto compositeKey4_2 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey4_2->addChallengeResponseKey(crKey2);
|
|
|
|
|
|
|
|
QTest::newRow("Change: CR Key -> Empty Key") << compositeKey4_1 << compositeKey0;
|
|
|
|
QTest::newRow("Change: Empty Key -> CR Key") << compositeKey0 << compositeKey4_1;
|
|
|
|
QTest::newRow("Change: Full Key -> CR Key 1") << compositeKey1_1 << compositeKey4_1;
|
|
|
|
QTest::newRow("Change: Full Key -> CR Key 2") << compositeKey1_1 << compositeKey4_2;
|
|
|
|
QTest::newRow("Change: CR Key 1 -> Full Key") << compositeKey4_1 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: CR Key 2 -> Full Key") << compositeKey4_2 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: CR Key 1 -> CR Key 2") << compositeKey4_1 << compositeKey4_2;
|
|
|
|
|
|
|
|
// rotate
|
|
|
|
QTest::newRow("Change: Password -> Key File") << compositeKey2_1 << compositeKey3_1;
|
|
|
|
QTest::newRow("Change: Key File -> Password") << compositeKey3_1 << compositeKey2_1;
|
|
|
|
QTest::newRow("Change: Password -> Key File") << compositeKey2_1 << compositeKey3_1;
|
|
|
|
QTest::newRow("Change: Key File -> Password") << compositeKey3_1 << compositeKey2_1;
|
|
|
|
QTest::newRow("Change: Password -> CR Key") << compositeKey2_1 << compositeKey4_1;
|
|
|
|
QTest::newRow("Change: CR Key -> Password") << compositeKey4_1 << compositeKey2_1;
|
|
|
|
QTest::newRow("Change: Key File -> CR Key") << compositeKey3_1 << compositeKey4_1;
|
|
|
|
QTest::newRow("Change: CR Key -> Key File") << compositeKey4_1 << compositeKey3_1;
|
|
|
|
|
|
|
|
// leave one out
|
|
|
|
auto compositeKey5_1 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey5_1->addKey(fileKey1);
|
|
|
|
compositeKey5_1->addChallengeResponseKey(crKey1);
|
|
|
|
auto compositeKey5_2 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey5_2->addKey(passwordKey1);
|
|
|
|
compositeKey5_2->addChallengeResponseKey(crKey1);
|
|
|
|
auto compositeKey5_3 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey5_3->addKey(passwordKey1);
|
|
|
|
compositeKey5_3->addKey(fileKey1);
|
|
|
|
|
|
|
|
QTest::newRow("Change: Full Key -> No Password") << compositeKey1_1 << compositeKey5_1;
|
|
|
|
QTest::newRow("Change: No Password -> Full Key") << compositeKey5_1 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: Full Key -> No Key File") << compositeKey1_1 << compositeKey5_2;
|
|
|
|
QTest::newRow("Change: No Key File -> Full Key") << compositeKey5_2 << compositeKey1_1;
|
|
|
|
QTest::newRow("Change: Full Key -> No CR Key") << compositeKey1_1 << compositeKey5_3;
|
|
|
|
QTest::newRow("Change: No CR Key -> Full Key") << compositeKey5_3 << compositeKey1_1;
|
|
|
|
}
|
|
|
|
|
2018-01-24 20:16:18 -05:00
|
|
|
/**
|
|
|
|
* Test for catching mapping errors with duplicate attachments.
|
|
|
|
*/
|
|
|
|
void TestKeePass2Format::testDuplicateAttachments()
|
|
|
|
{
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = QSharedPointer<Database>::create();
|
2018-05-13 17:21:43 -04:00
|
|
|
db->setKey(QSharedPointer<CompositeKey>::create());
|
2018-01-24 20:16:18 -05:00
|
|
|
|
|
|
|
const QByteArray attachment1("abc");
|
|
|
|
const QByteArray attachment2("def");
|
|
|
|
const QByteArray attachment3("ghi");
|
|
|
|
|
|
|
|
auto entry1 = new Entry();
|
|
|
|
entry1->setGroup(db->rootGroup());
|
2018-03-23 06:18:06 -04:00
|
|
|
entry1->setUuid(QUuid::fromRfc4122("aaaaaaaaaaaaaaaa"));
|
2018-01-24 20:16:18 -05:00
|
|
|
entry1->attachments()->set("a", attachment1);
|
|
|
|
|
|
|
|
auto entry2 = new Entry();
|
|
|
|
entry2->setGroup(db->rootGroup());
|
2018-03-23 06:18:06 -04:00
|
|
|
entry2->setUuid(QUuid::fromRfc4122("bbbbbbbbbbbbbbbb"));
|
2018-01-24 20:16:18 -05:00
|
|
|
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());
|
2018-03-23 06:18:06 -04:00
|
|
|
entry3->setUuid(QUuid::fromRfc4122("cccccccccccccccc"));
|
2018-01-24 20:16:18 -05:00
|
|
|
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);
|
2018-05-13 17:21:43 -04:00
|
|
|
readKdbx(&buffer, QSharedPointer<CompositeKey>::create(), db, hasError, errorString);
|
2018-01-24 20:16:18 -05:00
|
|
|
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);
|
|
|
|
}
|
2020-01-09 20:11:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return fast "dummy" KDF
|
|
|
|
*/
|
|
|
|
QSharedPointer<Kdf> TestKeePass2Format::fastKdf(QSharedPointer<Kdf> kdf) const
|
|
|
|
{
|
|
|
|
kdf->setRounds(1);
|
|
|
|
|
|
|
|
if (kdf->uuid() == KeePass2::KDF_ARGON2) {
|
|
|
|
kdf->processParameters({{KeePass2::KDFPARAM_ARGON2_MEMORY, 1024}, {KeePass2::KDFPARAM_ARGON2_PARALLELISM, 1}});
|
|
|
|
}
|
|
|
|
|
|
|
|
return kdf;
|
|
|
|
}
|