From f1bebe904a0e4842df5aeab9a027e1d99608c9ac Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sat, 20 Apr 2013 19:03:27 +0200 Subject: [PATCH] Add tests for broken databases. Make sure we detect the error and don't crash. --- tests/TestKeePass2XmlReader.cpp | 21 +++++++++++++++++++++ tests/TestKeePass2XmlReader.h | 2 ++ tests/data/BrokenNoEntryUuid.xml | 14 ++++++++++++++ tests/data/BrokenNoGroupUuid.xml | 8 ++++++++ tests/data/BrokenNoRootGroup.xml | 5 +++++ 5 files changed, 50 insertions(+) create mode 100644 tests/data/BrokenNoEntryUuid.xml create mode 100644 tests/data/BrokenNoGroupUuid.xml create mode 100644 tests/data/BrokenNoRootGroup.xml diff --git a/tests/TestKeePass2XmlReader.cpp b/tests/TestKeePass2XmlReader.cpp index a83c426bf..af075d7c4 100644 --- a/tests/TestKeePass2XmlReader.cpp +++ b/tests/TestKeePass2XmlReader.cpp @@ -17,6 +17,7 @@ #include "TestKeePass2XmlReader.h" +#include #include #include "tests.h" @@ -351,6 +352,26 @@ void TestKeePass2XmlReader::testDeletedObjects() QVERIFY(objList.isEmpty()); } +void TestKeePass2XmlReader::testBroken() +{ + QFETCH(QString, baseName); + + KeePass2XmlReader reader; + QString xmlFile = QString("%1/%2.xml").arg(KEEPASSX_TEST_DATA_DIR, baseName); + QVERIFY(QFile::exists(xmlFile)); + QScopedPointer db(reader.readDatabase(xmlFile)); + QVERIFY(reader.hasError()); +} + +void TestKeePass2XmlReader::testBroken_data() +{ + QTest::addColumn("baseName"); + + QTest::newRow("BrokenNoGroupUuid") << "BrokenNoGroupUuid"; + QTest::newRow("BrokenNoEntryUuid") << "BrokenNoEntryUuid"; + QTest::newRow("BrokenNoRootGroup") << "BrokenNoRootGroup"; +} + void TestKeePass2XmlReader::cleanupTestCase() { delete m_db; diff --git a/tests/TestKeePass2XmlReader.h b/tests/TestKeePass2XmlReader.h index 2ed25fadb..7b0f786a1 100644 --- a/tests/TestKeePass2XmlReader.h +++ b/tests/TestKeePass2XmlReader.h @@ -39,6 +39,8 @@ private Q_SLOTS: void testEntry2(); void testEntryHistory(); void testDeletedObjects(); + void testBroken(); + void testBroken_data(); void cleanupTestCase(); private: diff --git a/tests/data/BrokenNoEntryUuid.xml b/tests/data/BrokenNoEntryUuid.xml new file mode 100644 index 000000000..38ab96f2e --- /dev/null +++ b/tests/data/BrokenNoEntryUuid.xml @@ -0,0 +1,14 @@ + + + + + lmU+9n0aeESKZvcEze+bRg== + Test + + + Title + Sample Entry 1 + + + + diff --git a/tests/data/BrokenNoGroupUuid.xml b/tests/data/BrokenNoGroupUuid.xml new file mode 100644 index 000000000..1e5d32c82 --- /dev/null +++ b/tests/data/BrokenNoGroupUuid.xml @@ -0,0 +1,8 @@ + + + + + Test + + + diff --git a/tests/data/BrokenNoRootGroup.xml b/tests/data/BrokenNoRootGroup.xml new file mode 100644 index 000000000..dcb21c68b --- /dev/null +++ b/tests/data/BrokenNoRootGroup.xml @@ -0,0 +1,5 @@ + + + + +