testsyncplaceholders.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. SyncFileItemPtr findItem(const QSignalSpy &spy, const QString &path)
  12. {
  13. for (const QList<QVariant> &args : spy) {
  14. auto item = args[0].value<SyncFileItemPtr>();
  15. if (item->destination() == path)
  16. return item;
  17. }
  18. return SyncFileItemPtr(new SyncFileItem);
  19. }
  20. bool itemInstruction(const QSignalSpy &spy, const QString &path, const csync_instructions_e instr)
  21. {
  22. auto item = findItem(spy, path);
  23. return item->_instruction == instr;
  24. }
  25. SyncJournalFileRecord dbRecord(FakeFolder &folder, const QString &path)
  26. {
  27. SyncJournalFileRecord record;
  28. folder.syncJournal().getFileRecord(path, &record);
  29. return record;
  30. }
  31. class TestSyncPlaceholders : public QObject
  32. {
  33. Q_OBJECT
  34. private slots:
  35. void testPlaceholderLifecycle_data()
  36. {
  37. QTest::addColumn<bool>("doLocalDiscovery");
  38. QTest::newRow("full local discovery") << true;
  39. QTest::newRow("skip local discovery") << false;
  40. }
  41. void testPlaceholderLifecycle()
  42. {
  43. QFETCH(bool, doLocalDiscovery);
  44. FakeFolder fakeFolder{FileInfo()};
  45. SyncOptions syncOptions;
  46. syncOptions._usePlaceholders = true;
  47. fakeFolder.syncEngine().setSyncOptions(syncOptions);
  48. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  49. QSignalSpy completeSpy(&fakeFolder.syncEngine(), SIGNAL(itemCompleted(const SyncFileItemPtr &)));
  50. auto cleanup = [&]() {
  51. completeSpy.clear();
  52. if (!doLocalDiscovery)
  53. fakeFolder.syncEngine().setLocalDiscoveryOptions(LocalDiscoveryStyle::DatabaseAndFilesystem);
  54. };
  55. cleanup();
  56. // Create a placeholder for a new remote file
  57. fakeFolder.remoteModifier().mkdir("A");
  58. fakeFolder.remoteModifier().insert("A/a1", 64);
  59. QVERIFY(fakeFolder.syncOnce());
  60. QVERIFY(!fakeFolder.currentLocalState().find("A/a1"));
  61. QVERIFY(fakeFolder.currentLocalState().find("A/a1.owncloud"));
  62. QVERIFY(fakeFolder.currentRemoteState().find("A/a1"));
  63. QVERIFY(itemInstruction(completeSpy, "A/a1", CSYNC_INSTRUCTION_NEW));
  64. QCOMPARE(dbRecord(fakeFolder, "A/a1")._type, ItemTypePlaceholder);
  65. cleanup();
  66. // Another sync doesn't actually lead to changes
  67. QVERIFY(fakeFolder.syncOnce());
  68. QVERIFY(!fakeFolder.currentLocalState().find("A/a1"));
  69. QVERIFY(fakeFolder.currentLocalState().find("A/a1.owncloud"));
  70. QVERIFY(fakeFolder.currentRemoteState().find("A/a1"));
  71. QVERIFY(completeSpy.isEmpty());
  72. cleanup();
  73. // Neither does a remote change
  74. fakeFolder.remoteModifier().appendByte("A/a1");
  75. QVERIFY(fakeFolder.syncOnce());
  76. QVERIFY(!fakeFolder.currentLocalState().find("A/a1"));
  77. QVERIFY(fakeFolder.currentLocalState().find("A/a1.owncloud"));
  78. QVERIFY(fakeFolder.currentRemoteState().find("A/a1"));
  79. QVERIFY(itemInstruction(completeSpy, "A/a1", CSYNC_INSTRUCTION_UPDATE_METADATA));
  80. QCOMPARE(dbRecord(fakeFolder, "A/a1")._type, ItemTypePlaceholder);
  81. QCOMPARE(dbRecord(fakeFolder, "A/a1")._fileSize, 65);
  82. cleanup();
  83. // If the local placeholder file is removed, it'll just be recreated
  84. if (!doLocalDiscovery)
  85. fakeFolder.syncEngine().setLocalDiscoveryOptions(LocalDiscoveryStyle::DatabaseAndFilesystem, { "A" });
  86. fakeFolder.localModifier().remove("A/a1.owncloud");
  87. QVERIFY(fakeFolder.syncOnce());
  88. QVERIFY(!fakeFolder.currentLocalState().find("A/a1"));
  89. QVERIFY(fakeFolder.currentLocalState().find("A/a1.owncloud"));
  90. QVERIFY(fakeFolder.currentRemoteState().find("A/a1"));
  91. QVERIFY(itemInstruction(completeSpy, "A/a1", CSYNC_INSTRUCTION_NEW));
  92. QCOMPARE(dbRecord(fakeFolder, "A/a1")._type, ItemTypePlaceholder);
  93. QCOMPARE(dbRecord(fakeFolder, "A/a1")._fileSize, 65);
  94. cleanup();
  95. // Remote rename is propagated
  96. fakeFolder.remoteModifier().rename("A/a1", "A/a1m");
  97. QVERIFY(fakeFolder.syncOnce());
  98. QVERIFY(!fakeFolder.currentLocalState().find("A/a1"));
  99. QVERIFY(!fakeFolder.currentLocalState().find("A/a1m"));
  100. QVERIFY(!fakeFolder.currentLocalState().find("A/a1.owncloud"));
  101. QVERIFY(fakeFolder.currentLocalState().find("A/a1m.owncloud"));
  102. QVERIFY(!fakeFolder.currentRemoteState().find("A/a1"));
  103. QVERIFY(fakeFolder.currentRemoteState().find("A/a1m"));
  104. QVERIFY(itemInstruction(completeSpy, "A/a1m", CSYNC_INSTRUCTION_RENAME));
  105. QCOMPARE(dbRecord(fakeFolder, "A/a1m")._type, ItemTypePlaceholder);
  106. cleanup();
  107. // Remote remove is propagated
  108. fakeFolder.remoteModifier().remove("A/a1m");
  109. QVERIFY(fakeFolder.syncOnce());
  110. QVERIFY(!fakeFolder.currentLocalState().find("A/a1m.owncloud"));
  111. QVERIFY(!fakeFolder.currentRemoteState().find("A/a1m"));
  112. QVERIFY(itemInstruction(completeSpy, "A/a1m", CSYNC_INSTRUCTION_REMOVE));
  113. QVERIFY(!dbRecord(fakeFolder, "A/a1m").isValid());
  114. cleanup();
  115. }
  116. void testWithNormalSync()
  117. {
  118. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  119. SyncOptions syncOptions;
  120. syncOptions._usePlaceholders = true;
  121. fakeFolder.syncEngine().setSyncOptions(syncOptions);
  122. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  123. QSignalSpy completeSpy(&fakeFolder.syncEngine(), SIGNAL(itemCompleted(const SyncFileItemPtr &)));
  124. auto cleanup = [&]() {
  125. completeSpy.clear();
  126. };
  127. cleanup();
  128. // No effect sync
  129. QVERIFY(fakeFolder.syncOnce());
  130. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  131. cleanup();
  132. // Existing files are propagated just fine in both directions
  133. fakeFolder.localModifier().appendByte("A/a1");
  134. fakeFolder.localModifier().insert("A/a3");
  135. fakeFolder.remoteModifier().appendByte("A/a2");
  136. QVERIFY(fakeFolder.syncOnce());
  137. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  138. cleanup();
  139. // New files on the remote create placeholders
  140. fakeFolder.remoteModifier().insert("A/new");
  141. QVERIFY(fakeFolder.syncOnce());
  142. QVERIFY(!fakeFolder.currentLocalState().find("A/new"));
  143. QVERIFY(fakeFolder.currentLocalState().find("A/new.owncloud"));
  144. QVERIFY(fakeFolder.currentRemoteState().find("A/new"));
  145. QVERIFY(itemInstruction(completeSpy, "A/new", CSYNC_INSTRUCTION_NEW));
  146. QCOMPARE(dbRecord(fakeFolder, "A/new")._type, ItemTypePlaceholder);
  147. cleanup();
  148. }
  149. void testPlaceholderDownload()
  150. {
  151. FakeFolder fakeFolder{FileInfo()};
  152. SyncOptions syncOptions;
  153. syncOptions._usePlaceholders = true;
  154. fakeFolder.syncEngine().setSyncOptions(syncOptions);
  155. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  156. QSignalSpy completeSpy(&fakeFolder.syncEngine(), SIGNAL(itemCompleted(const SyncFileItemPtr &)));
  157. auto cleanup = [&]() {
  158. completeSpy.clear();
  159. };
  160. cleanup();
  161. auto triggerDownload = [&](const QByteArray &path) {
  162. auto &journal = fakeFolder.syncJournal();
  163. SyncJournalFileRecord record;
  164. journal.getFileRecord(path, &record);
  165. if (!record.isValid())
  166. return;
  167. record._type = ItemTypePlaceholderDownload;
  168. journal.setFileRecord(record);
  169. };
  170. // Create a placeholder for remote files
  171. fakeFolder.remoteModifier().mkdir("A");
  172. fakeFolder.remoteModifier().insert("A/a1");
  173. fakeFolder.remoteModifier().insert("A/a2");
  174. fakeFolder.remoteModifier().insert("A/a3");
  175. fakeFolder.remoteModifier().insert("A/a4");
  176. QVERIFY(fakeFolder.syncOnce());
  177. QVERIFY(fakeFolder.currentLocalState().find("A/a1.owncloud"));
  178. QVERIFY(fakeFolder.currentLocalState().find("A/a2.owncloud"));
  179. QVERIFY(fakeFolder.currentLocalState().find("A/a3.owncloud"));
  180. QVERIFY(fakeFolder.currentLocalState().find("A/a4.owncloud"));
  181. cleanup();
  182. // Download by changing the db entry
  183. triggerDownload("A/a1");
  184. triggerDownload("A/a2");
  185. triggerDownload("A/a3");
  186. triggerDownload("A/a4");
  187. fakeFolder.remoteModifier().appendByte("A/a2");
  188. fakeFolder.remoteModifier().remove("A/a3");
  189. fakeFolder.remoteModifier().rename("A/a4", "A/a4m");
  190. QVERIFY(fakeFolder.syncOnce());
  191. QVERIFY(itemInstruction(completeSpy, "A/a1", CSYNC_INSTRUCTION_NEW));
  192. QVERIFY(itemInstruction(completeSpy, "A/a2", CSYNC_INSTRUCTION_NEW));
  193. QVERIFY(itemInstruction(completeSpy, "A/a3", CSYNC_INSTRUCTION_REMOVE));
  194. QVERIFY(itemInstruction(completeSpy, "A/a4", CSYNC_INSTRUCTION_REMOVE));
  195. QVERIFY(itemInstruction(completeSpy, "A/a4m", CSYNC_INSTRUCTION_NEW));
  196. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  197. }
  198. };
  199. QTEST_GUILESS_MAIN(TestSyncPlaceholders)
  200. #include "testsyncplaceholders.moc"