syncenginetestutils.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. if (op == QNetworkAccessManager::CustomOperation) {
  898. qInfo() << "Operation" << request.attribute(QNetworkRequest::CustomVerbAttribute).toString() << request.url();
  899. } else {
  900. qInfo() << "Operation" << op << request.url();
  901. }
  902. QNetworkReply *reply = nullptr;
  903. auto newRequest = request;
  904. newRequest.setRawHeader("X-Request-ID", OCC::AccessManager::generateRequestId());
  905. auto contentType = request.header(QNetworkRequest::ContentTypeHeader).toString();
  906. if (_override) {
  907. qDebug() << "Using override!";
  908. if (auto _reply = _override(op, newRequest, outgoingData)) {
  909. reply = _reply;
  910. }
  911. }
  912. if (!reply) {
  913. qDebug() << newRequest.url();
  914. reply = overrideReplyWithError(getFilePathFromUrl(newRequest.url()), op, newRequest);
  915. }
  916. if (!reply) {
  917. const bool isUpload = newRequest.url().path().startsWith(sUploadUrl.path());
  918. FileInfo &info = isUpload ? _uploadFileInfo : _remoteRootFileInfo;
  919. auto verb = newRequest.attribute(QNetworkRequest::CustomVerbAttribute);
  920. if (verb == QLatin1String("PROPFIND")) {
  921. // Ignore outgoingData always returning somethign good enough, works for now.
  922. reply = new FakePropfindReply { info, op, newRequest, this };
  923. } else if (verb == QLatin1String("GET") || op == QNetworkAccessManager::GetOperation) {
  924. reply = new FakeGetReply { info, op, newRequest, this };
  925. } else if (verb == QLatin1String("PUT") || op == QNetworkAccessManager::PutOperation) {
  926. if (request.hasRawHeader(QByteArrayLiteral("X-OC-Mtime")) &&
  927. request.rawHeader(QByteArrayLiteral("X-OC-Mtime")).toLongLong() <= 0) {
  928. reply = new FakeErrorReply { op, request, this, 500 };
  929. } else {
  930. reply = new FakePutReply { info, op, newRequest, outgoingData->readAll(), this };
  931. }
  932. } else if (verb == QLatin1String("MKCOL")) {
  933. reply = new FakeMkcolReply { info, op, newRequest, this };
  934. } else if (verb == QLatin1String("DELETE") || op == QNetworkAccessManager::DeleteOperation) {
  935. reply = new FakeDeleteReply { info, op, newRequest, this };
  936. } else if (verb == QLatin1String("MOVE") && !isUpload) {
  937. reply = new FakeMoveReply { info, op, newRequest, this };
  938. } else if (verb == QLatin1String("MOVE") && isUpload) {
  939. reply = new FakeChunkMoveReply { info, _remoteRootFileInfo, op, newRequest, this };
  940. } else if (verb == QLatin1String("POST") || op == QNetworkAccessManager::PostOperation) {
  941. if (contentType.startsWith(QStringLiteral("multipart/related; boundary="))) {
  942. reply = new FakePutMultiFileReply { info, op, newRequest, contentType, outgoingData->readAll(), this };
  943. }
  944. } else if (verb == QLatin1String("LOCK") || verb == QLatin1String("UNLOCK")) {
  945. reply = new FakeFileLockReply{info, op, newRequest, this};
  946. } else {
  947. qDebug() << verb << outgoingData;
  948. Q_UNREACHABLE();
  949. }
  950. }
  951. OCC::HttpLogger::logRequest(reply, op, outgoingData);
  952. return reply;
  953. }
  954. QNetworkReply * FakeQNAM::overrideReplyWithError(QString fileName, QNetworkAccessManager::Operation op, QNetworkRequest newRequest)
  955. {
  956. QNetworkReply *reply = nullptr;
  957. Q_ASSERT(!fileName.isNull());
  958. if (_errorPaths.contains(fileName)) {
  959. reply = new FakeErrorReply { op, newRequest, this, _errorPaths[fileName] };
  960. }
  961. return reply;
  962. }
  963. FakeFolder::FakeFolder(const FileInfo &fileTemplate, const OCC::Optional<FileInfo> &localFileInfo, const QString &remotePath)
  964. : _localModifier(_tempDir.path())
  965. {
  966. // Needs to be done once
  967. OCC::SyncEngine::minimumFileAgeForUpload = std::chrono::milliseconds(0);
  968. OCC::Logger::instance()->setLogFile(QStringLiteral("-"));
  969. OCC::Logger::instance()->addLogRule({ QStringLiteral("sync.httplogger=true") });
  970. QDir rootDir { _tempDir.path() };
  971. qDebug() << "FakeFolder operating on" << rootDir;
  972. if (localFileInfo) {
  973. toDisk(rootDir, *localFileInfo);
  974. } else {
  975. toDisk(rootDir, fileTemplate);
  976. }
  977. _fakeQnam = new FakeQNAM(fileTemplate);
  978. _account = OCC::Account::create();
  979. _account->setUrl(QUrl(QStringLiteral("http://admin:admin@localhost/owncloud")));
  980. _account->setCredentials(new FakeCredentials { _fakeQnam });
  981. _account->setDavDisplayName(QStringLiteral("fakename"));
  982. _account->setServerVersion(QStringLiteral("10.0.0"));
  983. _journalDb = std::make_unique<OCC::SyncJournalDb>(localPath() + QStringLiteral(".sync_test.db"));
  984. _syncEngine = std::make_unique<OCC::SyncEngine>(_account, localPath(), OCC::SyncOptions{}, remotePath, _journalDb.get());
  985. // Ignore temporary files from the download. (This is in the default exclude list, but we don't load it)
  986. _syncEngine->excludedFiles().addManualExclude(QStringLiteral("]*.~*"));
  987. // handle aboutToRemoveAllFiles with a timeout in case our test does not handle it
  988. QObject::connect(_syncEngine.get(), &OCC::SyncEngine::aboutToRemoveAllFiles, _syncEngine.get(), [this](OCC::SyncFileItem::Direction, std::function<void(bool)> callback) {
  989. QTimer::singleShot(1 * 1000, _syncEngine.get(), [callback] {
  990. callback(false);
  991. });
  992. });
  993. // Ensure we have a valid VfsOff instance "running"
  994. switchToVfs(_syncEngine->syncOptions()._vfs);
  995. // A new folder will update the local file state database on first sync.
  996. // To have a state matching what users will encounter, we have to a sync
  997. // using an identical local/remote file tree first.
  998. ENFORCE(syncOnce());
  999. }
  1000. void FakeFolder::switchToVfs(QSharedPointer<OCC::Vfs> vfs)
  1001. {
  1002. auto opts = _syncEngine->syncOptions();
  1003. opts._vfs->stop();
  1004. QObject::disconnect(_syncEngine.get(), nullptr, opts._vfs.data(), nullptr);
  1005. opts._vfs = vfs;
  1006. _syncEngine->setSyncOptions(opts);
  1007. OCC::VfsSetupParams vfsParams;
  1008. vfsParams.filesystemPath = localPath();
  1009. vfsParams.remotePath = QLatin1Char('/');
  1010. vfsParams.account = _account;
  1011. vfsParams.journal = _journalDb.get();
  1012. vfsParams.providerName = QStringLiteral("OC-TEST");
  1013. vfsParams.providerVersion = QStringLiteral("0.1");
  1014. QObject::connect(_syncEngine.get(), &QObject::destroyed, vfs.data(), [vfs]() {
  1015. vfs->stop();
  1016. vfs->unregisterFolder();
  1017. });
  1018. vfs->start(vfsParams);
  1019. }
  1020. FileInfo FakeFolder::currentLocalState()
  1021. {
  1022. QDir rootDir { _tempDir.path() };
  1023. FileInfo rootTemplate;
  1024. fromDisk(rootDir, rootTemplate);
  1025. rootTemplate.fixupParentPathRecursively();
  1026. return rootTemplate;
  1027. }
  1028. QString FakeFolder::localPath() const
  1029. {
  1030. // SyncEngine wants a trailing slash
  1031. if (_tempDir.path().endsWith(QLatin1Char('/')))
  1032. return _tempDir.path();
  1033. return _tempDir.path() + QLatin1Char('/');
  1034. }
  1035. void FakeFolder::scheduleSync()
  1036. {
  1037. // Have to be done async, else, an error before exec() does not terminate the event loop.
  1038. QMetaObject::invokeMethod(_syncEngine.get(), "startSync", Qt::QueuedConnection);
  1039. }
  1040. void FakeFolder::execUntilBeforePropagation()
  1041. {
  1042. QSignalSpy spy(_syncEngine.get(), &OCC::SyncEngine::aboutToPropagate);
  1043. QVERIFY(spy.wait());
  1044. }
  1045. void FakeFolder::execUntilItemCompleted(const QString &relativePath)
  1046. {
  1047. QSignalSpy spy(_syncEngine.get(), &OCC::SyncEngine::itemCompleted);
  1048. QElapsedTimer t;
  1049. t.start();
  1050. while (t.elapsed() < 5000) {
  1051. spy.clear();
  1052. QVERIFY(spy.wait());
  1053. for (const QList<QVariant> &args : spy) {
  1054. auto item = args[0].value<OCC::SyncFileItemPtr>();
  1055. if (item->destination() == relativePath)
  1056. return;
  1057. }
  1058. }
  1059. QVERIFY(false);
  1060. }
  1061. void FakeFolder::toDisk(QDir &dir, const FileInfo &templateFi)
  1062. {
  1063. for(const auto &child : templateFi.children) {
  1064. if (child.isDir) {
  1065. QDir subDir(dir);
  1066. dir.mkdir(child.name);
  1067. subDir.cd(child.name);
  1068. toDisk(subDir, child);
  1069. } else {
  1070. QFile file { dir.filePath(child.name) };
  1071. file.open(QFile::WriteOnly);
  1072. file.write(QByteArray {}.fill(child.contentChar, child.size));
  1073. file.close();
  1074. OCC::FileSystem::setModTime(file.fileName(), OCC::Utility::qDateTimeToTime_t(child.lastModified));
  1075. }
  1076. }
  1077. }
  1078. void FakeFolder::fromDisk(QDir &dir, FileInfo &templateFi)
  1079. {
  1080. for(const auto &diskChild : dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
  1081. if (diskChild.isDir()) {
  1082. QDir subDir = dir;
  1083. subDir.cd(diskChild.fileName());
  1084. FileInfo &subFi = templateFi.children[diskChild.fileName()] = FileInfo { diskChild.fileName() };
  1085. fromDisk(subDir, subFi);
  1086. } else {
  1087. QFile f { diskChild.filePath() };
  1088. f.open(QFile::ReadOnly);
  1089. auto content = f.read(1);
  1090. if (content.size() == 0) {
  1091. qWarning() << "Empty file at:" << diskChild.filePath();
  1092. continue;
  1093. }
  1094. char contentChar = content.at(0);
  1095. templateFi.children.insert(diskChild.fileName(), FileInfo{diskChild.fileName(), diskChild.size(), contentChar, diskChild.lastModified()});
  1096. }
  1097. }
  1098. }
  1099. static FileInfo &findOrCreateDirs(FileInfo &base, PathComponents components)
  1100. {
  1101. if (components.isEmpty())
  1102. return base;
  1103. auto childName = components.pathRoot();
  1104. auto it = base.children.find(childName);
  1105. if (it != base.children.end()) {
  1106. return findOrCreateDirs(*it, components.subComponents());
  1107. }
  1108. auto &newDir = base.children[childName] = FileInfo { childName };
  1109. newDir.parentPath = base.path();
  1110. return findOrCreateDirs(newDir, components.subComponents());
  1111. }
  1112. FileInfo FakeFolder::dbState() const
  1113. {
  1114. FileInfo result;
  1115. [[maybe_unused]] const auto journalDbResult =_journalDb->getFilesBelowPath("", [&](const OCC::SyncJournalFileRecord &record) {
  1116. auto components = PathComponents(record.path());
  1117. auto &parentDir = findOrCreateDirs(result, components.parentDirComponents());
  1118. auto name = components.fileName();
  1119. auto &item = parentDir.children[name];
  1120. item.name = name;
  1121. item.parentPath = parentDir.path();
  1122. item.size = record._fileSize;
  1123. item.isDir = record._type == ItemTypeDirectory;
  1124. item.permissions = record._remotePerm;
  1125. item.etag = record._etag;
  1126. item.lastModified = OCC::Utility::qDateTimeFromTime_t(record._modtime);
  1127. item.fileId = record._fileId;
  1128. item.checksums = record._checksumHeader;
  1129. // item.contentChar can't be set from the db
  1130. });
  1131. return result;
  1132. }
  1133. OCC::SyncFileItemPtr ItemCompletedSpy::findItem(const QString &path) const
  1134. {
  1135. for (const QList<QVariant> &args : *this) {
  1136. auto item = args[0].value<OCC::SyncFileItemPtr>();
  1137. if (item->destination() == path)
  1138. return item;
  1139. }
  1140. return OCC::SyncFileItemPtr::create();
  1141. }
  1142. OCC::SyncFileItemPtr ItemCompletedSpy::findItemWithExpectedRank(const QString &path, int rank) const
  1143. {
  1144. Q_ASSERT(size() > rank);
  1145. Q_ASSERT(!(*this)[rank].isEmpty());
  1146. auto item = (*this)[rank][0].value<OCC::SyncFileItemPtr>();
  1147. if (item->destination() == path) {
  1148. return item;
  1149. } else {
  1150. return OCC::SyncFileItemPtr::create();
  1151. }
  1152. }
  1153. FakeReply::FakeReply(QObject *parent)
  1154. : QNetworkReply(parent)
  1155. {
  1156. setRawHeader(QByteArrayLiteral("Date"), QDateTime::currentDateTimeUtc().toString(Qt::RFC2822Date).toUtf8());
  1157. }
  1158. FakeReply::~FakeReply() = default;
  1159. FakeJsonErrorReply::FakeJsonErrorReply(QNetworkAccessManager::Operation op,
  1160. const QNetworkRequest &request,
  1161. QObject *parent,
  1162. int httpErrorCode,
  1163. const QJsonDocument &reply)
  1164. : FakeErrorReply{ op, request, parent, httpErrorCode, reply.toJson() }
  1165. {
  1166. }
  1167. FakeFileLockReply::FakeFileLockReply(FileInfo &remoteRootFileInfo,
  1168. QNetworkAccessManager::Operation op,
  1169. const QNetworkRequest &request,
  1170. QObject *parent)
  1171. : FakePropfindReply(remoteRootFileInfo, op, request, parent)
  1172. {
  1173. const auto verb = request.attribute(QNetworkRequest::CustomVerbAttribute);
  1174. setRequest(request);
  1175. setUrl(request.url());
  1176. setOperation(op);
  1177. open(QIODevice::ReadOnly);
  1178. QString fileName = getFilePathFromUrl(request.url());
  1179. Q_ASSERT(!fileName.isNull()); // for root, it should be empty
  1180. FileInfo *fileInfo = remoteRootFileInfo.find(fileName);
  1181. if (!fileInfo) {
  1182. QMetaObject::invokeMethod(this, "respond404", Qt::QueuedConnection);
  1183. return;
  1184. }
  1185. const QString prefix = request.url().path().left(request.url().path().size() - fileName.size());
  1186. // Don't care about the request and just return a full propfind
  1187. const QString davUri { QStringLiteral("DAV:") };
  1188. const QString ocUri { QStringLiteral("http://owncloud.org/ns") };
  1189. const QString ncUri { QStringLiteral("http://nextcloud.org/ns") };
  1190. payload.clear();
  1191. QBuffer buffer { &payload };
  1192. buffer.open(QIODevice::WriteOnly);
  1193. QXmlStreamWriter xml(&buffer);
  1194. xml.writeNamespace(davUri, QStringLiteral("d"));
  1195. xml.writeNamespace(ocUri, QStringLiteral("oc"));
  1196. xml.writeNamespace(ncUri, QStringLiteral("nc"));
  1197. xml.writeStartDocument();
  1198. xml.writeStartElement(davUri, QStringLiteral("prop"));
  1199. xml.writeTextElement(ncUri, QStringLiteral("lock"), verb == QStringLiteral("LOCK") ? "1" : "0");
  1200. xml.writeTextElement(ncUri, QStringLiteral("lock-owner-type"), QString::number(0));
  1201. xml.writeTextElement(ncUri, QStringLiteral("lock-owner"), QStringLiteral("admin"));
  1202. xml.writeTextElement(ncUri, QStringLiteral("lock-owner-displayname"), QStringLiteral("John Doe"));
  1203. xml.writeTextElement(ncUri, QStringLiteral("lock-owner-editor"), {});
  1204. xml.writeTextElement(ncUri, QStringLiteral("lock-time"), QString::number(1234560));
  1205. xml.writeTextElement(ncUri, QStringLiteral("lock-timeout"), QString::number(1800));
  1206. xml.writeEndElement(); // prop
  1207. xml.writeEndDocument();
  1208. }