TestHibp.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "TestHibp.h"
  18. #include "config-keepassx-tests.h"
  19. #include "core/Database.h"
  20. #include "core/Entry.h"
  21. #include "core/Group.h"
  22. #include "core/HibpOffline.h"
  23. #include "crypto/Crypto.h"
  24. #include <QBuffer>
  25. #include <QByteArray>
  26. #include <QFile>
  27. #include <QList>
  28. #include <QTest>
  29. QTEST_GUILESS_MAIN(TestHibp)
  30. const char* TEST_HIBP_CONTENTS = "0BEEC7B5EA3F0FDBC95D0DD47F3C5BC275DA8A33:123\n" // SHA-1 of "foo"
  31. "62cdb7020ff920e5aa642c3d4066950dd1f01f4d:456\n"; // SHA-1 of "bar"
  32. const char* TEST_BAD_HIBP_CONTENTS = "barf:nope\n";
  33. void TestHibp::initTestCase()
  34. {
  35. QVERIFY(Crypto::init());
  36. }
  37. void TestHibp::init()
  38. {
  39. m_db.reset(new Database());
  40. }
  41. void TestHibp::testBadHibpFormat()
  42. {
  43. QByteArray hibpContents(TEST_BAD_HIBP_CONTENTS);
  44. QBuffer hibpBuffer(&hibpContents);
  45. QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
  46. QList<QPair<const Entry*, int>> findings;
  47. QString error;
  48. QVERIFY(!HibpOffline::report(m_db, hibpBuffer, findings, &error));
  49. QVERIFY(!error.isEmpty());
  50. QCOMPARE(findings.size(), 0);
  51. }
  52. void TestHibp::testEmpty()
  53. {
  54. QByteArray hibpContents(TEST_HIBP_CONTENTS);
  55. QBuffer hibpBuffer(&hibpContents);
  56. QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
  57. QList<QPair<const Entry*, int>> findings;
  58. QString error;
  59. QVERIFY(HibpOffline::report(m_db, hibpBuffer, findings, &error));
  60. QCOMPARE(error, QString());
  61. QCOMPARE(findings.size(), 0);
  62. }
  63. void TestHibp::testIoError()
  64. {
  65. QBuffer hibpBuffer;
  66. // hibpBuffer has not been opened, so reading will cause I/O error
  67. QList<QPair<const Entry*, int>> findings;
  68. QString error;
  69. QVERIFY(!HibpOffline::report(m_db, hibpBuffer, findings, &error));
  70. QVERIFY(!error.isEmpty());
  71. QCOMPARE(findings.size(), 0);
  72. }
  73. void TestHibp::testPwned()
  74. {
  75. QByteArray hibpContents(TEST_HIBP_CONTENTS);
  76. QBuffer hibpBuffer(&hibpContents);
  77. QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
  78. Group* root = m_db->rootGroup();
  79. Entry* entry1 = new Entry();
  80. entry1->setPassword("foo");
  81. entry1->setGroup(root);
  82. Entry* entry2 = new Entry();
  83. entry2->setPassword("xyz");
  84. entry2->setGroup(root);
  85. Entry* entry3 = new Entry();
  86. entry3->setPassword("foo");
  87. m_db->recycleEntry(entry3);
  88. Group* group1 = new Group();
  89. group1->setParent(root);
  90. Entry* entry4 = new Entry();
  91. entry4->setPassword("bar");
  92. entry4->setGroup(group1);
  93. QList<QPair<const Entry*, int>> findings;
  94. QString error;
  95. QVERIFY(HibpOffline::report(m_db, hibpBuffer, findings, &error));
  96. QCOMPARE(error, QString());
  97. QCOMPARE(findings.size(), 2);
  98. QCOMPARE(findings[0].first, entry1);
  99. QCOMPARE(findings[0].second, 123);
  100. QCOMPARE(findings[1].first, entry4);
  101. QCOMPARE(findings[1].second, 456);
  102. }