2011-11-13 08:52:43 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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 "TestKeys.h"
|
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QTest>
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2011-12-21 17:22:07 -05:00
|
|
|
#include "config-keepassx-tests.h"
|
2012-01-01 16:04:27 -05:00
|
|
|
#include "tests.h"
|
2011-12-21 17:22:07 -05:00
|
|
|
#include "core/Database.h"
|
|
|
|
#include "core/Metadata.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
#include "crypto/Crypto.h"
|
2011-12-21 17:22:07 -05:00
|
|
|
#include "format/KeePass2Reader.h"
|
|
|
|
#include "format/KeePass2Writer.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
#include "keys/CompositeKey.h"
|
2011-12-21 17:22:07 -05:00
|
|
|
#include "keys/FileKey.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
#include "keys/PasswordKey.h"
|
|
|
|
|
2014-05-16 06:32:52 -04:00
|
|
|
QTEST_GUILESS_MAIN(TestKeys)
|
|
|
|
|
2011-11-13 08:52:43 -05:00
|
|
|
void TestKeys::initTestCase()
|
|
|
|
{
|
2014-06-15 05:17:40 -04:00
|
|
|
QVERIFY(Crypto::init());
|
2011-11-13 08:52:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testComposite()
|
|
|
|
{
|
|
|
|
CompositeKey* compositeKey1 = new CompositeKey();
|
|
|
|
PasswordKey* passwordKey1 = new PasswordKey();
|
|
|
|
PasswordKey* passwordKey2 = new PasswordKey("test");
|
2015-05-09 13:47:53 -04:00
|
|
|
bool ok;
|
|
|
|
QString errorString;
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2012-04-25 12:35:30 -04:00
|
|
|
// make sure that addKey() creates a copy of the keys
|
2011-11-13 08:52:43 -05:00
|
|
|
compositeKey1->addKey(*passwordKey1);
|
|
|
|
compositeKey1->addKey(*passwordKey2);
|
|
|
|
delete passwordKey1;
|
|
|
|
delete passwordKey2;
|
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
QByteArray transformed = compositeKey1->transform(QByteArray(32, '\0'), 1, &ok, &errorString);
|
|
|
|
QVERIFY(ok);
|
2012-04-25 12:35:30 -04:00
|
|
|
QCOMPARE(transformed.size(), 32);
|
2011-11-13 08:52:43 -05:00
|
|
|
|
|
|
|
// make sure the subkeys are copied
|
|
|
|
CompositeKey* compositeKey2 = compositeKey1->clone();
|
|
|
|
delete compositeKey1;
|
2015-05-09 13:47:53 -04:00
|
|
|
QCOMPARE(compositeKey2->transform(QByteArray(32, '\0'), 1, &ok, &errorString), transformed);
|
|
|
|
QVERIFY(ok);
|
2011-11-13 08:52:43 -05:00
|
|
|
delete compositeKey2;
|
2012-01-11 17:59:50 -05:00
|
|
|
|
|
|
|
CompositeKey* compositeKey3 = new CompositeKey();
|
|
|
|
CompositeKey* compositeKey4 = new CompositeKey();
|
|
|
|
|
2012-04-25 12:35:30 -04:00
|
|
|
// test clear()
|
2012-01-11 17:59:50 -05:00
|
|
|
compositeKey3->addKey(PasswordKey("test"));
|
|
|
|
compositeKey3->clear();
|
2012-04-25 12:35:30 -04:00
|
|
|
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
|
2012-01-11 17:59:50 -05:00
|
|
|
|
2012-04-25 12:35:30 -04:00
|
|
|
// test assignment operator
|
|
|
|
compositeKey3->addKey(PasswordKey("123"));
|
|
|
|
*compositeKey4 = *compositeKey3;
|
2012-01-11 17:59:50 -05:00
|
|
|
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
|
|
|
|
|
2012-07-17 04:47:56 -04:00
|
|
|
// test self-assignment
|
|
|
|
*compositeKey3 = *compositeKey3;
|
|
|
|
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
|
|
|
|
|
2012-01-11 17:59:50 -05:00
|
|
|
delete compositeKey3;
|
|
|
|
delete compositeKey4;
|
2011-11-13 08:52:43 -05:00
|
|
|
}
|
|
|
|
|
2011-12-21 17:22:07 -05:00
|
|
|
void TestKeys::testFileKey()
|
|
|
|
{
|
|
|
|
QFETCH(QString, type);
|
|
|
|
|
|
|
|
QString name = QString("FileKey").append(type);
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
|
|
|
|
QString dbFilename = QString("%1/%2.kdbx").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
QString keyFilename = QString("%1/%2.key").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
|
|
|
|
CompositeKey compositeKey;
|
|
|
|
FileKey fileKey;
|
|
|
|
QVERIFY(fileKey.load(keyFilename));
|
|
|
|
QCOMPARE(fileKey.rawKey().size(), 32);
|
|
|
|
compositeKey.addKey(fileKey);
|
|
|
|
|
|
|
|
Database* db = reader.readDatabase(dbFilename, compositeKey);
|
|
|
|
QVERIFY(db);
|
2012-01-06 14:03:13 -05:00
|
|
|
QVERIFY(!reader.hasError());
|
2011-12-21 17:22:07 -05:00
|
|
|
QCOMPARE(db->metadata()->name(), QString("%1 Database").arg(name));
|
|
|
|
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testFileKey_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("type");
|
|
|
|
QTest::newRow("Xml") << QString("Xml");
|
|
|
|
QTest::newRow("Binary") << QString("Binary");
|
|
|
|
QTest::newRow("Hex") << QString("Hex");
|
|
|
|
QTest::newRow("Hashed") << QString("Hashed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testCreateFileKey()
|
|
|
|
{
|
|
|
|
const QString dbName("testCreateFileKey database");
|
|
|
|
|
|
|
|
QBuffer keyBuffer;
|
|
|
|
keyBuffer.open(QBuffer::ReadWrite);
|
|
|
|
|
|
|
|
FileKey::create(&keyBuffer);
|
|
|
|
keyBuffer.reset();
|
|
|
|
|
|
|
|
FileKey fileKey;
|
|
|
|
QVERIFY(fileKey.load(&keyBuffer));
|
|
|
|
CompositeKey compositeKey;
|
|
|
|
compositeKey.addKey(fileKey);
|
|
|
|
|
|
|
|
Database* dbOrg = new Database();
|
2015-05-09 13:47:53 -04:00
|
|
|
QVERIFY(dbOrg->setKey(compositeKey));
|
2011-12-21 17:22:07 -05:00
|
|
|
dbOrg->metadata()->setName(dbName);
|
|
|
|
|
|
|
|
QBuffer dbBuffer;
|
|
|
|
dbBuffer.open(QBuffer::ReadWrite);
|
|
|
|
|
|
|
|
KeePass2Writer writer;
|
|
|
|
writer.writeDatabase(&dbBuffer, dbOrg);
|
|
|
|
dbBuffer.reset();
|
|
|
|
delete dbOrg;
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
Database* dbRead = reader.readDatabase(&dbBuffer, compositeKey);
|
|
|
|
QVERIFY(dbRead);
|
2012-01-06 14:03:13 -05:00
|
|
|
QVERIFY(!reader.hasError());
|
2011-12-21 17:22:07 -05:00
|
|
|
QCOMPARE(dbRead->metadata()->name(), dbName);
|
|
|
|
delete dbRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testFileKeyError()
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
QString errorMsg;
|
|
|
|
const QString fileName(QString(KEEPASSX_TEST_DATA_DIR).append("/does/not/exist"));
|
|
|
|
|
|
|
|
FileKey fileKey;
|
|
|
|
result = fileKey.load(fileName, &errorMsg);
|
|
|
|
QVERIFY(!result);
|
|
|
|
QVERIFY(!errorMsg.isEmpty());
|
|
|
|
errorMsg = "";
|
|
|
|
|
|
|
|
result = FileKey::create(fileName, &errorMsg);
|
|
|
|
QVERIFY(!result);
|
|
|
|
QVERIFY(!errorMsg.isEmpty());
|
|
|
|
errorMsg = "";
|
|
|
|
}
|
|
|
|
|
2014-01-19 09:26:32 -05:00
|
|
|
void TestKeys::benchmarkTransformKey()
|
|
|
|
{
|
|
|
|
QByteArray env = qgetenv("BENCHMARK");
|
|
|
|
|
|
|
|
if (env.isEmpty() || env == "0" || env == "no") {
|
|
|
|
QSKIP("Benchmark skipped. Set env variable BENCHMARK=1 to enable.", SkipAll);
|
|
|
|
}
|
|
|
|
|
|
|
|
PasswordKey pwKey;
|
|
|
|
pwKey.setPassword("password");
|
|
|
|
CompositeKey compositeKey;
|
|
|
|
compositeKey.addKey(pwKey);
|
|
|
|
|
|
|
|
QByteArray seed(32, '\x4B');
|
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
bool ok;
|
|
|
|
QString errorString;
|
|
|
|
|
2014-01-19 09:26:32 -05:00
|
|
|
QBENCHMARK {
|
2015-05-09 13:47:53 -04:00
|
|
|
compositeKey.transform(seed, 1e6, &ok, &errorString);
|
2014-01-19 09:26:32 -05:00
|
|
|
}
|
|
|
|
}
|