keepassxc

Форк
0
/
TestHibp.cpp 
120 строк · 3.3 Кб
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

18
#include "TestHibp.h"
19

20
#include "core/Group.h"
21
#include "core/HibpOffline.h"
22
#include "crypto/Crypto.h"
23

24
#include <QBuffer>
25
#include <QByteArray>
26
#include <QList>
27
#include <QTest>
28

29
QTEST_GUILESS_MAIN(TestHibp)
30

31
const char* TEST_HIBP_CONTENTS = "0BEEC7B5EA3F0FDBC95D0DD47F3C5BC275DA8A33:123\n" // SHA-1 of "foo"
32
                                 "62cdb7020ff920e5aa642c3d4066950dd1f01f4d:456\n"; // SHA-1 of "bar"
33

34
const char* TEST_BAD_HIBP_CONTENTS = "barf:nope\n";
35

36
void TestHibp::initTestCase()
37
{
38
    QVERIFY(Crypto::init());
39
}
40

41
void TestHibp::init()
42
{
43
    m_db.reset(new Database());
44
}
45

46
void TestHibp::testBadHibpFormat()
47
{
48
    QByteArray hibpContents(TEST_BAD_HIBP_CONTENTS);
49
    QBuffer hibpBuffer(&hibpContents);
50
    QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
51

52
    QList<QPair<const Entry*, int>> findings;
53
    QString error;
54
    QVERIFY(!HibpOffline::report(m_db, hibpBuffer, findings, &error));
55
    QVERIFY(!error.isEmpty());
56
    QCOMPARE(findings.size(), 0);
57
}
58

59
void TestHibp::testEmpty()
60
{
61
    QByteArray hibpContents(TEST_HIBP_CONTENTS);
62
    QBuffer hibpBuffer(&hibpContents);
63
    QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
64

65
    QList<QPair<const Entry*, int>> findings;
66
    QString error;
67
    QVERIFY(HibpOffline::report(m_db, hibpBuffer, findings, &error));
68
    QCOMPARE(error, QString());
69
    QCOMPARE(findings.size(), 0);
70
}
71

72
void TestHibp::testIoError()
73
{
74
    QBuffer hibpBuffer;
75
    // hibpBuffer has not been opened, so reading will cause I/O error
76

77
    QList<QPair<const Entry*, int>> findings;
78
    QString error;
79
    QVERIFY(!HibpOffline::report(m_db, hibpBuffer, findings, &error));
80
    QVERIFY(!error.isEmpty());
81
    QCOMPARE(findings.size(), 0);
82
}
83

84
void TestHibp::testPwned()
85
{
86
    QByteArray hibpContents(TEST_HIBP_CONTENTS);
87
    QBuffer hibpBuffer(&hibpContents);
88
    QVERIFY(hibpBuffer.open(QIODevice::ReadOnly));
89

90
    Group* root = m_db->rootGroup();
91

92
    auto entry1 = new Entry();
93
    entry1->setPassword("foo");
94
    entry1->setGroup(root);
95

96
    auto entry2 = new Entry();
97
    entry2->setPassword("xyz");
98
    entry2->setGroup(root);
99

100
    auto entry3 = new Entry();
101
    entry3->setPassword("foo");
102
    m_db->recycleEntry(entry3);
103

104
    auto group1 = new Group();
105
    group1->setParent(root);
106

107
    auto entry4 = new Entry();
108
    entry4->setPassword("bar");
109
    entry4->setGroup(group1);
110

111
    QList<QPair<const Entry*, int>> findings;
112
    QString error;
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);
120
}
121

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.