Fix KDBX reader tests not being executed

This commit is contained in:
Janek Bevendorff 2018-01-17 18:15:37 +01:00
parent 92dd4c0d99
commit cdefc7ea9b
No known key found for this signature in database
GPG Key ID: 2FDEB0D40BCA5E11
7 changed files with 261 additions and 226 deletions

View File

@ -107,10 +107,10 @@ endif()
add_unit_test(NAME testgroup SOURCES TestGroup.cpp add_unit_test(NAME testgroup SOURCES TestGroup.cpp
LIBS ${TEST_LIBRARIES}) LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testkdbx3xmlreader SOURCES TestKeePass2XmlReader.cpp TestKdbx3XmlReader.cpp add_unit_test(NAME testkdbx3 SOURCES TestKeePass2XmlReader.cpp TestKdbx3.cpp
LIBS ${TEST_LIBRARIES}) LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testkdbx4xmlreader SOURCES TestKeePass2XmlReader.cpp TestKdbx4XmlReader.cpp add_unit_test(NAME testkdbx4 SOURCES TestKeePass2XmlReader.cpp TestKdbx4.cpp
LIBS ${TEST_LIBRARIES}) LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testkeys SOURCES TestKeys.cpp add_unit_test(NAME testkeys SOURCES TestKeys.cpp

68
tests/TestKdbx3.cpp Normal file
View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
*
* 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 "TestKdbx3.h"
#include "core/Metadata.h"
#include "crypto/Crypto.h"
#include "format/KeePass2.h"
#include "format/KdbxXmlReader.h"
#include "format/KdbxXmlWriter.h"
#include "config-keepassx-tests.h"
#include <QTest>
QTEST_GUILESS_MAIN(TestKdbx3)
void TestKdbx3::initTestCase()
{
QVERIFY(Crypto::init());
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(true);
QString xmlFile = QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml");
m_db.reset(reader.readDatabase(xmlFile));
QVERIFY(m_db.data());
QVERIFY(!reader.hasError());
}
Database* TestKdbx3::readDatabase(QString path, bool strictMode, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
auto db = reader.readDatabase(path);
hasError = reader.hasError();
errorString = reader.errorString();
return db;
}
Database* TestKdbx3::readDatabase(QBuffer* buf, bool strictMode, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
auto db = reader.readDatabase(buf);
hasError = reader.hasError();
errorString = reader.errorString();
return db;
}
void TestKdbx3::writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString)
{
KdbxXmlWriter writer(KeePass2::FILE_VERSION_3);
writer.writeDatabase(buf, db);
hasError = writer.hasError();
errorString = writer.errorString();
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -15,8 +15,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QTest> #ifndef KEEPASSXC_TEST_KDBX3_H
#define KEEPASSXC_TEST_KDBX3_H
#include "TestKeePass2XmlReader.h" #include "TestKeePass2XmlReader.h"
QTEST_GUILESS_MAIN(TestKdbx3XmlReader) class TestKdbx3 : public TestKeePass2XmlReader
{
Q_OBJECT
private slots:
virtual void initTestCase() override;
protected:
virtual Database* readDatabase(QBuffer* buf, bool strictMode, bool& hasError, QString& errorString) override;
virtual Database* readDatabase(QString path, bool strictMode, bool& hasError, QString& errorString) override;
virtual void writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString) override;
};
#endif // KEEPASSXC_TEST_KDBX3_H

68
tests/TestKdbx4.cpp Normal file
View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
*
* 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 "TestKdbx4.h"
#include "core/Metadata.h"
#include "crypto/Crypto.h"
#include "format/KeePass2.h"
#include "format/KdbxXmlReader.h"
#include "format/KdbxXmlWriter.h"
#include "config-keepassx-tests.h"
#include <QTest>
QTEST_GUILESS_MAIN(TestKdbx4)
void TestKdbx4::initTestCase()
{
QVERIFY(Crypto::init());
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(true);
QString xmlFile = QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml");
m_db.reset(reader.readDatabase(xmlFile));
QVERIFY(m_db.data());
QVERIFY(!reader.hasError());
}
Database* TestKdbx4::readDatabase(QString path, bool strictMode, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
auto db = reader.readDatabase(path);
hasError = reader.hasError();
errorString = reader.errorString();
return db;
}
Database* TestKdbx4::readDatabase(QBuffer* buf, bool strictMode, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
auto db = reader.readDatabase(buf);
hasError = reader.hasError();
errorString = reader.errorString();
return db;
}
void TestKdbx4::writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString)
{
KdbxXmlWriter writer(KeePass2::FILE_VERSION_3);
writer.writeDatabase(buf, db);
hasError = writer.hasError();
errorString = writer.errorString();
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -15,8 +15,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QTest> #ifndef KEEPASSXC_TEST_KDBX4_H
#define KEEPASSXC_TEST_KDBX4_H
#include "TestKeePass2XmlReader.h" #include "TestKeePass2XmlReader.h"
QTEST_GUILESS_MAIN(TestKdbx4XmlReader) class TestKdbx4 : public TestKeePass2XmlReader
{
Q_OBJECT
private slots:
virtual void initTestCase() override;
protected:
virtual Database* readDatabase(QBuffer* buf, bool strictMode, bool& hasError, QString& errorString) override;
virtual Database* readDatabase(QString path, bool strictMode, bool& hasError, QString& errorString) override;
virtual void writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString) override;
};
#endif // KEEPASSXC_TEST_KDBX4_H

View File

@ -17,19 +17,15 @@
#include "TestKeePass2XmlReader.h" #include "TestKeePass2XmlReader.h"
#include <QBuffer>
#include <QFile>
#include <QTest>
#include <format/KeePass2.h>
#include "core/Database.h"
#include "core/Group.h" #include "core/Group.h"
#include "core/Metadata.h" #include "core/Metadata.h"
#include "crypto/Crypto.h"
#include "format/KdbxXmlReader.h" #include "format/KdbxXmlReader.h"
#include "format/KdbxXmlWriter.h" #include "format/KdbxXmlWriter.h"
#include "config-keepassx-tests.h" #include "config-keepassx-tests.h"
#include <QFile>
#include <QTest>
namespace QTest { namespace QTest {
template<> template<>
char* toString(const Uuid& uuid) char* toString(const Uuid& uuid)
@ -47,11 +43,9 @@ namespace QTest {
if (triState == Group::Inherit) { if (triState == Group::Inherit) {
value = "null"; value = "null";
} } else if (triState == Group::Enable) {
else if (triState == Group::Enable) {
value = "true"; value = "true";
} } else {
else {
value = "false"; value = "false";
} }
@ -70,90 +64,14 @@ QByteArray TestKeePass2XmlReader::strToBytes(const QString& str)
{ {
QByteArray result; QByteArray result;
for (int i = 0; i < str.size(); i++) { for (auto i : str) {
result.append(str.at(i).unicode() >> 8); result.append(static_cast<char>(i.unicode() >> 8));
result.append(str.at(i).unicode() & 0xFF); result.append(static_cast<char>(i.unicode() & 0xFF));
} }
return result; return result;
} }
void TestKdbx3XmlReader::initTestCase()
{
QVERIFY(Crypto::init());
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(true);
QString xmlFile = QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml");
m_db = reader.readDatabase(xmlFile);
QVERIFY(m_db);
QVERIFY(!reader.hasError());
}
void TestKdbx4XmlReader::initTestCase()
{
QVERIFY(Crypto::init());
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(true);
QString xmlFile = QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml");
m_db = reader.readDatabase(xmlFile);
QVERIFY(m_db);
QVERIFY(!reader.hasError());
}
void TestKdbx3XmlReader::readDatabase(QString path, bool strictMode, Database*& db, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
db = reader.readDatabase(path);
hasError = reader.hasError();
errorString = reader.errorString();
}
void TestKdbx3XmlReader::readDatabase(QBuffer* buf, bool strictMode, Database*& db, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
db = reader.readDatabase(buf);
hasError = reader.hasError();
errorString = reader.errorString();
}
void TestKdbx3XmlReader::writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString)
{
KdbxXmlWriter writer(KeePass2::FILE_VERSION_3);
writer.writeDatabase(buf, db);
hasError = writer.hasError();
errorString = writer.errorString();
}
void TestKdbx4XmlReader::readDatabase(QString path, bool strictMode, Database*& db, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
db = reader.readDatabase(path);
hasError = reader.hasError();
errorString = reader.errorString();
}
void TestKdbx4XmlReader::readDatabase(QBuffer* buf, bool strictMode, Database*& db, bool& hasError, QString& errorString)
{
KdbxXmlReader reader(KeePass2::FILE_VERSION_3);
reader.setStrictMode(strictMode);
db = reader.readDatabase(buf);
hasError = reader.hasError();
errorString = reader.errorString();
}
void TestKdbx4XmlReader::writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString)
{
KdbxXmlWriter writer(KeePass2::FILE_VERSION_3);
writer.writeDatabase(buf, db);
hasError = writer.hasError();
errorString = writer.errorString();
}
void TestKeePass2XmlReader::testMetadata() void TestKeePass2XmlReader::testMetadata()
{ {
QCOMPARE(m_db->metadata()->generator(), QString("KeePass")); QCOMPARE(m_db->metadata()->generator(), QString("KeePass"));
@ -437,20 +355,15 @@ void TestKeePass2XmlReader::testBroken()
QFETCH(bool, strictMode); QFETCH(bool, strictMode);
QFETCH(bool, expectError); QFETCH(bool, expectError);
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, baseName); QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, baseName);
QVERIFY(QFile::exists(xmlFile)); QVERIFY(QFile::exists(xmlFile));
bool hasError; bool hasError;
QString errorString; QString errorString;
Database* db; QScopedPointer<Database> db(readDatabase(xmlFile, strictMode, hasError, errorString));
readDatabase(xmlFile, strictMode, db, hasError, errorString);
if (hasError) { if (hasError) {
qWarning("Reader error: %s", qPrintable(errorString)); qWarning("Reader error: %s", qPrintable(errorString));
} }
QCOMPARE(hasError, expectError); QCOMPARE(hasError, expectError);
if (db) {
delete db;
}
} }
void TestKeePass2XmlReader::testBroken_data() void TestKeePass2XmlReader::testBroken_data()
@ -483,17 +396,13 @@ void TestKeePass2XmlReader::testEmptyUuids()
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "EmptyUuids"); QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "EmptyUuids");
QVERIFY(QFile::exists(xmlFile)); QVERIFY(QFile::exists(xmlFile));
Database* dbp;
bool hasError; bool hasError;
QString errorString; QString errorString;
readDatabase(xmlFile, true, dbp, hasError, errorString); QScopedPointer<Database> dbp(readDatabase(xmlFile, true, hasError, errorString));
if (hasError) { if (hasError) {
qWarning("Reader error: %s", qPrintable(errorString)); qWarning("Reader error: %s", qPrintable(errorString));
} }
QVERIFY(!hasError); QVERIFY(!hasError);
if (dbp) {
delete dbp;
}
} }
void TestKeePass2XmlReader::testInvalidXmlChars() void TestKeePass2XmlReader::testInvalidXmlChars()
@ -514,9 +423,10 @@ void TestKeePass2XmlReader::testInvalidXmlChars()
QString strSingleLowSurrogate2 = QString().append(QChar((0x31))).append(QChar(0xDC37)).append(QChar(0x32)); QString strSingleLowSurrogate2 = QString().append(QChar((0x31))).append(QChar(0xDC37)).append(QChar(0x32));
QString strLowLowSurrogate = QString().append(QChar(0xDC37)).append(QChar(0xDC37)); QString strLowLowSurrogate = QString().append(QChar(0xDC37)).append(QChar(0xDC37));
QString strSurrogateValid1 = QString().append(QChar(0xD801)).append(QChar(0xDC37)); QString strSurrogateValid1 = QString().append(QChar(0xD801)).append(QChar(0xDC37));
QString strSurrogateValid2 = QString().append(QChar(0x31)).append(QChar(0xD801)).append(QChar(0xDC37)).append(QChar(0x32)); QString strSurrogateValid2 = QString().append(QChar(0x31)).append(QChar(0xD801)).append(QChar(0xDC37))
.append(QChar(0x32));
Entry* entry = new Entry(); auto entry = new Entry();
entry->setUuid(Uuid::random()); entry->setUuid(Uuid::random());
entry->setGroup(dbWrite->rootGroup()); entry->setGroup(dbWrite->rootGroup());
entry->attributes()->set("PlainInvalid", strPlainInvalid); entry->attributes()->set("PlainInvalid", strPlainInvalid);
@ -538,13 +448,12 @@ void TestKeePass2XmlReader::testInvalidXmlChars()
QVERIFY(!hasError); QVERIFY(!hasError);
buffer.seek(0); buffer.seek(0);
Database* dbRead; QScopedPointer<Database> dbRead(readDatabase(&buffer, true, hasError, errorString));
readDatabase(&buffer, true, dbRead, hasError, errorString);
if (hasError) { if (hasError) {
qWarning("Database read error: %s", qPrintable(errorString)); qWarning("Database read error: %s", qPrintable(errorString));
} }
QVERIFY(!hasError); QVERIFY(!hasError);
QVERIFY(dbRead); QVERIFY(dbRead.data());
QCOMPARE(dbRead->rootGroup()->entries().size(), 1); QCOMPARE(dbRead->rootGroup()->entries().size(), 1);
Entry* entryRead = dbRead->rootGroup()->entries().at(0); Entry* entryRead = dbRead->rootGroup()->entries().at(0);
EntryAttributes* attrRead = entryRead->attributes(); EntryAttributes* attrRead = entryRead->attributes();
@ -559,27 +468,20 @@ void TestKeePass2XmlReader::testInvalidXmlChars()
QCOMPARE(strToBytes(attrRead->value("LowLowSurrogate")), QByteArray()); QCOMPARE(strToBytes(attrRead->value("LowLowSurrogate")), QByteArray());
QCOMPARE(strToBytes(attrRead->value("SurrogateValid1")), strToBytes(strSurrogateValid1)); QCOMPARE(strToBytes(attrRead->value("SurrogateValid1")), strToBytes(strSurrogateValid1));
QCOMPARE(strToBytes(attrRead->value("SurrogateValid2")), strToBytes(strSurrogateValid2)); QCOMPARE(strToBytes(attrRead->value("SurrogateValid2")), strToBytes(strSurrogateValid2));
if (dbRead) {
delete dbRead;
}
} }
void TestKeePass2XmlReader::testRepairUuidHistoryItem() void TestKeePass2XmlReader::testRepairUuidHistoryItem()
{ {
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "BrokenDifferentEntryHistoryUuid"); QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "BrokenDifferentEntryHistoryUuid");
QVERIFY(QFile::exists(xmlFile)); QVERIFY(QFile::exists(xmlFile));
Database* db;
bool hasError; bool hasError;
QString errorString; QString errorString;
readDatabase(xmlFile, true, db, hasError, errorString); QScopedPointer<Database> db(readDatabase(xmlFile, false, hasError, errorString));
if (hasError) { if (hasError) {
qWarning("Database read error: %s", qPrintable(errorString)); qWarning("Database read error: %s", qPrintable(errorString));
} }
QVERIFY(!hasError); QVERIFY(!hasError);
QList<Entry*> entries = db->rootGroup()->entries(); QList<Entry*> entries = db->rootGroup()->entries();
QCOMPARE(entries.size(), 1); QCOMPARE(entries.size(), 1);
Entry* entry = entries.at(0); Entry* entry = entries.at(0);
@ -591,13 +493,4 @@ void TestKeePass2XmlReader::testRepairUuidHistoryItem()
QVERIFY(!entry->uuid().isNull()); QVERIFY(!entry->uuid().isNull());
QVERIFY(!historyItem->uuid().isNull()); QVERIFY(!historyItem->uuid().isNull());
QCOMPARE(historyItem->uuid(), entry->uuid()); QCOMPARE(historyItem->uuid(), entry->uuid());
if (db) {
delete db;
}
}
void TestKeePass2XmlReader::cleanupTestCase()
{
delete m_db;
} }

View File

@ -21,8 +21,10 @@
#include <QDateTime> #include <QDateTime>
#include <QObject> #include <QObject>
#include <QBuffer> #include <QBuffer>
#include <QScopedPointer>
#include "core/Database.h"
class Database;
class TestKeePass2XmlReader : public QObject class TestKeePass2XmlReader : public QObject
{ {
@ -30,6 +32,8 @@ class TestKeePass2XmlReader : public QObject
protected slots: protected slots:
virtual void initTestCase() = 0; virtual void initTestCase() = 0;
private slots:
void testMetadata(); void testMetadata();
void testCustomIcons(); void testCustomIcons();
void testCustomData(); void testCustomData();
@ -45,42 +49,16 @@ protected slots:
void testEmptyUuids(); void testEmptyUuids();
void testInvalidXmlChars(); void testInvalidXmlChars();
void testRepairUuidHistoryItem(); void testRepairUuidHistoryItem();
void cleanupTestCase();
protected: protected:
virtual void readDatabase(QBuffer* buf, bool strictMode, Database*& db, bool& hasError, QString& errorString) = 0; virtual Database* readDatabase(QBuffer* buf, bool strictMode, bool& hasError, QString& errorString) = 0;
virtual void readDatabase(QString path, bool strictMode, Database*& db, bool& hasError, QString& errorString) = 0; virtual Database* readDatabase(QString path, bool strictMode, bool& hasError, QString& errorString) = 0;
virtual void writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString) = 0; virtual void writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString) = 0;
static QDateTime genDT(int year, int month, int day, int hour, int min, int second); static QDateTime genDT(int year, int month, int day, int hour, int min, int second);
static QByteArray strToBytes(const QString& str); static QByteArray strToBytes(const QString& str);
Database* m_db; QScopedPointer<Database> m_db;
}; };
class TestKdbx3XmlReader : public TestKeePass2XmlReader
{
Q_OBJECT
private slots:
virtual void initTestCase() override;
protected:
virtual void readDatabase(QBuffer* buf, bool strictMode, Database*& db, bool& hasError, QString& errorString) override;
virtual void readDatabase(QString path, bool strictMode, Database*& db, bool& hasError, QString& errorString) override;
virtual void writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString) override;
};
class TestKdbx4XmlReader : public TestKeePass2XmlReader
{
Q_OBJECT
private slots:
virtual void initTestCase() override;
protected:
virtual void readDatabase(QBuffer* buf, bool strictMode, Database*& db, bool& hasError, QString& errorString) override;
virtual void readDatabase(QString path, bool strictMode, Database*& db, bool& hasError, QString& errorString) override;
virtual void writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString) override;
};
#endif // KEEPASSX_TESTKEEPASS2XMLREADER_H #endif // KEEPASSX_TESTKEEPASS2XMLREADER_H