diff --git a/tests/autotests/autotests.pro b/tests/autotests/autotests.pro index 1e510f3ac..c8a27524c 100644 --- a/tests/autotests/autotests.pro +++ b/tests/autotests/autotests.pro @@ -10,6 +10,12 @@ TARGET = autotests !unix|mac: LIBS += -L$$PWD/../../bin -lQupZilla !mac:unix: LIBS += $$PWD/../../bin/libQupZilla.so +# Link plugins for PasswordBackends +!unix|mac: LIBS += -L$$PWD/../../bin/plugins -lGnomeKeyringPasswords -lKWalletPasswords +!mac:unix: LIBS += $$PWD/../../bin/plugins/libGnomeKeyringPasswords.so \ + $$PWD/../../bin/plugins/libKWalletPasswords.so + + unix:contains(DEFINES, "NO_SYSTEM_DATAPATH"): QMAKE_LFLAGS+=$${QMAKE_LFLAGS_RPATH}\\$\$ORIGIN include($$PWD/../../src/defines.pri) @@ -52,7 +58,8 @@ HEADERS += \ downloadstest.h \ adblocktest.h \ updatertest.h \ - pactest.h + pactest.h \ + passwordbackendtest.h SOURCES += \ qztoolstest.cpp \ @@ -62,4 +69,5 @@ SOURCES += \ downloadstest.cpp \ adblocktest.cpp \ updatertest.cpp \ - pactest.cpp + pactest.cpp \ + passwordbackendtest.cpp diff --git a/tests/autotests/main.cpp b/tests/autotests/main.cpp index 76af7da83..fd0b15ac0 100644 --- a/tests/autotests/main.cpp +++ b/tests/autotests/main.cpp @@ -22,6 +22,7 @@ #include "adblocktest.h" #include "updatertest.h" #include "pactest.h" +#include "passwordbackendtest.h" #include @@ -46,5 +47,9 @@ int main(int argc, char *argv[]) RUN_TEST(UpdaterTest) RUN_TEST(PacTest) + RUN_TEST(DatabasePasswordBackendTest) + RUN_TEST(KWalletPasswordBackendTest) + RUN_TEST(GnomeKeyringPasswordBackendTest) + return 0; } diff --git a/tests/autotests/passwordbackendtest.cpp b/tests/autotests/passwordbackendtest.cpp new file mode 100644 index 000000000..320ed0411 --- /dev/null +++ b/tests/autotests/passwordbackendtest.cpp @@ -0,0 +1,234 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* 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 . +* ============================================================ */ +#include "passwordbackendtest.h" + +#include +#include +#include +#include + +#include "unistd.h" + +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(0) +{ +} + +void PasswordBackendTest::initTestCase() +{ + init(); + + // Backup entries + reloadBackend(); + m_entries = m_backend->getAllEntries(); +} + +void PasswordBackendTest::cleanupTestCase() +{ + cleanup(); + + reloadBackend(); + foreach (const PasswordEntry &entry, m_entries) { + m_backend->addEntry(entry); + } +} + +void PasswordBackendTest::storeTest() +{ + reloadBackend(); + + /* Basic password entry */ + PasswordEntry entry; + entry.host = "org.qupzilla.google.com"; + entry.username = "user1"; + entry.password = "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("org.qupzilla.google.com")).first(); + QVERIFY(compareEntries(stored, entry) == true); + + reloadBackend(); + + // Check entry retrieved from backend engine + stored = m_backend->getEntries(QUrl("org.qupzilla.google.com")).first(); + QVERIFY(compareEntries(stored, entry) == true); + + + /* UTF-8 password entry */ + PasswordEntry entry2; + entry2.host = "org.qupzilla.qupzilla.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("org.qupzilla.qupzilla.com")).first(); + QVERIFY(compareEntries(stored2, entry2) == true); + + reloadBackend(); + + // Check entry retrieved from backend engine + stored2 = m_backend->getEntries(QUrl("org.qupzilla.qupzilla.com")).first(); + QVERIFY(compareEntries(stored2, entry2) == true); + + /* Cleanup */ + // Local cleanup + m_backend->removeEntry(stored); + QCOMPARE(m_backend->getEntries(QUrl("org.qupzilla.google.com")).count(), 0); + + m_backend->removeEntry(stored2); + QCOMPARE(m_backend->getEntries(QUrl("org.qupzilla.qupzilla.com")).count(), 0); + + reloadBackend(); + + // Backend engine cleanup + QCOMPARE(m_backend->getEntries(QUrl("org.qupzilla.google.com")).count(), 0); + QCOMPARE(m_backend->getEntries(QUrl("org.qupzilla.qupzilla.com")).count(), 0); +} + +void PasswordBackendTest::removeAllTest() +{ + reloadBackend(); + + PasswordEntry entry; + entry.host = "org.qupzilla.google.com"; + entry.username = "user1"; + entry.password = "pass1"; + entry.data = "entry1-data=23&username=user1&password=pass1"; + m_backend->addEntry(entry); + + entry.username.append("s"); + m_backend->addEntry(entry); + + entry.username.append("s"); + m_backend->addEntry(entry); + + entry.username.append("s"); + m_backend->addEntry(entry); + + entry.username.append("s"); + m_backend->addEntry(entry); + + entry.username.append("s"); + m_backend->addEntry(entry); + + entry.username.append("s"); + m_backend->addEntry(entry); + + QCOMPARE(m_backend->getEntries(QUrl("org.qupzilla.google.com")).count(), 7); + reloadBackend(); + QCOMPARE(m_backend->getEntries(QUrl("org.qupzilla.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 = "org.qupzilla.google.com"; + entry.username = "user1"; + entry.password = "pass1"; + entry.data = "entry1-data=23&username=user1&password=pass1"; + m_backend->addEntry(entry); + + sleep(1); + + entry.username.append("s"); + m_backend->addEntry(entry); + + QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl("org.qupzilla.google.com")).first())); + reloadBackend(); + QVERIFY(compareEntries(entry, m_backend->getEntries(QUrl("org.qupzilla.google.com")).first())); +} + + +// DatabasePasswordBackendTest +void DatabasePasswordBackendTest::reloadBackend() +{ + delete m_backend; + m_backend = new DatabasePasswordBackend; +} + +void DatabasePasswordBackendTest::init() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + db.open(); + + db.exec("CREATE TABLE autofill (data TEXT, id INTEGER PRIMARY KEY, password TEXT," + "server TEXT, username TEXT, last_used NUMERIC)"); +} + +void DatabasePasswordBackendTest::cleanup() +{ + QSqlDatabase::removeDatabase(QSqlDatabase::database().databaseName()); +} + + +// KWalletPassswordBackendTest +void KWalletPasswordBackendTest::reloadBackend() +{ + delete m_backend; + m_backend = new KWalletPasswordBackend; +} + + +// GnomeKeyringPassswordBackendTest +void GnomeKeyringPasswordBackendTest::reloadBackend() +{ + delete m_backend; + m_backend = new GnomeKeyringPasswordBackend; +} diff --git a/tests/autotests/passwordbackendtest.h b/tests/autotests/passwordbackendtest.h new file mode 100644 index 000000000..a818f9de9 --- /dev/null +++ b/tests/autotests/passwordbackendtest.h @@ -0,0 +1,83 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* 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 . +* ============================================================ */ +#ifndef PASSWORDBACKENDTEST_H +#define PASSWORDBACKENDTEST_H + +#include +#include + +#include "passwordbackends/passwordbackend.h" +#include "passwordmanager.h" + +class PasswordBackendTest : public QObject +{ + Q_OBJECT + +public: + explicit PasswordBackendTest(); + +private slots: + void initTestCase(); + void cleanupTestCase(); + + void storeTest(); + void removeAllTest(); + void updateLastUsedTest(); + +protected: + virtual void reloadBackend() = 0; + virtual void init() { } + virtual void cleanup() { } + + PasswordBackend* m_backend; + QVector m_entries; +}; + +#include "passwordbackends/databasepasswordbackend.h" + +class DatabasePasswordBackendTest : public PasswordBackendTest +{ + Q_OBJECT + +protected: + void reloadBackend(); + void init(); + void cleanup(); +}; + +#include "../../src/plugins/KWalletPasswords/kwalletpasswordbackend.h" + +class KWalletPasswordBackendTest : public PasswordBackendTest +{ + Q_OBJECT + +protected: + void reloadBackend(); +}; + +#include "../../src/plugins/GnomeKeyringPasswords/gnomekeyringpasswordbackend.h" + +class GnomeKeyringPasswordBackendTest : public PasswordBackendTest +{ + Q_OBJECT + +protected: + void reloadBackend(); +}; + +#endif // PASSWORDBACKENDTEST_H