2018-07-25 13:21:29 +03:00
|
|
|
/*
|
|
|
|
* 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 <QtTest>
|
|
|
|
#include "syncenginetestutils.h"
|
|
|
|
#include <syncengine.h>
|
|
|
|
#include <localdiscoverytracker.h>
|
|
|
|
|
|
|
|
using namespace OCC;
|
|
|
|
|
|
|
|
struct FakeBrokenXmlPropfindReply : FakePropfindReply {
|
|
|
|
FakeBrokenXmlPropfindReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op,
|
|
|
|
const QNetworkRequest &request, QObject *parent)
|
|
|
|
: FakePropfindReply(remoteRootFileInfo, op, request, parent) {
|
|
|
|
QVERIFY(payload.size() > 50);
|
|
|
|
// turncate the XML
|
|
|
|
payload.chop(20);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MissingPermissionsPropfindReply : FakePropfindReply {
|
|
|
|
MissingPermissionsPropfindReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op,
|
|
|
|
const QNetworkRequest &request, QObject *parent)
|
|
|
|
: FakePropfindReply(remoteRootFileInfo, op, request, parent) {
|
|
|
|
// If the propfind contains a single file without permissions, this is a server error
|
|
|
|
const char toRemove[] = "<oc:permissions>RDNVCKW</oc:permissions>";
|
|
|
|
auto pos = payload.indexOf(toRemove, payload.size()/2);
|
|
|
|
QVERIFY(pos > 0);
|
|
|
|
payload.remove(pos, sizeof(toRemove) - 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
enum ErrorKind : int {
|
|
|
|
// Lower code are corresponding to HTML error code
|
|
|
|
InvalidXML = 1000,
|
|
|
|
Timeout,
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(ErrorCategory)
|
|
|
|
|
|
|
|
class TestRemoteDiscovery : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
|
|
|
void testRemoteDiscoveryError_data()
|
|
|
|
{
|
|
|
|
qRegisterMetaType<ErrorCategory>();
|
|
|
|
QTest::addColumn<int>("errorKind");
|
|
|
|
QTest::addColumn<QString>("expectedErrorString");
|
2019-06-04 17:00:43 +03:00
|
|
|
QTest::addColumn<bool>("syncSucceeds");
|
2018-07-25 13:21:29 +03:00
|
|
|
|
2019-04-10 12:07:15 +03:00
|
|
|
QString itemErrorMessage = "Internal Server Fake Error";
|
2018-11-05 14:03:39 +03:00
|
|
|
|
2019-06-04 17:00:43 +03:00
|
|
|
QTest::newRow("400") << 400 << itemErrorMessage << false;
|
|
|
|
QTest::newRow("401") << 401 << itemErrorMessage << false;
|
|
|
|
QTest::newRow("403") << 403 << itemErrorMessage << true;
|
|
|
|
QTest::newRow("404") << 404 << itemErrorMessage << true;
|
|
|
|
QTest::newRow("500") << 500 << itemErrorMessage << true;
|
|
|
|
QTest::newRow("503") << 503 << itemErrorMessage << true;
|
2018-11-01 14:13:16 +03:00
|
|
|
// 200 should be an error since propfind should return 207
|
2019-06-04 17:00:43 +03:00
|
|
|
QTest::newRow("200") << 200 << itemErrorMessage << false;
|
|
|
|
QTest::newRow("InvalidXML") << +InvalidXML << "Unknown error" << false;
|
|
|
|
QTest::newRow("Timeout") << +Timeout << "Operation canceled" << false;
|
2018-07-25 13:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check what happens when there is an error.
|
|
|
|
void testRemoteDiscoveryError()
|
|
|
|
{
|
|
|
|
QFETCH(int, errorKind);
|
|
|
|
QFETCH(QString, expectedErrorString);
|
2019-06-04 17:00:43 +03:00
|
|
|
QFETCH(bool, syncSucceeds);
|
2018-07-25 13:21:29 +03:00
|
|
|
|
|
|
|
FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
|
|
|
|
|
|
|
|
// Do Some change as well
|
|
|
|
fakeFolder.localModifier().insert("A/z1");
|
|
|
|
fakeFolder.localModifier().insert("B/z1");
|
|
|
|
fakeFolder.localModifier().insert("C/z1");
|
|
|
|
fakeFolder.remoteModifier().insert("A/z2");
|
|
|
|
fakeFolder.remoteModifier().insert("B/z2");
|
|
|
|
fakeFolder.remoteModifier().insert("C/z2");
|
|
|
|
|
|
|
|
auto oldLocalState = fakeFolder.currentLocalState();
|
|
|
|
auto oldRemoteState = fakeFolder.currentRemoteState();
|
|
|
|
|
2021-07-26 09:56:48 +03:00
|
|
|
QString errorFolder = "dav/files/admin/B";
|
2021-07-29 10:40:31 +03:00
|
|
|
QString fatalErrorPrefix = "Server replied with an error while reading directory \"B\" : ";
|
2018-07-25 13:21:29 +03:00
|
|
|
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *)
|
|
|
|
-> QNetworkReply *{
|
2019-04-10 12:07:15 +03:00
|
|
|
if (req.attribute(QNetworkRequest::CustomVerbAttribute) == "PROPFIND" && req.url().path().endsWith(errorFolder)) {
|
2018-07-25 13:21:29 +03:00
|
|
|
if (errorKind == InvalidXML) {
|
|
|
|
return new FakeBrokenXmlPropfindReply(fakeFolder.remoteModifier(), op, req, this);
|
|
|
|
} else if (errorKind == Timeout) {
|
|
|
|
return new FakeHangingReply(op, req, this);
|
|
|
|
} else if (errorKind < 1000) {
|
|
|
|
return new FakeErrorReply(op, req, this, errorKind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
// So the test that test timeout finishes fast
|
|
|
|
QScopedValueRollback<int> setHttpTimeout(AbstractNetworkJob::httpTimeout, errorKind == Timeout ? 1 : 10000);
|
|
|
|
|
2019-10-07 16:25:36 +03:00
|
|
|
ItemCompletedSpy completeSpy(fakeFolder);
|
2018-07-25 13:21:29 +03:00
|
|
|
QSignalSpy errorSpy(&fakeFolder.syncEngine(), &SyncEngine::syncError);
|
2018-07-25 16:24:30 +03:00
|
|
|
QCOMPARE(fakeFolder.syncOnce(), syncSucceeds);
|
2018-07-25 13:21:29 +03:00
|
|
|
|
|
|
|
// The folder B should not have been sync'ed (and in particular not removed)
|
|
|
|
QCOMPARE(oldLocalState.children["B"], fakeFolder.currentLocalState().children["B"]);
|
|
|
|
QCOMPARE(oldRemoteState.children["B"], fakeFolder.currentRemoteState().children["B"]);
|
|
|
|
if (!syncSucceeds) {
|
2019-04-10 12:07:15 +03:00
|
|
|
QCOMPARE(errorSpy.size(), 1);
|
|
|
|
QCOMPARE(errorSpy[0][0].toString(), QString(fatalErrorPrefix + expectedErrorString));
|
2018-07-25 13:21:29 +03:00
|
|
|
} else {
|
2019-10-07 16:25:36 +03:00
|
|
|
QCOMPARE(completeSpy.findItem("B")->_instruction, CSYNC_INSTRUCTION_IGNORE);
|
|
|
|
QVERIFY(completeSpy.findItem("B")->_errorString.contains(expectedErrorString));
|
2019-04-10 12:07:15 +03:00
|
|
|
|
2018-07-25 13:21:29 +03:00
|
|
|
// The other folder should have been sync'ed as the sync just ignored the faulty dir
|
|
|
|
QCOMPARE(fakeFolder.currentRemoteState().children["A"], fakeFolder.currentLocalState().children["A"]);
|
|
|
|
QCOMPARE(fakeFolder.currentRemoteState().children["C"], fakeFolder.currentLocalState().children["C"]);
|
2019-10-07 16:25:36 +03:00
|
|
|
QCOMPARE(completeSpy.findItem("A/z1")->_instruction, CSYNC_INSTRUCTION_NEW);
|
2018-07-25 13:21:29 +03:00
|
|
|
}
|
2019-04-10 12:07:15 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Check the same discovery error on the sync root
|
|
|
|
//
|
2021-07-26 09:56:48 +03:00
|
|
|
errorFolder = "dav/files/admin/";
|
2021-07-29 10:40:31 +03:00
|
|
|
fatalErrorPrefix = "Server replied with an error while reading directory \"\" : ";
|
2019-04-10 12:07:15 +03:00
|
|
|
errorSpy.clear();
|
|
|
|
QVERIFY(!fakeFolder.syncOnce());
|
|
|
|
QCOMPARE(errorSpy.size(), 1);
|
|
|
|
QCOMPARE(errorSpy[0][0].toString(), QString(fatalErrorPrefix + expectedErrorString));
|
2018-07-25 13:21:29 +03:00
|
|
|
}
|
2019-03-26 13:19:42 +03:00
|
|
|
|
|
|
|
void testMissingData()
|
|
|
|
{
|
|
|
|
FakeFolder fakeFolder{ FileInfo() };
|
|
|
|
fakeFolder.remoteModifier().insert("good");
|
|
|
|
fakeFolder.remoteModifier().insert("noetag");
|
|
|
|
fakeFolder.remoteModifier().find("noetag")->etag.clear();
|
|
|
|
fakeFolder.remoteModifier().insert("nofileid");
|
|
|
|
fakeFolder.remoteModifier().find("nofileid")->fileId.clear();
|
|
|
|
fakeFolder.remoteModifier().mkdir("nopermissions");
|
|
|
|
fakeFolder.remoteModifier().insert("nopermissions/A");
|
|
|
|
|
|
|
|
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *)
|
|
|
|
-> QNetworkReply *{
|
|
|
|
if (req.attribute(QNetworkRequest::CustomVerbAttribute) == "PROPFIND" && req.url().path().endsWith("nopermissions"))
|
|
|
|
return new MissingPermissionsPropfindReply(fakeFolder.remoteModifier(), op, req, this);
|
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
|
2019-10-07 16:25:36 +03:00
|
|
|
ItemCompletedSpy completeSpy(fakeFolder);
|
2019-03-26 13:19:42 +03:00
|
|
|
QVERIFY(!fakeFolder.syncOnce());
|
|
|
|
|
2019-10-07 16:25:36 +03:00
|
|
|
QCOMPARE(completeSpy.findItem("good")->_instruction, CSYNC_INSTRUCTION_NEW);
|
|
|
|
QCOMPARE(completeSpy.findItem("noetag")->_instruction, CSYNC_INSTRUCTION_ERROR);
|
|
|
|
QCOMPARE(completeSpy.findItem("nofileid")->_instruction, CSYNC_INSTRUCTION_ERROR);
|
|
|
|
QCOMPARE(completeSpy.findItem("nopermissions")->_instruction, CSYNC_INSTRUCTION_NEW);
|
|
|
|
QCOMPARE(completeSpy.findItem("nopermissions/A")->_instruction, CSYNC_INSTRUCTION_ERROR);
|
2020-12-23 20:52:18 +03:00
|
|
|
QVERIFY(completeSpy.findItem("noetag")->_errorString.contains("ETag"));
|
2019-10-07 16:25:36 +03:00
|
|
|
QVERIFY(completeSpy.findItem("nofileid")->_errorString.contains("file id"));
|
2021-11-02 12:34:26 +03:00
|
|
|
QVERIFY(completeSpy.findItem("nopermissions/A")->_errorString.contains("permission"));
|
2019-03-26 13:19:42 +03:00
|
|
|
}
|
2018-07-25 13:21:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestRemoteDiscovery)
|
|
|
|
#include "testremotediscovery.moc"
|