2012-10-28 10:42:19 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
|
2017-06-09 17:40:36 -04:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2012-10-28 10:42:19 -04:00
|
|
|
*
|
|
|
|
* 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 "TestAutoType.h"
|
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QPluginLoader>
|
|
|
|
#include <QTest>
|
2012-10-28 10:42:19 -04:00
|
|
|
|
2014-05-15 12:26:01 -04:00
|
|
|
#include "core/Config.h"
|
2012-10-28 10:42:19 -04:00
|
|
|
#include "core/FilePath.h"
|
2012-10-28 13:13:49 -04:00
|
|
|
#include "core/Entry.h"
|
|
|
|
#include "core/Group.h"
|
2012-10-28 10:42:19 -04:00
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
#include "autotype/AutoType.h"
|
|
|
|
#include "autotype/AutoTypePlatformPlugin.h"
|
|
|
|
#include "autotype/test/AutoTypeTestInterface.h"
|
2014-04-07 02:54:32 -04:00
|
|
|
#include "gui/MessageBox.h"
|
2012-10-28 10:42:19 -04:00
|
|
|
|
2014-05-16 06:32:52 -04:00
|
|
|
QTEST_GUILESS_MAIN(TestAutoType)
|
|
|
|
|
2012-10-28 10:42:19 -04:00
|
|
|
void TestAutoType::initTestCase()
|
|
|
|
{
|
2014-06-15 05:17:40 -04:00
|
|
|
QVERIFY(Crypto::init());
|
2014-05-15 12:26:01 -04:00
|
|
|
Config::createTempFileInstance();
|
2012-10-28 10:42:19 -04:00
|
|
|
AutoType::createTestInstance();
|
2014-05-15 12:27:53 -04:00
|
|
|
config()->set("security/autotypeask", false);
|
2012-10-28 10:42:19 -04:00
|
|
|
|
|
|
|
QPluginLoader loader(filePath()->pluginPath("keepassx-autotype-test"));
|
|
|
|
loader.setLoadHints(QLibrary::ResolveAllSymbolsHint);
|
|
|
|
QVERIFY(loader.instance());
|
|
|
|
|
|
|
|
m_platform = qobject_cast<AutoTypePlatformInterface*>(loader.instance());
|
|
|
|
QVERIFY(m_platform);
|
|
|
|
|
|
|
|
m_test = qobject_cast<AutoTypeTestInterface*>(loader.instance());
|
|
|
|
QVERIFY(m_test);
|
2012-10-28 13:13:49 -04:00
|
|
|
|
|
|
|
m_autoType = AutoType::instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAutoType::init()
|
|
|
|
{
|
2015-10-10 11:23:57 -04:00
|
|
|
config()->set("AutoTypeEntryTitleMatch", false);
|
2012-10-28 13:13:49 -04:00
|
|
|
m_test->clearActions();
|
|
|
|
|
2012-11-02 10:07:44 -04:00
|
|
|
m_db = new Database();
|
2014-05-15 12:26:01 -04:00
|
|
|
m_dbList.clear();
|
|
|
|
m_dbList.append(m_db);
|
2012-10-28 13:13:49 -04:00
|
|
|
m_group = new Group();
|
2012-11-02 10:07:44 -04:00
|
|
|
m_db->setRootGroup(m_group);
|
2014-05-15 12:26:01 -04:00
|
|
|
|
2015-10-10 11:36:08 -04:00
|
|
|
AutoTypeAssociations::Association association;
|
|
|
|
|
2014-05-15 12:26:01 -04:00
|
|
|
m_entry1 = new Entry();
|
|
|
|
m_entry1->setGroup(m_group);
|
|
|
|
m_entry1->setUsername("myuser");
|
|
|
|
m_entry1->setPassword("mypass");
|
|
|
|
association.window = "custom window";
|
|
|
|
association.sequence = "{username}association{password}";
|
|
|
|
m_entry1->autoTypeAssociations()->add(association);
|
|
|
|
|
|
|
|
m_entry2 = new Entry();
|
|
|
|
m_entry2->setGroup(m_group);
|
|
|
|
m_entry2->setPassword("myuser");
|
|
|
|
m_entry2->setTitle("entry title");
|
2015-10-10 11:36:08 -04:00
|
|
|
|
|
|
|
m_entry3 = new Entry();
|
|
|
|
m_entry3->setGroup(m_group);
|
|
|
|
m_entry3->setPassword("regex");
|
|
|
|
association.window = "//REGEX1//";
|
|
|
|
association.sequence = "regex1";
|
|
|
|
m_entry3->autoTypeAssociations()->add(association);
|
|
|
|
association.window = "//^REGEX2$//";
|
|
|
|
association.sequence = "regex2";
|
|
|
|
m_entry3->autoTypeAssociations()->add(association);
|
|
|
|
association.window = "//^REGEX3-([rd]\\d){2}$//";
|
|
|
|
association.sequence = "regex3";
|
|
|
|
m_entry3->autoTypeAssociations()->add(association);
|
2016-11-25 12:26:59 -05:00
|
|
|
|
|
|
|
m_entry4 = new Entry();
|
|
|
|
m_entry4->setGroup(m_group);
|
|
|
|
m_entry4->setPassword("custom_attr");
|
|
|
|
m_entry4->attributes()->set("CUSTOM","Attribute",false);
|
|
|
|
association.window = "//^CustomAttr1$//";
|
2017-02-07 19:14:52 -05:00
|
|
|
association.sequence = "{PASSWORD}:{S:CUSTOM}";
|
2016-11-25 12:26:59 -05:00
|
|
|
m_entry4->autoTypeAssociations()->add(association);
|
|
|
|
association.window = "//^CustomAttr2$//";
|
2017-02-07 19:14:52 -05:00
|
|
|
association.sequence = "{S:CuStOm}";
|
2016-11-25 12:26:59 -05:00
|
|
|
m_entry4->autoTypeAssociations()->add(association);
|
|
|
|
association.window = "//^CustomAttr3$//";
|
|
|
|
association.sequence = "{PaSSworD}";
|
|
|
|
m_entry4->autoTypeAssociations()->add(association);
|
2017-05-13 19:02:54 -04:00
|
|
|
|
|
|
|
m_entry5 = new Entry();
|
|
|
|
m_entry5->setGroup(m_group);
|
|
|
|
m_entry5->setPassword("example5");
|
|
|
|
m_entry5->setTitle("some title");
|
|
|
|
m_entry5->setUrl("http://example.org");
|
2012-10-28 10:42:19 -04:00
|
|
|
}
|
|
|
|
|
2012-11-02 10:07:44 -04:00
|
|
|
void TestAutoType::cleanup()
|
|
|
|
{
|
|
|
|
delete m_db;
|
|
|
|
}
|
|
|
|
|
2012-10-28 10:42:19 -04:00
|
|
|
void TestAutoType::testInternal()
|
|
|
|
{
|
|
|
|
QVERIFY(m_platform->activeWindowTitle().isEmpty());
|
|
|
|
|
|
|
|
m_test->setActiveWindowTitle("Test");
|
|
|
|
QCOMPARE(m_platform->activeWindowTitle(), QString("Test"));
|
|
|
|
}
|
|
|
|
|
2012-10-28 13:13:49 -04:00
|
|
|
void TestAutoType::testAutoTypeWithoutSequence()
|
|
|
|
{
|
2015-07-24 12:28:12 -04:00
|
|
|
m_autoType->performAutoType(m_entry1, nullptr);
|
2012-10-28 13:13:49 -04:00
|
|
|
|
|
|
|
QCOMPARE(m_test->actionCount(), 14);
|
|
|
|
QCOMPARE(m_test->actionChars(),
|
|
|
|
QString("myuser%1mypass%2")
|
|
|
|
.arg(m_test->keyToString(Qt::Key_Tab))
|
|
|
|
.arg(m_test->keyToString(Qt::Key_Enter)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAutoType::testAutoTypeWithSequence()
|
|
|
|
{
|
2015-07-24 12:28:12 -04:00
|
|
|
m_autoType->performAutoType(m_entry1, nullptr, "{Username}abc{PaSsWoRd}");
|
2012-10-28 13:13:49 -04:00
|
|
|
|
|
|
|
QCOMPARE(m_test->actionCount(), 15);
|
|
|
|
QCOMPARE(m_test->actionChars(),
|
|
|
|
QString("%1abc%2")
|
2014-05-15 12:26:01 -04:00
|
|
|
.arg(m_entry1->username())
|
|
|
|
.arg(m_entry1->password()));
|
2012-10-28 13:13:49 -04:00
|
|
|
}
|
|
|
|
|
2012-11-02 10:07:44 -04:00
|
|
|
void TestAutoType::testGlobalAutoTypeWithNoMatch()
|
|
|
|
{
|
2014-05-15 12:26:01 -04:00
|
|
|
m_test->setActiveWindowTitle("nomatch");
|
2014-04-07 02:54:32 -04:00
|
|
|
MessageBox::setNextAnswer(QMessageBox::Ok);
|
2014-05-15 12:26:01 -04:00
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
2012-11-02 10:07:44 -04:00
|
|
|
|
|
|
|
QCOMPARE(m_test->actionChars(), QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAutoType::testGlobalAutoTypeWithOneMatch()
|
|
|
|
{
|
|
|
|
m_test->setActiveWindowTitle("custom window");
|
2014-05-15 12:26:01 -04:00
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
2012-11-02 10:07:44 -04:00
|
|
|
|
|
|
|
QCOMPARE(m_test->actionChars(),
|
|
|
|
QString("%1association%2")
|
2014-05-15 12:26:01 -04:00
|
|
|
.arg(m_entry1->username())
|
|
|
|
.arg(m_entry1->password()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAutoType::testGlobalAutoTypeTitleMatch()
|
|
|
|
{
|
|
|
|
config()->set("AutoTypeEntryTitleMatch", true);
|
|
|
|
|
|
|
|
m_test->setActiveWindowTitle("An Entry Title!");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
|
|
|
|
QCOMPARE(m_test->actionChars(),
|
|
|
|
QString("%1%2").arg(m_entry2->password(), m_test->keyToString(Qt::Key_Enter)));
|
|
|
|
}
|
|
|
|
|
2017-05-13 19:02:54 -04:00
|
|
|
void TestAutoType::testGlobalAutoTypeUrlMatch()
|
|
|
|
{
|
|
|
|
config()->set("AutoTypeEntryTitleMatch", true);
|
|
|
|
|
|
|
|
m_test->setActiveWindowTitle("Dummy - http://example.org/ - <My Browser>");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
|
|
|
|
QCOMPARE(m_test->actionChars(),
|
|
|
|
QString("%1%2").arg(m_entry5->password(), m_test->keyToString(Qt::Key_Enter)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAutoType::testGlobalAutoTypeUrlSubdomainMatch()
|
|
|
|
{
|
|
|
|
config()->set("AutoTypeEntryTitleMatch", true);
|
|
|
|
|
|
|
|
m_test->setActiveWindowTitle("Dummy - http://sub.example.org/ - <My Browser>");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
|
|
|
|
QCOMPARE(m_test->actionChars(),
|
|
|
|
QString("%1%2").arg(m_entry5->password(), m_test->keyToString(Qt::Key_Enter)));
|
|
|
|
}
|
|
|
|
|
2014-05-15 12:26:01 -04:00
|
|
|
void TestAutoType::testGlobalAutoTypeTitleMatchDisabled()
|
|
|
|
{
|
|
|
|
m_test->setActiveWindowTitle("An Entry Title!");
|
|
|
|
MessageBox::setNextAnswer(QMessageBox::Ok);
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
|
|
|
|
QCOMPARE(m_test->actionChars(), QString());
|
2015-10-10 11:36:08 -04:00
|
|
|
}
|
2014-05-15 12:26:01 -04:00
|
|
|
|
2015-10-10 11:36:08 -04:00
|
|
|
void TestAutoType::testGlobalAutoTypeRegExp()
|
|
|
|
{
|
|
|
|
// substring matches are ok
|
|
|
|
m_test->setActiveWindowTitle("lorem REGEX1 ipsum");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
QCOMPARE(m_test->actionChars(), QString("regex1"));
|
|
|
|
m_test->clearActions();
|
2014-05-15 12:26:01 -04:00
|
|
|
|
2015-10-10 11:36:08 -04:00
|
|
|
// should be case-insensitive
|
|
|
|
m_test->setActiveWindowTitle("lorem regex1 ipsum");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
QCOMPARE(m_test->actionChars(), QString("regex1"));
|
|
|
|
m_test->clearActions();
|
|
|
|
|
|
|
|
// exact match
|
|
|
|
m_test->setActiveWindowTitle("REGEX2");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
QCOMPARE(m_test->actionChars(), QString("regex2"));
|
|
|
|
m_test->clearActions();
|
|
|
|
|
|
|
|
// a bit more complicated regex
|
|
|
|
m_test->setActiveWindowTitle("REGEX3-R2D2");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
QCOMPARE(m_test->actionChars(), QString("regex3"));
|
|
|
|
m_test->clearActions();
|
2016-11-25 12:26:59 -05:00
|
|
|
|
|
|
|
// with custom attributes
|
|
|
|
m_test->setActiveWindowTitle("CustomAttr1");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
QCOMPARE(m_test->actionChars(), QString("custom_attr:Attribute"));
|
|
|
|
m_test->clearActions();
|
|
|
|
|
|
|
|
// with (non uppercase) undefined custom attributes
|
|
|
|
m_test->setActiveWindowTitle("CustomAttr2");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
QCOMPARE(m_test->actionChars(), QString(""));
|
|
|
|
m_test->clearActions();
|
|
|
|
|
|
|
|
// with mixedcase default attributes
|
|
|
|
m_test->setActiveWindowTitle("CustomAttr3");
|
|
|
|
m_autoType->performGlobalAutoType(m_dbList);
|
|
|
|
QCOMPARE(m_test->actionChars(), QString("custom_attr"));
|
|
|
|
m_test->clearActions();
|
2012-11-02 10:07:44 -04:00
|
|
|
}
|