syncenginetestutils.h 45 KB

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