add test for syntax checking

This commit is contained in:
thez3ro 2017-11-07 17:58:08 +01:00
parent 8ca444aee0
commit a02a49a184
No known key found for this signature in database
GPG Key ID: F628F9E41DD7C073
6 changed files with 32 additions and 28 deletions

View File

@ -130,7 +130,7 @@ QStringList AutoType::windowTitles()
return m_plugin->windowTitles();
}
void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow, const QString& customSequence, WId window)
void AutoType::_performAutoType(const Entry* entry, QWidget* hideWindow, const QString& customSequence, WId window)
{
if (m_inAutoType || !m_plugin) {
return;
@ -253,7 +253,7 @@ void AutoType::performAutoTypeFromGlobal(Entry* entry, const QString& sequence)
m_inAutoType = false;
performAutoTypeWithSyntaxCheckingDialog(entry, nullptr, sequence, m_windowFromGlobal);
performAutoType(entry, nullptr, sequence, m_windowFromGlobal);
}
void AutoType::resetInAutoType()
@ -556,14 +556,6 @@ QList<AutoTypeAction*> AutoType::createActionFromTemplate(const QString& tmpl, c
}
}
//allows to insert usernames and passwords multiple times
if (!list.isEmpty()) {
for (int i = 1; i < num; i++) {
for (int j = 0; j < resolved.size(); j++) {
list.append(list.at(j)->clone());
}
}
}
return list;
}
@ -679,13 +671,13 @@ bool AutoType::windowMatchesUrl(const QString& windowTitle, const QString& resol
bool AutoType::checkSyntax(const QString &string)
{
//checks things like {word 23}{F1 23}{~ 23}{% 23}{^}{F12}{(}{) 23}{[}{[}{]}{Delay=23}{+}{-}~+%@fixedstring
QString allowRepetition = "(\\s[0-9]*){0,1}";
QString allowRepetition = "(\\s\\d*){0,1}";
QString normalCommands = "[A-Z:]*" + allowRepetition; //the ":" allows custom commands
QString specialLiterals = "[\\^\\%\\(\\)~\\{\\}\\[\\]\\+-]" + allowRepetition;
QString functionKeys = "(F[1-9]" + allowRepetition + "|F1[0-2])" + allowRepetition;
QString numpad = "NUMPAD[0-9]" + allowRepetition;
QString delay = "DELAY=[0-9]+";
QString beep = "BEEP\\s[0-9]*\\s[0-9]*";
QString numpad = "NUMPAD\\d" + allowRepetition;
QString delay = "DELAY=\\d+";
QString beep = "BEEP\\s\\d*\\s\\d*";
QString vkey = "VKEY(-[EN]X){0,1}" + allowRepetition;
//these arent in parenthesis
@ -703,7 +695,7 @@ bool AutoType::checkSyntax(const QString &string)
bool AutoType::checkHighDelay(const QString &string)
{
QRegExp highDelay(".*\\{Delay\\s[0-9]{5,}\\}.*"); //5 digit numbers(10 seconds) are too much
QRegExp highDelay("\\{DELAY\\s\\d{5,}\\}"); //5 digit numbers(10 seconds) are too much
highDelay.setCaseSensitivity(Qt::CaseInsensitive);
highDelay.setPatternSyntax(QRegExp::RegExp);
return highDelay.exactMatch(string);
@ -711,16 +703,14 @@ bool AutoType::checkHighDelay(const QString &string)
bool AutoType::checkHighRepetition(const QString &string)
{
QRegExp highRepetition(".*\\s[0-9]{3,}.*");//3 digit numbers are too much
QRegExp highRepetition("\\{(?!DELAY\\s)\\w*\\s\\d{3,}\\}"); //3 digit numbers are too much
highRepetition.setCaseSensitivity(Qt::CaseInsensitive);
highRepetition.setPatternSyntax(QRegExp::RegExp);
return highRepetition.exactMatch(string);
}
void
AutoType::performAutoTypeWithSyntaxCheckingDialog(const Entry *entry,
QWidget *hideWindow,
const QString &customSequence,
WId window)
AutoType::performAutoType(const Entry *entry, QWidget *hideWindow, const QString &customSequence, WId window)
{
if (!AutoType::checkSyntax(entry->effectiveAutoTypeSequence())) {
QMessageBox messageBox;
@ -747,6 +737,6 @@ AutoType::performAutoTypeWithSyntaxCheckingDialog(const Entry *entry,
return;
}
}
performAutoType(entry, hideWindow, customSequence, window);
_performAutoType(entry, hideWindow, customSequence, window);
}

View File

@ -37,7 +37,7 @@ class AutoType : public QObject
public:
QStringList windowTitles();
void performAutoType(const Entry* entry, QWidget* hideWindow = nullptr,
void _performAutoType(const Entry* entry, QWidget* hideWindow = nullptr,
const QString& customSequence = QString(), WId window = 0);
bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers);
void unregisterGlobalShortcut();
@ -45,10 +45,8 @@ public:
static bool checkSyntax(const QString &string);
static bool checkHighRepetition(const QString &string);
static bool checkHighDelay(const QString &string);
void performAutoTypeWithSyntaxCheckingDialog(const Entry *entry,
QWidget *hideWindow = nullptr,
const QString &customSequence = QString(),
WId window = 0);
void performAutoType(const Entry *entry, QWidget *hideWindow = nullptr,
const QString &customSequence = QString(), WId window = 0);
inline bool isAvailable() {
return m_plugin;

View File

@ -584,7 +584,7 @@ void DatabaseWidget::performAutoType()
return;
}
autoType()->performAutoTypeWithSyntaxCheckingDialog(currentEntry, window());
autoType()->performAutoType(currentEntry, window());
}
void DatabaseWidget::openUrl()

View File

@ -32,8 +32,8 @@
#include <QMimeData>
#include <QEvent>
#include <iostream>
#include <autotype/AutoType.h>
#include "autotype/AutoType.h"
#include "core/Config.h"
#include "core/Database.h"
#include "core/Entry.h"

View File

@ -282,3 +282,18 @@ void TestAutoType::testGlobalAutoTypeRegExp()
QCOMPARE(m_test->actionChars(), QString("custom_attr_third"));
m_test->clearActions();
}
void TestAutoType::testAutoTypeSyntaxChecks()
{
// Huge sequence
QCOMPARE(true, AutoType::checkSyntax("{word 23}{F1 23}{~ 23}{% 23}{^}{F12}{(}{) 23}{[}{[}{]}{Delay=23}{+}{-}~+%@fixedstring"));
// Bad sequence
QCOMPARE(false, AutoType::checkSyntax("{{{}}{}{}}{{}}"));
// High DelAY / low delay
QCOMPARE(true, AutoType::checkHighDelay("{DelAY 50000}"));
QCOMPARE(false, AutoType::checkHighDelay("{delay 50}"));
// Many repetition / few repetition / delay not repetition
QCOMPARE(true, AutoType::checkHighRepetition("{LEFT 50000000}"));
QCOMPARE(false, AutoType::checkHighRepetition("{SPACE 10}{TAB 3}{RIGHT 50}"));
QCOMPARE(false, AutoType::checkHighRepetition("{delay 5000000000}"));
}

View File

@ -47,6 +47,7 @@ private slots:
void testGlobalAutoTypeUrlSubdomainMatch();
void testGlobalAutoTypeTitleMatchDisabled();
void testGlobalAutoTypeRegExp();
void testAutoTypeSyntaxChecks();
private:
AutoTypePlatformInterface* m_platform;