TestPassphraseGenerator.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestPassphraseGenerator.h"
  18. #include "core/PassphraseGenerator.h"
  19. #include "crypto/Crypto.h"
  20. #include <QRegularExpression>
  21. #include <QTest>
  22. QTEST_GUILESS_MAIN(TestPassphraseGenerator)
  23. void TestPassphraseGenerator::initTestCase()
  24. {
  25. QVERIFY(Crypto::init());
  26. }
  27. void TestPassphraseGenerator::testWordCase()
  28. {
  29. PassphraseGenerator generator;
  30. generator.setWordSeparator(" ");
  31. QVERIFY(generator.isValid());
  32. QString passphrase;
  33. passphrase = generator.generatePassphrase();
  34. QCOMPARE(passphrase, passphrase.toLower());
  35. generator.setWordCase(PassphraseGenerator::LOWERCASE);
  36. passphrase = generator.generatePassphrase();
  37. QCOMPARE(passphrase, passphrase.toLower());
  38. generator.setWordCase(PassphraseGenerator::UPPERCASE);
  39. passphrase = generator.generatePassphrase();
  40. QCOMPARE(passphrase, passphrase.toUpper());
  41. generator.setWordCase(PassphraseGenerator::TITLECASE);
  42. passphrase = generator.generatePassphrase();
  43. QRegularExpression regex("^([A-Z][a-z]* ?)+$");
  44. QVERIFY(regex.match(passphrase).hasMatch());
  45. }