20
#include "core/Group.h"
21
#include "core/HibpOffline.h"
22
#include "crypto/Crypto.h"
29
QTEST_GUILESS_MAIN(TestHibp)
31
const char* TEST_HIBP_CONTENTS = "0BEEC7B5EA3F0FDBC95D0DD47F3C5BC275DA8A33:123\n"
32
"62cdb7020ff920e5aa642c3d4066950dd1f01f4d:456\n";
34
const char* TEST_BAD_HIBP_CONTENTS = "barf:nope\n";
36
void TestHibp::initTestCase()
38
QVERIFY(Crypto::init());
43
m_db.reset(new Database());
46
void TestHibp::testBadHibpFormat()
48
QByteArray hibpContents(TEST_BAD_HIBP_CONTENTS);
49
QBuffer hibpBuffer(&hibpContents);
50
QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
52
QList<QPair<const Entry*, int>> findings;
54
QVERIFY(!HibpOffline::report(m_db, hibpBuffer, findings, &error));
55
QVERIFY(!error.isEmpty());
56
QCOMPARE(findings.size(), 0);
59
void TestHibp::testEmpty()
61
QByteArray hibpContents(TEST_HIBP_CONTENTS);
62
QBuffer hibpBuffer(&hibpContents);
63
QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
65
QList<QPair<const Entry*, int>> findings;
67
QVERIFY(HibpOffline::report(m_db, hibpBuffer, findings, &error));
68
QCOMPARE(error, QString());
69
QCOMPARE(findings.size(), 0);
72
void TestHibp::testIoError()
77
QList<QPair<const Entry*, int>> findings;
79
QVERIFY(!HibpOffline::report(m_db, hibpBuffer, findings, &error));
80
QVERIFY(!error.isEmpty());
81
QCOMPARE(findings.size(), 0);
84
void TestHibp::testPwned()
86
QByteArray hibpContents(TEST_HIBP_CONTENTS);
87
QBuffer hibpBuffer(&hibpContents);
88
QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
90
Group* root = m_db->rootGroup();
92
auto entry1 = new Entry();
93
entry1->setPassword("foo");
94
entry1->setGroup(root);
96
auto entry2 = new Entry();
97
entry2->setPassword("xyz");
98
entry2->setGroup(root);
100
auto entry3 = new Entry();
101
entry3->setPassword("foo");
102
m_db->recycleEntry(entry3);
104
auto group1 = new Group();
105
group1->setParent(root);
107
auto entry4 = new Entry();
108
entry4->setPassword("bar");
109
entry4->setGroup(group1);
111
QList<QPair<const Entry*, int>> findings;
113
QVERIFY(HibpOffline::report(m_db, hibpBuffer, findings, &error));
114
QCOMPARE(error, QString());
115
QCOMPARE(findings.size(), 2);
116
QCOMPARE(findings[0].first, entry1);
117
QCOMPARE(findings[0].second, 123);
118
QCOMPARE(findings[1].first, entry4);
119
QCOMPARE(findings[1].second, 456);