2011-12-26 13:18:21 -05:00
|
|
|
/*
|
|
|
|
* 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 "TestGui.h"
|
|
|
|
|
|
|
|
#include <QtTest/QTest>
|
|
|
|
#include <QtGui/QAction>
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
#include <QtGui/QDialogButtonBox>
|
|
|
|
#include <QtGui/QLineEdit>
|
|
|
|
#include <QtGui/QPushButton>
|
2012-04-26 11:52:02 -04:00
|
|
|
#include <QtGui/QToolBar>
|
2012-05-20 15:09:34 -04:00
|
|
|
#include <QtGui/QToolButton>
|
2011-12-26 13:18:21 -05:00
|
|
|
|
|
|
|
#include "config-keepassx-tests.h"
|
2012-01-01 16:04:27 -05:00
|
|
|
#include "tests.h"
|
2012-06-14 16:55:25 -04:00
|
|
|
#include "core/Config.h"
|
2012-07-06 13:21:19 -04:00
|
|
|
#include "core/Database.h"
|
2012-05-10 04:29:25 -04:00
|
|
|
#include "core/Entry.h"
|
2012-07-06 13:21:19 -04:00
|
|
|
#include "core/Metadata.h"
|
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
#include "format/KeePass2Reader.h"
|
2012-04-14 14:31:35 -04:00
|
|
|
#include "gui/DatabaseTabWidget.h"
|
|
|
|
#include "gui/DatabaseWidget.h"
|
2011-12-26 13:18:21 -05:00
|
|
|
#include "gui/FileDialog.h"
|
|
|
|
#include "gui/MainWindow.h"
|
2012-05-16 04:16:32 -04:00
|
|
|
#include "gui/entry/EditEntryWidget.h"
|
|
|
|
#include "gui/entry/EntryView.h"
|
2012-07-06 13:21:19 -04:00
|
|
|
#include "keys/PasswordKey.h"
|
2011-12-26 13:18:21 -05:00
|
|
|
|
|
|
|
void TestGui::initTestCase()
|
|
|
|
{
|
|
|
|
Crypto::init();
|
2012-06-14 16:55:25 -04:00
|
|
|
Config::createTempFileInstance();
|
2011-12-29 14:10:19 -05:00
|
|
|
m_mainWindow = new MainWindow();
|
2012-07-06 13:21:19 -04:00
|
|
|
m_tabWidget = m_mainWindow->findChild<DatabaseTabWidget*>("tabWidget");
|
2012-04-26 11:52:02 -04:00
|
|
|
m_mainWindow->show();
|
|
|
|
QTest::qWaitForWindowShown(m_mainWindow);
|
2011-12-26 13:18:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGui::testOpenDatabase()
|
|
|
|
{
|
2011-12-29 14:10:19 -05:00
|
|
|
QAction* actionDatabaseOpen = m_mainWindow->findChild<QAction*>("actionDatabaseOpen");
|
2011-12-26 13:18:21 -05:00
|
|
|
fileDialog()->setNextFileName(QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx"));
|
|
|
|
actionDatabaseOpen->trigger();
|
2012-06-28 03:21:15 -04:00
|
|
|
QTest::qWait(20);
|
2011-12-26 13:18:21 -05:00
|
|
|
|
2012-06-28 03:21:15 -04:00
|
|
|
QWidget* databaseOpenWidget = m_mainWindow->findChild<QWidget*>("databaseOpenWidget");
|
|
|
|
QLineEdit* editPassword = databaseOpenWidget->findChild<QLineEdit*>("editPassword");
|
2011-12-26 13:18:21 -05:00
|
|
|
QVERIFY(editPassword);
|
2012-06-28 03:21:15 -04:00
|
|
|
|
2011-12-26 13:18:21 -05:00
|
|
|
QTest::keyClicks(editPassword, "a");
|
2012-06-28 03:21:15 -04:00
|
|
|
QTest::keyClick(editPassword, Qt::Key_Enter);
|
2011-12-26 13:18:21 -05:00
|
|
|
|
2012-04-14 14:31:35 -04:00
|
|
|
QTest::qWait(20);
|
2011-12-29 14:10:19 -05:00
|
|
|
}
|
2011-12-26 13:18:21 -05:00
|
|
|
|
2011-12-29 14:10:19 -05:00
|
|
|
void TestGui::testTabs()
|
|
|
|
{
|
2012-07-06 13:21:19 -04:00
|
|
|
QCOMPARE(m_tabWidget->count(), 1);
|
|
|
|
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("NewDatabase.kdbx"));
|
2011-12-26 13:18:21 -05:00
|
|
|
}
|
|
|
|
|
2012-04-14 14:31:35 -04:00
|
|
|
void TestGui::testEditEntry()
|
|
|
|
{
|
2012-07-06 13:21:19 -04:00
|
|
|
DatabaseWidget* dbWidget = m_tabWidget->currentDatabaseWidget();
|
2012-04-14 14:31:35 -04:00
|
|
|
EntryView* entryView = dbWidget->findChild<EntryView*>("entryView");
|
2012-05-12 07:22:41 -04:00
|
|
|
QModelIndex item = entryView->model()->index(0, 1);
|
2012-04-14 14:31:35 -04:00
|
|
|
QRect itemRect = entryView->visualRect(item);
|
|
|
|
QTest::mouseClick(entryView->viewport(), Qt::LeftButton, Qt::NoModifier, itemRect.center());
|
|
|
|
QTest::qWait(20);
|
2012-04-26 11:52:02 -04:00
|
|
|
|
|
|
|
QAction* entryEditAction = m_mainWindow->findChild<QAction*>("actionEntryEdit");
|
|
|
|
QVERIFY(entryEditAction->isEnabled());
|
|
|
|
QToolBar* toolBar = m_mainWindow->findChild<QToolBar*>("toolBar");
|
|
|
|
QWidget* entryEditWidget = toolBar->widgetForAction(entryEditAction);
|
|
|
|
QVERIFY(entryEditWidget->isVisible());
|
|
|
|
QVERIFY(entryEditWidget->isEnabled());
|
|
|
|
QTest::mouseClick(entryEditWidget, Qt::LeftButton);
|
2012-04-14 14:31:35 -04:00
|
|
|
QTest::qWait(20);
|
2012-04-26 11:52:02 -04:00
|
|
|
|
2012-04-14 14:31:35 -04:00
|
|
|
EditEntryWidget* editEntryWidget = dbWidget->findChild<EditEntryWidget*>("editEntryWidget");
|
2012-04-26 11:52:02 -04:00
|
|
|
QVERIFY(dbWidget->currentWidget() == editEntryWidget);
|
2012-04-14 14:31:35 -04:00
|
|
|
QDialogButtonBox* editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
|
|
|
|
QVERIFY(editEntryWidgetButtonBox);
|
|
|
|
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
// make sure the database isn't marked as modified
|
2012-07-06 13:21:19 -04:00
|
|
|
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("NewDatabase.kdbx"));
|
2012-04-14 14:31:35 -04:00
|
|
|
}
|
|
|
|
|
2012-05-10 04:29:25 -04:00
|
|
|
void TestGui::testAddEntry()
|
|
|
|
{
|
2012-07-06 13:21:19 -04:00
|
|
|
DatabaseWidget* dbWidget = m_tabWidget->currentDatabaseWidget();
|
2012-05-10 04:29:25 -04:00
|
|
|
|
|
|
|
EntryView* entryView = dbWidget->findChild<EntryView*>("entryView");
|
|
|
|
QAction* entryNewAction = m_mainWindow->findChild<QAction*>("actionEntryNew");
|
|
|
|
QVERIFY(entryNewAction->isEnabled());
|
|
|
|
QToolBar* toolBar = m_mainWindow->findChild<QToolBar*>("toolBar");
|
|
|
|
QWidget* entryNewWidget = toolBar->widgetForAction(entryNewAction);
|
|
|
|
QVERIFY(entryNewWidget->isVisible());
|
|
|
|
QVERIFY(entryNewWidget->isEnabled());
|
|
|
|
|
|
|
|
QTest::mouseClick(entryNewWidget, Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QCOMPARE(dbWidget->currentMode(), DatabaseWidget::EditMode);
|
|
|
|
|
|
|
|
EditEntryWidget* editEntryWidget = dbWidget->findChild<EditEntryWidget*>("editEntryWidget");
|
|
|
|
QLineEdit* titleEdit = editEntryWidget->findChild<QLineEdit*>("titleEdit");
|
|
|
|
QTest::keyClicks(titleEdit, "test");
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QDialogButtonBox* editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
|
|
|
|
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QCOMPARE(dbWidget->currentMode(), DatabaseWidget::ViewMode);
|
2012-05-12 07:22:41 -04:00
|
|
|
QModelIndex item = entryView->model()->index(1, 1);
|
2012-05-11 06:01:01 -04:00
|
|
|
Entry* entry = entryView->entryFromIndex(item);
|
2012-05-10 04:29:25 -04:00
|
|
|
|
|
|
|
QCOMPARE(entry->title(), QString("test"));
|
|
|
|
QCOMPARE(entry->historyItems().size(), 0);
|
2012-06-24 11:53:01 -04:00
|
|
|
QTest::qWait(200);
|
2012-07-06 13:21:19 -04:00
|
|
|
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("NewDatabase.kdbx*"));
|
2012-05-10 04:29:25 -04:00
|
|
|
|
|
|
|
QAction* entryEditAction = m_mainWindow->findChild<QAction*>("actionEntryEdit");
|
|
|
|
QVERIFY(entryEditAction->isEnabled());
|
|
|
|
QWidget* entryEditWidget = toolBar->widgetForAction(entryEditAction);
|
|
|
|
QVERIFY(entryEditWidget->isVisible());
|
|
|
|
QVERIFY(entryEditWidget->isEnabled());
|
|
|
|
QTest::mouseClick(entryEditWidget, Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QCOMPARE(dbWidget->currentMode(), DatabaseWidget::EditMode);
|
|
|
|
QTest::keyClicks(titleEdit, "something");
|
|
|
|
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QCOMPARE(entry->title(), QString("testsomething"));
|
|
|
|
QCOMPARE(entry->historyItems().size(), 1);
|
|
|
|
}
|
|
|
|
|
2012-05-16 03:56:40 -04:00
|
|
|
void TestGui::testSearch()
|
|
|
|
{
|
2012-07-06 13:21:19 -04:00
|
|
|
DatabaseWidget* dbWidget = m_tabWidget->currentDatabaseWidget();
|
2012-05-16 03:56:40 -04:00
|
|
|
|
2012-06-24 18:09:40 -04:00
|
|
|
QAction* searchAction = m_mainWindow->findChild<QAction*>("actionSearch");
|
|
|
|
QVERIFY(searchAction->isEnabled());
|
2012-05-19 05:53:32 -04:00
|
|
|
QToolBar* toolBar = m_mainWindow->findChild<QToolBar*>("toolBar");
|
2012-06-24 18:09:40 -04:00
|
|
|
QWidget* searchActionWidget = toolBar->widgetForAction(searchAction);
|
|
|
|
QVERIFY(searchActionWidget->isVisible());
|
|
|
|
QVERIFY(searchActionWidget->isEnabled());
|
|
|
|
QTest::mouseClick(searchActionWidget, Qt::LeftButton);
|
2012-05-19 05:53:32 -04:00
|
|
|
QTest::qWait(20);
|
|
|
|
|
2012-05-16 03:56:40 -04:00
|
|
|
EntryView* entryView = dbWidget->findChild<EntryView*>("entryView");
|
|
|
|
QLineEdit* searchEdit = dbWidget->findChild<QLineEdit*>("searchEdit");
|
2012-05-20 15:09:34 -04:00
|
|
|
QToolButton* clearSearch = dbWidget->findChild<QToolButton*>("clearButton");
|
2012-05-16 03:56:40 -04:00
|
|
|
|
|
|
|
QTest::keyClicks(searchEdit, "ZZZ");
|
2012-07-02 16:12:07 -04:00
|
|
|
QTest::qWait(200);
|
2012-05-16 03:56:40 -04:00
|
|
|
|
|
|
|
QCOMPARE(entryView->model()->rowCount(), 0);
|
|
|
|
|
|
|
|
QTest::mouseClick(clearSearch, Qt::LeftButton);
|
|
|
|
QTest::keyClicks(searchEdit, "some");
|
2012-07-02 16:12:07 -04:00
|
|
|
QTest::qWait(200);
|
2012-05-16 03:56:40 -04:00
|
|
|
|
|
|
|
QCOMPARE(entryView->model()->rowCount(), 2);
|
|
|
|
|
|
|
|
QModelIndex item = entryView->model()->index(0, 1);
|
|
|
|
QRect itemRect = entryView->visualRect(item);
|
|
|
|
QTest::mouseClick(entryView->viewport(), Qt::LeftButton, Qt::NoModifier, itemRect.center());
|
|
|
|
QAction* entryEditAction = m_mainWindow->findChild<QAction*>("actionEntryEdit");
|
|
|
|
QVERIFY(entryEditAction->isEnabled());
|
|
|
|
QWidget* entryEditWidget = toolBar->widgetForAction(entryEditAction);
|
|
|
|
QVERIFY(entryEditWidget->isVisible());
|
|
|
|
QVERIFY(entryEditWidget->isEnabled());
|
|
|
|
QTest::mouseClick(entryEditWidget, Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QCOMPARE(dbWidget->currentMode(), DatabaseWidget::EditMode);
|
|
|
|
|
|
|
|
EditEntryWidget* editEntryWidget = dbWidget->findChild<EditEntryWidget*>("editEntryWidget");
|
|
|
|
QDialogButtonBox* editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
|
|
|
|
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QCOMPARE(dbWidget->currentMode(), DatabaseWidget::ViewMode);
|
|
|
|
|
|
|
|
QModelIndex item2 = entryView->model()->index(1, 0);
|
|
|
|
QRect itemRect2 = entryView->visualRect(item2);
|
|
|
|
QTest::mouseClick(entryView->viewport(), Qt::LeftButton, Qt::NoModifier, itemRect2.center());
|
|
|
|
QAction* entryDeleteAction = m_mainWindow->findChild<QAction*>("actionEntryDelete");
|
|
|
|
|
|
|
|
QWidget* entryDeleteWidget = toolBar->widgetForAction(entryDeleteAction);
|
|
|
|
QVERIFY(entryDeleteWidget->isVisible());
|
|
|
|
QVERIFY(entryDeleteWidget->isEnabled());
|
|
|
|
|
|
|
|
QTest::mouseClick(entryDeleteWidget, Qt::LeftButton);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QCOMPARE(entryView->model()->rowCount(), 1);
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:21:19 -04:00
|
|
|
void TestGui::testSaveAs()
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo(QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx"));
|
|
|
|
QDateTime lastModified = fileInfo.lastModified();
|
|
|
|
|
|
|
|
Database* db = m_tabWidget->currentDatabaseWidget()->database();
|
|
|
|
db->metadata()->setName("SaveAs");
|
|
|
|
|
|
|
|
// open temporary file so it creates a filename
|
|
|
|
QVERIFY(tmpFile.open());
|
|
|
|
tmpFile.close();
|
|
|
|
fileDialog()->setNextFileName(tmpFile.fileName());
|
|
|
|
|
|
|
|
QAction* actionDatabaseSaveAs = m_mainWindow->findChild<QAction*>("actionDatabaseSaveAs");
|
|
|
|
actionDatabaseSaveAs->trigger();
|
|
|
|
|
|
|
|
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("SaveAs"));
|
|
|
|
|
|
|
|
CompositeKey key;
|
|
|
|
key.addKey(PasswordKey("a"));
|
|
|
|
KeePass2Reader reader;
|
|
|
|
QScopedPointer<Database> dbSaved(reader.readDatabase(tmpFile.fileName(), key));
|
|
|
|
QVERIFY(dbSaved);
|
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
QCOMPARE(dbSaved->metadata()->name(), db->metadata()->name());
|
|
|
|
|
|
|
|
fileInfo.refresh();
|
|
|
|
QCOMPARE(fileInfo.lastModified(), lastModified);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGui::testSave()
|
|
|
|
{
|
|
|
|
Database* db = m_tabWidget->currentDatabaseWidget()->database();
|
|
|
|
db->metadata()->setName("Save");
|
|
|
|
QTest::qWait(200);
|
|
|
|
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("Save*"));
|
|
|
|
|
|
|
|
QAction* actionDatabaseSave = m_mainWindow->findChild<QAction*>("actionDatabaseSave");
|
|
|
|
actionDatabaseSave->trigger();
|
|
|
|
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("Save"));
|
|
|
|
|
|
|
|
CompositeKey key;
|
|
|
|
key.addKey(PasswordKey("a"));
|
|
|
|
KeePass2Reader reader;
|
|
|
|
QScopedPointer<Database> dbSaved(reader.readDatabase(tmpFile.fileName(), key));
|
|
|
|
QVERIFY(dbSaved);
|
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
QCOMPARE(dbSaved->metadata()->name(), db->metadata()->name());
|
|
|
|
}
|
|
|
|
|
2012-06-29 09:54:34 -04:00
|
|
|
void TestGui::testKeePass1Import()
|
|
|
|
{
|
|
|
|
QAction* actionImportKeePass1 = m_mainWindow->findChild<QAction*>("actionImportKeePass1");
|
|
|
|
fileDialog()->setNextFileName(QString(KEEPASSX_TEST_DATA_DIR).append("/basic.kdb"));
|
|
|
|
actionImportKeePass1->trigger();
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
|
|
|
QWidget* keepass1OpenWidget = m_mainWindow->findChild<QWidget*>("keepass1OpenWidget");
|
|
|
|
QLineEdit* editPassword = keepass1OpenWidget->findChild<QLineEdit*>("editPassword");
|
|
|
|
QVERIFY(editPassword);
|
|
|
|
|
|
|
|
QTest::keyClicks(editPassword, "masterpw");
|
|
|
|
QTest::keyClick(editPassword, Qt::Key_Enter);
|
|
|
|
QTest::qWait(20);
|
|
|
|
|
2012-07-06 13:21:19 -04:00
|
|
|
QCOMPARE(m_tabWidget->count(), 2);
|
|
|
|
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("basic [New database]*"));
|
2012-06-29 09:54:34 -04:00
|
|
|
}
|
|
|
|
|
2011-12-29 14:10:19 -05:00
|
|
|
void TestGui::cleanupTestCase()
|
|
|
|
{
|
2012-04-18 16:08:22 -04:00
|
|
|
delete m_mainWindow;
|
2011-12-29 14:10:19 -05:00
|
|
|
}
|
|
|
|
|
2012-01-01 16:04:27 -05:00
|
|
|
KEEPASSX_QTEST_GUI_MAIN(TestGui)
|