1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*
*/
#include "account.h"
#include "libsync/creds/credentialmanager.h"
#include "testutils/syncenginetestutils.h"
#include <QTest>
namespace OCC {
class TestCredentialManager : public QObject
{
Q_OBJECT
bool _finished = false;
QTemporaryFile _credStoreFile;
QSettings _credStore;
void setFallbackEnabled(QKeychain::Job *job)
{
// store the test credentials in a plain text settings file on unsupported platforms
job->setSettings(&_credStore);
job->setInsecureFallback(true);
}
private Q_SLOTS:
void init()
{
_finished = false;
QVERIFY(_credStoreFile.open());
_credStore.setPath(QSettings::IniFormat, QSettings::UserScope, _credStoreFile.fileName());
_credStore.clear();
}
void testSetGet_data()
{
QTest::addColumn<QVariant>("data");
QTest::newRow("bool") << QVariant::fromValue(true);
QTest::newRow("int") << QVariant::fromValue(1);
QTest::newRow("map") << QVariant::fromValue(
QVariantMap{{QStringLiteral("foo"), QColor(Qt::red).name()}, {QStringLiteral("bar"), QStringLiteral("42")}});
}
void testSetGet()
{
QFETCH(QVariant, data);
FakeFolder fakeFolder { FileInfo::A12_B12_C12_S12() };
auto creds = fakeFolder.account()->credentialManager();
const QString key = QStringLiteral("test");
auto setJob = creds->set(key, data);
setFallbackEnabled(setJob);
connect(setJob, &QKeychain::Job::finished, this, [creds, data, key, setJob, this] {
#ifdef Q_OS_LINUX
if (!qEnvironmentVariableIsSet("DBUS_SESSION_BUS_ADDRESS")) {
QEXPECT_FAIL("", "QKeychain might not use the plaintext fallback and fail if dbus is not present", Abort);
QCOMPARE(setJob->error(), QKeychain::NoError);
}
#endif
QCOMPARE(setJob->error(), QKeychain::NoError);
auto getJob = creds->get(key);
setFallbackEnabled(getJob->_job);
connect(getJob, &CredentialJob::finished, this, [getJob, data, creds, this] {
QCOMPARE(getJob->error(), QKeychain::NoError);
QCOMPARE(getJob->data(), data);
const auto jobs = creds->clear();
for (auto &job : jobs) {
setFallbackEnabled(job);
}
connect(jobs[0], &QKeychain::Job::finished, this, [creds, data, jobs, this] {
QCOMPARE(jobs[0]->error(), QKeychain::NoError);
QVERIFY(creds->knownKeys().isEmpty());
_finished = true;
});
});
});
#ifdef Q_OS_LINUX
// As we have the skip condition on linux the wait might time out here.
bool ok = QTest::qWaitFor([this] { return _finished; });
Q_UNUSED(ok)
#else
QVERIFY(QTest::qWaitFor([this] { return _finished; }));
#endif
}
void testSetGet2()
{
FakeFolder fakeFolder { FileInfo::A12_B12_C12_S12() };
auto creds = fakeFolder.account()->credentialManager();
const QVariantMap data {
{ QStringLiteral("foo/test"), QColor(Qt::red) },
{ QStringLiteral("foo/test2"), QColor(Qt::gray) },
{ QStringLiteral("bar/test"), QColor(Qt::blue) },
{ QStringLiteral("narf/test"), QColor(Qt::green) }
};
QVector<QSignalSpy *> spies;
for (auto it = data.cbegin(); it != data.cend(); ++it) {
auto setJob = creds->set(it.key(), it.value());
setFallbackEnabled(setJob);
spies.append(new QSignalSpy(setJob, &QKeychain::Job::finished));
}
QTest::qWait(1000);
for (const auto s : spies) {
QCOMPARE(s->count(), 1);
}
qDeleteAll(spies);
spies.clear();
{
auto jobs = creds->clear(QStringLiteral("foo"));
#ifdef Q_OS_LINUX
if (!qEnvironmentVariableIsSet("DBUS_SESSION_BUS_ADDRESS")) {
QEXPECT_FAIL("", "QKeychain might not use the plaintext fallback and fail if dbus is not present", Abort);
QCOMPARE(jobs.size(), 2);
}
#endif
QCOMPARE(jobs.size(), 2);
for (auto &job : jobs) {
setFallbackEnabled(job);
spies.append(new QSignalSpy(job, &QKeychain::Job::finished));
}
QTest::qWait(1000);
for (const auto s : spies) {
QCOMPARE(s->count(), 1);
}
qDeleteAll(spies);
}
}
};
}
QTEST_GUILESS_MAIN(OCC::TestCredentialManager)
#include "testcredentialmanager.moc"
|