data/CredentialStore.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "CredentialStore.h" | ||
| 2 | |||
| 3 | #include <QCoreApplication> | ||
| 4 | #include <QThread> | ||
| 5 | #include <QHash> | ||
| 6 | #include <QLoggingCategory> | ||
| 7 | #include <QPointer> | ||
| 8 | #include <QTimer> | ||
| 9 | #include <memory> | ||
| 10 | #include <qt6keychain/keychain.h> | ||
| 11 | |||
| 12 |
4/7✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 8 taken 1 time.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 1 time.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
|
2 | Q_LOGGING_CATEGORY(lcCredentialStore, "mailjd.credentials") |
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | #ifdef MAILJD_TEST_KEYRING | ||
| 17 | // Test-only seam: MAILJD_FAKE_KEYRING=1 replaces QtKeychain with an | ||
| 18 | // in-process store. The entire implementation is omitted from product | ||
| 19 | // targets, rather than merely guarded by a runtime environment variable. | ||
| 20 | 529 | QHash<QString, QByteArray> &fakeKeyring() { | |
| 21 |
3/4✓ Branch 2 → 3 taken 7 times.
✓ Branch 2 → 7 taken 522 times.
✓ Branch 4 → 5 taken 7 times.
✗ Branch 4 → 7 not taken.
|
529 | static QHash<QString, QByteArray> store; |
| 22 | 529 | return store; | |
| 23 | } | ||
| 24 | 365 | bool useFakeKeyring() { | |
| 25 | 365 | return qEnvironmentVariableIsSet("MAILJD_FAKE_KEYRING"); | |
| 26 | } | ||
| 27 | #else | ||
| 28 | 23 | void warnIfTestKeyringRequested() { | |
| 29 | static bool warned = false; | ||
| 30 |
3/6✓ Branch 2 → 3 taken 23 times.
✗ Branch 2 → 8 not taken.
✓ Branch 4 → 5 taken 23 times.
✗ Branch 4 → 7 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 21 taken 23 times.
|
69 | if (!warned && |
| 31 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 23 times.
|
46 | (qEnvironmentVariableIsSet("MAILJD_FAKE_KEYRING") || |
| 32 | 23 | qEnvironmentVariableIsSet("MAILJD_FAKE_KEYRING_FAIL_WRITES"))) { | |
| 33 | ✗ | warned = true; | |
| 34 | ✗ | qCCritical(lcCredentialStore) | |
| 35 | ✗ | << "Ignoring test-keyring environment in a production build"; | |
| 36 | } | ||
| 37 | 23 | } | |
| 38 | #endif | ||
| 39 | |||
| 40 | // QKeychain jobs share a process-wide executor that belongs to the GUI | ||
| 41 | // thread; a job started from another thread crashed the process (settings | ||
| 42 | // sync worker, 2026-09-29). Refuse instead of crashing. | ||
| 43 | 390 | bool keyringThreadAllowed(const char *operation) { | |
| 44 | 390 | auto *application = QCoreApplication::instance(); | |
| 45 |
5/6✓ Branch 3 → 4 taken 390 times.
✗ Branch 3 → 7 not taken.
✓ Branch 6 → 7 taken 388 times.
✓ Branch 6 → 8 taken 2 times.
✓ Branch 9 → 10 taken 388 times.
✓ Branch 9 → 11 taken 2 times.
|
390 | if (!application || QThread::currentThread() == application->thread()) return true; |
| 46 |
7/7✓ Branch 11 → 12 taken 2 times.
✓ Branch 15 → 16 taken 2 times.
✓ Branch 16 → 17 taken 2 times.
✓ Branch 17 → 18 taken 2 times.
✓ Branch 18 → 19 taken 2 times.
✓ Branch 22 → 13 taken 2 times.
✓ Branch 22 → 23 taken 2 times.
|
4 | qCCritical(lcCredentialStore) << "Keyring" << operation << "refused off the GUI thread"; |
| 47 | 2 | return false; | |
| 48 | } | ||
| 49 | } // namespace | ||
| 50 | |||
| 51 | 303 | CredentialStore::CredentialStore(QObject *parent) : QObject(parent) {} | |
| 52 | |||
| 53 | 26 | QObject *CredentialStore::deletePasswordsAsync(QObject *owner, | |
| 54 | const QList<SecretKey> &keys, | ||
| 55 | DeleteCallback callback) { | ||
| 56 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 26 times.
|
26 | if (!owner) return nullptr; |
| 57 |
3/6✓ Branch 4 → 5 taken 26 times.
✓ Branch 5 → 6 taken 26 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 26 times.
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 38 not taken.
|
26 | auto *operation = new CredentialStore(owner); |
| 58 |
3/6✓ Branch 8 → 9 taken 26 times.
✓ Branch 9 → 10 taken 26 times.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 26 times.
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 41 not taken.
|
26 | auto *deadline = new QTimer(operation); |
| 59 |
1/1✓ Branch 12 → 13 taken 26 times.
|
26 | deadline->setSingleShot(true); |
| 60 | struct State { qsizetype pending = 0; bool success = true; bool finished = false; }; | ||
| 61 |
1/1✓ Branch 13 → 14 taken 26 times.
|
26 | auto state = std::make_shared<State>(); |
| 62 | 26 | state->pending = keys.size(); | |
| 63 |
1/1✓ Branch 16 → 17 taken 26 times.
|
26 | QPointer<CredentialStore> guard(operation); |
| 64 | 20 | const auto finish = [guard, deadline, state, callback](bool success) { | |
| 65 |
3/6✓ Branch 3 → 4 taken 20 times.
✗ Branch 3 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 20 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 20 times.
|
20 | if (!guard || state->finished) return; |
| 66 | 20 | state->finished = true; | |
| 67 | 20 | deadline->stop(); | |
| 68 | 20 | guard->deleteLater(); | |
| 69 |
2/2✓ Branch 15 → 16 taken 13 times.
✓ Branch 15 → 17 taken 7 times.
|
20 | if (callback) callback(success); |
| 70 |
1/5✓ Branch 19 → 20 taken 26 times.
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 47 not taken.
|
26 | }; |
| 71 |
2/2✓ Branch 20 → 21 taken 26 times.
✓ Branch 21 → 22 taken 26 times.
|
31 | connect(deadline, &QTimer::timeout, operation, [finish] { finish(false); }); |
| 72 |
1/1✓ Branch 24 → 25 taken 26 times.
|
26 | deadline->start(30000); |
| 73 | // Schedule the complete batch so neither an empty list nor an immediately | ||
| 74 | // available keyring can call back into the account-removal stack. | ||
| 75 |
2/8✓ Branch 28 → 29 taken 26 times.
✓ Branch 29 → 30 taken 26 times.
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 56 not taken.
✗ Branch 57 → 58 not taken.
✗ Branch 57 → 59 not taken.
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 62 not taken.
|
26 | QTimer::singleShot(0, operation, [guard, keys, state, finish] { |
| 76 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 21 times.
|
22 | if (keys.isEmpty()) { finish(true); return; } |
| 77 |
2/2✓ Branch 19 → 8 taken 47 times.
✓ Branch 19 → 20 taken 21 times.
|
68 | for (const auto &key : keys) { |
| 78 |
1/1✓ Branch 14 → 15 taken 47 times.
|
94 | guard->deletePassword(key.service, key.accountName, |
| 79 |
2/6✓ Branch 12 → 13 taken 47 times.
✓ Branch 13 → 14 taken 47 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
|
94 | [guard, state, finish](bool success) { |
| 80 |
3/6✓ Branch 3 → 4 taken 32 times.
✗ Branch 3 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 32 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 32 times.
|
32 | if (!guard || state->finished) return; |
| 81 |
2/4✓ Branch 11 → 12 taken 32 times.
✗ Branch 11 → 14 not taken.
✓ Branch 12 → 13 taken 32 times.
✗ Branch 12 → 14 not taken.
|
32 | state->success = state->success && success; |
| 82 |
2/2✓ Branch 17 → 18 taken 14 times.
✓ Branch 17 → 20 taken 18 times.
|
32 | if (--state->pending == 0) finish(state->success); |
| 83 | }, true); | ||
| 84 | } | ||
| 85 | }); | ||
| 86 | 26 | return operation; | |
| 87 | 26 | } | |
| 88 | |||
| 89 | 389 | QString CredentialStore::keyringKey(const QString &service, | |
| 90 | const QString &accountName) { | ||
| 91 |
1/1✓ Branch 5 → 6 taken 389 times.
|
389 | return QStringLiteral("mailjd/%1/%2").arg(service, accountName); |
| 92 | } | ||
| 93 | |||
| 94 | 206 | void CredentialStore::readPassword(const QString &service, | |
| 95 | const QString &accountName, | ||
| 96 | ReadCallback callback) { | ||
| 97 |
3/3✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 8 taken 204 times.
✓ Branch 5 → 6 taken 2 times.
|
206 | if (!keyringThreadAllowed("read")) { callback(false, {}); return; } |
| 98 | #ifdef MAILJD_TEST_KEYRING | ||
| 99 |
1/2✓ Branch 9 → 10 taken 197 times.
✗ Branch 9 → 17 not taken.
|
197 | if (useFakeKeyring()) { |
| 100 |
1/1✓ Branch 10 → 11 taken 197 times.
|
197 | const QString key = keyringKey(service, accountName); |
| 101 | // Deliver asynchronously to preserve the QtKeychain calling contract. | ||
| 102 |
2/4✓ Branch 11 → 12 taken 197 times.
✓ Branch 13 → 14 taken 197 times.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
|
197 | QTimer::singleShot(0, this, [callback, key]() { |
| 103 | 184 | const bool found = fakeKeyring().contains(key); | |
| 104 |
1/1✓ Branch 6 → 7 taken 184 times.
|
184 | callback(found, fakeKeyring().value(key)); |
| 105 | 184 | }); | |
| 106 | 197 | return; | |
| 107 | 197 | } | |
| 108 | #else | ||
| 109 | 7 | warnIfTestKeyringRequested(); | |
| 110 | #endif | ||
| 111 | auto *job = new QKeychain::ReadPasswordJob( | ||
| 112 |
2/10✓ Branch 13 → 14 taken 7 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 7 times.
✗ Branch 21 → 22 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 41 not taken.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
|
14 | QStringLiteral("mailjd"), this); |
| 113 | 7 | job->setAutoDelete(true); | |
| 114 |
2/4✓ Branch 19 → 20 taken 7 times.
✓ Branch 20 → 21 taken 7 times.
✗ Branch 27 → 28 not taken.
✗ Branch 28 → 29 not taken.
|
7 | job->setKey(keyringKey(service, accountName)); |
| 115 | |||
| 116 | 7 | connect(job, &QKeychain::ReadPasswordJob::finished, this, | |
| 117 |
2/12✓ Branch 22 → 23 taken 7 times.
✓ Branch 25 → 26 taken 7 times.
✗ Branch 30 → 31 not taken.
✗ Branch 33 → 34 not taken.
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 49 not taken.
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 52 not taken.
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 66 not taken.
✗ Branch 67 → 68 not taken.
✗ Branch 67 → 69 not taken.
|
14 | [callback, service, accountName](QKeychain::Job *j) { |
| 118 | ✗ | auto *readJob = qobject_cast<QKeychain::ReadPasswordJob *>(j); | |
| 119 | ✗ | if (readJob && readJob->error() == QKeychain::NoError) { | |
| 120 | // QKeychain stores as QString — convert to QByteArray | ||
| 121 | ✗ | QByteArray pw = readJob->textData().toUtf8(); | |
| 122 | ✗ | qCDebug(lcCredentialStore) // GCOVR_EXCL_BR_LINE — Qt logging-macro cleanup only; callback generation remains measured. | |
| 123 | ✗ | << "Read password for" << service << accountName; | |
| 124 | ✗ | callback(true, pw); | |
| 125 | ✗ | } else { | |
| 126 | ✗ | QString errMsg = readJob ? readJob->errorString() | |
| 127 | ✗ | : QStringLiteral("Unknown error"); | |
| 128 | ✗ | qCWarning(lcCredentialStore) | |
| 129 | ✗ | << "Failed to read password for" << service << accountName | |
| 130 | ✗ | << ":" << errMsg; | |
| 131 | ✗ | callback(false, QByteArray()); | |
| 132 | ✗ | } | |
| 133 | ✗ | }); | |
| 134 | |||
| 135 | 7 | job->start(); | |
| 136 | } | ||
| 137 | |||
| 138 | 119 | void CredentialStore::writePassword(const QString &service, | |
| 139 | const QString &accountName, | ||
| 140 | const QByteArray &password, | ||
| 141 | WriteCallback callback) { | ||
| 142 |
1/3✗ Branch 3 → 4 not taken.
✓ Branch 3 → 11 taken 119 times.
✗ Branch 7 → 8 not taken.
|
119 | if (!keyringThreadAllowed("write")) { callback(false, QStringLiteral("Keyring access off the GUI thread")); return; } |
| 143 | #ifdef MAILJD_TEST_KEYRING | ||
| 144 |
1/2✓ Branch 12 → 13 taken 116 times.
✗ Branch 12 → 21 not taken.
|
116 | if (useFakeKeyring()) { |
| 145 |
1/1✓ Branch 13 → 14 taken 116 times.
|
116 | const QString key = keyringKey(service, accountName); |
| 146 |
2/6✓ Branch 14 → 15 taken 116 times.
✓ Branch 17 → 18 taken 116 times.
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 55 not taken.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
|
116 | QTimer::singleShot(0, this, [callback, key, password]() { |
| 147 |
2/2✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 11 taken 111 times.
|
116 | if (qEnvironmentVariableIsSet("MAILJD_FAKE_KEYRING_FAIL_WRITES")) { |
| 148 |
1/1✓ Branch 7 → 8 taken 5 times.
|
10 | callback(false, |
| 149 | 10 | QStringLiteral("Injected fake keyring write failure " | |
| 150 | "[MAILJD_COMPILED_TEST_KEYRING]")); | ||
| 151 | 5 | return; | |
| 152 | } | ||
| 153 | 111 | fakeKeyring().insert(key, password); | |
| 154 |
1/1✓ Branch 14 → 15 taken 111 times.
|
111 | callback(true, QString()); |
| 155 | }); | ||
| 156 | 116 | return; | |
| 157 | 116 | } | |
| 158 | #else | ||
| 159 | 3 | warnIfTestKeyringRequested(); | |
| 160 | #endif | ||
| 161 | auto *job = new QKeychain::WritePasswordJob( | ||
| 162 |
2/10✓ Branch 16 → 17 taken 3 times.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 3 times.
✗ Branch 25 → 26 not taken.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ Branch 69 → 70 not taken.
✗ Branch 69 → 71 not taken.
|
6 | QStringLiteral("mailjd"), this); |
| 163 | 3 | job->setAutoDelete(true); | |
| 164 |
2/4✓ Branch 22 → 23 taken 3 times.
✓ Branch 23 → 24 taken 3 times.
✗ Branch 31 → 32 not taken.
✗ Branch 32 → 33 not taken.
|
3 | job->setKey(keyringKey(service, accountName)); |
| 165 |
2/4✓ Branch 25 → 26 taken 3 times.
✓ Branch 26 → 27 taken 3 times.
✗ Branch 34 → 35 not taken.
✗ Branch 35 → 36 not taken.
|
3 | job->setTextData(QString::fromUtf8(password)); |
| 166 | |||
| 167 | 3 | connect(job, &QKeychain::WritePasswordJob::finished, this, | |
| 168 |
2/12✓ Branch 28 → 29 taken 3 times.
✓ Branch 31 → 32 taken 3 times.
✗ Branch 37 → 38 not taken.
✗ Branch 40 → 41 not taken.
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 61 not taken.
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
✗ Branch 80 → 81 not taken.
✗ Branch 80 → 82 not taken.
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 85 not taken.
|
6 | [callback, service, accountName](QKeychain::Job *j) { |
| 169 | ✗ | if (j->error() == QKeychain::NoError) { | |
| 170 | ✗ | qCInfo(lcCredentialStore) // GCOVR_EXCL_BR_LINE — Qt logging-macro cleanup only; callback outcome remains measured. | |
| 171 | ✗ | << "Stored password for" << service << accountName; | |
| 172 | ✗ | callback(true, QString()); | |
| 173 | } else { | ||
| 174 | ✗ | qCWarning(lcCredentialStore) | |
| 175 | ✗ | << "Failed to write password for" << service << accountName | |
| 176 | ✗ | << ":" << j->errorString(); | |
| 177 | ✗ | callback(false, j->errorString()); | |
| 178 | } | ||
| 179 | ✗ | }); | |
| 180 | |||
| 181 | 3 | job->start(); | |
| 182 | } | ||
| 183 | |||
| 184 | 65 | void CredentialStore::deletePassword(const QString &service, | |
| 185 | const QString &accountName, | ||
| 186 | DeleteCallback callback, bool missingIsSuccess) { | ||
| 187 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 65 times.
|
65 | if (!keyringThreadAllowed("delete")) { callback(false); return; } |
| 188 | #ifdef MAILJD_TEST_KEYRING | ||
| 189 |
1/2✓ Branch 7 → 8 taken 52 times.
✗ Branch 7 → 15 not taken.
|
52 | if (useFakeKeyring()) { |
| 190 |
1/1✓ Branch 8 → 9 taken 52 times.
|
52 | const QString key = keyringKey(service, accountName); |
| 191 |
2/6✓ Branch 9 → 10 taken 52 times.
✓ Branch 11 → 12 taken 52 times.
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 40 not taken.
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
|
52 | QTimer::singleShot(0, this, [callback, key, missingIsSuccess]() { |
| 192 | 50 | const bool removed = fakeKeyring().remove(key) > 0; | |
| 193 |
4/4✓ Branch 4 → 5 taken 24 times.
✓ Branch 4 → 6 taken 26 times.
✓ Branch 5 → 6 taken 13 times.
✓ Branch 5 → 7 taken 11 times.
|
50 | callback(removed || missingIsSuccess); |
| 194 | 50 | }); | |
| 195 | 52 | return; | |
| 196 | 52 | } | |
| 197 | #else | ||
| 198 | 13 | warnIfTestKeyringRequested(); | |
| 199 | #endif | ||
| 200 | auto *job = new QKeychain::DeletePasswordJob( | ||
| 201 |
2/10✓ Branch 11 → 12 taken 13 times.
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 13 times.
✗ Branch 19 → 20 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 36 not taken.
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 56 not taken.
|
26 | QStringLiteral("mailjd"), this); |
| 202 | 13 | job->setAutoDelete(true); | |
| 203 |
2/4✓ Branch 17 → 18 taken 13 times.
✓ Branch 18 → 19 taken 13 times.
✗ Branch 25 → 26 not taken.
✗ Branch 26 → 27 not taken.
|
13 | job->setKey(keyringKey(service, accountName)); |
| 204 | |||
| 205 | 13 | connect(job, &QKeychain::DeletePasswordJob::finished, this, | |
| 206 |
2/16✓ Branch 20 → 21 taken 13 times.
✓ Branch 23 → 24 taken 13 times.
✗ Branch 28 → 29 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 47 not taken.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 67 not taken.
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 70 not taken.
|
26 | [callback, service, accountName, missingIsSuccess](QKeychain::Job *j) { |
| 207 | ✗ | if (j->error() == QKeychain::NoError || | |
| 208 | ✗ | (missingIsSuccess && j->error() == QKeychain::EntryNotFound)) { | |
| 209 | ✗ | qCInfo(lcCredentialStore) // GCOVR_EXCL_BR_LINE — Qt logging-macro cleanup only; callback outcome remains measured. | |
| 210 | ✗ | << "Removed password for" << service << accountName; | |
| 211 | ✗ | callback(true); | |
| 212 | } else { | ||
| 213 | ✗ | qCWarning(lcCredentialStore) | |
| 214 | ✗ | << "Failed to delete password for" << service << accountName | |
| 215 | ✗ | << ":" << j->errorString(); | |
| 216 | ✗ | callback(false); | |
| 217 | } | ||
| 218 | ✗ | }); | |
| 219 | |||
| 220 | 13 | job->start(); | |
| 221 | } | ||
| 222 |