File indexing completed on 2023-10-03 07:58:48

0001 /*************************************************************************************
0002  *  Copyright (C) 2013 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include <QTest>
0020 #include <QObject>
0021 
0022 #include "../src/keyringplugin.h"
0023 #include "../src/secretsstorage.h"
0024 
0025 class testExtension : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 private Q_SLOTS:
0030     void initTestCase();
0031     void testUpdateLoadRemoveCredentials();
0032     void testStoreLoadAndRemoveData();
0033 
0034 private:
0035     SecretsStorage *m_storage;
0036 };
0037 
0038 void testExtension::initTestCase()
0039 {
0040     KeyringPlugin *plugin = new KeyringPlugin(this);
0041     m_storage = qobject_cast<SecretsStorage *>(plugin->secretsStorage(0));
0042     QVERIFY2(m_storage, "SecretsStorage pointer is null");
0043     QCOMPARE(m_storage->initialize(QVariantMap()), true);
0044 }
0045 
0046 void testExtension::testUpdateLoadRemoveCredentials()
0047 {
0048     QString user, password;
0049     QVERIFY(m_storage->updateCredentials(42, "testUser", "testPassword"));
0050     QVERIFY(m_storage->loadCredentials(42, user, password));
0051     QCOMPARE(user, QString("testUser"));
0052     QCOMPARE(password, QString("testPassword"));
0053     QVERIFY(m_storage->removeCredentials(42));
0054 
0055     user = "";
0056     password = "";
0057     QVERIFY(!m_storage->loadCredentials(42, user, password));
0058     QCOMPARE(user, QString());
0059     QCOMPARE(password, QString());
0060 
0061     m_storage->updateCredentials(44, "user44", "pass44");
0062     m_storage->updateCredentials(43, "user43", "pass43");
0063     m_storage->clear();
0064 
0065     QVERIFY(!m_storage->loadCredentials(43, user, password));
0066     QCOMPARE(user, QString());
0067     QCOMPARE(password, QString());
0068 
0069     QVERIFY(!m_storage->loadCredentials(44, user, password));
0070     QCOMPARE(user, QString());
0071     QCOMPARE(password, QString());
0072 }
0073 
0074 void testExtension::testStoreLoadAndRemoveData()
0075 {
0076     QVariantMap data;
0077     data["int"] = 4;
0078     data["string"] = QString("string");
0079     data["bool"] = false;
0080 
0081     QVERIFY(m_storage->storeData(43, 2, data));
0082 
0083     data = m_storage->loadData(43, 2);
0084     QCOMPARE(data["int"].toInt(), 4);
0085     QCOMPARE(data["string"].toString(), QString("string"));
0086     QCOMPARE(data["bool"].toBool(), false);
0087 
0088     QVERIFY(m_storage->removeData(43, 2));
0089 
0090 }
0091 
0092 QTEST_MAIN(testExtension)
0093 
0094 #include "testextension.moc"