mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-12-26 15:59:50 -05:00
b2c2f42f30
See issue #3271 for a motivation of this feature. This patch adds an additional text input to the advanced view of the password generator. All characters of this input field (if not empty) will be added as another group to the password generator. The characters from the excluded field have precedence over the characters from this new field, meaning any character added to both fields will *not* appear in any generated password. As the characters from this new field will be added as their own group to the password generator, checking the 'Include characters from every group' checkbox will force at least character to be chosen from the new input field. The `PasswordGenerator` class has also been changed so that the `isValid` method returns `true` if only characters from the new input field would be used. There is a new, simple test that covers the new feature. While the test only uses ASCII characters, any Unicode characters can be used with the new feature.
144 lines
5.5 KiB
C++
144 lines
5.5 KiB
C++
/*
|
|
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
|
|
*
|
|
* 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 "TestPasswordGenerator.h"
|
|
#include "core/PasswordGenerator.h"
|
|
#include "crypto/Crypto.h"
|
|
|
|
#include <QRegularExpression>
|
|
#include <QTest>
|
|
|
|
QTEST_GUILESS_MAIN(TestPasswordGenerator)
|
|
|
|
void TestPasswordGenerator::initTestCase()
|
|
{
|
|
QVERIFY(Crypto::init());
|
|
}
|
|
|
|
void TestPasswordGenerator::testAdditionalChars()
|
|
{
|
|
PasswordGenerator generator;
|
|
QVERIFY(!generator.isValid());
|
|
generator.setAdditionalChars("aql");
|
|
generator.setLength(2000);
|
|
QVERIFY(generator.isValid());
|
|
QString password = generator.generatePassword();
|
|
QCOMPARE(password.size(), 2000);
|
|
QRegularExpression regex(R"(^[aql]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
}
|
|
|
|
void TestPasswordGenerator::testCharClasses()
|
|
{
|
|
PasswordGenerator generator;
|
|
QVERIFY(!generator.isValid());
|
|
generator.setCharClasses(PasswordGenerator::CharClass::LowerLetters);
|
|
generator.setLength(16);
|
|
QVERIFY(generator.isValid());
|
|
QCOMPARE(generator.generatePassword().size(), 16);
|
|
|
|
generator.setLength(2000);
|
|
QString password = generator.generatePassword();
|
|
QCOMPARE(password.size(), 2000);
|
|
QRegularExpression regex(R"(^[a-z]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::UpperLetters);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[A-Z]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Numbers);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^\d+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Braces);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[\(\)\[\]\{\}]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Punctuation);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[\.,:;]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Quotes);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^["']+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Dashes);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[\-/\\_|]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Math);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[!\*\+\-<=>\?]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Logograms);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[#`~%&^$@]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::EASCII);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[^a-zA-Z0-9\.,:;"'\-/\\_|!\*\+\-<=>\?#`~%&^$@]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::LowerLetters | PasswordGenerator::CharClass::UpperLetters
|
|
| PasswordGenerator::CharClass::Braces);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^[a-zA-Z\(\)\[\]\{\}]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::Quotes | PasswordGenerator::CharClass::Numbers
|
|
| PasswordGenerator::CharClass::Dashes);
|
|
password = generator.generatePassword();
|
|
regex.setPattern(R"(^["'\d\-/\\_|]+$)");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
}
|
|
|
|
void TestPasswordGenerator::testLookalikeExclusion()
|
|
{
|
|
PasswordGenerator generator;
|
|
generator.setLength(2000);
|
|
generator.setCharClasses(PasswordGenerator::CharClass::LowerLetters | PasswordGenerator::CharClass::UpperLetters);
|
|
QVERIFY(generator.isValid());
|
|
QString password = generator.generatePassword();
|
|
QCOMPARE(password.size(), 2000);
|
|
|
|
generator.setFlags(PasswordGenerator::GeneratorFlag::ExcludeLookAlike);
|
|
password = generator.generatePassword();
|
|
QRegularExpression regex("^[^lI0]+$");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::LowerLetters | PasswordGenerator::CharClass::UpperLetters
|
|
| PasswordGenerator::CharClass::Numbers);
|
|
password = generator.generatePassword();
|
|
regex.setPattern("^[^lI01]+$");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
|
|
generator.setCharClasses(PasswordGenerator::CharClass::LowerLetters | PasswordGenerator::CharClass::UpperLetters
|
|
| PasswordGenerator::CharClass::Numbers | PasswordGenerator::CharClass::EASCII);
|
|
password = generator.generatePassword();
|
|
regex.setPattern("^[^lI01﹒]+$");
|
|
QVERIFY(regex.match(password).hasMatch());
|
|
}
|