syncenginetestutils.cpp 37 KB

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