TestPasswordHealth.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "TestPasswordHealth.h"
  18. #include "core/PasswordHealth.h"
  19. #include <QTest>
  20. QTEST_GUILESS_MAIN(TestPasswordHealth)
  21. void TestPasswordHealth::initTestCase()
  22. {
  23. }
  24. void TestPasswordHealth::testNoDb()
  25. {
  26. const auto empty = PasswordHealth("");
  27. QCOMPARE(empty.score(), 0);
  28. QCOMPARE(empty.entropy(), 0.0);
  29. QCOMPARE(empty.quality(), PasswordHealth::Quality::Bad);
  30. QVERIFY(!empty.scoreReason().isEmpty());
  31. QVERIFY(!empty.scoreDetails().isEmpty());
  32. const auto poor = PasswordHealth("secret");
  33. QCOMPARE(poor.score(), 6);
  34. QCOMPARE(int(poor.entropy()), 6);
  35. QCOMPARE(poor.quality(), PasswordHealth::Quality::Poor);
  36. QVERIFY(!poor.scoreReason().isEmpty());
  37. QVERIFY(!poor.scoreDetails().isEmpty());
  38. const auto weak = PasswordHealth("Yohb2ChR4");
  39. QCOMPARE(weak.score(), 47);
  40. QCOMPARE(int(weak.entropy()), 47);
  41. QCOMPARE(weak.quality(), PasswordHealth::Quality::Weak);
  42. QVERIFY(!weak.scoreReason().isEmpty());
  43. QVERIFY(!weak.scoreDetails().isEmpty());
  44. const auto good = PasswordHealth("MIhIN9UKrgtPL2hp");
  45. QCOMPARE(good.score(), 78);
  46. QCOMPARE(int(good.entropy()), 78);
  47. QCOMPARE(good.quality(), PasswordHealth::Quality::Good);
  48. QVERIFY(good.scoreReason().isEmpty());
  49. QVERIFY(good.scoreDetails().isEmpty());
  50. const auto excellent = PasswordHealth("prompter-ream-oversleep-step-extortion-quarrel-reflected-prefix");
  51. QCOMPARE(excellent.score(), 164);
  52. QCOMPARE(int(excellent.entropy()), 164);
  53. QCOMPARE(excellent.quality(), PasswordHealth::Quality::Excellent);
  54. QVERIFY(excellent.scoreReason().isEmpty());
  55. QVERIFY(excellent.scoreDetails().isEmpty());
  56. }