TestHibp.cpp 3.3 KB

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