syncenginetestutils.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. *
  6. */
  7. #include "syncenginetestutils.h"
  8. #include "httplogger.h"
  9. #include "accessmanager.h"
  10. #include <QJsonDocument>
  11. #include <QJsonArray>
  12. #include <QJsonObject>
  13. #include <QJsonValue>
  14. #include <memory>
  15. PathComponents::PathComponents(const char *path)
  16. : PathComponents { QString::fromUtf8(path) }
  17. {
  18. }
  19. PathComponents::PathComponents(const QString &path)
  20. : QStringList { path.split(QLatin1Char('/'), Qt::SkipEmptyParts) }
  21. {
  22. }
  23. PathComponents::PathComponents(const QStringList &pathComponents)
  24. : QStringList { pathComponents }
  25. {
  26. }
  27. PathComponents PathComponents::parentDirComponents() const
  28. {
  29. return PathComponents { mid(0, size() - 1) };
  30. }
  31. PathComponents PathComponents::subComponents() const &
  32. {
  33. return PathComponents { mid(1) };
  34. }
  35. void DiskFileModifier::remove(const QString &relativePath)
  36. {
  37. QFileInfo fi { _rootDir.filePath(relativePath) };
  38. if (fi.isFile())
  39. QVERIFY(_rootDir.remove(relativePath));
  40. else
  41. QVERIFY(QDir { fi.filePath() }.removeRecursively());
  42. }
  43. void DiskFileModifier::insert(const QString &relativePath, qint64 size, char contentChar)
  44. {
  45. QFile file { _rootDir.filePath(relativePath) };
  46. QVERIFY(!file.exists());
  47. file.open(QFile::WriteOnly);
  48. QByteArray buf(1024, contentChar);
  49. for (int x = 0; x < size / buf.size(); ++x) {
  50. file.write(buf);
  51. }
  52. file.write(buf.data(), size % buf.size());
  53. file.close();
  54. // Set the mtime 30 seconds in the past, for some tests that need to make sure that the mtime differs.
  55. OCC::FileSystem::setModTime(file.fileName(), OCC::Utility::qDateTimeToTime_t(QDateTime::currentDateTimeUtc().addSecs(-30)));
  56. QCOMPARE(file.size(), size);
  57. }
  58. void DiskFileModifier::setContents(const QString &relativePath, char contentChar)
  59. {
  60. QFile file { _rootDir.filePath(relativePath) };
  61. QVERIFY(file.exists());
  62. qint64 size = file.size();
  63. file.open(QFile::WriteOnly);
  64. file.write(QByteArray {}.fill(contentChar, size));
  65. }
  66. void DiskFileModifier::appendByte(const QString &relativePath)
  67. {
  68. QFile file { _rootDir.filePath(relativePath) };
  69. QVERIFY(file.exists());
  70. file.open(QFile::ReadWrite);
  71. QByteArray contents = file.read(1);
  72. file.seek(file.size());
  73. file.write(contents);
  74. }
  75. void DiskFileModifier::mkdir(const QString &relativePath)
  76. {
  77. _rootDir.mkpath(relativePath);
  78. }
  79. void DiskFileModifier::rename(const QString &from, const QString &to)
  80. {
  81. QVERIFY(_rootDir.exists(from));
  82. QVERIFY(_rootDir.rename(from, to));
  83. }
  84. void DiskFileModifier::setModTime(const QString &relativePath, const QDateTime &modTime)
  85. {
  86. OCC::FileSystem::setModTime(_rootDir.filePath(relativePath), OCC::Utility::qDateTimeToTime_t(modTime));
  87. }
  88. FileInfo FileInfo::A12_B12_C12_S12()
  89. {
  90. FileInfo fi { QString {}, {
  91. { QStringLiteral("A"), { { QStringLiteral("a1"), 4 }, { QStringLiteral("a2"), 4 } } },
  92. { QStringLiteral("B"), { { QStringLiteral("b1"), 16 }, { QStringLiteral("b2"), 16 } } },
  93. { QStringLiteral("C"), { { QStringLiteral("c1"), 24 }, { QStringLiteral("c2"), 24 } } },
  94. } };
  95. FileInfo sharedFolder { QStringLiteral("S"), { { QStringLiteral("s1"), 32 }, { QStringLiteral("s2"), 32 } } };
  96. sharedFolder.isShared = true;
  97. sharedFolder.children[QStringLiteral("s1")].isShared = true;
  98. sharedFolder.children[QStringLiteral("s2")].isShared = true;
  99. fi.children.insert(sharedFolder.name, std::move(sharedFolder));
  100. return fi;
  101. }
  102. FileInfo::FileInfo(const QString &name, const std::initializer_list<FileInfo> &children)
  103. : name { name }
  104. {
  105. for (const auto &source : children)
  106. addChild(source);
  107. }
  108. void FileInfo::addChild(const FileInfo &info)
  109. {
  110. auto &dest = this->children[info.name] = info;
  111. dest.parentPath = path();
  112. dest.fixupParentPathRecursively();
  113. }
  114. void FileInfo::remove(const QString &relativePath)
  115. {
  116. const PathComponents pathComponents { relativePath };
  117. FileInfo *parent = findInvalidatingEtags(pathComponents.parentDirComponents());
  118. Q_ASSERT(parent);
  119. parent->children.erase(std::find_if(parent->children.begin(), parent->children.end(),
  120. [&pathComponents](const FileInfo &fi) { return fi.name == pathComponents.fileName(); }));
  121. }
  122. void FileInfo::insert(const QString &relativePath, qint64 size, char contentChar)
  123. {
  124. create(relativePath, size, contentChar);
  125. }
  126. void FileInfo::setContents(const QString &relativePath, char contentChar)
  127. {
  128. FileInfo *file = findInvalidatingEtags(relativePath);
  129. Q_ASSERT(file);
  130. file->contentChar = contentChar;
  131. }
  132. void FileInfo::appendByte(const QString &relativePath)
  133. {
  134. FileInfo *file = findInvalidatingEtags(relativePath);
  135. Q_ASSERT(file);
  136. file->size += 1;
  137. }
  138. void FileInfo::mkdir(const QString &relativePath)
  139. {
  140. createDir(relativePath);
  141. }
  142. void FileInfo::rename(const QString &oldPath, const QString &newPath)
  143. {
  144. const PathComponents newPathComponents { newPath };
  145. FileInfo *dir = findInvalidatingEtags(newPathComponents.parentDirComponents());
  146. Q_ASSERT(dir);
  147. Q_ASSERT(dir->isDir);
  148. const PathComponents pathComponents { oldPath };
  149. FileInfo *parent = findInvalidatingEtags(pathComponents.parentDirComponents());
  150. Q_ASSERT(parent);
  151. FileInfo fi = parent->children.take(pathComponents.fileName());
  152. fi.parentPath = dir->path();
  153. fi.name = newPathComponents.fileName();
  154. fi.fixupParentPathRecursively();
  155. dir->children.insert(newPathComponents.fileName(), std::move(fi));
  156. }
  157. void FileInfo::setModTime(const QString &relativePath, const QDateTime &modTime)
  158. {
  159. FileInfo *file = findInvalidatingEtags(relativePath);
  160. Q_ASSERT(file);
  161. file->lastModified = modTime;
  162. }
  163. void FileInfo::setModTimeKeepEtag(const QString &relativePath, const QDateTime &modTime)
  164. {
  165. FileInfo *file = find(relativePath);
  166. Q_ASSERT(file);
  167. file->lastModified = modTime;
  168. }
  169. FileInfo *FileInfo::find(PathComponents pathComponents, const bool invalidateEtags)
  170. {
  171. if (pathComponents.isEmpty()) {
  172. if (invalidateEtags) {
  173. etag = generateEtag();
  174. }
  175. return this;
  176. }
  177. QString childName = pathComponents.pathRoot();
  178. auto it = children.find(childName);
  179. if (it != children.end()) {
  180. auto file = it->find(std::move(pathComponents).subComponents(), invalidateEtags);
  181. if (file && invalidateEtags) {
  182. // Update parents on the way back
  183. etag = generateEtag();
  184. }
  185. return file;
  186. }
  187. return nullptr;
  188. }
  189. FileInfo *FileInfo::createDir(const QString &relativePath)
  190. {
  191. const PathComponents pathComponents { relativePath };
  192. FileInfo *parent = findInvalidatingEtags(pathComponents.parentDirComponents());
  193. Q_ASSERT(parent);
  194. FileInfo &child = parent->children[pathComponents.fileName()] = FileInfo { pathComponents.fileName() };
  195. child.parentPath = parent->path();
  196. child.etag = generateEtag();
  197. return &child;
  198. }
  199. FileInfo *FileInfo::create(const QString &relativePath, qint64 size, char contentChar)
  200. {
  201. const PathComponents pathComponents { relativePath };
  202. FileInfo *parent = findInvalidatingEtags(pathComponents.parentDirComponents());
  203. Q_ASSERT(parent);
  204. FileInfo &child = parent->children[pathComponents.fileName()] = FileInfo { pathComponents.fileName(), size };
  205. child.parentPath = parent->path();
  206. child.contentChar = contentChar;
  207. child.etag = generateEtag();
  208. return &child;
  209. }
  210. bool FileInfo::operator==(const FileInfo &other) const
  211. {
  212. // Consider files to be equal between local<->remote as a user would.
  213. return name == other.name
  214. && isDir == other.isDir
  215. && size == other.size
  216. && contentChar == other.contentChar
  217. && children == other.children;
  218. }
  219. QString FileInfo::path() const
  220. {
  221. return (parentPath.isEmpty() ? QString() : (parentPath + QLatin1Char('/'))) + name;
  222. }
  223. QString FileInfo::absolutePath() const
  224. {
  225. if (parentPath.endsWith(QLatin1Char('/'))) {
  226. return parentPath + name;
  227. } else {
  228. return parentPath + QLatin1Char('/') + name;
  229. }
  230. }
  231. void FileInfo::fixupParentPathRecursively()
  232. {
  233. auto p = path();
  234. for (auto it = children.begin(); it != children.end(); ++it) {
  235. Q_ASSERT(it.key() == it->name);
  236. it->parentPath = p;
  237. it->fixupParentPathRecursively();
  238. }
  239. }
  240. FileInfo *FileInfo::findInvalidatingEtags(PathComponents pathComponents)
  241. {
  242. return find(std::move(pathComponents), true);
  243. }
  244. FakePropfindReply::FakePropfindReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  245. : FakeReply { parent }
  246. {
  247. setRequest(request);
  248. setUrl(request.url());
  249. setOperation(op);
  250. open(QIODevice::ReadOnly);
  251. QString fileName = getFilePathFromUrl(request.url());
  252. Q_ASSERT(!fileName.isNull()); // for root, it should be empty
  253. const FileInfo *fileInfo = remoteRootFileInfo.find(fileName);
  254. if (!fileInfo) {
  255. QMetaObject::invokeMethod(this, "respond404", Qt::QueuedConnection);
  256. return;
  257. }
  258. const QString prefix = request.url().path().left(request.url().path().size() - fileName.size());
  259. // Don't care about the request and just return a full propfind
  260. const QString davUri { QStringLiteral("DAV:") };
  261. const QString ocUri { QStringLiteral("http://owncloud.org/ns") };
  262. QBuffer buffer { &payload };
  263. buffer.open(QIODevice::WriteOnly);
  264. QXmlStreamWriter xml(&buffer);
  265. xml.writeNamespace(davUri, QStringLiteral("d"));
  266. xml.writeNamespace(ocUri, QStringLiteral("oc"));
  267. xml.writeStartDocument();
  268. xml.writeStartElement(davUri, QStringLiteral("multistatus"));
  269. auto writeFileResponse = [&](const FileInfo &fileInfo) {
  270. xml.writeStartElement(davUri, QStringLiteral("response"));
  271. auto url = QString::fromUtf8(QUrl::toPercentEncoding(fileInfo.absolutePath(), "/"));
  272. if (!url.endsWith(QChar('/'))) {
  273. url.append(QChar('/'));
  274. }
  275. const auto href = OCC::Utility::concatUrlPath(prefix, url).path();
  276. xml.writeTextElement(davUri, QStringLiteral("href"), href);
  277. xml.writeStartElement(davUri, QStringLiteral("propstat"));
  278. xml.writeStartElement(davUri, QStringLiteral("prop"));
  279. if (fileInfo.isDir) {
  280. xml.writeStartElement(davUri, QStringLiteral("resourcetype"));
  281. xml.writeEmptyElement(davUri, QStringLiteral("collection"));
  282. xml.writeEndElement(); // resourcetype
  283. } else
  284. xml.writeEmptyElement(davUri, QStringLiteral("resourcetype"));
  285. auto gmtDate = fileInfo.lastModified.toUTC();
  286. auto stringDate = QLocale::c().toString(gmtDate, QStringLiteral("ddd, dd MMM yyyy HH:mm:ss 'GMT'"));
  287. xml.writeTextElement(davUri, QStringLiteral("getlastmodified"), stringDate);
  288. xml.writeTextElement(davUri, QStringLiteral("getcontentlength"), QString::number(fileInfo.size));
  289. xml.writeTextElement(davUri, QStringLiteral("getetag"), QStringLiteral("\"%1\"").arg(QString::fromLatin1(fileInfo.etag)));
  290. xml.writeTextElement(ocUri, QStringLiteral("permissions"), !fileInfo.permissions.isNull() ? QString(fileInfo.permissions.toString()) : fileInfo.isShared ? QStringLiteral("SRDNVCKW") : QStringLiteral("RDNVCKW"));
  291. xml.writeTextElement(ocUri, QStringLiteral("id"), QString::fromUtf8(fileInfo.fileId));
  292. xml.writeTextElement(ocUri, QStringLiteral("checksums"), QString::fromUtf8(fileInfo.checksums));
  293. buffer.write(fileInfo.extraDavProperties);
  294. xml.writeEndElement(); // prop
  295. xml.writeTextElement(davUri, QStringLiteral("status"), QStringLiteral("HTTP/1.1 200 OK"));
  296. xml.writeEndElement(); // propstat
  297. xml.writeEndElement(); // response
  298. };
  299. writeFileResponse(*fileInfo);
  300. foreach (const FileInfo &childFileInfo, fileInfo->children)
  301. writeFileResponse(childFileInfo);
  302. xml.writeEndElement(); // multistatus
  303. xml.writeEndDocument();
  304. QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
  305. }
  306. void FakePropfindReply::respond()
  307. {
  308. setHeader(QNetworkRequest::ContentLengthHeader, payload.size());
  309. setHeader(QNetworkRequest::ContentTypeHeader, QByteArrayLiteral("application/xml; charset=utf-8"));
  310. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 207);
  311. setFinished(true);
  312. emit metaDataChanged();
  313. if (bytesAvailable())
  314. emit readyRead();
  315. emit finished();
  316. }
  317. void FakePropfindReply::respond404()
  318. {
  319. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 404);
  320. setError(InternalServerError, QStringLiteral("Not Found"));
  321. emit metaDataChanged();
  322. emit finished();
  323. }
  324. qint64 FakePropfindReply::bytesAvailable() const
  325. {
  326. return payload.size() + QIODevice::bytesAvailable();
  327. }
  328. qint64 FakePropfindReply::readData(char *data, qint64 maxlen)
  329. {
  330. qint64 len = std::min(qint64 { payload.size() }, maxlen);
  331. std::copy(payload.cbegin(), payload.cbegin() + len, data);
  332. payload.remove(0, static_cast<int>(len));
  333. return len;
  334. }
  335. FakePutReply::FakePutReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, const QByteArray &putPayload, QObject *parent)
  336. : FakeReply { parent }
  337. {
  338. setRequest(request);
  339. setUrl(request.url());
  340. setOperation(op);
  341. open(QIODevice::ReadOnly);
  342. fileInfo = perform(remoteRootFileInfo, request, putPayload);
  343. QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
  344. }
  345. FileInfo *FakePutReply::perform(FileInfo &remoteRootFileInfo, const QNetworkRequest &request, const QByteArray &putPayload)
  346. {
  347. QString fileName = getFilePathFromUrl(request.url());
  348. Q_ASSERT(!fileName.isEmpty());
  349. FileInfo *fileInfo = remoteRootFileInfo.find(fileName);
  350. if (fileInfo) {
  351. fileInfo->size = putPayload.size();
  352. fileInfo->contentChar = putPayload.at(0);
  353. } else {
  354. // Assume that the file is filled with the same character
  355. fileInfo = remoteRootFileInfo.create(fileName, putPayload.size(), putPayload.at(0));
  356. }
  357. fileInfo->lastModified = OCC::Utility::qDateTimeFromTime_t(request.rawHeader("X-OC-Mtime").toLongLong());
  358. remoteRootFileInfo.find(fileName, /*invalidateEtags=*/true);
  359. return fileInfo;
  360. }
  361. void FakePutReply::respond()
  362. {
  363. emit uploadProgress(fileInfo->size, fileInfo->size);
  364. setRawHeader("OC-ETag", fileInfo->etag);
  365. setRawHeader("ETag", fileInfo->etag);
  366. setRawHeader("OC-FileID", fileInfo->fileId);
  367. setRawHeader("X-OC-MTime", "accepted"); // Prevents Q_ASSERT(!_runningNow) since we'll call PropagateItemJob::done twice in that case.
  368. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
  369. emit metaDataChanged();
  370. emit finished();
  371. }
  372. void FakePutReply::abort()
  373. {
  374. setError(OperationCanceledError, QStringLiteral("abort"));
  375. emit finished();
  376. }
  377. FakePutMultiFileReply::FakePutMultiFileReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, const QString &contentType, const QByteArray &putPayload, QObject *parent)
  378. : FakeReply { parent }
  379. {
  380. setRequest(request);
  381. setUrl(request.url());
  382. setOperation(op);
  383. open(QIODevice::ReadOnly);
  384. _allFileInfo = performMultiPart(remoteRootFileInfo, request, putPayload, contentType);
  385. QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
  386. }
  387. QVector<FileInfo *> FakePutMultiFileReply::performMultiPart(FileInfo &remoteRootFileInfo, const QNetworkRequest &request, const QByteArray &putPayload, const QString &contentType)
  388. {
  389. QVector<FileInfo *> result;
  390. auto stringPutPayload = QString::fromUtf8(putPayload);
  391. constexpr int boundaryPosition = sizeof("multipart/related; boundary=");
  392. const QString boundaryValue = QStringLiteral("--") + contentType.mid(boundaryPosition, contentType.length() - boundaryPosition - 1) + QStringLiteral("\r\n");
  393. auto stringPutPayloadRef = QString{stringPutPayload}.left(stringPutPayload.size() - 2 - boundaryValue.size());
  394. auto allParts = stringPutPayloadRef.split(boundaryValue, Qt::SkipEmptyParts);
  395. for (const auto &onePart : allParts) {
  396. auto headerEndPosition = onePart.indexOf(QStringLiteral("\r\n\r\n"));
  397. auto onePartHeaderPart = onePart.left(headerEndPosition);
  398. auto onePartBody = onePart.mid(headerEndPosition + 4, onePart.size() - headerEndPosition - 6);
  399. auto onePartHeaders = onePartHeaderPart.split(QStringLiteral("\r\n"));
  400. QMap<QString, QString> allHeaders;
  401. for(auto oneHeader : onePartHeaders) {
  402. auto headerParts = oneHeader.split(QStringLiteral(": "));
  403. allHeaders[headerParts.at(0)] = headerParts.at(1);
  404. }
  405. const auto fileName = allHeaders[QStringLiteral("X-File-Path")];
  406. const auto modtime = allHeaders[QByteArrayLiteral("X-File-Mtime")].toLongLong();
  407. Q_ASSERT(!fileName.isEmpty());
  408. Q_ASSERT(modtime > 0);
  409. FileInfo *fileInfo = remoteRootFileInfo.find(fileName);
  410. if (fileInfo) {
  411. fileInfo->size = onePartBody.size();
  412. fileInfo->contentChar = onePartBody.at(0).toLatin1();
  413. } else {
  414. // Assume that the file is filled with the same character
  415. fileInfo = remoteRootFileInfo.create(fileName, onePartBody.size(), onePartBody.at(0).toLatin1());
  416. }
  417. fileInfo->lastModified = OCC::Utility::qDateTimeFromTime_t(request.rawHeader("X-OC-Mtime").toLongLong());
  418. remoteRootFileInfo.find(fileName, /*invalidateEtags=*/true);
  419. result.push_back(fileInfo);
  420. }
  421. return result;
  422. }
  423. void FakePutMultiFileReply::respond()
  424. {
  425. QJsonDocument reply;
  426. QJsonObject allFileInfoReply;
  427. qint64 totalSize = 0;
  428. std::for_each(_allFileInfo.begin(), _allFileInfo.end(), [&totalSize](const auto &fileInfo) {
  429. totalSize += fileInfo->size;
  430. });
  431. for(auto fileInfo : qAsConst(_allFileInfo)) {
  432. QJsonObject fileInfoReply;
  433. fileInfoReply.insert("error", QStringLiteral("false"));
  434. fileInfoReply.insert("etag", QLatin1String{fileInfo->etag});
  435. emit uploadProgress(fileInfo->size, totalSize);
  436. allFileInfoReply.insert(QChar('/') + fileInfo->path(), fileInfoReply);
  437. }
  438. reply.setObject(allFileInfoReply);
  439. _payload = reply.toJson();
  440. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
  441. setFinished(true);
  442. if (bytesAvailable()) {
  443. emit readyRead();
  444. }
  445. emit metaDataChanged();
  446. emit finished();
  447. }
  448. void FakePutMultiFileReply::abort()
  449. {
  450. setError(OperationCanceledError, QStringLiteral("abort"));
  451. emit finished();
  452. }
  453. qint64 FakePutMultiFileReply::bytesAvailable() const
  454. {
  455. return _payload.size() + QIODevice::bytesAvailable();
  456. }
  457. qint64 FakePutMultiFileReply::readData(char *data, qint64 maxlen)
  458. {
  459. qint64 len = std::min(qint64 { _payload.size() }, maxlen);
  460. std::copy(_payload.cbegin(), _payload.cbegin() + len, data);
  461. _payload.remove(0, static_cast<int>(len));
  462. return len;
  463. }
  464. FakeMkcolReply::FakeMkcolReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  465. : FakeReply { parent }
  466. {
  467. setRequest(request);
  468. setUrl(request.url());
  469. setOperation(op);
  470. open(QIODevice::ReadOnly);
  471. QString fileName = getFilePathFromUrl(request.url());
  472. Q_ASSERT(!fileName.isEmpty());
  473. fileInfo = remoteRootFileInfo.createDir(fileName);
  474. if (!fileInfo) {
  475. abort();
  476. return;
  477. }
  478. QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
  479. }
  480. void FakeMkcolReply::respond()
  481. {
  482. setRawHeader("OC-FileId", fileInfo->fileId);
  483. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 201);
  484. emit metaDataChanged();
  485. emit finished();
  486. }
  487. FakeDeleteReply::FakeDeleteReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  488. : FakeReply { parent }
  489. {
  490. setRequest(request);
  491. setUrl(request.url());
  492. setOperation(op);
  493. open(QIODevice::ReadOnly);
  494. QString fileName = getFilePathFromUrl(request.url());
  495. Q_ASSERT(!fileName.isEmpty());
  496. remoteRootFileInfo.remove(fileName);
  497. QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
  498. }
  499. void FakeDeleteReply::respond()
  500. {
  501. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 204);
  502. emit metaDataChanged();
  503. emit finished();
  504. }
  505. FakeMoveReply::FakeMoveReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  506. : FakeReply { parent }
  507. {
  508. setRequest(request);
  509. setUrl(request.url());
  510. setOperation(op);
  511. open(QIODevice::ReadOnly);
  512. QString fileName = getFilePathFromUrl(request.url());
  513. Q_ASSERT(!fileName.isEmpty());
  514. QString dest = getFilePathFromUrl(QUrl::fromEncoded(request.rawHeader("Destination")));
  515. Q_ASSERT(!dest.isEmpty());
  516. remoteRootFileInfo.rename(fileName, dest);
  517. QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
  518. }
  519. void FakeMoveReply::respond()
  520. {
  521. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 201);
  522. emit metaDataChanged();
  523. emit finished();
  524. }
  525. FakeGetReply::FakeGetReply(FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  526. : FakeReply { parent }
  527. {
  528. setRequest(request);
  529. setUrl(request.url());
  530. setOperation(op);
  531. open(QIODevice::ReadOnly);
  532. QString fileName = getFilePathFromUrl(request.url());
  533. Q_ASSERT(!fileName.isEmpty());
  534. fileInfo = remoteRootFileInfo.find(fileName);
  535. if (!fileInfo) {
  536. qDebug() << "meh;";
  537. }
  538. Q_ASSERT_X(fileInfo, Q_FUNC_INFO, "Could not find file on the remote");
  539. QMetaObject::invokeMethod(this, &FakeGetReply::respond, Qt::QueuedConnection);
  540. }
  541. void FakeGetReply::respond()
  542. {
  543. if (aborted) {
  544. setError(OperationCanceledError, QStringLiteral("Operation Canceled"));
  545. emit metaDataChanged();
  546. emit finished();
  547. return;
  548. }
  549. payload = fileInfo->contentChar;
  550. size = fileInfo->size;
  551. setHeader(QNetworkRequest::ContentLengthHeader, size);
  552. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
  553. setRawHeader("OC-ETag", fileInfo->etag);
  554. setRawHeader("ETag", fileInfo->etag);
  555. setRawHeader("OC-FileId", fileInfo->fileId);
  556. emit metaDataChanged();
  557. if (bytesAvailable())
  558. emit readyRead();
  559. emit finished();
  560. }
  561. void FakeGetReply::abort()
  562. {
  563. setError(OperationCanceledError, QStringLiteral("Operation Canceled"));
  564. aborted = true;
  565. }
  566. qint64 FakeGetReply::bytesAvailable() const
  567. {
  568. if (aborted)
  569. return 0;
  570. return size + QIODevice::bytesAvailable();
  571. }
  572. qint64 FakeGetReply::readData(char *data, qint64 maxlen)
  573. {
  574. qint64 len = std::min(qint64 { size }, maxlen);
  575. std::fill_n(data, len, payload);
  576. size -= len;
  577. return len;
  578. }
  579. FakeGetWithDataReply::FakeGetWithDataReply(FileInfo &remoteRootFileInfo, const QByteArray &data, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  580. : FakeReply { parent }
  581. {
  582. setRequest(request);
  583. setUrl(request.url());
  584. setOperation(op);
  585. open(QIODevice::ReadOnly);
  586. Q_ASSERT(!data.isEmpty());
  587. payload = data;
  588. QString fileName = getFilePathFromUrl(request.url());
  589. Q_ASSERT(!fileName.isEmpty());
  590. fileInfo = remoteRootFileInfo.find(fileName);
  591. QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection);
  592. if (request.hasRawHeader("Range")) {
  593. const QString range = QString::fromUtf8(request.rawHeader("Range"));
  594. const QRegularExpression bytesPattern(QStringLiteral("bytes=(?<start>\\d+)-(?<end>\\d+)"));
  595. const QRegularExpressionMatch match = bytesPattern.match(range);
  596. if (match.hasMatch()) {
  597. const int start = match.captured(QStringLiteral("start")).toInt();
  598. const int end = match.captured(QStringLiteral("end")).toInt();
  599. payload = payload.mid(start, end - start + 1);
  600. }
  601. }
  602. }
  603. void FakeGetWithDataReply::respond()
  604. {
  605. if (aborted) {
  606. setError(OperationCanceledError, QStringLiteral("Operation Canceled"));
  607. emit metaDataChanged();
  608. emit finished();
  609. return;
  610. }
  611. setHeader(QNetworkRequest::ContentLengthHeader, payload.size());
  612. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
  613. setRawHeader("OC-ETag", fileInfo->etag);
  614. setRawHeader("ETag", fileInfo->etag);
  615. setRawHeader("OC-FileId", fileInfo->fileId);
  616. emit metaDataChanged();
  617. if (bytesAvailable())
  618. emit readyRead();
  619. emit finished();
  620. }
  621. void FakeGetWithDataReply::abort()
  622. {
  623. setError(OperationCanceledError, QStringLiteral("Operation Canceled"));
  624. aborted = true;
  625. }
  626. qint64 FakeGetWithDataReply::bytesAvailable() const
  627. {
  628. if (aborted)
  629. return 0;
  630. return payload.size() - offset + QIODevice::bytesAvailable();
  631. }
  632. qint64 FakeGetWithDataReply::readData(char *data, qint64 maxlen)
  633. {
  634. qint64 len = std::min(payload.size() - offset, quint64(maxlen));
  635. std::memcpy(data, payload.constData() + offset, len);
  636. offset += len;
  637. return len;
  638. }
  639. FakeChunkMoveReply::FakeChunkMoveReply(FileInfo &uploadsFileInfo, FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  640. : FakeReply { parent }
  641. {
  642. setRequest(request);
  643. setUrl(request.url());
  644. setOperation(op);
  645. open(QIODevice::ReadOnly);
  646. fileInfo = perform(uploadsFileInfo, remoteRootFileInfo, request);
  647. if (!fileInfo) {
  648. QTimer::singleShot(0, this, &FakeChunkMoveReply::respondPreconditionFailed);
  649. } else {
  650. QTimer::singleShot(0, this, &FakeChunkMoveReply::respond);
  651. }
  652. }
  653. FileInfo *FakeChunkMoveReply::perform(FileInfo &uploadsFileInfo, FileInfo &remoteRootFileInfo, const QNetworkRequest &request)
  654. {
  655. QString source = getFilePathFromUrl(request.url());
  656. Q_ASSERT(!source.isEmpty());
  657. Q_ASSERT(source.endsWith(QLatin1String("/.file")));
  658. source = source.left(source.length() - static_cast<int>(qstrlen("/.file")));
  659. auto sourceFolder = uploadsFileInfo.find(source);
  660. Q_ASSERT(sourceFolder);
  661. Q_ASSERT(sourceFolder->isDir);
  662. int count = 0;
  663. qlonglong size = 0;
  664. char payload = '\0';
  665. QString fileName = getFilePathFromUrl(QUrl::fromEncoded(request.rawHeader("Destination")));
  666. Q_ASSERT(!fileName.isEmpty());
  667. // Compute the size and content from the chunks if possible
  668. for (auto chunkName : sourceFolder->children.keys()) {
  669. auto &x = sourceFolder->children[chunkName];
  670. Q_ASSERT(!x.isDir);
  671. Q_ASSERT(x.size > 0); // There should not be empty chunks
  672. size += x.size;
  673. Q_ASSERT(!payload || payload == x.contentChar);
  674. payload = x.contentChar;
  675. ++count;
  676. }
  677. Q_ASSERT(sourceFolder->children.count() == count); // There should not be holes or extra files
  678. // NOTE: This does not actually assemble the file data from the chunks!
  679. FileInfo *fileInfo = remoteRootFileInfo.find(fileName);
  680. if (fileInfo) {
  681. // The client should put this header
  682. Q_ASSERT(request.hasRawHeader("If"));
  683. // And it should condition on the destination file
  684. auto start = QByteArray("<" + request.rawHeader("Destination") + ">");
  685. Q_ASSERT(request.rawHeader("If").startsWith(start));
  686. if (request.rawHeader("If") != start + " ([\"" + fileInfo->etag + "\"])") {
  687. return nullptr;
  688. }
  689. fileInfo->size = size;
  690. fileInfo->contentChar = payload;
  691. } else {
  692. Q_ASSERT(!request.hasRawHeader("If"));
  693. // Assume that the file is filled with the same character
  694. fileInfo = remoteRootFileInfo.create(fileName, size, payload);
  695. }
  696. fileInfo->lastModified = OCC::Utility::qDateTimeFromTime_t(request.rawHeader("X-OC-Mtime").toLongLong());
  697. remoteRootFileInfo.find(fileName, /*invalidateEtags=*/true);
  698. return fileInfo;
  699. }
  700. void FakeChunkMoveReply::respond()
  701. {
  702. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 201);
  703. setRawHeader("OC-ETag", fileInfo->etag);
  704. setRawHeader("ETag", fileInfo->etag);
  705. setRawHeader("OC-FileId", fileInfo->fileId);
  706. emit metaDataChanged();
  707. emit finished();
  708. }
  709. void FakeChunkMoveReply::respondPreconditionFailed()
  710. {
  711. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 412);
  712. setError(InternalServerError, QStringLiteral("Precondition Failed"));
  713. emit metaDataChanged();
  714. emit finished();
  715. }
  716. void FakeChunkMoveReply::abort()
  717. {
  718. setError(OperationCanceledError, QStringLiteral("abort"));
  719. emit finished();
  720. }
  721. FakePayloadReply::FakePayloadReply(QNetworkAccessManager::Operation op, const QNetworkRequest &request, const QByteArray &body, QObject *parent)
  722. : FakePayloadReply(op, request, body, FakePayloadReply::defaultDelay, parent)
  723. {
  724. }
  725. FakePayloadReply::FakePayloadReply(
  726. QNetworkAccessManager::Operation op, const QNetworkRequest &request, const QByteArray &body, int delay, QObject *parent)
  727. : FakeReply{parent}
  728. , _body(body)
  729. {
  730. setRequest(request);
  731. setUrl(request.url());
  732. setOperation(op);
  733. open(QIODevice::ReadOnly);
  734. QTimer::singleShot(delay, this, &FakePayloadReply::respond);
  735. }
  736. void FakePayloadReply::respond()
  737. {
  738. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
  739. setHeader(QNetworkRequest::ContentLengthHeader, _body.size());
  740. emit metaDataChanged();
  741. emit readyRead();
  742. setFinished(true);
  743. emit finished();
  744. }
  745. qint64 FakePayloadReply::readData(char *buf, qint64 max)
  746. {
  747. max = qMin<qint64>(max, _body.size());
  748. memcpy(buf, _body.constData(), max);
  749. _body = _body.mid(max);
  750. return max;
  751. }
  752. qint64 FakePayloadReply::bytesAvailable() const
  753. {
  754. return _body.size();
  755. }
  756. FakeErrorReply::FakeErrorReply(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent, int httpErrorCode, const QByteArray &body)
  757. : FakeReply { parent }
  758. , _body(body)
  759. {
  760. setRequest(request);
  761. setUrl(request.url());
  762. setOperation(op);
  763. open(QIODevice::ReadOnly);
  764. setAttribute(QNetworkRequest::HttpStatusCodeAttribute, httpErrorCode);
  765. setError(InternalServerError, QStringLiteral("Internal Server Fake Error"));
  766. QMetaObject::invokeMethod(this, &FakeErrorReply::respond, Qt::QueuedConnection);
  767. }
  768. void FakeErrorReply::respond()
  769. {
  770. emit metaDataChanged();
  771. emit readyRead();
  772. // finishing can come strictly after readyRead was called
  773. QTimer::singleShot(5, this, &FakeErrorReply::slotSetFinished);
  774. }
  775. void FakeErrorReply::slotSetFinished()
  776. {
  777. setFinished(true);
  778. emit finished();
  779. }
  780. qint64 FakeErrorReply::readData(char *buf, qint64 max)
  781. {
  782. max = qMin<qint64>(max, _body.size());
  783. memcpy(buf, _body.constData(), max);
  784. _body = _body.mid(max);
  785. return max;
  786. }
  787. qint64 FakeErrorReply::bytesAvailable() const
  788. {
  789. return _body.size();
  790. }
  791. FakeHangingReply::FakeHangingReply(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QObject *parent)
  792. : FakeReply(parent)
  793. {
  794. setRequest(request);
  795. setUrl(request.url());
  796. setOperation(op);
  797. open(QIODevice::ReadOnly);
  798. }
  799. void FakeHangingReply::abort()
  800. {
  801. // Follow more or less the implementation of QNetworkReplyImpl::abort
  802. close();
  803. setError(OperationCanceledError, tr("Operation canceled"));
  804. emit errorOccurred(OperationCanceledError);
  805. setFinished(true);
  806. emit finished();
  807. }
  808. FakeQNAM::FakeQNAM(FileInfo initialRoot)
  809. : _remoteRootFileInfo { std::move(initialRoot) }
  810. {
  811. setCookieJar(new OCC::CookieJar);
  812. }
  813. QJsonObject FakeQNAM::forEachReplyPart(QIODevice *outgoingData,
  814. const QString &contentType,
  815. std::function<QJsonObject (const QMap<QString, QByteArray> &)> replyFunction)
  816. {
  817. auto fullReply = QJsonObject{};
  818. auto putPayload = outgoingData->peek(outgoingData->bytesAvailable());
  819. outgoingData->reset();
  820. auto stringPutPayload = QString::fromUtf8(putPayload);
  821. constexpr int boundaryPosition = sizeof("multipart/related; boundary=");
  822. const QString boundaryValue = QStringLiteral("--") + contentType.mid(boundaryPosition, contentType.length() - boundaryPosition - 1) + QStringLiteral("\r\n");
  823. auto stringPutPayloadRef = QString{stringPutPayload}.left(stringPutPayload.size() - 2 - boundaryValue.size());
  824. auto allParts = stringPutPayloadRef.split(boundaryValue, Qt::SkipEmptyParts);
  825. for (const auto &onePart : qAsConst(allParts)) {
  826. auto headerEndPosition = onePart.indexOf(QStringLiteral("\r\n\r\n"));
  827. auto onePartHeaderPart = onePart.left(headerEndPosition);
  828. auto onePartHeaders = onePartHeaderPart.split(QStringLiteral("\r\n"));
  829. QMap<QString, QByteArray> allHeaders;
  830. for(const auto &oneHeader : qAsConst(onePartHeaders)) {
  831. auto headerParts = oneHeader.split(QStringLiteral(": "));
  832. allHeaders[headerParts.at(0)] = headerParts.at(1).toLatin1();
  833. }
  834. auto reply = replyFunction(allHeaders);
  835. if (reply.contains(QStringLiteral("error")) &&
  836. reply.contains(QStringLiteral("etag"))) {
  837. fullReply.insert(allHeaders[QStringLiteral("X-File-Path")], reply);
  838. }
  839. }
  840. return fullReply;
  841. }
  842. QNetworkReply *FakeQNAM::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
  843. {
  844. QNetworkReply *reply = nullptr;
  845. auto newRequest = request;
  846. newRequest.setRawHeader("X-Request-ID", OCC::AccessManager::generateRequestId());
  847. auto contentType = request.header(QNetworkRequest::ContentTypeHeader).toString();
  848. if (_override) {
  849. if (auto _reply = _override(op, newRequest, outgoingData)) {
  850. reply = _reply;
  851. }
  852. }
  853. if (!reply) {
  854. reply = overrideReplyWithError(getFilePathFromUrl(newRequest.url()), op, newRequest);
  855. }
  856. if (!reply) {
  857. const bool isUpload = newRequest.url().path().startsWith(sUploadUrl.path());
  858. FileInfo &info = isUpload ? _uploadFileInfo : _remoteRootFileInfo;
  859. auto verb = newRequest.attribute(QNetworkRequest::CustomVerbAttribute);
  860. if (verb == QLatin1String("PROPFIND")) {
  861. // Ignore outgoingData always returning somethign good enough, works for now.
  862. reply = new FakePropfindReply { info, op, newRequest, this };
  863. } else if (verb == QLatin1String("GET") || op == QNetworkAccessManager::GetOperation) {
  864. reply = new FakeGetReply { info, op, newRequest, this };
  865. } else if (verb == QLatin1String("PUT") || op == QNetworkAccessManager::PutOperation) {
  866. if (request.hasRawHeader(QByteArrayLiteral("X-OC-Mtime")) &&
  867. request.rawHeader(QByteArrayLiteral("X-OC-Mtime")).toLongLong() <= 0) {
  868. reply = new FakeErrorReply { op, request, this, 500 };
  869. } else {
  870. reply = new FakePutReply { info, op, newRequest, outgoingData->readAll(), this };
  871. }
  872. } else if (verb == QLatin1String("MKCOL")) {
  873. reply = new FakeMkcolReply { info, op, newRequest, this };
  874. } else if (verb == QLatin1String("DELETE") || op == QNetworkAccessManager::DeleteOperation) {
  875. reply = new FakeDeleteReply { info, op, newRequest, this };
  876. } else if (verb == QLatin1String("MOVE") && !isUpload) {
  877. reply = new FakeMoveReply { info, op, newRequest, this };
  878. } else if (verb == QLatin1String("MOVE") && isUpload) {
  879. reply = new FakeChunkMoveReply { info, _remoteRootFileInfo, op, newRequest, this };
  880. } else if (verb == QLatin1String("POST") || op == QNetworkAccessManager::PostOperation) {
  881. if (contentType.startsWith(QStringLiteral("multipart/related; boundary="))) {
  882. reply = new FakePutMultiFileReply { info, op, newRequest, contentType, outgoingData->readAll(), this };
  883. }
  884. } else {
  885. qDebug() << verb << outgoingData;
  886. Q_UNREACHABLE();
  887. }
  888. }
  889. OCC::HttpLogger::logRequest(reply, op, outgoingData);
  890. return reply;
  891. }
  892. QNetworkReply * FakeQNAM::overrideReplyWithError(QString fileName, QNetworkAccessManager::Operation op, QNetworkRequest newRequest)
  893. {
  894. QNetworkReply *reply = nullptr;
  895. Q_ASSERT(!fileName.isNull());
  896. if (_errorPaths.contains(fileName)) {
  897. reply = new FakeErrorReply { op, newRequest, this, _errorPaths[fileName] };
  898. }
  899. return reply;
  900. }
  901. FakeFolder::FakeFolder(const FileInfo &fileTemplate, const OCC::Optional<FileInfo> &localFileInfo, const QString &remotePath)
  902. : _localModifier(_tempDir.path())
  903. {
  904. // Needs to be done once
  905. OCC::SyncEngine::minimumFileAgeForUpload = std::chrono::milliseconds(0);
  906. OCC::Logger::instance()->setLogFile(QStringLiteral("-"));
  907. OCC::Logger::instance()->addLogRule({ QStringLiteral("sync.httplogger=true") });
  908. QDir rootDir { _tempDir.path() };
  909. qDebug() << "FakeFolder operating on" << rootDir;
  910. if (localFileInfo) {
  911. toDisk(rootDir, *localFileInfo);
  912. } else {
  913. toDisk(rootDir, fileTemplate);
  914. }
  915. _fakeQnam = new FakeQNAM(fileTemplate);
  916. _account = OCC::Account::create();
  917. _account->setUrl(QUrl(QStringLiteral("http://admin:admin@localhost/owncloud")));
  918. _account->setCredentials(new FakeCredentials { _fakeQnam });
  919. _account->setDavDisplayName(QStringLiteral("fakename"));
  920. _account->setServerVersion(QStringLiteral("10.0.0"));
  921. _journalDb = std::make_unique<OCC::SyncJournalDb>(localPath() + QStringLiteral(".sync_test.db"));
  922. _syncEngine = std::make_unique<OCC::SyncEngine>(_account, localPath(), remotePath, _journalDb.get());
  923. // Ignore temporary files from the download. (This is in the default exclude list, but we don't load it)
  924. _syncEngine->excludedFiles().addManualExclude(QStringLiteral("]*.~*"));
  925. // handle aboutToRemoveAllFiles with a timeout in case our test does not handle it
  926. QObject::connect(_syncEngine.get(), &OCC::SyncEngine::aboutToRemoveAllFiles, _syncEngine.get(), [this](OCC::SyncFileItem::Direction, std::function<void(bool)> callback) {
  927. QTimer::singleShot(1 * 1000, _syncEngine.get(), [callback] {
  928. callback(false);
  929. });
  930. });
  931. // Ensure we have a valid VfsOff instance "running"
  932. switchToVfs(_syncEngine->syncOptions()._vfs);
  933. // A new folder will update the local file state database on first sync.
  934. // To have a state matching what users will encounter, we have to a sync
  935. // using an identical local/remote file tree first.
  936. ENFORCE(syncOnce());
  937. }
  938. void FakeFolder::switchToVfs(QSharedPointer<OCC::Vfs> vfs)
  939. {
  940. auto opts = _syncEngine->syncOptions();
  941. opts._vfs->stop();
  942. QObject::disconnect(_syncEngine.get(), nullptr, opts._vfs.data(), nullptr);
  943. opts._vfs = vfs;
  944. _syncEngine->setSyncOptions(opts);
  945. OCC::VfsSetupParams vfsParams;
  946. vfsParams.filesystemPath = localPath();
  947. vfsParams.remotePath = QLatin1Char('/');
  948. vfsParams.account = _account;
  949. vfsParams.journal = _journalDb.get();
  950. vfsParams.providerName = QStringLiteral("OC-TEST");
  951. vfsParams.providerVersion = QStringLiteral("0.1");
  952. QObject::connect(_syncEngine.get(), &QObject::destroyed, vfs.data(), [vfs]() {
  953. vfs->stop();
  954. vfs->unregisterFolder();
  955. });
  956. vfs->start(vfsParams);
  957. }
  958. FileInfo FakeFolder::currentLocalState()
  959. {
  960. QDir rootDir { _tempDir.path() };
  961. FileInfo rootTemplate;
  962. fromDisk(rootDir, rootTemplate);
  963. rootTemplate.fixupParentPathRecursively();
  964. return rootTemplate;
  965. }
  966. QString FakeFolder::localPath() const
  967. {
  968. // SyncEngine wants a trailing slash
  969. if (_tempDir.path().endsWith(QLatin1Char('/')))
  970. return _tempDir.path();
  971. return _tempDir.path() + QLatin1Char('/');
  972. }
  973. void FakeFolder::scheduleSync()
  974. {
  975. // Have to be done async, else, an error before exec() does not terminate the event loop.
  976. QMetaObject::invokeMethod(_syncEngine.get(), "startSync", Qt::QueuedConnection);
  977. }
  978. void FakeFolder::execUntilBeforePropagation()
  979. {
  980. QSignalSpy spy(_syncEngine.get(), SIGNAL(aboutToPropagate(SyncFileItemVector &)));
  981. QVERIFY(spy.wait());
  982. }
  983. void FakeFolder::execUntilItemCompleted(const QString &relativePath)
  984. {
  985. QSignalSpy spy(_syncEngine.get(), SIGNAL(itemCompleted(const SyncFileItemPtr &)));
  986. QElapsedTimer t;
  987. t.start();
  988. while (t.elapsed() < 5000) {
  989. spy.clear();
  990. QVERIFY(spy.wait());
  991. for (const QList<QVariant> &args : spy) {
  992. auto item = args[0].value<OCC::SyncFileItemPtr>();
  993. if (item->destination() == relativePath)
  994. return;
  995. }
  996. }
  997. QVERIFY(false);
  998. }
  999. void FakeFolder::toDisk(QDir &dir, const FileInfo &templateFi)
  1000. {
  1001. foreach (const FileInfo &child, templateFi.children) {
  1002. if (child.isDir) {
  1003. QDir subDir(dir);
  1004. dir.mkdir(child.name);
  1005. subDir.cd(child.name);
  1006. toDisk(subDir, child);
  1007. } else {
  1008. QFile file { dir.filePath(child.name) };
  1009. file.open(QFile::WriteOnly);
  1010. file.write(QByteArray {}.fill(child.contentChar, child.size));
  1011. file.close();
  1012. OCC::FileSystem::setModTime(file.fileName(), OCC::Utility::qDateTimeToTime_t(child.lastModified));
  1013. }
  1014. }
  1015. }
  1016. void FakeFolder::fromDisk(QDir &dir, FileInfo &templateFi)
  1017. {
  1018. foreach (const QFileInfo &diskChild, dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
  1019. if (diskChild.isDir()) {
  1020. QDir subDir = dir;
  1021. subDir.cd(diskChild.fileName());
  1022. FileInfo &subFi = templateFi.children[diskChild.fileName()] = FileInfo { diskChild.fileName() };
  1023. fromDisk(subDir, subFi);
  1024. } else {
  1025. QFile f { diskChild.filePath() };
  1026. f.open(QFile::ReadOnly);
  1027. auto content = f.read(1);
  1028. if (content.size() == 0) {
  1029. qWarning() << "Empty file at:" << diskChild.filePath();
  1030. continue;
  1031. }
  1032. char contentChar = content.at(0);
  1033. templateFi.children.insert(diskChild.fileName(), FileInfo { diskChild.fileName(), diskChild.size(), contentChar });
  1034. }
  1035. }
  1036. }
  1037. static FileInfo &findOrCreateDirs(FileInfo &base, PathComponents components)
  1038. {
  1039. if (components.isEmpty())
  1040. return base;
  1041. auto childName = components.pathRoot();
  1042. auto it = base.children.find(childName);
  1043. if (it != base.children.end()) {
  1044. return findOrCreateDirs(*it, components.subComponents());
  1045. }
  1046. auto &newDir = base.children[childName] = FileInfo { childName };
  1047. newDir.parentPath = base.path();
  1048. return findOrCreateDirs(newDir, components.subComponents());
  1049. }
  1050. FileInfo FakeFolder::dbState() const
  1051. {
  1052. FileInfo result;
  1053. _journalDb->getFilesBelowPath("", [&](const OCC::SyncJournalFileRecord &record) {
  1054. auto components = PathComponents(record.path());
  1055. auto &parentDir = findOrCreateDirs(result, components.parentDirComponents());
  1056. auto name = components.fileName();
  1057. auto &item = parentDir.children[name];
  1058. item.name = name;
  1059. item.parentPath = parentDir.path();
  1060. item.size = record._fileSize;
  1061. item.isDir = record._type == ItemTypeDirectory;
  1062. item.permissions = record._remotePerm;
  1063. item.etag = record._etag;
  1064. item.lastModified = OCC::Utility::qDateTimeFromTime_t(record._modtime);
  1065. item.fileId = record._fileId;
  1066. item.checksums = record._checksumHeader;
  1067. // item.contentChar can't be set from the db
  1068. });
  1069. return result;
  1070. }
  1071. OCC::SyncFileItemPtr ItemCompletedSpy::findItem(const QString &path) const
  1072. {
  1073. for (const QList<QVariant> &args : *this) {
  1074. auto item = args[0].value<OCC::SyncFileItemPtr>();
  1075. if (item->destination() == path)
  1076. return item;
  1077. }
  1078. return OCC::SyncFileItemPtr::create();
  1079. }
  1080. OCC::SyncFileItemPtr ItemCompletedSpy::findItemWithExpectedRank(const QString &path, int rank) const
  1081. {
  1082. Q_ASSERT(size() > rank);
  1083. Q_ASSERT(!(*this)[rank].isEmpty());
  1084. auto item = (*this)[rank][0].value<OCC::SyncFileItemPtr>();
  1085. if (item->destination() == path) {
  1086. return item;
  1087. } else {
  1088. return OCC::SyncFileItemPtr::create();
  1089. }
  1090. }
  1091. FakeReply::FakeReply(QObject *parent)
  1092. : QNetworkReply(parent)
  1093. {
  1094. setRawHeader(QByteArrayLiteral("Date"), QDateTime::currentDateTimeUtc().toString(Qt::RFC2822Date).toUtf8());
  1095. }
  1096. FakeReply::~FakeReply() = default;
  1097. FakeJsonErrorReply::FakeJsonErrorReply(QNetworkAccessManager::Operation op,
  1098. const QNetworkRequest &request,
  1099. QObject *parent,
  1100. int httpErrorCode,
  1101. const QJsonDocument &reply)
  1102. : FakeErrorReply{ op, request, parent, httpErrorCode, reply.toJson() }
  1103. {
  1104. }