testchunkingng.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 <QtTest>
  8. #include "syncenginetestutils.h"
  9. #include <syncengine.h>
  10. using namespace OCC;
  11. /* Upload a 1/3 of a file of given size.
  12. * fakeFolder needs to be synchronized */
  13. static void partialUpload(FakeFolder &fakeFolder, const QString &name, qint64 size)
  14. {
  15. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  16. QCOMPARE(fakeFolder.uploadState().children.count(), 0); // The state should be clean
  17. fakeFolder.localModifier().insert(name, size);
  18. // Abort when the upload is at 1/3
  19. qint64 sizeWhenAbort = -1;
  20. auto con = QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::transmissionProgress,
  21. [&](const ProgressInfo &progress) {
  22. if (progress.completedSize() > (progress.totalSize() /3 )) {
  23. sizeWhenAbort = progress.completedSize();
  24. fakeFolder.syncEngine().abort();
  25. }
  26. });
  27. QVERIFY(!fakeFolder.syncOnce()); // there should have been an error
  28. QObject::disconnect(con);
  29. QVERIFY(sizeWhenAbort > 0);
  30. QVERIFY(sizeWhenAbort < size);
  31. QCOMPARE(fakeFolder.uploadState().children.count(), 1); // the transfer was done with chunking
  32. auto upStateChildren = fakeFolder.uploadState().children.first().children;
  33. QCOMPARE(sizeWhenAbort, std::accumulate(upStateChildren.cbegin(), upStateChildren.cend(), 0,
  34. [](int s, const FileInfo &i) { return s + i.size; }));
  35. }
  36. // Reduce max chunk size a bit so we get more chunks
  37. static void setChunkSize(SyncEngine &engine, qint64 size)
  38. {
  39. SyncOptions options;
  40. options._maxChunkSize = size;
  41. options._initialChunkSize = size;
  42. options._minChunkSize = size;
  43. engine.setSyncOptions(options);
  44. }
  45. class TestChunkingNG : public QObject
  46. {
  47. Q_OBJECT
  48. private slots:
  49. void testFileUpload() {
  50. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  51. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  52. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  53. const int size = 10 * 1000 * 1000; // 10 MB
  54. fakeFolder.localModifier().insert("A/a0", size);
  55. QVERIFY(fakeFolder.syncOnce());
  56. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  57. QCOMPARE(fakeFolder.uploadState().children.count(), 1); // the transfer was done with chunking
  58. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size);
  59. // Check that another upload of the same file also work.
  60. fakeFolder.localModifier().appendByte("A/a0");
  61. QVERIFY(fakeFolder.syncOnce());
  62. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  63. QCOMPARE(fakeFolder.uploadState().children.count(), 2); // the transfer was done with chunking
  64. }
  65. // Test resuming when there's a confusing chunk added
  66. void testResume1() {
  67. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  68. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  69. const int size = 10 * 1000 * 1000; // 10 MB
  70. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  71. partialUpload(fakeFolder, "A/a0", size);
  72. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  73. auto chunkingId = fakeFolder.uploadState().children.first().name;
  74. const auto &chunkMap = fakeFolder.uploadState().children.first().children;
  75. qint64 uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0LL, [](qint64 s, const FileInfo &f) { return s + f.size; });
  76. QVERIFY(uploadedSize > 2 * 1000 * 1000); // at least 2 MB
  77. // Add a fake chunk to make sure it gets deleted
  78. fakeFolder.uploadState().children.first().insert("10000", size);
  79. fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
  80. if (op == QNetworkAccessManager::PutOperation) {
  81. // Test that we properly resuming and are not sending past data again.
  82. Q_ASSERT(request.rawHeader("OC-Chunk-Offset").toLongLong() >= uploadedSize);
  83. } else if (op == QNetworkAccessManager::DeleteOperation) {
  84. Q_ASSERT(request.url().path().endsWith("/10000"));
  85. }
  86. return nullptr;
  87. });
  88. QVERIFY(fakeFolder.syncOnce());
  89. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  90. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size);
  91. // The same chunk id was re-used
  92. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  93. QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
  94. }
  95. // Test resuming when one of the uploaded chunks got removed
  96. void testResume2() {
  97. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  98. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  99. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  100. const int size = 30 * 1000 * 1000; // 30 MB
  101. partialUpload(fakeFolder, "A/a0", size);
  102. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  103. auto chunkingId = fakeFolder.uploadState().children.first().name;
  104. const auto &chunkMap = fakeFolder.uploadState().children.first().children;
  105. qint64 uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0LL, [](qint64 s, const FileInfo &f) { return s + f.size; });
  106. QVERIFY(uploadedSize > 2 * 1000 * 1000); // at least 50 MB
  107. QVERIFY(chunkMap.size() >= 3); // at least three chunks
  108. QStringList chunksToDelete;
  109. // Remove the second chunk, so all further chunks will be deleted and resent
  110. auto firstChunk = chunkMap.first();
  111. auto secondChunk = *(chunkMap.begin() + 1);
  112. for (const auto& name : chunkMap.keys().mid(2)) {
  113. chunksToDelete.append(name);
  114. }
  115. fakeFolder.uploadState().children.first().remove(secondChunk.name);
  116. QStringList deletedPaths;
  117. fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
  118. if (op == QNetworkAccessManager::PutOperation) {
  119. // Test that we properly resuming, not resending the first chunk
  120. Q_ASSERT(request.rawHeader("OC-Chunk-Offset").toLongLong() >= firstChunk.size);
  121. } else if (op == QNetworkAccessManager::DeleteOperation) {
  122. deletedPaths.append(request.url().path());
  123. }
  124. return nullptr;
  125. });
  126. QVERIFY(fakeFolder.syncOnce());
  127. for (const auto& toDelete : chunksToDelete) {
  128. bool wasDeleted = false;
  129. for (const auto& deleted : deletedPaths) {
  130. if (deleted.mid(deleted.lastIndexOf('/') + 1) == toDelete) {
  131. wasDeleted = true;
  132. break;
  133. }
  134. }
  135. QVERIFY(wasDeleted);
  136. }
  137. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  138. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size);
  139. // The same chunk id was re-used
  140. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  141. QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
  142. }
  143. // Test resuming when all chunks are already present
  144. void testResume3() {
  145. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  146. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  147. const int size = 30 * 1000 * 1000; // 30 MB
  148. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  149. partialUpload(fakeFolder, "A/a0", size);
  150. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  151. auto chunkingId = fakeFolder.uploadState().children.first().name;
  152. const auto &chunkMap = fakeFolder.uploadState().children.first().children;
  153. qint64 uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0LL, [](qint64 s, const FileInfo &f) { return s + f.size; });
  154. QVERIFY(uploadedSize > 5 * 1000 * 1000); // at least 5 MB
  155. // Add a chunk that makes the file completely uploaded
  156. fakeFolder.uploadState().children.first().insert(
  157. QString::number(chunkMap.size()).rightJustified(16, '0'), size - uploadedSize);
  158. bool sawPut = false;
  159. bool sawDelete = false;
  160. bool sawMove = false;
  161. fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
  162. if (op == QNetworkAccessManager::PutOperation) {
  163. sawPut = true;
  164. } else if (op == QNetworkAccessManager::DeleteOperation) {
  165. sawDelete = true;
  166. } else if (request.attribute(QNetworkRequest::CustomVerbAttribute) == "MOVE") {
  167. sawMove = true;
  168. }
  169. return nullptr;
  170. });
  171. QVERIFY(fakeFolder.syncOnce());
  172. QVERIFY(sawMove);
  173. QVERIFY(!sawPut);
  174. QVERIFY(!sawDelete);
  175. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  176. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size);
  177. // The same chunk id was re-used
  178. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  179. QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
  180. }
  181. // Test resuming (or rather not resuming!) for the error case of the sum of
  182. // chunk sizes being larger than the file size
  183. void testResume4() {
  184. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  185. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  186. const int size = 30 * 1000 * 1000; // 30 MB
  187. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  188. partialUpload(fakeFolder, "A/a0", size);
  189. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  190. auto chunkingId = fakeFolder.uploadState().children.first().name;
  191. const auto &chunkMap = fakeFolder.uploadState().children.first().children;
  192. qint64 uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0LL, [](qint64 s, const FileInfo &f) { return s + f.size; });
  193. QVERIFY(uploadedSize > 5 * 1000 * 1000); // at least 5 MB
  194. // Add a chunk that makes the file more than completely uploaded
  195. fakeFolder.uploadState().children.first().insert(
  196. QString::number(chunkMap.size()).rightJustified(16, '0'), size - uploadedSize + 100);
  197. QVERIFY(fakeFolder.syncOnce());
  198. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  199. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size);
  200. // Used a new transfer id but wiped the old one
  201. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  202. QVERIFY(fakeFolder.uploadState().children.first().name != chunkingId);
  203. }
  204. // Check what happens when we abort during the final MOVE and the
  205. // the final MOVE takes longer than the abort-delay
  206. void testLateAbortHard()
  207. {
  208. FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
  209. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ { "chunking", "1.0" } } }, { "checksums", QVariantMap{ { "supportedTypes", QStringList() << "SHA1" } } } });
  210. const int size = 15 * 1000 * 1000; // 15 MB
  211. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  212. // Make the MOVE never reply, but trigger a client-abort and apply the change remotely
  213. QObject parent;
  214. QByteArray moveChecksumHeader;
  215. int nGET = 0;
  216. int responseDelay = 100000; // bigger than abort-wait timeout
  217. fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
  218. if (request.attribute(QNetworkRequest::CustomVerbAttribute) == "MOVE") {
  219. QTimer::singleShot(50, &parent, [&]() { fakeFolder.syncEngine().abort(); });
  220. moveChecksumHeader = request.rawHeader("OC-Checksum");
  221. return new DelayedReply<FakeChunkMoveReply>(responseDelay, fakeFolder.uploadState(), fakeFolder.remoteModifier(), op, request, &parent);
  222. } else if (op == QNetworkAccessManager::GetOperation) {
  223. nGET++;
  224. }
  225. return nullptr;
  226. });
  227. // Test 1: NEW file aborted
  228. fakeFolder.localModifier().insert("A/a0", size);
  229. QVERIFY(!fakeFolder.syncOnce()); // error: abort!
  230. // Now the next sync gets a NEW/NEW conflict and since there's no checksum
  231. // it just becomes a UPDATE_METADATA
  232. auto checkEtagUpdated = [&](SyncFileItemVector &items) {
  233. QCOMPARE(items.size(), 1);
  234. QCOMPARE(items[0]->_file, QLatin1String("A"));
  235. SyncJournalFileRecord record;
  236. QVERIFY(fakeFolder.syncJournal().getFileRecord(QByteArray("A/a0"), &record));
  237. QCOMPARE(record._etag, fakeFolder.remoteModifier().find("A/a0")->etag);
  238. };
  239. auto connection = connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToPropagate, checkEtagUpdated);
  240. QVERIFY(fakeFolder.syncOnce());
  241. disconnect(connection);
  242. QCOMPARE(nGET, 0);
  243. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  244. // Test 2: modified file upload aborted
  245. fakeFolder.localModifier().appendByte("A/a0");
  246. QVERIFY(!fakeFolder.syncOnce()); // error: abort!
  247. // An EVAL/EVAL conflict is also UPDATE_METADATA when there's no checksums
  248. connection = connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToPropagate, checkEtagUpdated);
  249. QVERIFY(fakeFolder.syncOnce());
  250. disconnect(connection);
  251. QCOMPARE(nGET, 0);
  252. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  253. // Test 3: modified file upload aborted, with good checksums
  254. fakeFolder.localModifier().appendByte("A/a0");
  255. QVERIFY(!fakeFolder.syncOnce()); // error: abort!
  256. // Set the remote checksum -- the test setup doesn't do it automatically
  257. QVERIFY(!moveChecksumHeader.isEmpty());
  258. fakeFolder.remoteModifier().find("A/a0")->checksums = moveChecksumHeader;
  259. QVERIFY(fakeFolder.syncOnce());
  260. disconnect(connection);
  261. QCOMPARE(nGET, 0); // no new download, just a metadata update!
  262. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  263. // Test 4: New file, that gets deleted locally before the next sync
  264. fakeFolder.localModifier().insert("A/a3", size);
  265. QVERIFY(!fakeFolder.syncOnce()); // error: abort!
  266. fakeFolder.localModifier().remove("A/a3");
  267. // bug: in this case we must expect a re-download of A/A3
  268. QVERIFY(fakeFolder.syncOnce());
  269. QCOMPARE(nGET, 1);
  270. QVERIFY(fakeFolder.currentLocalState().find("A/a3"));
  271. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  272. }
  273. // Check what happens when we abort during the final MOVE and the
  274. // the final MOVE is short enough for the abort-delay to help
  275. void testLateAbortRecoverable()
  276. {
  277. FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
  278. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ { "chunking", "1.0" } } }, { "checksums", QVariantMap{ { "supportedTypes", QStringList() << "SHA1" } } } });
  279. const int size = 15 * 1000 * 1000; // 15 MB
  280. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  281. // Make the MOVE never reply, but trigger a client-abort and apply the change remotely
  282. QObject parent;
  283. int responseDelay = 200; // smaller than abort-wait timeout
  284. fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
  285. if (request.attribute(QNetworkRequest::CustomVerbAttribute) == "MOVE") {
  286. QTimer::singleShot(50, &parent, [&]() { fakeFolder.syncEngine().abort(); });
  287. return new DelayedReply<FakeChunkMoveReply>(responseDelay, fakeFolder.uploadState(), fakeFolder.remoteModifier(), op, request, &parent);
  288. }
  289. return nullptr;
  290. });
  291. // Test 1: NEW file aborted
  292. fakeFolder.localModifier().insert("A/a0", size);
  293. QVERIFY(fakeFolder.syncOnce());
  294. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  295. // Test 2: modified file upload aborted
  296. fakeFolder.localModifier().appendByte("A/a0");
  297. QVERIFY(fakeFolder.syncOnce());
  298. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  299. }
  300. // We modify the file locally after it has been partially uploaded
  301. void testRemoveStale1() {
  302. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  303. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  304. const int size = 10 * 1000 * 1000; // 10 MB
  305. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  306. partialUpload(fakeFolder, "A/a0", size);
  307. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  308. auto chunkingId = fakeFolder.uploadState().children.first().name;
  309. fakeFolder.localModifier().setContents("A/a0", 'B');
  310. fakeFolder.localModifier().appendByte("A/a0");
  311. QVERIFY(fakeFolder.syncOnce());
  312. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  313. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size + 1);
  314. // A different chunk id was used, and the previous one is removed
  315. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  316. QVERIFY(fakeFolder.uploadState().children.first().name != chunkingId);
  317. }
  318. // We remove the file locally after it has been partially uploaded
  319. void testRemoveStale2() {
  320. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  321. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  322. const int size = 10 * 1000 * 1000; // 10 MB
  323. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  324. partialUpload(fakeFolder, "A/a0", size);
  325. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  326. fakeFolder.localModifier().remove("A/a0");
  327. QVERIFY(fakeFolder.syncOnce());
  328. QCOMPARE(fakeFolder.uploadState().children.count(), 0);
  329. }
  330. void testCreateConflictWhileSyncing() {
  331. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  332. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  333. const int size = 10 * 1000 * 1000; // 10 MB
  334. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  335. // Put a file on the server and download it.
  336. fakeFolder.remoteModifier().insert("A/a0", size);
  337. QVERIFY(fakeFolder.syncOnce());
  338. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  339. // Modify the file localy and start the upload
  340. fakeFolder.localModifier().setContents("A/a0", 'B');
  341. fakeFolder.localModifier().appendByte("A/a0");
  342. // But in the middle of the sync, modify the file on the server
  343. QMetaObject::Connection con = QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::transmissionProgress,
  344. [&](const ProgressInfo &progress) {
  345. if (progress.completedSize() > (progress.totalSize() / 2 )) {
  346. fakeFolder.remoteModifier().setContents("A/a0", 'C');
  347. QObject::disconnect(con);
  348. }
  349. });
  350. QVERIFY(!fakeFolder.syncOnce());
  351. // There was a precondition failed error, this means wen need to sync again
  352. QCOMPARE(fakeFolder.syncEngine().isAnotherSyncNeeded(), ImmediateFollowUp);
  353. QCOMPARE(fakeFolder.uploadState().children.count(), 1); // We did not clean the chunks at this point
  354. // Now we will download the server file and create a conflict
  355. QVERIFY(fakeFolder.syncOnce());
  356. auto localState = fakeFolder.currentLocalState();
  357. // A0 is the one from the server
  358. QCOMPARE(localState.find("A/a0")->size, size);
  359. QCOMPARE(localState.find("A/a0")->contentChar, 'C');
  360. // There is a conflict file with our version
  361. auto &stateAChildren = localState.find("A")->children;
  362. auto it = std::find_if(stateAChildren.cbegin(), stateAChildren.cend(), [&](const FileInfo &fi) {
  363. return fi.name.startsWith("a0 (conflicted copy");
  364. });
  365. QVERIFY(it != stateAChildren.cend());
  366. QCOMPARE(it->contentChar, 'B');
  367. QCOMPARE(it->size, size+1);
  368. // Remove the conflict file so the comparison works!
  369. fakeFolder.localModifier().remove("A/" + it->name);
  370. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  371. QCOMPARE(fakeFolder.uploadState().children.count(), 0); // The last sync cleaned the chunks
  372. }
  373. void testModifyLocalFileWhileUploading() {
  374. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  375. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  376. const int size = 10 * 1000 * 1000; // 10 MB
  377. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  378. fakeFolder.localModifier().insert("A/a0", size);
  379. // middle of the sync, modify the file
  380. QMetaObject::Connection con = QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::transmissionProgress,
  381. [&](const ProgressInfo &progress) {
  382. if (progress.completedSize() > (progress.totalSize() / 2 )) {
  383. fakeFolder.localModifier().setContents("A/a0", 'B');
  384. fakeFolder.localModifier().appendByte("A/a0");
  385. QObject::disconnect(con);
  386. }
  387. });
  388. QVERIFY(!fakeFolder.syncOnce());
  389. // There should be a followup sync
  390. QCOMPARE(fakeFolder.syncEngine().isAnotherSyncNeeded(), ImmediateFollowUp);
  391. QCOMPARE(fakeFolder.uploadState().children.count(), 1); // We did not clean the chunks at this point
  392. auto chunkingId = fakeFolder.uploadState().children.first().name;
  393. // Now we make a new sync which should upload the file for good.
  394. QVERIFY(fakeFolder.syncOnce());
  395. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  396. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size+1);
  397. // A different chunk id was used, and the previous one is removed
  398. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  399. QVERIFY(fakeFolder.uploadState().children.first().name != chunkingId);
  400. }
  401. void testResumeServerDeletedChunks() {
  402. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  403. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  404. const int size = 30 * 1000 * 1000; // 30 MB
  405. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  406. partialUpload(fakeFolder, "A/a0", size);
  407. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  408. auto chunkingId = fakeFolder.uploadState().children.first().name;
  409. // Delete the chunks on the server
  410. fakeFolder.uploadState().children.clear();
  411. QVERIFY(fakeFolder.syncOnce());
  412. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  413. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size);
  414. // A different chunk id was used
  415. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  416. QVERIFY(fakeFolder.uploadState().children.first().name != chunkingId);
  417. }
  418. // Check what happens when the connection is dropped on the PUT (non-chunking) or MOVE (chunking)
  419. // for on the issue #5106
  420. void connectionDroppedBeforeEtagRecieved_data()
  421. {
  422. QTest::addColumn<bool>("chunking");
  423. QTest::newRow("big file") << true;
  424. QTest::newRow("small file") << false;
  425. }
  426. void connectionDroppedBeforeEtagRecieved()
  427. {
  428. QFETCH(bool, chunking);
  429. FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
  430. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ { "chunking", "1.0" } } }, { "checksums", QVariantMap{ { "supportedTypes", QStringList() << "SHA1" } } } });
  431. const int size = chunking ? 1 * 1000 * 1000 : 300;
  432. setChunkSize(fakeFolder.syncEngine(), 300 * 1000);
  433. // Make the MOVE never reply, but trigger a client-abort and apply the change remotely
  434. QByteArray checksumHeader;
  435. int nGET = 0;
  436. QScopedValueRollback<int> setHttpTimeout(AbstractNetworkJob::httpTimeout, 1);
  437. int responseDelay = AbstractNetworkJob::httpTimeout * 1000 * 1000; // much bigger than http timeout (so a timeout will occur)
  438. // This will perform the operation on the server, but the reply will not come to the client
  439. fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData) -> QNetworkReply * {
  440. if (!chunking) {
  441. Q_ASSERT(!request.url().path().contains("/uploads/")
  442. && "Should not touch uploads endpoint when not chunking");
  443. }
  444. if (!chunking && op == QNetworkAccessManager::PutOperation) {
  445. checksumHeader = request.rawHeader("OC-Checksum");
  446. return new DelayedReply<FakePutReply>(responseDelay, fakeFolder.remoteModifier(), op, request, outgoingData->readAll(), &fakeFolder.syncEngine());
  447. } else if (chunking && request.attribute(QNetworkRequest::CustomVerbAttribute) == "MOVE") {
  448. checksumHeader = request.rawHeader("OC-Checksum");
  449. return new DelayedReply<FakeChunkMoveReply>(responseDelay, fakeFolder.uploadState(), fakeFolder.remoteModifier(), op, request, &fakeFolder.syncEngine());
  450. } else if (op == QNetworkAccessManager::GetOperation) {
  451. nGET++;
  452. }
  453. return nullptr;
  454. });
  455. // Test 1: a NEW file
  456. fakeFolder.localModifier().insert("A/a0", size);
  457. QVERIFY(!fakeFolder.syncOnce()); // timeout!
  458. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState()); // but the upload succeeded
  459. QVERIFY(!checksumHeader.isEmpty());
  460. fakeFolder.remoteModifier().find("A/a0")->checksums = checksumHeader; // The test system don't do that automatically
  461. // Should be resolved properly
  462. QVERIFY(fakeFolder.syncOnce());
  463. QCOMPARE(nGET, 0);
  464. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  465. // Test 2: Modify the file further
  466. fakeFolder.localModifier().appendByte("A/a0");
  467. QVERIFY(!fakeFolder.syncOnce()); // timeout!
  468. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState()); // but the upload succeeded
  469. fakeFolder.remoteModifier().find("A/a0")->checksums = checksumHeader;
  470. // modify again, should not cause conflict
  471. fakeFolder.localModifier().appendByte("A/a0");
  472. QVERIFY(!fakeFolder.syncOnce()); // now it's trying to upload the modified file
  473. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  474. fakeFolder.remoteModifier().find("A/a0")->checksums = checksumHeader;
  475. QVERIFY(fakeFolder.syncOnce());
  476. QCOMPARE(nGET, 0);
  477. }
  478. void testPercentEncoding() {
  479. QTextCodec::codecForLocale()->setCodecForLocale(QTextCodec::codecForName("UTF-8"));
  480. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  481. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  482. const int size = 5 * 1000 * 1000;
  483. setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
  484. fakeFolder.localModifier().insert("A/file % \u20ac", size);
  485. QVERIFY(fakeFolder.syncOnce());
  486. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  487. // Only the second upload contains an "If" header
  488. fakeFolder.localModifier().appendByte("A/file % \u20ac");
  489. QVERIFY(fakeFolder.syncOnce());
  490. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  491. }
  492. // Test uploading large files (2.5GiB)
  493. void testVeryBigFiles() {
  494. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  495. fakeFolder.syncEngine().account()->setCapabilities({ { "dav", QVariantMap{ {"chunking", "1.0"} } } });
  496. const qint64 size = 2.5 * 1024 * 1024 * 1024; // 2.5 GiB
  497. // Partial upload of big files
  498. partialUpload(fakeFolder, "A/a0", size);
  499. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  500. auto chunkingId = fakeFolder.uploadState().children.first().name;
  501. // Now resume
  502. QVERIFY(fakeFolder.syncOnce());
  503. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  504. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size);
  505. // The same chunk id was re-used
  506. QCOMPARE(fakeFolder.uploadState().children.count(), 1);
  507. QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
  508. // Upload another file again, this time without interruption
  509. fakeFolder.localModifier().appendByte("A/a0");
  510. QVERIFY(fakeFolder.syncOnce());
  511. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  512. QCOMPARE(fakeFolder.currentRemoteState().find("A/a0")->size, size + 1);
  513. }
  514. };
  515. QTEST_GUILESS_MAIN(TestChunkingNG)
  516. #include "testchunkingng.moc"