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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2018 ownCloud GmbH
* SPDX-License-Identifier: CC0-1.0
*
* 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>
using namespace OCC;
class FakeAsyncReply : public FakeReply
{
Q_OBJECT
QByteArray _pollLocation;
public:
FakeAsyncReply(const QByteArray &pollLocation, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
: FakeReply { parent }
, _pollLocation(pollLocation)
{
setRequest(request);
setUrl(request.url());
setOperation(op);
open(QIODevice::ReadOnly);
QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
}
Q_INVOKABLE void respond()
{
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 202);
setRawHeader("OC-JobStatus-Location", _pollLocation);
emit metaDataChanged();
emit finished();
}
void abort() override {}
qint64 readData(char *, qint64) override { return 0; }
};
class TestAsyncOp : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
}
void asyncUploadOperations()
{
FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ { "chunking", "1.0" } } } });
// Reduce max chunk size a bit so we get more chunks
SyncOptions options;
options.setMaxChunkSize(20 * 1000);
fakeFolder.syncEngine().setSyncOptions(options);
int nGET = 0;
// This test is made of several testcases.
// the testCases maps a filename to a couple of callback.
// When a file is uploaded, the fake server will always return the 202 code, and will set
// the `perform` functor to what needs to be done to complete the transaction.
// The testcase consist of the `pollRequest` which will be called when the sync engine
// calls the poll url.
struct TestCase
{
using PollRequest_t = std::function<QNetworkReply *(TestCase *, const QNetworkRequest &request)>;
PollRequest_t pollRequest;
std::function<FileInfo *()> perform = nullptr;
};
QHash<QString, TestCase> testCases;
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData) -> QNetworkReply * {
auto path = request.url().path();
if (op == QNetworkAccessManager::GetOperation && path.startsWith("/async-poll/")) {
auto file = path.mid(sizeof("/async-poll/") - 1);
Q_ASSERT(testCases.contains(file));
auto &testCase = testCases[file];
return testCase.pollRequest(&testCase, request);
}
if (op == QNetworkAccessManager::PutOperation && !path.contains("/uploads/")) {
// Not chunking
auto file = getFilePathFromUrl(request.url());
Q_ASSERT(testCases.contains(file));
auto &testCase = testCases[file];
Q_ASSERT(!testCase.perform);
auto putPayload = outgoingData->readAll();
testCase.perform = [putPayload, request, &fakeFolder] {
return FakePutReply::perform(fakeFolder.remoteModifier(), request, putPayload);
};
return new FakeAsyncReply("/async-poll/" + file.toUtf8(), op, request, &fakeFolder.syncEngine());
} else if (request.attribute(QNetworkRequest::CustomVerbAttribute).toString() == "MOVE") {
QString file = getFilePathFromUrl(QUrl::fromEncoded(request.rawHeader("Destination")));
Q_ASSERT(testCases.contains(file));
auto &testCase = testCases[file];
Q_ASSERT(!testCase.perform);
testCase.perform = [request, &fakeFolder] {
return FakeChunkMoveReply::perform(fakeFolder.uploadState(), fakeFolder.remoteModifier(), request);
};
return new FakeAsyncReply("/async-poll/" + file.toUtf8(), op, request, &fakeFolder.syncEngine());
} else if (op == QNetworkAccessManager::GetOperation) {
nGET++;
}
return nullptr;
});
// Callback to be used to finalize the transaction and return the success
auto successCallback = [](TestCase *tc, const QNetworkRequest &request) {
tc->pollRequest = [](TestCase *, const QNetworkRequest &) -> QNetworkReply * { std::abort(); }; // shall no longer be called
FileInfo *info = tc->perform();
QByteArray body = R"({ "status":"finished", "ETag":"\")" + info->etag + R"(\"", "fileId":")" + info->fileId + "\"}\n";
return new FakePayloadReply(QNetworkAccessManager::GetOperation, request, body, nullptr);
};
// Callback that never finishes
auto waitForeverCallback = [](TestCase *, const QNetworkRequest &request) {
QByteArray body = "{\"status\":\"started\"}\n";
return new FakePayloadReply(QNetworkAccessManager::GetOperation, request, body, nullptr);
};
// Callback that simulate an error.
auto errorCallback = [](TestCase *tc, const QNetworkRequest &request) {
tc->pollRequest = [](TestCase *, const QNetworkRequest &) -> QNetworkReply * { std::abort(); }; // shall no longer be called;
QByteArray body = "{\"status\":\"error\",\"errorCode\":500,\"errorMessage\":\"TestingErrors\"}\n";
return new FakePayloadReply(QNetworkAccessManager::GetOperation, request, body, nullptr);
};
// This lambda takes another functor as a parameter, and returns a callback that will
// tell the client needs to poll again, and further call to the poll url will call the
// given callback
auto waitAndChain = [](const TestCase::PollRequest_t &chain) {
return [chain](TestCase *tc, const QNetworkRequest &request) {
tc->pollRequest = chain;
QByteArray body = "{\"status\":\"started\"}\n";
return new FakePayloadReply(QNetworkAccessManager::GetOperation, request, body, nullptr);
};
};
// Create a testcase by creating a file of a given size locally and assigning it a callback
auto insertFile = [&](const QString &file, qint64 size, TestCase::PollRequest_t cb) {
fakeFolder.localModifier().insert(file, size);
testCases[file] = { std::move(cb) };
};
fakeFolder.localModifier().mkdir("success");
insertFile("success/chunked_success", options.maxChunkSize() * 3, successCallback);
insertFile("success/single_success", 300, successCallback);
insertFile("success/chunked_patience", options.maxChunkSize() * 3, waitAndChain(waitAndChain(successCallback)));
insertFile("success/single_patience", 300,
waitAndChain(waitAndChain(successCallback)));
fakeFolder.localModifier().mkdir("err");
insertFile("err/chunked_error", options.maxChunkSize() * 3, errorCallback);
insertFile("err/single_error", 300, errorCallback);
insertFile("err/chunked_error2", options.maxChunkSize() * 3, waitAndChain(errorCallback));
insertFile("err/single_error2", 300, waitAndChain(errorCallback));
// First sync should finish by itself.
// All the things in "success/" should be transferred, the things in "err/" not
QVERIFY(!fakeFolder.syncOnce());
QCOMPARE(nGET, 0);
QCOMPARE(*fakeFolder.currentLocalState().find("success"),
*fakeFolder.currentRemoteState().find("success"));
testCases.clear();
testCases["err/chunked_error"] = { successCallback };
testCases["err/chunked_error2"] = { successCallback };
testCases["err/single_error"] = { successCallback };
testCases["err/single_error2"] = { successCallback };
fakeFolder.localModifier().mkdir("waiting");
insertFile("waiting/small", 300, waitForeverCallback);
insertFile("waiting/willNotConflict", 300, waitForeverCallback);
insertFile("waiting/big", options.maxChunkSize() * 3, waitAndChain(waitAndChain([&](TestCase *tc, const QNetworkRequest &request) {
QTimer::singleShot(0, &fakeFolder.syncEngine(), &SyncEngine::abort);
return waitAndChain(waitForeverCallback)(tc, request);
})));
QVERIFY(fakeFolder.syncJournal().wipeErrorBlacklist() != -1);
// This second sync will redo the files that had errors
// But the waiting folder will not complete before it is aborted.
QVERIFY(!fakeFolder.syncOnce());
QCOMPARE(nGET, 0);
QCOMPARE(*fakeFolder.currentLocalState().find("err"),
*fakeFolder.currentRemoteState().find("err"));
testCases["waiting/small"].pollRequest = waitAndChain(waitAndChain(successCallback));
testCases["waiting/big"].pollRequest = waitAndChain(successCallback);
testCases["waiting/willNotConflict"].pollRequest =
[&fakeFolder, &successCallback](TestCase *tc, const QNetworkRequest &request) {
auto &remoteModifier = fakeFolder.remoteModifier(); // successCallback destroys the capture
auto reply = successCallback(tc, request);
// This is going to succeed, and after we just change the file.
// This should not be a conflict, but this should be downloaded in the
// next sync
remoteModifier.appendByte("waiting/willNotConflict");
return reply;
};
int nPUT = 0;
int nMOVE = 0;
int nDELETE = 0;
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
auto path = request.url().path();
if (op == QNetworkAccessManager::GetOperation && path.startsWith("/async-poll/")) {
auto file = path.mid(sizeof("/async-poll/") - 1);
Q_ASSERT(testCases.contains(file));
auto &testCase = testCases[file];
return testCase.pollRequest(&testCase, request);
} else if (op == QNetworkAccessManager::PutOperation) {
nPUT++;
} else if (op == QNetworkAccessManager::GetOperation) {
nGET++;
} else if (op == QNetworkAccessManager::DeleteOperation) {
nDELETE++;
} else if (request.attribute(QNetworkRequest::CustomVerbAttribute).toString() == "MOVE") {
nMOVE++;
}
return nullptr;
});
// This last sync will do the waiting stuff
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(nGET, 1); // "waiting/willNotConflict"
QCOMPARE(nPUT, 0);
QCOMPARE(nMOVE, 0);
QCOMPARE(nDELETE, 0);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
}
};
QTEST_GUILESS_MAIN(TestAsyncOp)
#include "testasyncop.moc"
|