From 820941fd402e3dfcedececdece0294220e31b300 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sat, 10 Oct 2015 17:36:08 +0200 Subject: [PATCH] Auto-Type: Only require a substring match for regex. This matches the behavior of KeePass. Refs #357 --- src/autotype/AutoType.cpp | 2 +- tests/TestAutoType.cpp | 44 +++++++++++++++++++++++++++++++++++++-- tests/TestAutoType.h | 2 ++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/autotype/AutoType.cpp b/src/autotype/AutoType.cpp index 19c8575f..f1b7e3ec 100644 --- a/src/autotype/AutoType.cpp +++ b/src/autotype/AutoType.cpp @@ -575,7 +575,7 @@ bool AutoType::windowMatches(const QString& windowTitle, const QString& windowPa { if (windowPattern.startsWith("//") && windowPattern.endsWith("//") && windowPattern.size() >= 4) { QRegExp regExp(windowPattern.mid(2, windowPattern.size() - 4), Qt::CaseInsensitive, QRegExp::RegExp2); - return regExp.exactMatch(windowTitle); + return (regExp.indexIn(windowTitle) != -1); } else { return WildcardMatcher(windowTitle).match(windowPattern); diff --git a/tests/TestAutoType.cpp b/tests/TestAutoType.cpp index b041e8e9..89683480 100644 --- a/tests/TestAutoType.cpp +++ b/tests/TestAutoType.cpp @@ -64,11 +64,12 @@ void TestAutoType::init() m_group = new Group(); m_db->setRootGroup(m_group); + AutoTypeAssociations::Association association; + m_entry1 = new Entry(); m_entry1->setGroup(m_group); m_entry1->setUsername("myuser"); m_entry1->setPassword("mypass"); - AutoTypeAssociations::Association association; association.window = "custom window"; association.sequence = "{username}association{password}"; m_entry1->autoTypeAssociations()->add(association); @@ -77,6 +78,19 @@ void TestAutoType::init() m_entry2->setGroup(m_group); m_entry2->setPassword("myuser"); m_entry2->setTitle("entry title"); + + 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); } void TestAutoType::cleanup() @@ -152,5 +166,31 @@ void TestAutoType::testGlobalAutoTypeTitleMatchDisabled() m_autoType->performGlobalAutoType(m_dbList); QCOMPARE(m_test->actionChars(), QString()); - +} + +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(); + + // 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(); } diff --git a/tests/TestAutoType.h b/tests/TestAutoType.h index d46a5596..c12817b1 100644 --- a/tests/TestAutoType.h +++ b/tests/TestAutoType.h @@ -43,6 +43,7 @@ private Q_SLOTS: void testGlobalAutoTypeWithOneMatch(); void testGlobalAutoTypeTitleMatch(); void testGlobalAutoTypeTitleMatchDisabled(); + void testGlobalAutoTypeRegExp(); private: AutoTypePlatformInterface* m_platform; @@ -53,6 +54,7 @@ private: Group* m_group; Entry* m_entry1; Entry* m_entry2; + Entry* m_entry3; }; #endif // KEEPASSX_TESTAUTOTYPE_H