Falkon Develop
Cross-platform Qt-based web browser
passwordbackendtest.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "passwordbackendtest.h"
19
20#include <QtTest/QtTest>
21#include <QDebug>
22
23#ifdef Q_OS_WIN
24#include "qt_windows.h"
25#else
26#include "unistd.h"
27#endif
28
29static bool compareEntries(const PasswordEntry &value, const PasswordEntry &ref)
30{
31 if (ref.host != value.host) {
32 qDebug() << "Host mismatch. Value =" << value.host << "Reference =" << ref.host;
33 return false;
34 }
35
36 if (ref.username != value.username) {
37 qDebug() << "Username mismatch. Value =" << value.username << "Reference =" << ref.username;
38 return false;
39 }
40
41 if (ref.password != value.password) {
42 qDebug() << "Password mismatch. Value =" << value.password << "Reference =" << ref.password;
43 return false;
44 }
45
46 if (ref.data != value.data) {
47 qDebug() << "Data mismatch. Value =" << value.data << "Reference =" << ref.data;
48 return false;
49 }
50
51 return true;
52}
53
55 : QObject()
56 , m_backend(nullptr)
57{
58}
59
60void PasswordBackendTest::initTestCase()
61{
62 init();
63
64 // Backup entries
68}
69
70void PasswordBackendTest::cleanupTestCase()
71{
72 cleanup();
73
75 for (const PasswordEntry &entry : std::as_const(m_entries)) {
76 m_backend->addEntry(entry);
77 }
78}
79
80void PasswordBackendTest::storeTest()
81{
83
84 /* Basic password entry */
85 PasswordEntry entry;
86 entry.host = QSL("org.falkon.google.com");
87 entry.username = QSL("user1");
88 entry.password = QSL("pass1");
89 entry.data = "entry1-data=23&username=user1&password=pass1";
90
91 m_backend->addEntry(entry);
92
93 // Check entry that may be stored in cache
94 PasswordEntry stored = m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst();
95 QVERIFY(compareEntries(stored, entry) == true);
96
98
99 // Check entry retrieved from backend engine
100 QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty());
101 stored = m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst();
102 QVERIFY(compareEntries(stored, entry) == true);
103
104
105 /* UTF-8 password entry */
106 PasswordEntry entry2;
107 entry2.host = QSL("org.falkon.falkon.com");
108 entry2.username = QString::fromUtf8("+ě ++ éí§`]|~đ11 +!:");
109 entry2.password = QString::fromUtf8("+ěš asn~đ°#&# |€");
110 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"
111 "&pA+%5DsQ+%2Bword=%2B%C4%9B%C5%A1+asn%7E%C4%91%C2%B0%23%26%23+%7C%E2%82%AC";
112
113 m_backend->addEntry(entry2);
114
115 // Check entry that may be stored in cache
116 PasswordEntry stored2 = m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).constFirst();
117 QVERIFY(compareEntries(stored2, entry2) == true);
118
120
121 // Check entry retrieved from backend engine
122 stored2 = m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).constFirst();
123 QVERIFY(compareEntries(stored2, entry2) == true);
124
125 /* Cleanup */
126 // Local cleanup
127 m_backend->removeEntry(stored);
128 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 0);
129
130 m_backend->removeEntry(stored2);
131 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).count(), 0);
132
134
135 // Backend engine cleanup
136 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 0);
137 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).count(), 0);
138}
139
140void PasswordBackendTest::removeAllTest()
141{
143
144 PasswordEntry entry;
145 entry.host = QSL("org.falkon.google.com");
146 entry.username = QSL("user1");
147 entry.password = QSL("pass1");
148 entry.data = "entry1-data=23&username=user1&password=pass1";
149 m_backend->addEntry(entry);
150
151 entry.username.append(QSL("s"));
152 m_backend->addEntry(entry);
153
154 entry.username.append(QSL("s"));
155 m_backend->addEntry(entry);
156
157 entry.username.append(QSL("s"));
158 m_backend->addEntry(entry);
159
160 entry.username.append(QSL("s"));
161 m_backend->addEntry(entry);
162
163 entry.username.append(QSL("s"));
164 m_backend->addEntry(entry);
165
166 entry.username.append(QSL("s"));
167 m_backend->addEntry(entry);
168
169 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 7);
171 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 7);
172
174
175 QCOMPARE(m_backend->getAllEntries().count(), 0);
177 QCOMPARE(m_backend->getAllEntries().count(), 0);
178}
179
180void PasswordBackendTest::updateLastUsedTest()
181{
183
184 PasswordEntry entry;
185 entry.host = QSL("org.falkon.google.com");
186 entry.username = QSL("user1");
187 entry.password = QSL("pass1");
188 entry.data = "entry1-data=23&username=user1&password=pass1";
189 m_backend->addEntry(entry);
190
191#ifdef Q_OS_WIN
192 Sleep(1000);
193#else
194 sleep(1);
195#endif
196
197 entry.username.append(QSL("s"));
198 m_backend->addEntry(entry);
199
200 QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty());
201 QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst()));
203 QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty());
204 QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst()));
205
206 m_backend->removeEntry(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst());
207 m_backend->removeEntry(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst());
208
209 QCOMPARE(m_backend->getAllEntries().count(), 0);
211 QCOMPARE(m_backend->getAllEntries().count(), 0);
212}
virtual void addEntry(const PasswordEntry &entry)=0
virtual QVector< PasswordEntry > getAllEntries()=0
virtual void removeAll()=0
virtual void removeEntry(const PasswordEntry &entry)=0
virtual QVector< PasswordEntry > getEntries(const QUrl &url)=0
PasswordBackend * m_backend
virtual void reloadBackend()=0
QVector< PasswordEntry > m_entries
int value(const QColor &c)
Definition: colors.cpp:238
#define QSL(x)
Definition: qzcommon.h:40
QString password
QString host
QByteArray data
QString username