TestPassphraseGenerator.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "config-keepassx-tests.h"
  19. #include "core/PassphraseGenerator.h"
  20. #include "crypto/Crypto.h"
  21. #include <QRegularExpression>
  22. #include <QTest>
  23. QTEST_GUILESS_MAIN(TestPassphraseGenerator)
  24. void TestPassphraseGenerator::initTestCase()
  25. {
  26. QVERIFY(Crypto::init());
  27. }
  28. void TestPassphraseGenerator::testWordCase()
  29. {
  30. PassphraseGenerator generator;
  31. generator.setWordSeparator(" ");
  32. QVERIFY(generator.isValid());
  33. QString passphrase;
  34. passphrase = generator.generatePassphrase();
  35. QCOMPARE(passphrase, passphrase.toLower());
  36. generator.setWordCase(PassphraseGenerator::LOWERCASE);
  37. passphrase = generator.generatePassphrase();
  38. QCOMPARE(passphrase, passphrase.toLower());
  39. generator.setWordCase(PassphraseGenerator::UPPERCASE);
  40. passphrase = generator.generatePassphrase();
  41. QCOMPARE(passphrase, passphrase.toUpper());
  42. generator.setWordCase(PassphraseGenerator::TITLECASE);
  43. passphrase = generator.generatePassphrase();
  44. QRegularExpression regex("^(?:[A-Z][a-z-]* )*[A-Z][a-z-]*$");
  45. QVERIFY2(regex.match(passphrase).hasMatch(), qPrintable(passphrase));
  46. }
  47. void TestPassphraseGenerator::testUniqueEntriesInWordlist()
  48. {
  49. PassphraseGenerator generator;
  50. // set the limit down, so we don;t have to do a very large file
  51. generator.m_minimum_wordlist_length = 4;
  52. // link to bad wordlist
  53. QString path = QString(KEEPASSX_TEST_DATA_DIR).append("/wordlists/bad_wordlist_with_duplicate_entries.wordlist");
  54. // setting will work, it creates the warning however, and isValid will fail
  55. generator.setWordList(path);
  56. // so this fails
  57. QVERIFY(!generator.isValid());
  58. }