presentation/DavConnectionTest.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "DavConnectionTest.h" | ||
| 2 | |||
| 3 | #include "app/core/CalendarSyncStatus.h" | ||
| 4 | #include "service/CalDavClient.h" | ||
| 5 | #include "util/SecureUtil.h" | ||
| 6 | |||
| 7 | #include <QLoggingCategory> | ||
| 8 | #include <QTimer> | ||
| 9 | #include <QUrl> | ||
| 10 | |||
| 11 |
4/7✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 8 taken 6 times.
✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 2 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
|
8 | Q_LOGGING_CATEGORY(lcDavTest, "mailjd.dav.test") |
| 12 | |||
| 13 | 1598 | DavConnectionTest::DavConnectionTest(QObject *parent) : QObject(parent) { | |
| 14 | 800 | m_clientFactory = [](const QString &serverUrl, const QString &username, const QByteArray &password) { | |
| 15 |
3/6✓ Branch 3 → 4 taken 1 time.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
|
2 | return new CalDavClient(serverUrl, username, QString::fromUtf8(password)); |
| 16 | 799 | }; | |
| 17 | 799 | m_passwordReader = [](QObject *owner, const QString &accountId, const QString &serverUrl, | |
| 18 | const QString &username, DavCredentials::PasswordCallback callback) { | ||
| 19 | ✗ | DavCredentials::readPasswordAsync(owner, accountId, serverUrl, username, std::move(callback)); | |
| 20 | 799 | }; | |
| 21 | 799 | } | |
| 22 | |||
| 23 | 799 | DavConnectionTest::~DavConnectionTest() { | |
| 24 | 799 | ++m_generation; | |
| 25 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 799 times.
|
799 | delete m_run.data(); |
| 26 | 799 | } | |
| 27 | |||
| 28 | 7 | void DavConnectionTest::cancel() { | |
| 29 | 7 | ++m_generation; | |
| 30 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 7 times.
|
7 | if (m_run) m_run->deleteLater(); |
| 31 | 7 | m_run = nullptr; | |
| 32 |
1/2✗ Branch 9 → 10 not taken.
✓ Branch 9 → 18 taken 7 times.
|
7 | if (m_state == QLatin1String("testing")) { |
| 33 | ✗ | m_state = QStringLiteral("idle"); | |
| 34 | ✗ | m_message.clear(); | |
| 35 | ✗ | emit stateChanged(); | |
| 36 | } | ||
| 37 | 7 | } | |
| 38 | |||
| 39 | 7 | void DavConnectionTest::finish(const QString &state, const QString &message) { | |
| 40 | 7 | ++m_generation; | |
| 41 |
2/2✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 6 taken 1 time.
|
7 | if (m_run) m_run->deleteLater(); |
| 42 | 7 | m_run = nullptr; | |
| 43 | 7 | m_state = state; | |
| 44 | 7 | m_message = message; | |
| 45 |
8/8✓ Branch 9 → 10 taken 7 times.
✓ Branch 13 → 14 taken 7 times.
✓ Branch 14 → 15 taken 7 times.
✓ Branch 15 → 16 taken 7 times.
✓ Branch 16 → 17 taken 7 times.
✓ Branch 17 → 18 taken 7 times.
✓ Branch 21 → 11 taken 7 times.
✓ Branch 21 → 22 taken 7 times.
|
14 | qCInfo(lcDavTest) << "DAV connection test" << state << "account" << m_accountId; |
| 46 | 7 | emit stateChanged(); | |
| 47 | 7 | } | |
| 48 | |||
| 49 | 7 | void DavConnectionTest::start(const QVariantMap &fields) { | |
| 50 |
1/1✓ Branch 2 → 3 taken 7 times.
|
7 | cancel(); |
| 51 |
3/3✓ Branch 7 → 8 taken 7 times.
✓ Branch 8 → 9 taken 7 times.
✓ Branch 9 → 10 taken 7 times.
|
14 | m_accountId = fields.value(QStringLiteral("id")).toString().trimmed(); |
| 52 |
3/3✓ Branch 21 → 22 taken 7 times.
✓ Branch 22 → 23 taken 7 times.
✓ Branch 23 → 24 taken 7 times.
|
14 | const QString serverUrl = fields.value(QStringLiteral("serverUrl")).toString().trimmed(); |
| 53 |
3/3✓ Branch 33 → 34 taken 7 times.
✓ Branch 34 → 35 taken 7 times.
✓ Branch 35 → 36 taken 7 times.
|
14 | const QString username = fields.value(QStringLiteral("username")).toString().trimmed(); |
| 54 |
3/3✓ Branch 45 → 46 taken 7 times.
✓ Branch 46 → 47 taken 7 times.
✓ Branch 47 → 48 taken 7 times.
|
14 | QByteArray password = fields.value(QStringLiteral("password")).toString().toUtf8(); |
| 55 |
1/1✓ Branch 53 → 54 taken 7 times.
|
7 | const QUrl url(serverUrl); |
| 56 |
7/10✓ Branch 54 → 55 taken 7 times.
✓ Branch 55 → 56 taken 7 times.
✗ Branch 55 → 68 not taken.
✓ Branch 57 → 58 taken 7 times.
✓ Branch 59 → 60 taken 6 times.
✓ Branch 59 → 68 taken 1 time.
✓ Branch 76 → 77 taken 1 time.
✓ Branch 76 → 86 taken 6 times.
✗ Branch 237 → 238 not taken.
✗ Branch 237 → 239 not taken.
|
27 | if (!url.isValid() || url.host().isEmpty() || |
| 57 |
10/16✓ Branch 61 → 62 taken 6 times.
✓ Branch 63 → 64 taken 1 time.
✓ Branch 63 → 69 taken 5 times.
✓ Branch 65 → 66 taken 1 time.
✗ Branch 67 → 68 not taken.
✓ Branch 67 → 69 taken 1 time.
✓ Branch 70 → 71 taken 1 time.
✓ Branch 70 → 72 taken 6 times.
✓ Branch 72 → 73 taken 6 times.
✓ Branch 72 → 74 taken 1 time.
✓ Branch 74 → 75 taken 7 times.
✗ Branch 74 → 76 not taken.
✗ Branch 227 → 228 not taken.
✗ Branch 227 → 229 not taken.
✗ Branch 232 → 233 not taken.
✗ Branch 232 → 234 not taken.
|
13 | (url.scheme() != QLatin1String("https") && url.scheme() != QLatin1String("http"))) { |
| 58 |
2/2✓ Branch 77 → 78 taken 1 time.
✓ Branch 81 → 82 taken 1 time.
|
2 | finish(QStringLiteral("failed"), tr("Enter the server address as https://…")); |
| 59 | 1 | return; | |
| 60 | } | ||
| 61 |
1/2✗ Branch 87 → 88 not taken.
✓ Branch 87 → 97 taken 6 times.
|
6 | if (username.isEmpty()) { |
| 62 | ✗ | finish(QStringLiteral("failed"), tr("Enter the user name.")); | |
| 63 | ✗ | return; | |
| 64 | } | ||
| 65 |
4/7✓ Branch 97 → 98 taken 6 times.
✓ Branch 98 → 99 taken 6 times.
✓ Branch 99 → 100 taken 6 times.
✗ Branch 100 → 101 not taken.
✓ Branch 100 → 102 taken 6 times.
✗ Branch 260 → 261 not taken.
✗ Branch 260 → 262 not taken.
|
6 | m_run = new QObject(this); |
| 66 | 6 | const quint64 generation = ++m_generation; | |
| 67 | 6 | m_state = QStringLiteral("testing"); | |
| 68 |
1/1✓ Branch 108 → 109 taken 6 times.
|
6 | m_message = tr("Testing the connection…"); |
| 69 |
1/1✓ Branch 111 → 112 taken 6 times.
|
6 | emit stateChanged(); |
| 70 |
1/1✓ Branch 112 → 113 taken 6 times.
|
6 | QPointer<DavConnectionTest> guard(this); |
| 71 |
1/3✓ Branch 115 → 116 taken 6 times.
✗ Branch 266 → 267 not taken.
✗ Branch 266 → 268 not taken.
|
6 | QTimer::singleShot(m_timeoutMs, m_run, [guard, generation] { |
| 72 | ✗ | if (guard && guard->current(generation)) | |
| 73 | ✗ | guard->finish(QStringLiteral("failed"), CalendarSyncStatus::messageForKind(QStringLiteral("timeout"))); | |
| 74 | ✗ | }); | |
| 75 |
2/2✓ Branch 118 → 119 taken 3 times.
✓ Branch 118 → 124 taken 3 times.
|
6 | if (!password.isEmpty()) { |
| 76 |
1/1✓ Branch 121 → 122 taken 3 times.
|
3 | probe(generation, serverUrl, username, std::move(password)); |
| 77 | 3 | return; | |
| 78 | } | ||
| 79 |
2/2✓ Branch 125 → 126 taken 1 time.
✓ Branch 125 → 135 taken 2 times.
|
3 | if (m_accountId.isEmpty()) { |
| 80 |
2/2✓ Branch 126 → 127 taken 1 time.
✓ Branch 130 → 131 taken 1 time.
|
2 | finish(QStringLiteral("failed"), tr("Enter the password.")); |
| 81 | 1 | return; | |
| 82 | } | ||
| 83 | // An empty field means "the stored password" (as when saving). | ||
| 84 |
1/1✓ Branch 140 → 141 taken 2 times.
|
2 | m_passwordReader(m_run, m_accountId, serverUrl, username, |
| 85 |
1/5✓ Branch 138 → 139 taken 2 times.
✗ Branch 286 → 287 not taken.
✗ Branch 286 → 288 not taken.
✗ Branch 289 → 290 not taken.
✗ Branch 289 → 291 not taken.
|
4 | [guard, generation, serverUrl, username](QByteArray stored, QString error) { |
| 86 |
3/6✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 7 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
|
2 | if (!guard || !guard->current(generation)) return; |
| 87 |
2/2✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 36 taken 1 time.
|
2 | if (stored.isEmpty()) { |
| 88 |
7/7✓ Branch 13 → 14 taken 1 time.
✓ Branch 17 → 18 taken 1 time.
✓ Branch 18 → 19 taken 1 time.
✓ Branch 19 → 20 taken 1 time.
✓ Branch 20 → 21 taken 1 time.
✓ Branch 25 → 15 taken 1 time.
✓ Branch 25 → 26 taken 1 time.
|
2 | qCWarning(lcDavTest) << "No stored DAV password:" << error.left(200); |
| 89 |
1/1✓ Branch 31 → 32 taken 1 time.
|
2 | guard->finish(QStringLiteral("failed"), |
| 90 |
1/1✓ Branch 27 → 28 taken 1 time.
|
2 | tr("No password is stored for this account. Enter it above, test again and save.")); |
| 91 | 1 | return; | |
| 92 | } | ||
| 93 |
1/1✓ Branch 39 → 40 taken 1 time.
|
1 | guard->probe(generation, serverUrl, username, std::move(stored)); |
| 94 | }); | ||
| 95 | 26 | } | |
| 96 | |||
| 97 | 4 | void DavConnectionTest::probe(quint64 generation, const QString &serverUrl, const QString &username, | |
| 98 | QByteArray password) { | ||
| 99 |
1/1✓ Branch 2 → 3 taken 4 times.
|
4 | CalDavClient *client = m_clientFactory(serverUrl, username, password); |
| 100 |
1/1✓ Branch 3 → 4 taken 4 times.
|
4 | SecureUtil::zeroMemory(password); |
| 101 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 14 taken 4 times.
|
4 | if (!client) { |
| 102 | ✗ | finish(QStringLiteral("failed"), tr("The connection test could not start.")); | |
| 103 | ✗ | return; | |
| 104 | } | ||
| 105 |
1/1✓ Branch 15 → 16 taken 4 times.
|
4 | client->setParent(m_run); |
| 106 |
1/1✓ Branch 16 → 17 taken 4 times.
|
4 | QPointer<DavConnectionTest> guard(this); |
| 107 |
1/1✓ Branch 19 → 20 taken 4 times.
|
4 | connect(client, &CalDavClient::calendarsDiscovered, m_run, |
| 108 |
0/2✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
|
8 | [guard, generation](const QList<CalendarInfo> &calendars) { |
| 109 |
3/6✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 7 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
|
2 | if (!guard || !guard->current(generation)) return; |
| 110 | 2 | const int count = int(calendars.size()); | |
| 111 |
1/1✓ Branch 19 → 20 taken 2 times.
|
4 | guard->finish(QStringLiteral("success"), |
| 112 |
2/4✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 2 times.
✗ Branch 14 → 16 not taken.
✓ Branch 15 → 16 taken 2 times.
|
4 | count == 1 ? tr("Connection works: 1 calendar found.") |
| 113 | : tr("Connection works: %n calendars found.", "", count)); | ||
| 114 | }); | ||
| 115 |
1/3✓ Branch 24 → 25 taken 4 times.
✗ Branch 49 → 50 not taken.
✗ Branch 49 → 51 not taken.
|
4 | connect(client, &CalDavClient::syncFailed, m_run, [guard, generation, client](const QString &error) { |
| 116 |
3/6✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 7 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
|
2 | if (!guard || !guard->current(generation)) return; |
| 117 |
1/1✓ Branch 12 → 13 taken 2 times.
|
2 | const QString kind = CalendarSyncStatus::kindForNetworkError(client->lastNetworkError()); |
| 118 |
2/2✓ Branch 17 → 18 taken 2 times.
✓ Branch 23 → 24 taken 2 times.
|
8 | guard->finish(QStringLiteral("failed"), CalendarSyncStatus::messageForKind(kind) + |
| 119 |
3/3✓ Branch 14 → 15 taken 2 times.
✓ Branch 18 → 19 taken 2 times.
✓ Branch 19 → 20 taken 2 times.
|
8 | QLatin1Char('\n') + error.left(300)); |
| 120 | 2 | }); | |
| 121 |
1/1✓ Branch 27 → 28 taken 4 times.
|
4 | client->discoverCalendars(); |
| 122 | 4 | } | |
| 123 |