2023-11-27 20:47:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Oleksandr Zolotov <alex@nextcloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
#include "account.h"
|
|
|
|
#include "accountstate.h"
|
2023-11-30 19:34:09 +03:00
|
|
|
#include "clientstatusreportingcommon.h"
|
|
|
|
#include "clientstatusreportingdatabase.h"
|
|
|
|
#include "clientstatusreportingnetwork.h"
|
2023-11-27 20:47:17 +03:00
|
|
|
#include "syncenginetestutils.h"
|
|
|
|
|
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTest>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static QByteArray fake200Response = R"({"ocs":{"meta":{"status":"success","statuscode":200},"data":[]}})";
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestClientStatusReporting : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
TestClientStatusReporting() = default;
|
|
|
|
|
|
|
|
QScopedPointer<FakeQNAM> fakeQnam;
|
|
|
|
OCC::Account *account;
|
|
|
|
QScopedPointer<OCC::AccountState> accountState;
|
|
|
|
QString dbFilePath;
|
|
|
|
QVariantMap bodyReceivedAndParsed;
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void initTestCase()
|
|
|
|
{
|
2023-11-30 19:34:09 +03:00
|
|
|
OCC::ClientStatusReportingNetwork::clientStatusReportingTrySendTimerInterval = 1000;
|
|
|
|
OCC::ClientStatusReportingNetwork::repordSendIntervalMs = 2000;
|
2023-11-27 20:47:17 +03:00
|
|
|
|
|
|
|
fakeQnam.reset(new FakeQNAM({}));
|
|
|
|
account = OCC::Account::create().get();
|
|
|
|
account->setCredentials(new FakeCredentials{fakeQnam.data()});
|
|
|
|
account->setUrl(QUrl(("http://example.de")));
|
|
|
|
|
|
|
|
accountState.reset(new OCC::AccountState(account->sharedFromThis()));
|
|
|
|
|
|
|
|
const auto databaseId = QStringLiteral("%1@%2").arg(account->davUser(), account->url().toString());
|
|
|
|
const auto databaseIdHash = QCryptographicHash::hash(databaseId.toUtf8(), QCryptographicHash::Md5);
|
|
|
|
dbFilePath = QDir::tempPath() + QStringLiteral("/.tests_userdata_%1.db").arg(QString::fromLatin1(databaseIdHash.left(6).toHex()));
|
|
|
|
QFile(dbFilePath).remove();
|
2023-11-30 19:34:09 +03:00
|
|
|
OCC::ClientStatusReportingDatabase::dbPathForTesting = dbFilePath;
|
2023-11-27 20:47:17 +03:00
|
|
|
|
|
|
|
QVariantMap capabilities;
|
|
|
|
capabilities[QStringLiteral("security_guard")] = QVariantMap{
|
|
|
|
{ QStringLiteral("diagnostics"), true }
|
|
|
|
};
|
|
|
|
account->setCapabilities(capabilities);
|
|
|
|
|
|
|
|
fakeQnam->setOverride([this](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *device) {
|
|
|
|
QNetworkReply *reply = nullptr;
|
|
|
|
const auto reqBody = device->readAll();
|
|
|
|
bodyReceivedAndParsed = QJsonDocument::fromJson(reqBody).toVariant().toMap();
|
|
|
|
reply = new FakePayloadReply(op, req, fake200Response, fakeQnam.data());
|
|
|
|
return reply;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void testReportAndSendStatuses()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2024-02-19 22:37:34 +03:00
|
|
|
// 2 conflicts
|
2023-11-30 19:34:09 +03:00
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::DownloadError_ConflictInvalidCharacters);
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::DownloadError_ConflictCaseClash);
|
2023-11-27 20:47:17 +03:00
|
|
|
|
2024-02-19 22:37:34 +03:00
|
|
|
// 3 problems
|
2023-11-30 19:34:09 +03:00
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::UploadError_ServerError);
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::DownloadError_ServerError);
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::DownloadError_Virtual_File_Hydration_Failure);
|
|
|
|
// 3 occurances of case ClientStatusReportingStatus::UploadError_No_Write_Permissions
|
2024-02-19 22:37:34 +03:00
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::DownloadError_Virtual_File_Hydration_Failure);
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::DownloadError_Virtual_File_Hydration_Failure);
|
|
|
|
|
|
|
|
// 2 occurances of E2EeError_GeneralError
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::E2EeError_GeneralError);
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::E2EeError_GeneralError);
|
2023-11-30 19:34:09 +03:00
|
|
|
|
|
|
|
// 3 occurances of case ClientStatusReportingStatus::UploadError_Virus_Detected
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::UploadError_Virus_Detected);
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::UploadError_Virus_Detected);
|
|
|
|
account->reportClientStatus(OCC::ClientStatusReportingStatus::UploadError_Virus_Detected);
|
2023-11-30 16:25:33 +03:00
|
|
|
|
2023-11-30 19:34:09 +03:00
|
|
|
QTest::qWait(OCC::ClientStatusReportingNetwork::clientStatusReportingTrySendTimerInterval + OCC::ClientStatusReportingNetwork::repordSendIntervalMs);
|
2023-11-27 20:47:17 +03:00
|
|
|
|
|
|
|
QVERIFY(!bodyReceivedAndParsed.isEmpty());
|
|
|
|
|
2024-02-19 22:37:34 +03:00
|
|
|
// we must have 3 virus_detected category statuses
|
2023-11-30 16:25:33 +03:00
|
|
|
const auto virusDetectedErrorsReceived = bodyReceivedAndParsed.value("virus_detected").toMap();
|
|
|
|
QVERIFY(!virusDetectedErrorsReceived.isEmpty());
|
2023-11-30 19:34:09 +03:00
|
|
|
QCOMPARE(virusDetectedErrorsReceived.value("count"), 3);
|
2023-11-30 16:25:33 +03:00
|
|
|
|
|
|
|
// we must have 2 e2ee errors
|
2024-01-17 17:27:55 +03:00
|
|
|
const auto e2eeErrorsReceived = bodyReceivedAndParsed.value("e2ee_errors").toMap();
|
2023-11-30 16:25:33 +03:00
|
|
|
QVERIFY(!e2eeErrorsReceived.isEmpty());
|
2023-11-30 19:34:09 +03:00
|
|
|
QCOMPARE(e2eeErrorsReceived.value("count"), 2);
|
2023-11-27 20:47:17 +03:00
|
|
|
|
2024-02-19 22:37:34 +03:00
|
|
|
// we must have 2 conflicts
|
2023-11-27 20:47:17 +03:00
|
|
|
const auto conflictsReceived = bodyReceivedAndParsed.value("sync_conflicts").toMap();
|
|
|
|
QVERIFY(!conflictsReceived.isEmpty());
|
2024-02-19 22:37:34 +03:00
|
|
|
QCOMPARE(conflictsReceived.value("count"), 2);
|
2023-11-27 20:47:17 +03:00
|
|
|
|
2024-02-19 22:37:34 +03:00
|
|
|
// we must have 3 problems
|
2023-11-27 20:47:17 +03:00
|
|
|
const auto problemsReceived = bodyReceivedAndParsed.value("problems").toMap();
|
|
|
|
QVERIFY(!problemsReceived.isEmpty());
|
2024-02-19 22:37:34 +03:00
|
|
|
QCOMPARE(problemsReceived.size(), 3);
|
|
|
|
const auto specificProblemMultipleOccurances = problemsReceived.value(OCC::clientStatusstatusStringFromNumber(OCC::ClientStatusReportingStatus::DownloadError_Virtual_File_Hydration_Failure)).toMap();
|
|
|
|
// among those, 3 occurances of specific problem
|
|
|
|
QCOMPARE(specificProblemMultipleOccurances.value("count"), 3);
|
2023-11-27 20:47:17 +03:00
|
|
|
|
|
|
|
bodyReceivedAndParsed.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNothingReportedAndNothingSent()
|
|
|
|
{
|
2023-11-30 19:34:09 +03:00
|
|
|
QTest::qWait(OCC::ClientStatusReportingNetwork::clientStatusReportingTrySendTimerInterval + OCC::ClientStatusReportingNetwork::repordSendIntervalMs);
|
2023-11-27 20:47:17 +03:00
|
|
|
QVERIFY(bodyReceivedAndParsed.isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanupTestCase()
|
|
|
|
{
|
|
|
|
accountState.reset(nullptr);
|
|
|
|
delete account;
|
|
|
|
QFile(dbFilePath).remove();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_MAIN(TestClientStatusReporting)
|
|
|
|
#include "testclientstatusreporting.moc"
|