File indexing completed on 2024-03-24 04:58:33
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "passwordbackendtest.h" 0019 0020 #include <QtTest/QtTest> 0021 #include <QDebug> 0022 0023 #ifdef Q_OS_WIN 0024 #include "qt_windows.h" 0025 #else 0026 #include "unistd.h" 0027 #endif 0028 0029 static bool compareEntries(const PasswordEntry &value, const PasswordEntry &ref) 0030 { 0031 if (ref.host != value.host) { 0032 qDebug() << "Host mismatch. Value =" << value.host << "Reference =" << ref.host; 0033 return false; 0034 } 0035 0036 if (ref.username != value.username) { 0037 qDebug() << "Username mismatch. Value =" << value.username << "Reference =" << ref.username; 0038 return false; 0039 } 0040 0041 if (ref.password != value.password) { 0042 qDebug() << "Password mismatch. Value =" << value.password << "Reference =" << ref.password; 0043 return false; 0044 } 0045 0046 if (ref.data != value.data) { 0047 qDebug() << "Data mismatch. Value =" << value.data << "Reference =" << ref.data; 0048 return false; 0049 } 0050 0051 return true; 0052 } 0053 0054 PasswordBackendTest::PasswordBackendTest() 0055 : QObject() 0056 , m_backend(nullptr) 0057 { 0058 } 0059 0060 void PasswordBackendTest::initTestCase() 0061 { 0062 init(); 0063 0064 // Backup entries 0065 reloadBackend(); 0066 m_entries = m_backend->getAllEntries(); 0067 m_backend->removeAll(); 0068 } 0069 0070 void PasswordBackendTest::cleanupTestCase() 0071 { 0072 cleanup(); 0073 0074 reloadBackend(); 0075 for (const PasswordEntry &entry : std::as_const(m_entries)) { 0076 m_backend->addEntry(entry); 0077 } 0078 } 0079 0080 void PasswordBackendTest::storeTest() 0081 { 0082 reloadBackend(); 0083 0084 /* Basic password entry */ 0085 PasswordEntry entry; 0086 entry.host = QSL("org.falkon.google.com"); 0087 entry.username = QSL("user1"); 0088 entry.password = QSL("pass1"); 0089 entry.data = "entry1-data=23&username=user1&password=pass1"; 0090 0091 m_backend->addEntry(entry); 0092 0093 // Check entry that may be stored in cache 0094 PasswordEntry stored = m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst(); 0095 QVERIFY(compareEntries(stored, entry) == true); 0096 0097 reloadBackend(); 0098 0099 // Check entry retrieved from backend engine 0100 QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty()); 0101 stored = m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst(); 0102 QVERIFY(compareEntries(stored, entry) == true); 0103 0104 0105 /* UTF-8 password entry */ 0106 PasswordEntry entry2; 0107 entry2.host = QSL("org.falkon.falkon.com"); 0108 entry2.username = QString::fromUtf8("+ě ++ éí§`]|~đ11 +!:"); 0109 entry2.password = QString::fromUtf8("+ěš asn~đ°#&# |€"); 0110 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" 0111 "&pA+%5DsQ+%2Bword=%2B%C4%9B%C5%A1+asn%7E%C4%91%C2%B0%23%26%23+%7C%E2%82%AC"; 0112 0113 m_backend->addEntry(entry2); 0114 0115 // Check entry that may be stored in cache 0116 PasswordEntry stored2 = m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).constFirst(); 0117 QVERIFY(compareEntries(stored2, entry2) == true); 0118 0119 reloadBackend(); 0120 0121 // Check entry retrieved from backend engine 0122 stored2 = m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).constFirst(); 0123 QVERIFY(compareEntries(stored2, entry2) == true); 0124 0125 /* Cleanup */ 0126 // Local cleanup 0127 m_backend->removeEntry(stored); 0128 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 0); 0129 0130 m_backend->removeEntry(stored2); 0131 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).count(), 0); 0132 0133 reloadBackend(); 0134 0135 // Backend engine cleanup 0136 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 0); 0137 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.falkon.com"))).count(), 0); 0138 } 0139 0140 void PasswordBackendTest::removeAllTest() 0141 { 0142 reloadBackend(); 0143 0144 PasswordEntry entry; 0145 entry.host = QSL("org.falkon.google.com"); 0146 entry.username = QSL("user1"); 0147 entry.password = QSL("pass1"); 0148 entry.data = "entry1-data=23&username=user1&password=pass1"; 0149 m_backend->addEntry(entry); 0150 0151 entry.username.append(QSL("s")); 0152 m_backend->addEntry(entry); 0153 0154 entry.username.append(QSL("s")); 0155 m_backend->addEntry(entry); 0156 0157 entry.username.append(QSL("s")); 0158 m_backend->addEntry(entry); 0159 0160 entry.username.append(QSL("s")); 0161 m_backend->addEntry(entry); 0162 0163 entry.username.append(QSL("s")); 0164 m_backend->addEntry(entry); 0165 0166 entry.username.append(QSL("s")); 0167 m_backend->addEntry(entry); 0168 0169 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 7); 0170 reloadBackend(); 0171 QCOMPARE(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).count(), 7); 0172 0173 m_backend->removeAll(); 0174 0175 QCOMPARE(m_backend->getAllEntries().count(), 0); 0176 reloadBackend(); 0177 QCOMPARE(m_backend->getAllEntries().count(), 0); 0178 } 0179 0180 void PasswordBackendTest::updateLastUsedTest() 0181 { 0182 reloadBackend(); 0183 0184 PasswordEntry entry; 0185 entry.host = QSL("org.falkon.google.com"); 0186 entry.username = QSL("user1"); 0187 entry.password = QSL("pass1"); 0188 entry.data = "entry1-data=23&username=user1&password=pass1"; 0189 m_backend->addEntry(entry); 0190 0191 #ifdef Q_OS_WIN 0192 Sleep(1000); 0193 #else 0194 sleep(1); 0195 #endif 0196 0197 entry.username.append(QSL("s")); 0198 m_backend->addEntry(entry); 0199 0200 QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty()); 0201 QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst())); 0202 reloadBackend(); 0203 QVERIFY(!m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).isEmpty()); 0204 QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst())); 0205 0206 m_backend->removeEntry(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst()); 0207 m_backend->removeEntry(m_backend->getEntries(QUrl(QSL("org.falkon.google.com"))).constFirst()); 0208 0209 QCOMPARE(m_backend->getAllEntries().count(), 0); 0210 reloadBackend(); 0211 QCOMPARE(m_backend->getAllEntries().count(), 0); 0212 }