File: passwordbackendtest.cpp

package info (click to toggle)
falkon 25.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,344 kB
  • sloc: cpp: 66,939; javascript: 21,781; sh: 578; xml: 564; python: 496; sql: 75; makefile: 26
file content (212 lines) | stat: -rw-r--r-- 6,679 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "passwordbackendtest.h"

#include <QtTest/QtTest>
#include <QDebug>

#ifdef Q_OS_WIN
#include "qt_windows.h"
#else
#include "unistd.h"
#endif

static bool compareEntries(const PasswordEntry &value, const PasswordEntry &ref)
{
    if (ref.host != value.host) {
        qDebug() << "Host mismatch. Value =" << value.host << "Reference =" << ref.host;
        return false;
    }

    if (ref.username != value.username) {
        qDebug() << "Username mismatch. Value =" << value.username << "Reference =" << ref.username;
        return false;
    }

    if (ref.password != value.password) {
        qDebug() << "Password mismatch. Value =" << value.password << "Reference =" << ref.password;
        return false;
    }

    if (ref.data != value.data) {
        qDebug() << "Data mismatch. Value =" << value.data << "Reference =" << ref.data;
        return false;
    }

    return true;
}

PasswordBackendTest::PasswordBackendTest()
    : QObject()
    , m_backend(nullptr)
{
}

void PasswordBackendTest::initTestCase()
{
    init();

    // Backup entries
    reloadBackend();
    m_entries = m_backend->getAllEntries();
    m_backend->removeAll();
}

void PasswordBackendTest::cleanupTestCase()
{
    cleanup();

    reloadBackend();
    for (const PasswordEntry &entry : std::as_const(m_entries)) {
        m_backend->addEntry(entry);
    }
}

void PasswordBackendTest::storeTest()
{
    reloadBackend();

    /* Basic password entry */
    PasswordEntry entry;
    entry.host = QSL("org.falkon.google.com");
    entry.username = QSL("user1");
    entry.password = QSL("pass1");
    entry.data = "entry1-data=23&username=user1&password=pass1";

    m_backend->addEntry(entry);

    // Check entry that may be stored in cache
    PasswordEntry stored = m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst();
    QVERIFY(compareEntries(stored, entry) == true);

    reloadBackend();

    // Check entry retrieved from backend engine
    QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty());
    stored = m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst();
    QVERIFY(compareEntries(stored, entry) == true);


    /* UTF-8 password entry */
    PasswordEntry entry2;
    entry2.host = QSL("org.falkon.falkon.com");
    entry2.username = QString::fromUtf8("+ě ++ éí§`]|~đ11 +!:");
    entry2.password = QString::fromUtf8("+ěš asn~đ°#&# |€");
    entry2.data = "use%C2%B6+_nam%C4%8D=%2B%C4%9B+%2B%2B+%C3%A9%C3%AD%C2%A7%60%5D%7C%7E%C4%9111+%2B%21%3A"
            "&pA+%5DsQ+%2Bword=%2B%C4%9B%C5%A1+asn%7E%C4%91%C2%B0%23%26%23+%7C%E2%82%AC";

    m_backend->addEntry(entry2);

    // Check entry that may be stored in cache
    PasswordEntry stored2 = m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).constFirst();
    QVERIFY(compareEntries(stored2, entry2) == true);

    reloadBackend();

    // Check entry retrieved from backend engine
    stored2 = m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).constFirst();
    QVERIFY(compareEntries(stored2, entry2) == true);

    /* Cleanup */
    // Local cleanup
    m_backend->removeEntry(stored);
    QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 0);

    m_backend->removeEntry(stored2);
    QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).count(), 0);

    reloadBackend();

    // Backend engine cleanup
    QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 0);
    QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).count(), 0);
}

void PasswordBackendTest::removeAllTest()
{
    reloadBackend();

    PasswordEntry entry;
    entry.host = QSL("org.falkon.google.com");
    entry.username = QSL("user1");
    entry.password = QSL("pass1");
    entry.data = "entry1-data=23&username=user1&password=pass1";
    m_backend->addEntry(entry);

    entry.username.append(QSL("s"));
    m_backend->addEntry(entry);

    entry.username.append(QSL("s"));
    m_backend->addEntry(entry);

    entry.username.append(QSL("s"));
    m_backend->addEntry(entry);

    entry.username.append(QSL("s"));
    m_backend->addEntry(entry);

    entry.username.append(QSL("s"));
    m_backend->addEntry(entry);

    entry.username.append(QSL("s"));
    m_backend->addEntry(entry);

    QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 7);
    reloadBackend();
    QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 7);

    m_backend->removeAll();

    QCOMPARE(m_backend->getAllEntries().count(), 0);
    reloadBackend();
    QCOMPARE(m_backend->getAllEntries().count(), 0);
}

void PasswordBackendTest::updateLastUsedTest()
{
    reloadBackend();

    PasswordEntry entry;
    entry.host = QSL("org.falkon.google.com");
    entry.username = QSL("user1");
    entry.password = QSL("pass1");
    entry.data = "entry1-data=23&username=user1&password=pass1";
    m_backend->addEntry(entry);

#ifdef Q_OS_WIN
    Sleep(1000);
#else
    sleep(1);
#endif

    entry.username.append(QSL("s"));
    m_backend->addEntry(entry);

    QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty());
    QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst()));
    reloadBackend();
    QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty());
    QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst()));

    m_backend->removeEntry(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst());
    m_backend->removeEntry(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst());

    QCOMPARE(m_backend->getAllEntries().count(), 0);
    reloadBackend();
    QCOMPARE(m_backend->getAllEntries().count(), 0);
}