diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4472fc27a..1e7512268 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -105,94 +105,94 @@ if(YUBIKEY_FOUND) endif() add_unit_test(NAME testgroup SOURCES TestGroup.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) -add_unit_test(NAME testkdbx3xmlreader SOURCES TestKeePass2XmlReader.cpp TestKdbx3XmlReader.cpp - LIBS ${TEST_LIBRARIES}) +add_unit_test(NAME testkdbx3 SOURCES TestKeePass2XmlReader.cpp TestKdbx3.cpp + LIBS ${TEST_LIBRARIES}) -add_unit_test(NAME testkdbx4xmlreader SOURCES TestKeePass2XmlReader.cpp TestKdbx4XmlReader.cpp - LIBS ${TEST_LIBRARIES}) +add_unit_test(NAME testkdbx4 SOURCES TestKeePass2XmlReader.cpp TestKdbx4.cpp + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testkeys SOURCES TestKeys.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testkeepass2reader SOURCES TestKeePass2Reader.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testkeepass2writer SOURCES TestKeePass2Writer.cpp - LIBS testsupport ${TEST_LIBRARIES}) + LIBS testsupport ${TEST_LIBRARIES}) add_unit_test(NAME testgroupmodel SOURCES TestGroupModel.cpp - LIBS testsupport ${TEST_LIBRARIES}) + LIBS testsupport ${TEST_LIBRARIES}) add_unit_test(NAME testentrymodel SOURCES TestEntryModel.cpp - LIBS testsupport ${TEST_LIBRARIES}) + LIBS testsupport ${TEST_LIBRARIES}) add_unit_test(NAME testcryptohash SOURCES TestCryptoHash.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testsymmetriccipher SOURCES TestSymmetricCipher.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testhashedblockstream SOURCES TestHashedBlockStream.cpp - LIBS testsupport ${TEST_LIBRARIES}) + LIBS testsupport ${TEST_LIBRARIES}) add_unit_test(NAME testkeepass2randomstream SOURCES TestKeePass2RandomStream.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testmodified SOURCES TestModified.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testdeletedobjects SOURCES TestDeletedObjects.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testkeepass1reader SOURCES TestKeePass1Reader.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testwildcardmatcher SOURCES TestWildcardMatcher.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) if(WITH_XC_AUTOTYPE) add_unit_test(NAME testautotype SOURCES TestAutoType.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) set_target_properties(testautotype PROPERTIES ENABLE_EXPORTS ON) endif() if(WITH_XC_SSHAGENT) add_unit_test(NAME testopensshkey SOURCES TestOpenSSHKey.cpp - LIBS sshagent ${TEST_LIBRARIES}) + LIBS sshagent ${TEST_LIBRARIES}) endif() add_unit_test(NAME testentry SOURCES TestEntry.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testmerge SOURCES TestMerge.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testtotp SOURCES TestTotp.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testbase32 SOURCES TestBase32.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testcsvparser SOURCES TestCsvParser.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testrandom SOURCES TestRandom.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testentrysearcher SOURCES TestEntrySearcher.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testcsvexporter SOURCES TestCsvExporter.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testykchallengeresponsekey - SOURCES TestYkChallengeResponseKey.cpp TestYkChallengeResponseKey.h - LIBS ${TEST_LIBRARIES}) + SOURCES TestYkChallengeResponseKey.cpp TestYkChallengeResponseKey.h + LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testdatabase SOURCES TestDatabase.cpp - LIBS ${TEST_LIBRARIES}) + LIBS ${TEST_LIBRARIES}) if(WITH_GUI_TESTS) add_subdirectory(gui) diff --git a/tests/TestKdbx3.cpp b/tests/TestKdbx3.cpp new file mode 100644 index 000000000..9bb5d8b54 --- /dev/null +++ b/tests/TestKdbx3.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 KeePassXC Team + * + * 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 . + */ + +#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_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(); +} diff --git a/tests/TestKdbx3XmlReader.cpp b/tests/TestKdbx3.h similarity index 52% rename from tests/TestKdbx3XmlReader.cpp rename to tests/TestKdbx3.h index d4ce58542..fac8f7eb1 100644 --- a/tests/TestKdbx3XmlReader.cpp +++ b/tests/TestKdbx3.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 KeePassXC Team + * Copyright (C) 2018 KeePassXC Team * * 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 @@ -15,8 +15,22 @@ * along with this program. If not, see . */ -#include +#ifndef KEEPASSXC_TEST_KDBX3_H +#define KEEPASSXC_TEST_KDBX3_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 diff --git a/tests/TestKdbx4.cpp b/tests/TestKdbx4.cpp new file mode 100644 index 000000000..e67274cbd --- /dev/null +++ b/tests/TestKdbx4.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 KeePassXC Team + * + * 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 . + */ + +#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_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(); +} diff --git a/tests/TestKdbx4XmlReader.cpp b/tests/TestKdbx4.h similarity index 52% rename from tests/TestKdbx4XmlReader.cpp rename to tests/TestKdbx4.h index c1a0b42ee..0615ff3b7 100644 --- a/tests/TestKdbx4XmlReader.cpp +++ b/tests/TestKdbx4.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 KeePassXC Team + * Copyright (C) 2018 KeePassXC Team * * 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 @@ -15,8 +15,22 @@ * along with this program. If not, see . */ -#include +#ifndef KEEPASSXC_TEST_KDBX4_H +#define KEEPASSXC_TEST_KDBX4_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 diff --git a/tests/TestKeePass2XmlReader.cpp b/tests/TestKeePass2XmlReader.cpp index 4970fe927..a89b9547f 100644 --- a/tests/TestKeePass2XmlReader.cpp +++ b/tests/TestKeePass2XmlReader.cpp @@ -17,46 +17,40 @@ #include "TestKeePass2XmlReader.h" -#include -#include -#include -#include - -#include "core/Database.h" #include "core/Group.h" #include "core/Metadata.h" -#include "crypto/Crypto.h" #include "format/KdbxXmlReader.h" #include "format/KdbxXmlWriter.h" #include "config-keepassx-tests.h" +#include +#include + namespace QTest { - template<> - char* toString(const Uuid& uuid) - { - QByteArray ba = "Uuid("; - ba += uuid.toBase64().toLatin1().constData(); - ba += ")"; - return qstrdup(ba.constData()); +template<> +char* toString(const Uuid& uuid) +{ + QByteArray ba = "Uuid("; + ba += uuid.toBase64().toLatin1().constData(); + ba += ")"; + return qstrdup(ba.constData()); +} + +template<> +char* toString(const Group::TriState& triState) +{ + QString value; + + if (triState == Group::Inherit) { + value = "null"; + } else if (triState == Group::Enable) { + value = "true"; + } else { + value = "false"; } - template<> - char* toString(const Group::TriState& triState) - { - QString value; - - if (triState == Group::Inherit) { - value = "null"; - } - else if (triState == Group::Enable) { - value = "true"; - } - else { - value = "false"; - } - - return qstrdup(value.toLocal8Bit().constData()); - } + return qstrdup(value.toLocal8Bit().constData()); +} } QDateTime TestKeePass2XmlReader::genDT(int year, int month, int day, int hour, int min, int second) @@ -70,90 +64,14 @@ QByteArray TestKeePass2XmlReader::strToBytes(const QString& str) { QByteArray result; - for (int i = 0; i < str.size(); i++) { - result.append(str.at(i).unicode() >> 8); - result.append(str.at(i).unicode() & 0xFF); + for (auto i : str) { + result.append(static_cast(i.unicode() >> 8)); + result.append(static_cast(i.unicode() & 0xFF)); } 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() { QCOMPARE(m_db->metadata()->generator(), QString("KeePass")); @@ -437,20 +355,15 @@ void TestKeePass2XmlReader::testBroken() QFETCH(bool, strictMode); QFETCH(bool, expectError); - QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, baseName); QVERIFY(QFile::exists(xmlFile)); bool hasError; QString errorString; - Database* db; - readDatabase(xmlFile, strictMode, db, hasError, errorString); + QScopedPointer db(readDatabase(xmlFile, strictMode, hasError, errorString)); if (hasError) { qWarning("Reader error: %s", qPrintable(errorString)); } QCOMPARE(hasError, expectError); - if (db) { - delete db; - } } void TestKeePass2XmlReader::testBroken_data() @@ -459,22 +372,22 @@ void TestKeePass2XmlReader::testBroken_data() QTest::addColumn("strictMode"); QTest::addColumn("expectError"); - // 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; + // 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; QTest::newRow("BrokenDifferentEntryHistoryUuid (not strict)") << "BrokenDifferentEntryHistoryUuid" << false << false; } @@ -483,17 +396,13 @@ void TestKeePass2XmlReader::testEmptyUuids() QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "EmptyUuids"); QVERIFY(QFile::exists(xmlFile)); - Database* dbp; bool hasError; QString errorString; - readDatabase(xmlFile, true, dbp, hasError, errorString); + QScopedPointer dbp(readDatabase(xmlFile, true, hasError, errorString)); if (hasError) { qWarning("Reader error: %s", qPrintable(errorString)); } QVERIFY(!hasError); - if (dbp) { - delete dbp; - } } void TestKeePass2XmlReader::testInvalidXmlChars() @@ -514,9 +423,10 @@ void TestKeePass2XmlReader::testInvalidXmlChars() 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)); - 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->setGroup(dbWrite->rootGroup()); entry->attributes()->set("PlainInvalid", strPlainInvalid); @@ -538,13 +448,12 @@ void TestKeePass2XmlReader::testInvalidXmlChars() QVERIFY(!hasError); buffer.seek(0); - Database* dbRead; - readDatabase(&buffer, true, dbRead, hasError, errorString); + QScopedPointer dbRead(readDatabase(&buffer, true, hasError, errorString)); if (hasError) { qWarning("Database read error: %s", qPrintable(errorString)); } QVERIFY(!hasError); - QVERIFY(dbRead); + QVERIFY(dbRead.data()); QCOMPARE(dbRead->rootGroup()->entries().size(), 1); Entry* entryRead = dbRead->rootGroup()->entries().at(0); EntryAttributes* attrRead = entryRead->attributes(); @@ -559,27 +468,20 @@ 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() { QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, "BrokenDifferentEntryHistoryUuid"); QVERIFY(QFile::exists(xmlFile)); - Database* db; bool hasError; QString errorString; - readDatabase(xmlFile, true, db, hasError, errorString); + QScopedPointer db(readDatabase(xmlFile, false, hasError, errorString)); if (hasError) { qWarning("Database read error: %s", qPrintable(errorString)); } QVERIFY(!hasError); - - QList entries = db->rootGroup()->entries(); QCOMPARE(entries.size(), 1); Entry* entry = entries.at(0); @@ -591,13 +493,4 @@ void TestKeePass2XmlReader::testRepairUuidHistoryItem() QVERIFY(!entry->uuid().isNull()); QVERIFY(!historyItem->uuid().isNull()); QCOMPARE(historyItem->uuid(), entry->uuid()); - - if (db) { - delete db; - } -} - -void TestKeePass2XmlReader::cleanupTestCase() -{ - delete m_db; } diff --git a/tests/TestKeePass2XmlReader.h b/tests/TestKeePass2XmlReader.h index e07f575b3..ba69eb80f 100644 --- a/tests/TestKeePass2XmlReader.h +++ b/tests/TestKeePass2XmlReader.h @@ -21,8 +21,10 @@ #include #include #include +#include + +#include "core/Database.h" -class Database; class TestKeePass2XmlReader : public QObject { @@ -30,6 +32,8 @@ class TestKeePass2XmlReader : public QObject protected slots: virtual void initTestCase() = 0; + +private slots: void testMetadata(); void testCustomIcons(); void testCustomData(); @@ -45,42 +49,16 @@ protected slots: void testEmptyUuids(); void testInvalidXmlChars(); void testRepairUuidHistoryItem(); - void cleanupTestCase(); 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 Database* readDatabase(QBuffer* buf, bool strictMode, 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; static QDateTime genDT(int year, int month, int day, int hour, int min, int second); static QByteArray strToBytes(const QString& str); - Database* m_db; + QScopedPointer 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