syncenginetestutils.cpp 50 KB

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