Rename KeePass2{,Xml}{R,W} to Kdbx3{,Xml}{R,W}, and add a redirection class

This class will in future select Kdbx4{R,W} as appropriate.
This commit is contained in:
angelsl
2017-11-13 02:55:03 +08:00
committed by Jonathan White
parent e5ec585f98
commit 3461cbfb06
20 changed files with 1099 additions and 734 deletions

View File

@@ -107,7 +107,7 @@ endif()
add_unit_test(NAME testgroup SOURCES TestGroup.cpp
LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testkeepass2xmlreader SOURCES TestKeePass2XmlReader.cpp
add_unit_test(NAME testkdbx3xmlreader SOURCES TestKeePass2XmlReader.cpp TestKdbx3XmlReader.cpp
LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testkeys SOURCES TestKeys.cpp

View File

@@ -22,7 +22,7 @@
#include "core/Database.h"
#include "core/Group.h"
#include "crypto/Crypto.h"
#include "format/KeePass2XmlReader.h"
#include "format/Kdbx3XmlReader.h"
#include "config-keepassx-tests.h"
QTEST_GUILESS_MAIN(TestDeletedObjects)
@@ -88,7 +88,7 @@ void TestDeletedObjects::createAndDelete(Database* db, int delObjectsSize)
void TestDeletedObjects::testDeletedObjectsFromFile()
{
KeePass2XmlReader reader;
Kdbx3XmlReader reader;
reader.setStrictMode(true);
QString xmlFile = QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml");
Database* db = reader.readDatabase(xmlFile);

View File

@@ -0,0 +1,22 @@
/*
* Copyright (C) 2017 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 <QTest>
#include "TestKeePass2XmlReader.h"
QTEST_GUILESS_MAIN(TestKdbx3XmlReader)

View File

@@ -30,7 +30,6 @@
#include "format/KeePass2Reader.h"
#include "format/KeePass2Repair.h"
#include "format/KeePass2Writer.h"
#include "format/KeePass2XmlWriter.h"
#include "keys/PasswordKey.h"
QTEST_GUILESS_MAIN(TestKeePass2Writer)

View File

@@ -25,12 +25,10 @@
#include "core/Group.h"
#include "core/Metadata.h"
#include "crypto/Crypto.h"
#include "format/KeePass2XmlReader.h"
#include "format/KeePass2XmlWriter.h"
#include "format/Kdbx3XmlReader.h"
#include "format/Kdbx3XmlWriter.h"
#include "config-keepassx-tests.h"
QTEST_GUILESS_MAIN(TestKeePass2XmlReader)
namespace QTest {
template<>
char* toString(const Uuid& uuid)
@@ -79,11 +77,11 @@ QByteArray TestKeePass2XmlReader::strToBytes(const QString& str)
return result;
}
void TestKeePass2XmlReader::initTestCase()
void TestKdbx3XmlReader::initTestCase()
{
QVERIFY(Crypto::init());
KeePass2XmlReader reader;
Kdbx3XmlReader reader;
reader.setStrictMode(true);
QString xmlFile = QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.xml");
m_db = reader.readDatabase(xmlFile);
@@ -91,6 +89,32 @@ void TestKeePass2XmlReader::initTestCase()
QVERIFY(!reader.hasError());
}
void TestKdbx3XmlReader::readDatabase(QString path, bool strictMode, Database*& db, bool& hasError, QString& errorString)
{
Kdbx3XmlReader reader;
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)
{
Kdbx3XmlReader reader;
reader.setStrictMode(strictMode);
db = reader.readDatabase(buf);
hasError = reader.hasError();
errorString = reader.errorString();
}
void TestKdbx3XmlReader::writeDatabase(QBuffer* buf, Database* db, bool& hasError, QString& errorString)
{
Kdbx3XmlWriter writer;
writer.writeDatabase(buf, db);
hasError = writer.hasError();
errorString = writer.errorString();
}
void TestKeePass2XmlReader::testMetadata()
{
QCOMPARE(m_db->metadata()->generator(), QString("KeePass"));
@@ -374,15 +398,20 @@ void TestKeePass2XmlReader::testBroken()
QFETCH(bool, strictMode);
QFETCH(bool, expectError);
KeePass2XmlReader reader;
reader.setStrictMode(strictMode);
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, baseName);
QVERIFY(QFile::exists(xmlFile));
QScopedPointer<Database> db(reader.readDatabase(xmlFile));
if (reader.hasError()) {
qWarning("Reader error: %s", qPrintable(reader.errorString()));
bool hasError;
QString errorString;
Database* db;
readDatabase(xmlFile, strictMode, db, hasError, errorString);
if (hasError) {
qWarning("Reader error: %s", qPrintable(errorString));
}
QCOMPARE(hasError, expectError);
if (db) {
delete db;
}
QCOMPARE(reader.hasError(), expectError);
}
void TestKeePass2XmlReader::testBroken_data()
@@ -412,15 +441,20 @@ void TestKeePass2XmlReader::testBroken_data()
void TestKeePass2XmlReader::testEmptyUuids()
{
KeePass2XmlReader reader;
reader.setStrictMode(true);
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "EmptyUuids");
QVERIFY(QFile::exists(xmlFile));
QScopedPointer<Database> db(reader.readDatabase(xmlFile));
if (reader.hasError()) {
qWarning("Reader error: %s", qPrintable(reader.errorString()));
Database* dbp;
bool hasError;
QString errorString;
readDatabase(xmlFile, true, dbp, hasError, errorString);
if (hasError) {
qWarning("Reader error: %s", qPrintable(errorString));
}
QVERIFY(!hasError);
if (dbp) {
delete dbp;
}
QVERIFY(!reader.hasError());
}
void TestKeePass2XmlReader::testInvalidXmlChars()
@@ -459,19 +493,19 @@ void TestKeePass2XmlReader::testInvalidXmlChars()
QBuffer buffer;
buffer.open(QIODevice::ReadWrite);
KeePass2XmlWriter writer;
writer.writeDatabase(&buffer, dbWrite.data());
QVERIFY(!writer.hasError());
bool hasError;
QString errorString;
writeDatabase(&buffer, dbWrite.data(), hasError, errorString);
QVERIFY(!hasError);
buffer.seek(0);
KeePass2XmlReader reader;
reader.setStrictMode(true);
QScopedPointer<Database> dbRead(reader.readDatabase(&buffer));
if (reader.hasError()) {
qWarning("Database read error: %s", qPrintable(reader.errorString()));
Database* dbRead;
readDatabase(&buffer, true, dbRead, hasError, errorString);
if (hasError) {
qWarning("Database read error: %s", qPrintable(errorString));
}
QVERIFY(!reader.hasError());
QVERIFY(!dbRead.isNull());
QVERIFY(!hasError);
QVERIFY(dbRead);
QCOMPARE(dbRead->rootGroup()->entries().size(), 1);
Entry* entryRead = dbRead->rootGroup()->entries().at(0);
EntryAttributes* attrRead = entryRead->attributes();
@@ -486,22 +520,28 @@ void TestKeePass2XmlReader::testInvalidXmlChars()
QCOMPARE(strToBytes(attrRead->value("LowLowSurrogate")), QByteArray());
QCOMPARE(strToBytes(attrRead->value("SurrogateValid1")), strToBytes(strSurrogateValid1));
QCOMPARE(strToBytes(attrRead->value("SurrogateValid2")), strToBytes(strSurrogateValid2));
if (dbRead) {
delete dbRead;
}
}
void TestKeePass2XmlReader::testRepairUuidHistoryItem()
{
KeePass2XmlReader reader;
QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "BrokenDifferentEntryHistoryUuid");
QVERIFY(QFile::exists(xmlFile));
QScopedPointer<Database> db(reader.readDatabase(xmlFile));
if (reader.hasError()) {
qWarning("Database read error: %s", qPrintable(reader.errorString()));
Database* db;
bool hasError;
QString errorString;
readDatabase(xmlFile, true, db, hasError, errorString);
if (hasError) {
qWarning("Database read error: %s", qPrintable(errorString));
}
QVERIFY(!reader.hasError());
QVERIFY(!hasError);
QList<Entry*> entries = db.data()->rootGroup()->entries();
QList<Entry*> entries = db->rootGroup()->entries();
QCOMPARE(entries.size(), 1);
Entry* entry = entries.at(0);
@@ -512,6 +552,10 @@ void TestKeePass2XmlReader::testRepairUuidHistoryItem()
QVERIFY(!entry->uuid().isNull());
QVERIFY(!historyItem->uuid().isNull());
QCOMPARE(historyItem->uuid(), entry->uuid());
if (db) {
delete db;
}
}
void TestKeePass2XmlReader::cleanupTestCase()

View File

@@ -20,6 +20,7 @@
#include <QDateTime>
#include <QObject>
#include <QBuffer>
class Database;
@@ -27,8 +28,8 @@ class TestKeePass2XmlReader : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
protected slots:
virtual void initTestCase() = 0;
void testMetadata();
void testCustomIcons();
void testCustomData();
@@ -46,11 +47,27 @@ private slots:
void testRepairUuidHistoryItem();
void cleanupTestCase();
private:
protected:
virtual void readDatabase(QBuffer* buf, bool strictMode, Database*& db, bool& hasError, QString& errorString) = 0;
virtual void readDatabase(QString path, bool strictMode, 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 QByteArray strToBytes(const QString& str);
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;
};
#endif // KEEPASSX_TESTKEEPASS2XMLREADER_H