Do not fix up input during validation

This change is a workaround for behaviour of QML controls: when fixup is
called during input validation, the `acceptableInput` property is not
updated correctly.
master
Johan Ouwerkerk 2020-03-25 20:05:58 +01:00
parent 82a4fcce5f
commit 28454721df
11 changed files with 134 additions and 283 deletions

View File

@ -22,45 +22,38 @@
using namespace validators::test;
static void define_padding_test_cases(const QString &prefix, const QString &fixed, QValidator::State result)
static void define_padding_test_cases(const QString &prefix, const QString &fixed, QValidator::State result, bool validEvery8th = false)
{
QString input = prefix;
int prefixSize = prefix.size();
for (int p = 0; p < 10; ++p) {
input += QLatin1Char('=');
int cursor = prefix.size();
if (result == QValidator::Acceptable) {
cursor = input.size();
// expect the excess padding to be trimmed and the cursor to be
// moved accordingly
int cap = (prefixSize % 8) == 0
? prefixSize
: (8 * (1 + prefixSize / 8));
if (cursor > cap) {
cursor = cap;
}
if (validEvery8th && (input.size() % 8) == 0 && input.size() > 0) {
continue;
}
define_test_case(input, fixed, cursor, result);
define_test_case(input, fixed, result);
}
}
static void define_padding_table(void)
{
define_padding_test_cases(QLatin1String(""), QLatin1String(""), QValidator::Invalid);
define_padding_test_cases(QLatin1String("V"), QLatin1String("V"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VV"), QLatin1String("VV======"), QValidator::Acceptable);
define_padding_test_cases(QLatin1String("VV"), QLatin1String("VV"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VA"), QLatin1String("VA======"), QValidator::Intermediate, true);
define_padding_test_cases(QLatin1String("VVV"), QLatin1String("VVV"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VVVV"), QLatin1String("VVVV===="), QValidator::Acceptable);
define_padding_test_cases(QLatin1String("VVVVV"), QLatin1String("VVVVV==="), QValidator::Acceptable);
define_padding_test_cases(QLatin1String("VVVV"), QLatin1String("VVVV"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VVVA"), QLatin1String("VVVA===="), QValidator::Intermediate, true);
define_padding_test_cases(QLatin1String("VVVVV"), QLatin1String("VVVVV"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VVVVA"), QLatin1String("VVVVA==="), QValidator::Intermediate, true);
define_padding_test_cases(QLatin1String("VVVVVV"), QLatin1String("VVVVVV"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VVVVVVV"), QLatin1String("VVVVVVV="), QValidator::Acceptable);
define_padding_test_cases(QLatin1String("VVVVVVVV"), QLatin1String("VVVVVVVV"), QValidator::Acceptable);
define_padding_test_cases(QLatin1String("VVVVVVV"), QLatin1String("VVVVVVV"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VVVVVVA"), QLatin1String("VVVVVVA="), QValidator::Intermediate, true);
define_test_case(QLatin1String("VVVVVVVV"), QLatin1String("VVVVVVVV"), QValidator::Acceptable);
define_padding_test_cases(QLatin1String("VVVVVVVV="), QLatin1String("VVVVVVVV"), QValidator::Intermediate);
// check things still work when upgrading to secrets > 8 characters long ;)
define_padding_test_cases(QLatin1String("VVVVVVVVV"), QLatin1String("VVVVVVVVV"), QValidator::Intermediate);
define_padding_test_cases(QLatin1String("VVVVVVVVVV"), QLatin1String("VVVVVVVVVV======"), QValidator::Acceptable);
define_padding_test_cases(QLatin1String("VVVVVVVVVA"), QLatin1String("VVVVVVVVVA======"), QValidator::Intermediate, true);
}
static void define_conversion_table(void)
@ -69,31 +62,31 @@ static void define_conversion_table(void)
* check that case conversion is applied for better UX
* check that leading and trailing whitespace is stripped for better UX
*/
define_test_case(QLatin1String(" v"), QLatin1String("V"), 1, QValidator::Intermediate);
define_test_case(QLatin1String("vv "), QLatin1String("VV======"), 2, QValidator::Acceptable);
define_test_case(QLatin1String("\tkeybytes\n "), QLatin1String("KEYBYTES"), 8, QValidator::Acceptable);
define_test_case(QLatin1String("key \t\r\nbytes"), QLatin1String("KEYBYTES"), 8, QValidator::Acceptable);
define_test_case(QLatin1String("\t\n\r valid===\r\t \n"), QLatin1String("VALID==="), 8, QValidator::Acceptable);
define_test_case(QLatin1String("\t\n\r valid \t\r\n===\r\t \n"), QLatin1String("VALID==="), 8, QValidator::Acceptable);
define_test_case(QLatin1String(" v"), QLatin1String("V"), QValidator::Invalid);
define_test_case(QLatin1String("va "), QLatin1String("VA======"), QValidator::Invalid);
define_test_case(QLatin1String("\tkeybytes\n "), QLatin1String("KEYBYTES"), QValidator::Invalid);
define_test_case(QLatin1String("key \t\r\nbytes"), QLatin1String("KEYBYTES"), QValidator::Invalid);
define_test_case(QLatin1String("\t\n\r value===\r\t \n"), QLatin1String("VALUE==="), QValidator::Invalid);
define_test_case(QLatin1String("\t\n\r value \t\r\n===\r\t \n"), QLatin1String("VALUE==="), QValidator::Invalid);
}
static void define_valid_table(void)
{
// check that some random valid keys are accepted
define_test_case(QLatin1String("KEYBYTES"), QLatin1String("KEYBYTES"), 8, QValidator::Acceptable);
define_test_case(QLatin1String("VALID==="), QLatin1String("VALID==="), 8, QValidator::Acceptable);
define_test_case(QLatin1String("KEYBYTES=="), QLatin1String("KEYBYTES"), 8, QValidator::Acceptable);
define_test_case(QLatin1String("KEYBYTES"), QLatin1String("KEYBYTES"), QValidator::Acceptable);
define_test_case(QLatin1String("VALUE==="), QLatin1String("VALUE==="), QValidator::Acceptable);
define_test_case(QLatin1String("KEYBYTES=="), QLatin1String("KEYBYTES"), QValidator::Intermediate);
}
static void define_empty_table()
{
define_test_case(QLatin1String(""), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String(" "), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String("\t"), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String("\r\n"), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String(""), QLatin1String(""), QValidator::Intermediate);
define_test_case(QLatin1String(" "), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String("\t"), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String("\r\n"), QLatin1String(""), QValidator::Invalid);
}
static void define_validate_data(void)
static void define_data(void)
{
define_test_data_columns();
define_empty_table();
@ -101,41 +94,18 @@ static void define_validate_data(void)
define_conversion_table();
define_valid_table();
// these cases should be 'caught' and prohibited by the regex pattern
// expected that fixup() logic is not applied during validation
define_test_case(QLatin1String("="), QLatin1String("="), 1, QValidator::Invalid);
define_test_case(QLatin1String("=="), QLatin1String("=="), 2, QValidator::Invalid);
define_test_case(QLatin1String("\r\n="), QLatin1String("="), 1, QValidator::Invalid);
define_test_case(QLatin1String(" \t== \n"), QLatin1String("=="), 2, QValidator::Invalid);
define_test_case(QLatin1String("INVALID1"), QLatin1String("INVALID1"), 8, QValidator::Invalid);
define_test_case(QLatin1String("=INVALID"), QLatin1String("=INVALID"), 8, QValidator::Invalid);
define_test_case(QLatin1String("INV= LID"), QLatin1String("INV=LID"), 7, QValidator::Invalid);
define_test_case(QLatin1String("INVA=LID"), QLatin1String("INVA=LID"), 8, QValidator::Invalid);
define_test_case(QLatin1String("WILLNOTFIXTHIS1"), QLatin1String("WILLNOTFIXTHIS1"), 15, QValidator::Invalid);
// expected that fixup() logic is not applied
define_test_case(QLatin1String("\r\n="), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String(" \t== \n"), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String("INVALID1"), QLatin1String("INVALID1"), QValidator::Invalid);
define_test_case(QLatin1String("=INVALID"), QLatin1String("=INVALID"), QValidator::Invalid);
define_test_case(QLatin1String("INV= LID"), QLatin1String("INV=LID"), QValidator::Invalid);
define_test_case(QLatin1String("INVA=LID"), QLatin1String("INVA=LID"), QValidator::Invalid);
define_test_case(QLatin1String("WILLNOTFIXTHIS1"), QLatin1String("WILLNOTFIXTHIS1"), QValidator::Invalid);
}
static void define_fixup_data(void)
{
define_test_data_columns();
define_empty_table();
define_padding_table();
define_conversion_table();
define_valid_table();
// check various no data, just padding cases
define_test_case(QLatin1String("="), QLatin1String(""));
define_test_case(QLatin1String("=="), QLatin1String(""));
define_test_case(QLatin1String("==="), QLatin1String(""));
define_test_case(QLatin1String("===="), QLatin1String(""));
define_test_case(QLatin1String("====="), QLatin1String(""));
define_test_case(QLatin1String("======"), QLatin1String(""));
define_test_case(QLatin1String("======="), QLatin1String(""));
define_test_case(QLatin1String("========"), QLatin1String(""));
define_test_case(QLatin1String("========="), QLatin1String(""));
}
DEFINE_VALIDATOR_TEST(Base32ValidatorTest, validators::Base32Validator, define_fixup_data, define_validate_data);
DEFINE_VALIDATOR_TEST(Base32ValidatorTest, validators::Base32Validator, define_data);
QTEST_APPLESS_MAIN(Base32ValidatorTest)

View File

@ -24,17 +24,17 @@ using namespace validators::test;
static void define_valid_table(void)
{
define_test_case(QLatin1String("Outis"), QLatin1String("Outis"), 5, QValidator::Acceptable);
define_test_case(QLatin1String("test\taccount"), QLatin1String("test account"), 12, QValidator::Acceptable);
define_test_case(QLatin1String("\r \n\ttest\r\t \naccount \r\t\n"), QLatin1String("test account"), 12, QValidator::Acceptable);
define_test_case(QLatin1String("Outis"), QLatin1String("Outis"), QValidator::Acceptable);
define_test_case(QLatin1String("test\taccount"), QLatin1String("test account"), QValidator::Invalid);
define_test_case(QLatin1String("\r \n\ttest\r\t \naccount \r\t\n"), QLatin1String("test account"), QValidator::Invalid);
}
static void define_empty_table(void)
{
define_test_case(QLatin1String(""), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String(" "), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String("\t"), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String("\r\n"), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String(""), QLatin1String(""), QValidator::Intermediate);
define_test_case(QLatin1String(" "), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String("\t"), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String("\r\n"), QLatin1String(""), QValidator::Invalid);
}
/*
@ -51,10 +51,10 @@ static void define_data(void)
define_empty_table();
define_valid_table();
define_test_case(QLatin1String("test "), QLatin1String("test "), 5, QValidator::Intermediate);
define_test_case(QLatin1String("test "), QLatin1String("test "), QValidator::Invalid);
}
DEFINE_VALIDATOR_TEST(NameValidatorTest, validators::NameValidator, define_data, define_data);
DEFINE_VALIDATOR_TEST(NameValidatorTest, validators::NameValidator, define_data);
QTEST_APPLESS_MAIN(NameValidatorTest)

View File

@ -36,24 +36,21 @@ namespace validators
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("fixed");
QTest::addColumn<int>("cursor");
QTest::addColumn<QLocale>("locale");
QTest::addColumn<QValidator::State>("result");
};
void define_test_case(
const QString &input,
const QString &fixed,
int cursor=0,
QValidator::State result=QValidator::Intermediate,
const QLocale &locale=QLocale::c())
{
QString name = QStringLiteral("locale=%1, input=%2").arg(locale.name()).arg(input);
QTest::newRow(qPrintable(name)) << input << fixed << cursor << locale << result;
QTest::newRow(qPrintable(name)) << input << fixed << locale << result;
};
template<class T, auto f_fixup_data, auto f_validate_data>
template<class T, auto f_data>
class ValidatorTestBase : public QObject
{
public:
@ -63,28 +60,25 @@ namespace validators
void testValidate(void)
{
T uut;
QFETCH(QLocale, locale);
uut.setLocale(locale);
QFETCH(QString, input);
QFETCH(QString, fixed);
QFETCH(int, cursor);
QFETCH(QLocale, locale);
T uut;
uut.setLocale(locale);
int position = input.size();
int copy = position;
QTEST(uut.validate(input, position), "result");
QCOMPARE(input, fixed);
QCOMPARE(position, cursor);
QCOMPARE(position, copy);
};
void testFixup(void)
{
T uut;
QFETCH(QLocale, locale);
uut.setLocale(locale);
QFETCH(QString, input);
QFETCH(QLocale, locale);
T uut;
uut.setLocale(locale);
uut.fixup(input);
QTEST(input, "fixed");
@ -98,20 +92,20 @@ namespace validators
void testValidate_data(void)
{
define_test_data_columns();
f_validate_data();
f_data();
};
void testFixup_data(void)
{
define_test_data_columns();
f_fixup_data();
f_data();
};
};
}
}
#define DEFINE_VALIDATOR_TEST(name, type, fixup_data, validate_data) \
class name : public validators::test::ValidatorTestBase<type, fixup_data, validate_data> \
#define DEFINE_VALIDATOR_TEST(name, type, data_tables) \
class name : public validators::test::ValidatorTestBase<type, data_tables> \
{ \
Q_OBJECT \
private Q_SLOTS: \

View File

@ -32,39 +32,41 @@ static QLocale enUK(QLocale::English, QLocale::UnitedKingdom);
* Sometimes you get 123\u00A0456, sometimes you get 123\202f\456...
*/
static const QString french = frFR.toString(123456ULL);
static const QString french2 = frFR.toString(12345ULL);
static void define_valid_table(void)
{
define_test_case(QLatin1String("42"), QLatin1String("42"), 2, QValidator::Acceptable);
define_test_case(QLatin1String("\r \n\t42\r\t \n"), QLatin1String("42"), 2, QValidator::Acceptable);
define_test_case(QLatin1String("42"), QLatin1String("42"), QValidator::Acceptable);
define_test_case(QLatin1String("\r \n\t42\r\t \n"), QLatin1String("42"), QValidator::Acceptable);
define_test_case(QLatin1String("33 "), QLatin1String("33"), 2, QValidator::Acceptable);
define_test_case(QLatin1String("2 2"), QLatin1String("22"), 2, QValidator::Acceptable);
define_test_case(QLatin1String("2\t\r\n2"), QLatin1String("22"), 2, QValidator::Acceptable);
define_test_case(QLatin1String("33 "), QLatin1String("33"), QValidator::Acceptable);
define_test_case(QLatin1String("2 2"), QLatin1String("22"), QValidator::Invalid);
define_test_case(QLatin1String("2\t\r\n2"), QLatin1String("22"), QValidator::Invalid);
define_test_case(QLatin1String("123,456"), QLatin1String("123456"), 6, QValidator::Acceptable, QLocale::c());
define_test_case(QLatin1String("123,456"), QLatin1String("123456"), QValidator::Acceptable, QLocale::c());
define_test_case(QLatin1String("123456"), QLatin1String("123,456"), 7, QValidator::Acceptable, enUK);
define_test_case(QLatin1String("123,456"), QLatin1String("123,456"), 7, QValidator::Acceptable, enUK);
define_test_case(QLatin1String("123 456"), QLatin1String("123,456"), 7, QValidator::Acceptable, enUK);
define_test_case(QLatin1String("123456"), QLatin1String("123,456"), QValidator::Acceptable, enUK);
define_test_case(QLatin1String("123,456"), QLatin1String("123,456"), QValidator::Acceptable, enUK);
define_test_case(QLatin1String("123 456"), QLatin1String("123,456"), QValidator::Invalid, enUK);
define_test_case(QLatin1String("123456"), QLatin1String("123.456"), 7, QValidator::Acceptable, nlNL);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), 7, QValidator::Acceptable, nlNL);
define_test_case(QLatin1String("123 456"), QLatin1String("123.456"), 7, QValidator::Acceptable, nlNL);
define_test_case(QLatin1String("123,456"), QLatin1String("123.456"), 7, QValidator::Acceptable, nlNL);
define_test_case(QLatin1String("123456"), QLatin1String("123.456"), QValidator::Acceptable, nlNL);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), QValidator::Acceptable, nlNL);
define_test_case(QLatin1String("123 456"), QLatin1String("123.456"), QValidator::Invalid, nlNL);
define_test_case(QLatin1String("123,456"), QLatin1String("123.456"), QValidator::Acceptable, nlNL);
define_test_case(french, french, 7, QValidator::Acceptable, frFR);
define_test_case(QLatin1String("123456"), french, 7, QValidator::Acceptable, frFR);
define_test_case(QLatin1String("123,456"), french, 7, QValidator::Acceptable, frFR);
define_test_case(QLatin1String("123 456"), french, 7, QValidator::Acceptable, frFR);
define_test_case(french, french, QValidator::Acceptable, frFR);
define_test_case(QLatin1String("123456"), french, QValidator::Acceptable, frFR);
define_test_case(QLatin1String("123,456"), french, QValidator::Acceptable, frFR);
define_test_case(QLatin1String("123 456"), french, QValidator::Acceptable, frFR);
define_test_case(QLatin1String("123 45"), french2, QValidator::Invalid, frFR);
}
static void define_empty_table(void)
{
define_test_case(QLatin1String(""), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String(" "), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String("\t"), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String("\r\n"), QLatin1String(""), 0, QValidator::Intermediate);
define_test_case(QLatin1String(""), QLatin1String(""), QValidator::Intermediate);
define_test_case(QLatin1String(" "), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String("\t"), QLatin1String(""), QValidator::Invalid);
define_test_case(QLatin1String("\r\n"), QLatin1String(""), QValidator::Invalid);
}
/*
@ -80,17 +82,17 @@ static void define_data(void)
define_empty_table();
define_valid_table();
define_test_case(QLatin1String("-33"), QLatin1String("-33"), 3, QValidator::Invalid);
define_test_case(QLatin1String("ZZ9 Plural Z Alpha"), QLatin1String("ZZ9PluralZAlpha"), 15, QValidator::Invalid);
define_test_case(QLatin1String("-33"), QLatin1String("-33"), QValidator::Invalid);
define_test_case(QLatin1String("ZZ9 Plural Z Alpha"), QLatin1String("ZZ9PluralZAlpha"), QValidator::Invalid);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), 7, QValidator::Invalid, frFR);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), 7, QValidator::Invalid, enUK);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), 7, QValidator::Invalid, QLocale::c());
define_test_case(french, french, 7, QValidator::Invalid, enUK);
define_test_case(french, french, 7, QValidator::Invalid, nlNL);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), QValidator::Invalid, frFR);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), QValidator::Invalid, enUK);
define_test_case(QLatin1String("123.456"), QLatin1String("123.456"), QValidator::Invalid, QLocale::c());
define_test_case(french, french, QValidator::Invalid, enUK);
define_test_case(french, french, QValidator::Invalid, nlNL);
}
DEFINE_VALIDATOR_TEST(UnsignedLongValidatorTest, validators::UnsignedLongValidator, define_data, define_data);
DEFINE_VALIDATOR_TEST(UnsignedLongValidatorTest, validators::UnsignedLongValidator, define_data);
QTEST_APPLESS_MAIN(UnsignedLongValidatorTest)

View File

@ -1,4 +1,7 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2019-2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
#
# This directory contains (custom) validators to support form field validation in the QML UI
# For the QML bindings see: qmlsupport.cpp
#
@ -12,4 +15,4 @@ set(validator_SRCS
)
add_library(validator_lib STATIC ${validator_SRCS})
target_link_libraries(validator_lib Qt5::Core Qt5::Qml Qt5::Gui)
target_link_libraries(validator_lib Qt5::Core Qt5::Qml Qt5::Gui base32_lib)

View File

@ -1,20 +1,7 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project 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 3 of the License, or *
* (at your option) any later version. *
* *
* This project 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/>. *
* *
****************************************************************************/
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2019-2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
*/
#include "countervalidator.h"
@ -77,28 +64,9 @@ namespace validators
QValidator::State UnsignedLongValidator::validate(QString &input, int &cursor) const
{
int s = input.size();
fixup(input);
/*
* Size might have changed:
*
* - decreased through the removal of whitespace
* - increased through the addition of digit group separators
*
* Adjust the cursor as necessary, in particular make sure to
* advance the cursor if the size has increased and the cursor was
* at the end of the original input string. In this way the user
* can simply continue typing input digits while enjoying
* 'auto formatting'.
*/
int size = input.size();
if (size != s && (cursor == s || cursor > size)) {
cursor = size;
}
Q_UNUSED(cursor);
// Avoid a hard error on empty string.
if (size == 0) {
if (input.size() == 0) {
return QValidator::Intermediate;
} else {

View File

@ -1,20 +1,7 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project 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 3 of the License, or *
* (at your option) any later version. *
* *
* This project 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/>. *
* *
****************************************************************************/
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
*/
#ifndef COUNTER_VALIDATOR_H
#define COUNTER_VALIDATOR_H

View File

@ -1,20 +1,7 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project 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 3 of the License, or *
* (at your option) any later version. *
* *
* This project 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/>. *
* *
****************************************************************************/
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2019-2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
*/
#include "namevalidator.h"
@ -50,14 +37,6 @@ namespace validators
QValidator::State NameValidator::validate(QString &input, int &cursor) const
{
fixup(input);
// spaces may have been removed, adjust cursor
int size = input.size();
if (cursor > size) {
cursor = size;
}
return m_pattern.validate(input, cursor);
}
}

View File

@ -1,20 +1,7 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project 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 3 of the License, or *
* (at your option) any later version. *
* *
* This project 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/>. *
* *
****************************************************************************/
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
*/
#ifndef NAME_VALIDATOR_H
#define NAME_VALIDATOR_H

View File

@ -1,24 +1,12 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project 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 3 of the License, or *
* (at your option) any later version. *
* *
* This project 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/>. *
* *
****************************************************************************/
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2019-2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
*/
#include "secretvalidator.h"
#include "util.h"
#include "../base32/base32.h"
#include <QRegularExpression>
#include <QString>
@ -69,8 +57,13 @@ namespace validators
QValidator::State result = check_padding(length);
if (result == QValidator::Acceptable) {
while ((fixed.size() % 8) != 0) {
fixed += QLatin1Char('=');
QString maybeFixed = fixed;
while ((maybeFixed.size() % 8) != 0) {
maybeFixed += QLatin1Char('=');
}
if (base32::validate(maybeFixed)) {
fixed = maybeFixed;
}
}
@ -79,29 +72,10 @@ namespace validators
QValidator::State Base32Validator::validate(QString &input, int &cursor) const
{
input = strip_spaces(input);
// spaces may have been removed, adjust cursor
int size = input.size();
if (cursor > size) {
cursor = size;
}
// if the basics are covered, check the padding & alignment
QValidator::State s = m_pattern.validate(input, cursor);
if (s == QValidator::Acceptable) {
fixup(input);
// fixup might have trimmed the string, adjust cursor
size = input.size();
if (cursor > size) {
cursor = size;
}
// check if despite auto-fixup the string is still misaligned
return (size % 8) == 0
? QValidator::Acceptable
: QValidator::Intermediate;
// if the basics are covered, check the padding & alignment
return base32::validate(input) ? QValidator::Acceptable : QValidator::Intermediate;
} else {
return s;
}

View File

@ -1,20 +1,7 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project 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 3 of the License, or *
* (at your option) any later version. *
* *
* This project 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/>. *
* *
****************************************************************************/
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
*/
#ifndef SECRET_VALIDATOR_H
#define SECRET_VALIDATOR_H