File indexing completed on 2024-05-12 05:52:47

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 #include "account/account.h"
0006 
0007 #include "../test-utils/output.h"
0008 #include "../../test-utils/spy.h"
0009 
0010 #include <QSignalSpy>
0011 #include <QString>
0012 #include <QTest>
0013 #include <QtDebug>
0014 
0015 static QString testIniResource(QLatin1String("test.ini"));
0016 
0017 class StorageAbortLifeCycleTest: public QObject
0018 {
0019     Q_OBJECT
0020 private Q_SLOTS:
0021     void initTestCase(void);
0022     void testLifecycle(void);
0023 };
0024 
0025 void StorageAbortLifeCycleTest::initTestCase(void)
0026 {
0027     QVERIFY2(test::ensureOutputDirectory(), "output directory should be available");
0028     QVERIFY2(test::copyResourceAsWritable(QStringLiteral(":/storage-lifecycles/starting.ini"), testIniResource), "test corpus INI resource should be available as file");
0029 }
0030 
0031 void StorageAbortLifeCycleTest::testLifecycle(void)
0032 {
0033     const QString iniResource = test::path(testIniResource);
0034 
0035     const accounts::SettingsProvider settings([&iniResource](const accounts::PersistenceAction &action) -> void
0036     {
0037         QSettings data(iniResource, QSettings::IniFormat);
0038         action(data);
0039     });
0040 
0041     accounts::AccountStorage *uut = accounts::AccountStorage::open(settings);
0042     QSignalSpy error(uut, &accounts::AccountStorage::error);
0043     QSignalSpy loaded(uut, &accounts::AccountStorage::loaded);
0044     QSignalSpy accountAdded(uut, &accounts::AccountStorage::added);
0045     QSignalSpy storageDisposed(uut, &accounts::AccountStorage::disposed);
0046     QSignalSpy storageCleaned(uut, &accounts::AccountStorage::destroyed);
0047 
0048     accounts::AccountSecret *secret = uut->secret();
0049     QSignalSpy existingPasswordNeeded(secret, &accounts::AccountSecret::existingPasswordNeeded);
0050     QSignalSpy newPasswordNeeded(secret, &accounts::AccountSecret::newPasswordNeeded);
0051     QSignalSpy passwordAvailable(secret, &accounts::AccountSecret::passwordAvailable);
0052     QSignalSpy keyAvailable(secret, &accounts::AccountSecret::keyAvailable);
0053     QSignalSpy passwordRequestsCancelled(secret, &accounts::AccountSecret::requestsCancelled);
0054     QSignalSpy secretCleaned(secret, &accounts::AccountSecret::destroyed);
0055 
0056     // first phase:  expect that unlocking is scheduled automatically, so advancing the event loop should trigger the signal
0057     QVERIFY2(test::signal_eventually_emitted_once(existingPasswordNeeded), "(existing) password should be asked by now");
0058     QCOMPARE(newPasswordNeeded.count(), 0);
0059 
0060     // second phase: check that disposing storage cleans up objects properly
0061     uut->dispose();
0062 
0063     QVERIFY2(test::signal_eventually_emitted_once(passwordRequestsCancelled), "account secret should have signalled cancellation by now");
0064     QVERIFY2(test::signal_eventually_emitted_once(storageDisposed), "storage should be disposed of by now");
0065     QVERIFY2(test::signal_eventually_emitted_once(secretCleaned), "account secret should be cleaned up by now");
0066     QVERIFY2(test::signal_eventually_emitted_once(storageCleaned), "storage should be cleaned up by now");
0067 
0068     QCOMPARE(passwordAvailable.count(), 0);
0069     QCOMPARE(keyAvailable.count(), 0);
0070     QCOMPARE(accountAdded.count(), 0);
0071     QCOMPARE(loaded.count(), 0);
0072     QCOMPARE(error.count(), 1);
0073 }
0074 
0075 QTEST_MAIN(StorageAbortLifeCycleTest)
0076 
0077 #include "storage-aborted-lifecycle.moc"