Fix parser and add unit tests.

This commit is contained in:
Felix Geyer
2010-08-13 18:08:06 +02:00
parent b64dbce2da
commit bd1ea05017
18 changed files with 690 additions and 103 deletions

70
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,70 @@
# Copyright (C) 2010 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/>.
set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/src)
set( KEEPASSX_TEST_TREE ${CMAKE_SOURCE_DIR}/tests )
configure_file( config-keepassx-tests.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-keepassx-tests.h )
macro (ADD_UNIT_TEST _test_NAME)
set(_srcList ${ARGN})
set(_targetName ${_test_NAME})
if( ${ARGV1} STREQUAL "TESTNAME" )
set(_targetName ${ARGV2})
list(REMOVE_AT _srcList 0 1)
endif( ${ARGV1} STREQUAL "TESTNAME" )
set(_nogui)
list(GET ${_srcList} 0 first_PARAM)
if( ${first_PARAM} STREQUAL "NOGUI" )
set(_nogui "NOGUI")
endif( ${first_PARAM} STREQUAL "NOGUI" )
automoc4_add_executable( ${_test_NAME} ${_srcList} )
if(NOT TEST_OUTPUT)
set(TEST_OUTPUT plaintext)
endif(NOT TEST_OUTPUT)
set(TEST_OUTPUT ${TEST_OUTPUT} CACHE STRING "The output to generate when running the QTest unit tests")
get_target_property( loc ${_test_NAME} LOCATION )
if (KDE4_TEST_OUTPUT STREQUAL "xml")
add_test( ${_targetName} ${loc} -xml -o ${_targetName}.tml)
else (KDE4_TEST_OUTPUT STREQUAL "xml")
add_test( ${_targetName} ${loc} )
endif (KDE4_TEST_OUTPUT STREQUAL "xml")
if (NOT MSVC_IDE) #not needed for the ide
# if the tests are EXCLUDE_FROM_ALL, add a target "buildtests" to build all tests
if (NOT WITH_TESTS)
get_directory_property(_buildtestsAdded BUILDTESTS_ADDED)
if(NOT _buildtestsAdded)
add_custom_target(buildtests)
set_directory_properties(PROPERTIES BUILDTESTS_ADDED TRUE)
endif(NOT _buildtestsAdded)
add_dependencies(buildtests ${_test_NAME})
endif (NOT WITH_TESTS)
endif (NOT MSVC_IDE)
endmacro (ADD_UNIT_TEST)
add_unit_test( testparser TestParser.cpp )
target_link_libraries( testparser keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} )
add_unit_test( testgroup TestGroup.cpp )
target_link_libraries( testgroup keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} )

287
tests/NewDatabase.xml Normal file
View File

@@ -0,0 +1,287 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<KeePassFile>
<Meta>
<Generator>KeePass</Generator>
<DatabaseName>ANAME</DatabaseName>
<DatabaseNameChanged>2010-08-08T17:24:53Z</DatabaseNameChanged>
<DatabaseDescription>ADESC</DatabaseDescription>
<DatabaseDescriptionChanged>2010-08-08T17:27:12Z</DatabaseDescriptionChanged>
<DefaultUserName>DEFUSERNAME</DefaultUserName>
<DefaultUserNameChanged>2010-08-08T17:27:45Z</DefaultUserNameChanged>
<MaintenanceHistoryDays>127</MaintenanceHistoryDays>
<MemoryProtection>
<ProtectTitle>False</ProtectTitle>
<ProtectUserName>True</ProtectUserName>
<ProtectPassword>False</ProtectPassword>
<ProtectURL>True</ProtectURL>
<ProtectNotes>False</ProtectNotes>
<AutoEnableVisualHiding>True</AutoEnableVisualHiding>
</MemoryProtection>
<RecycleBinEnabled>True</RecycleBinEnabled>
<RecycleBinUUID>7PAwxNhPaE2klutz45i2xg==</RecycleBinUUID>
<RecycleBinChanged>2010-08-08T17:24:17Z</RecycleBinChanged>
<EntryTemplatesGroup>AAAAAAAAAAAAAAAAAAAAAA==</EntryTemplatesGroup>
<EntryTemplatesGroupChanged>2010-08-08T17:24:19Z</EntryTemplatesGroupChanged>
<LastSelectedGroup>zKuE27EWr0mlU75b2SRkTQ==</LastSelectedGroup>
<LastTopVisibleGroup>zKuE27EWr0mlU75b2SRkTQ==</LastTopVisibleGroup>
<CustomData />
</Meta>
<Root>
<Group>
<UUID>zKuE27EWr0mlU75b2SRkTQ==</UUID>
<Name>NewDatabase</Name>
<Notes />
<IconID>49</IconID>
<Times>
<LastModificationTime>2010-08-08T17:24:27Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:27Z</CreationTime>
<LastAccessTime>2010-08-09T09:09:44Z</LastAccessTime>
<ExpiryTime>2010-08-08T17:24:17Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>2</UsageCount>
<LocationChanged>2010-08-08T17:24:27Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>QW4G0r/z90qql4iKZ0RwlA==</LastTopVisibleEntry>
<Entry>
<UUID>QW4G0r/z90qql4iKZ0RwlA==</UUID>
<IconID>0</IconID>
<ForegroundColor>#0000FF</ForegroundColor>
<BackgroundColor>#000000</BackgroundColor>
<OverrideURL>OMGAURL</OverrideURL>
<Times>
<LastModificationTime>2010-08-09T09:09:31Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:53Z</CreationTime>
<LastAccessTime>2010-08-09T09:09:31Z</LastAccessTime>
<ExpiryTime>2011-08-08T17:25:58Z</ExpiryTime>
<Expires>True</Expires>
<UsageCount>6</UsageCount>
<LocationChanged>2010-08-08T17:24:53Z</LocationChanged>
</Times>
<String>
<Key>customfield</Key>
<Value>customfield value</Value>
</String>
<String>
<Key>Notes</Key>
<Value>Notes</Value>
</String>
<String>
<Key>Password</Key>
<Value>Password</Value>
</String>
<String>
<Key>Title</Key>
<Value>Sample Entry</Value>
</String>
<String>
<Key>URL</Key>
<Value>bleh</Value>
</String>
<String>
<Key>UserName</Key>
<Value>User Name</Value>
</String>
<AutoType>
<Enabled>True</Enabled>
<DataTransferObfuscation>1</DataTransferObfuscation>
<DefaultSequence>{USERNAME}{TAB}{PASSWORD}{ENTER}</DefaultSequence>
<Association>
<Window>Target Window</Window>
<KeystrokeSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{ENTER}</KeystrokeSequence>
</Association>
<Association>
<Window>Edit Entry</Window>
<KeystrokeSequence>{Title}{UserName}{UserName}</KeystrokeSequence>
</Association>
</AutoType>
<History />
</Entry>
<Group>
<UUID>abLbFtNUfEi5TmbaxiW6yg==</UUID>
<Name>General</Name>
<Notes />
<IconID>48</IconID>
<Times>
<LastModificationTime>2010-08-08T17:24:53Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:53Z</CreationTime>
<LastAccessTime>2010-08-08T17:24:53Z</LastAccessTime>
<ExpiryTime>2010-08-08T17:24:17Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2010-08-08T17:24:53Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
</Group>
<Group>
<UUID>u1lTRAICOkWv5QSl2xyU8w==</UUID>
<Name>Windows</Name>
<Notes />
<IconID>38</IconID>
<Times>
<LastModificationTime>2010-08-08T17:24:53Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:53Z</CreationTime>
<LastAccessTime>2010-08-08T17:24:53Z</LastAccessTime>
<ExpiryTime>2010-08-08T17:24:17Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2010-08-08T17:24:53Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
</Group>
<Group>
<UUID>bFe1/LfewEuvlTsT8nJRRg==</UUID>
<Name>Network</Name>
<Notes />
<IconID>3</IconID>
<Times>
<LastModificationTime>2010-08-08T17:24:53Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:53Z</CreationTime>
<LastAccessTime>2010-08-08T17:24:53Z</LastAccessTime>
<ExpiryTime>2010-08-08T17:24:17Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2010-08-08T17:24:53Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
</Group>
<Group>
<UUID>wHmj/+6vTkOpG/eeVp3yjg==</UUID>
<Name>Internet</Name>
<Notes />
<IconID>1</IconID>
<Times>
<LastModificationTime>2010-08-08T17:24:53Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:53Z</CreationTime>
<LastAccessTime>2010-08-08T17:24:53Z</LastAccessTime>
<ExpiryTime>2010-08-08T17:24:17Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2010-08-08T17:24:53Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
</Group>
<Group>
<UUID>9QLqqFgc5EC7ptm2TI1hDA==</UUID>
<Name>eMail</Name>
<Notes />
<IconID>19</IconID>
<Times>
<LastModificationTime>2010-08-08T17:24:53Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:53Z</CreationTime>
<LastAccessTime>2010-08-08T17:24:53Z</LastAccessTime>
<ExpiryTime>2010-08-08T17:24:17Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2010-08-08T17:24:53Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
</Group>
<Group>
<UUID>wQru0ArOaEOy0uUio3subA==</UUID>
<Name>Homebanking</Name>
<Notes />
<IconID>37</IconID>
<Times>
<LastModificationTime>2010-08-08T17:24:53Z</LastModificationTime>
<CreationTime>2010-08-08T17:24:53Z</CreationTime>
<LastAccessTime>2010-08-08T17:24:53Z</LastAccessTime>
<ExpiryTime>2010-08-08T17:24:17Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2010-08-08T17:24:53Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
</Group>
<Group>
<UUID>7PAwxNhPaE2klutz45i2xg==</UUID>
<Name>Recycle Bin</Name>
<Notes />
<IconID>43</IconID>
<Times>
<LastModificationTime>2010-08-09T09:09:44Z</LastModificationTime>
<CreationTime>2010-08-09T09:09:44Z</CreationTime>
<LastAccessTime>2010-08-09T09:09:44Z</LastAccessTime>
<ExpiryTime>2010-08-09T09:07:16Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>1</UsageCount>
<LocationChanged>2010-08-09T09:09:44Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence />
<EnableAutoType>false</EnableAutoType>
<EnableSearching>false</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
<Entry>
<UUID>KIeQe6yDN0SbjIJ83NB++Q==</UUID>
<IconID>0</IconID>
<ForegroundColor />
<BackgroundColor />
<OverrideURL />
<Times>
<LastModificationTime>2010-08-09T09:09:40Z</LastModificationTime>
<CreationTime>2010-08-09T09:09:37Z</CreationTime>
<LastAccessTime>2010-08-09T09:09:44Z</LastAccessTime>
<ExpiryTime>2010-08-09T09:07:16Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>2</UsageCount>
<LocationChanged>2010-08-09T09:09:44Z</LocationChanged>
</Times>
<String>
<Key>Notes</Key>
<Value />
</String>
<String>
<Key>Password</Key>
<Value>5Ciyy3kcVSPFUFqTuK1o</Value>
</String>
<String>
<Key>Title</Key>
<Value>test</Value>
</String>
<String>
<Key>URL</Key>
<Value />
</String>
<String>
<Key>UserName</Key>
<Value>DEFUSERNAME</Value>
</String>
<AutoType>
<Enabled>True</Enabled>
<DataTransferObfuscation>0</DataTransferObfuscation>
</AutoType>
<History />
</Entry>
</Group>
</Group>
<DeletedObjects />
</Root>
</KeePassFile>

68
tests/TestGroup.cpp Normal file
View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2010 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 <QtTest/QTest>
#include "core/Database.h"
class TestGroup : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testParenting();
};
void TestGroup::testParenting()
{
Database* db = new Database();
Group* tmpRoot = new Group();
tmpRoot->setParent(db);
Group* g1 = new Group();
Group* g2 = new Group();
Group* g3 = new Group();
Group* g4 = new Group();
g1->setParent(tmpRoot);
g2->setParent(tmpRoot);
g3->setParent(tmpRoot);
g4->setParent(tmpRoot);
g2->setParent(g1);
g4->setParent(g3);
g3->setParent(g1);
g1->setParent(db);
QVERIFY(g1->parent() == db);
QVERIFY(g2->parent() == g1);
QVERIFY(g3->parent() == g1);
QVERIFY(g4->parent() == g3);
QVERIFY(tmpRoot->children().size() == 0);
QVERIFY(g1->children().size() == 2);
QVERIFY(g2->children().size() == 0);
QVERIFY(g3->children().size() == 1);
QVERIFY(g4->children().size() == 0);
QVERIFY(g1->children().contains(g2));
QVERIFY(g1->children().contains(g3));
QVERIFY(g3->children().contains(g4));
}
QTEST_MAIN(TestGroup);
#include "TestGroup.moc"

92
tests/TestParser.cpp Normal file
View File

@@ -0,0 +1,92 @@
/*
* Copyright (C) 2010 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 <QtTest/QTest>
#include "core/Database.h"
#include "core/Metadata.h"
#include "core/Parser.h"
#include "config-keepassx-tests.h"
class TestParser : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testMetadata();
void testGroups();
private:
QDateTime genDT(int year, int month, int day, int hour, int min, int second);
Database* m_db;
};
QDateTime TestParser::genDT(int year, int month, int day, int hour, int min, int second)
{
QDate date(year, month, day);
QTime time(hour, min, second);
return QDateTime(date, time, Qt::UTC);
}
void TestParser::initTestCase()
{
m_db = new Database();
Parser* parser = new Parser(m_db);
QString xmlFile = QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml");
QVERIFY(parser->parse(xmlFile));
}
void TestParser::testMetadata()
{
QCOMPARE(m_db->metadata()->generator(), QLatin1String("KeePass"));
QCOMPARE(m_db->metadata()->name(), QLatin1String("ANAME"));
QCOMPARE(m_db->metadata()->nameChanged(), genDT(2010, 8, 8, 17, 24, 53));
QCOMPARE(m_db->metadata()->description(), QLatin1String("ADESC"));
QCOMPARE(m_db->metadata()->descriptionChanged(), genDT(2010, 8, 8, 17, 27, 12));
QCOMPARE(m_db->metadata()->defaultUserName(), QLatin1String("DEFUSERNAME"));
QCOMPARE(m_db->metadata()->defaultUserNameChanged(), genDT(2010, 8, 8, 17, 27, 45));
QCOMPARE(m_db->metadata()->maintenanceHistoryDays(), 127);
QCOMPARE(m_db->metadata()->protectTitle(), false);
QCOMPARE(m_db->metadata()->protectUsername(), true);
QCOMPARE(m_db->metadata()->protectPassword(), false);
QCOMPARE(m_db->metadata()->protectUrl(), true);
QCOMPARE(m_db->metadata()->protectNotes(), false);
QCOMPARE(m_db->metadata()->autoEnableVisualHiding(), true);
QCOMPARE(m_db->metadata()->recycleBinEnabled(), true);
QVERIFY(m_db->metadata()->recycleBin() != 0);
QCOMPARE(m_db->metadata()->recycleBin()->name(), QLatin1String("Recycle Bin"));
QCOMPARE(m_db->metadata()->recycleBinChanged(), genDT(2010, 8, 8, 17, 24, 17));
QVERIFY(m_db->metadata()->entryTemplatesGroup() == 0);
QCOMPARE(m_db->metadata()->entryTemplatesGroupChanged(), genDT(2010, 8, 8, 17, 24, 19));
QVERIFY(m_db->metadata()->lastSelectedGroup() != 0);
QCOMPARE(m_db->metadata()->lastSelectedGroup()->name(), QLatin1String("NewDatabase"));
QVERIFY(m_db->metadata()->lastTopVisibleGroup() != 0);
QCOMPARE(m_db->metadata()->lastTopVisibleGroup()->name(), QLatin1String("NewDatabase"));
}
void TestParser::testGroups()
{
QVERIFY(m_db->rootGroup()->name() == QLatin1String("NewDatabase"));
QVERIFY(m_db->rootGroup()->uuid().toBase64() == QLatin1String("zKuE27EWr0mlU75b2SRkTQ=="));
QVERIFY(m_db->rootGroup()->isExpanded() == true);
}
QTEST_MAIN(TestParser);
#include "TestParser.moc"

View File

@@ -0,0 +1,3 @@
/* config-keepassx-tests.h. Generated by cmake from config-keepassx-tests.h.cmake */
#define KEEPASSX_TEST_DIR "${KEEPASSX_TEST_TREE}"