1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2017 ownCloud GmbH
* SPDX-License-Identifier: CC0-1.0
*
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*/
#include <QtTest>
#include "syncenginetestutils.h"
#include <syncengine.h>
#include <configfile.h>
using namespace OCC;
static void changeAllFileId(FileInfo &info) {
info.fileId = generateFileId();
if (!info.isDir)
return;
info.etag = generateEtag();
for (auto &child : info.children) {
changeAllFileId(child);
}
}
/*
* This test ensure that the SyncEngine::aboutToRemoveAllFiles is correctly called and that when
* we the user choose to remove all files SyncJournalDb::clearFileTable makes works as expected
*/
class TestAllFilesDeleted : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
}
void testAllFilesDeletedKeep_data()
{
QTest::addColumn<bool>("deleteOnRemote");
QTest::newRow("local") << false;
QTest::newRow("remote") << true;
}
/*
* In this test, all files are deleted in the client, or the server, and we simulate
* that the users press "keep"
*/
void testAllFilesDeletedKeep()
{
QFETCH(bool, deleteOnRemote);
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
ConfigFile config;
config.setPromptDeleteFiles(true);
//Just set a blacklist so we can check it is still there. This directory does not exists but
// that does not matter for our purposes.
QStringList selectiveSyncBlackList = { "Q/" };
fakeFolder.syncEngine().journal()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList,
selectiveSyncBlackList);
auto initialState = fakeFolder.currentLocalState();
int aboutToRemoveAllFilesCalled = 0;
QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
[&](SyncFileItem::Direction dir, std::function<void(bool)> callback) {
QCOMPARE(aboutToRemoveAllFilesCalled, 0);
aboutToRemoveAllFilesCalled++;
QCOMPARE(dir, deleteOnRemote ? SyncFileItem::Down : SyncFileItem::Up);
callback(true);
fakeFolder.syncEngine().journal()->clearFileTable(); // That's what Folder is doing
});
auto &modifier = deleteOnRemote ? fakeFolder.remoteModifier() : static_cast<FileModifier&>(fakeFolder.localModifier());
const auto childrenKeys = fakeFolder.currentRemoteState().children.keys();
for (const auto &key : childrenKeys) {
modifier.remove(key);
}
QVERIFY(!fakeFolder.syncOnce()); // Should fail because we cancel the sync
QCOMPARE(aboutToRemoveAllFilesCalled, 1);
// Next sync should recover all files
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), initialState);
QCOMPARE(fakeFolder.currentRemoteState(), initialState);
// The selective sync blacklist should be not have been deleted.
bool ok = true;
QCOMPARE(fakeFolder.syncEngine().journal()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok),
selectiveSyncBlackList);
}
void testAllFilesDeletedDelete_data()
{
testAllFilesDeletedKeep_data();
}
/*
* This test is like the previous one but we simulate that the user presses "delete"
*/
void testAllFilesDeletedDelete()
{
QFETCH(bool, deleteOnRemote);
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
int aboutToRemoveAllFilesCalled = 0;
QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
[&](SyncFileItem::Direction dir, std::function<void(bool)> callback) {
QCOMPARE(aboutToRemoveAllFilesCalled, 0);
aboutToRemoveAllFilesCalled++;
QCOMPARE(dir, deleteOnRemote ? SyncFileItem::Down : SyncFileItem::Up);
callback(false);
});
auto &modifier = deleteOnRemote ? fakeFolder.remoteModifier() : static_cast<FileModifier&>(fakeFolder.localModifier());
const auto childrenKeys = fakeFolder.currentRemoteState().children.keys();
for (const auto &key : childrenKeys) {
modifier.remove(key);
}
QVERIFY(fakeFolder.syncOnce()); // Should succeed, and all files must then be deleted
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentLocalState().children.count(), 0);
// Try another sync to be sure.
QVERIFY(fakeFolder.syncOnce()); // Should succeed (doing nothing)
QCOMPARE(aboutToRemoveAllFilesCalled, 1); // should not have been called.
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentLocalState().children.count(), 0);
}
void testNotDeleteMetaDataChange() {
/**
* This test make sure that we don't popup a file deleted message if all the metadata have
* been updated (for example when the server is upgraded or something)
**/
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
// We never remove all files.
QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
[&] { QVERIFY(false); });
QVERIFY(fakeFolder.syncOnce());
const auto childrenKeys = fakeFolder.currentRemoteState().children.keys();
for (const auto &s : childrenKeys) {
fakeFolder.syncJournal().avoidRenamesOnNextSync(s); // clears all the fileid and inodes.
}
fakeFolder.localModifier().remove("A/a1");
auto expectedState = fakeFolder.currentLocalState();
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), expectedState);
QCOMPARE(fakeFolder.currentRemoteState(), expectedState);
fakeFolder.remoteModifier().remove("B/b1");
changeAllFileId(fakeFolder.remoteModifier());
expectedState = fakeFolder.currentRemoteState();
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), expectedState);
QCOMPARE(fakeFolder.currentRemoteState(), expectedState);
}
void testResetServer()
{
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
int aboutToRemoveAllFilesCalled = 0;
QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
[&](SyncFileItem::Direction dir, std::function<void(bool)> callback) {
QCOMPARE(aboutToRemoveAllFilesCalled, 0);
aboutToRemoveAllFilesCalled++;
QCOMPARE(dir, SyncFileItem::Down);
callback(false);
});
// Some small changes
fakeFolder.localModifier().mkdir("Q");
fakeFolder.localModifier().insert("Q/q1");
fakeFolder.localModifier().appendByte("B/b1");
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(aboutToRemoveAllFilesCalled, 0);
// Do some change locally
fakeFolder.localModifier().appendByte("A/a1");
// reset the server.
fakeFolder.remoteModifier() = FileInfo::A12_B12_C12_S12();
// Now, aboutToRemoveAllFiles with down as a direction
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(aboutToRemoveAllFilesCalled, 1);
}
void testDataFingetPrint_data()
{
QTest::addColumn<bool>("hasInitialFingerPrint");
QTest::newRow("initial finger print") << true;
QTest::newRow("no initial finger print") << false;
}
void testDataFingetPrint()
{
QFETCH(bool, hasInitialFingerPrint);
FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
fakeFolder.remoteModifier().setContents("C/c1", 'N');
fakeFolder.remoteModifier().setModTime("C/c1", QDateTime::currentDateTimeUtc().addDays(-2));
fakeFolder.remoteModifier().remove("C/c2");
if (hasInitialFingerPrint) {
fakeFolder.remoteModifier().extraDavProperties = "<oc:data-fingerprint>initial_finger_print</oc:data-fingerprint>";
} else {
//Server support finger print, but none is set.
fakeFolder.remoteModifier().extraDavProperties = "<oc:data-fingerprint></oc:data-fingerprint>";
}
int fingerprintRequests = 0;
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation, const QNetworkRequest &request, QIODevice *stream) -> QNetworkReply * {
auto verb = request.attribute(QNetworkRequest::CustomVerbAttribute).toString();
if (verb == "PROPFIND") {
auto data = stream->readAll();
if (data.contains("data-fingerprint")) {
if (request.url().path().endsWith("dav/files/admin/")) {
++fingerprintRequests;
} else {
fingerprintRequests = -10000; // fingerprint queried on incorrect path
}
}
}
return nullptr;
});
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fingerprintRequests, 1);
// First sync, we did not change the finger print, so the file should be downloaded as normal
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find("C/c1")->contentChar, 'N');
QVERIFY(!fakeFolder.currentRemoteState().find("C/c2"));
/* Simulate a backup restoration */
// A/a1 is an old file
fakeFolder.remoteModifier().setContents("A/a1", 'O');
fakeFolder.remoteModifier().setModTime("A/a1", QDateTime::currentDateTimeUtc().addDays(-2));
// B/b1 did not exist at the time of the backup
fakeFolder.remoteModifier().remove("B/b1");
// B/b2 was uploaded by another user in the mean time.
fakeFolder.remoteModifier().setContents("B/b2", 'N');
fakeFolder.remoteModifier().setModTime("B/b2", QDateTime::currentDateTimeUtc().addDays(2));
// C/c3 was removed since we made the backup
fakeFolder.remoteModifier().insert("C/c3_removed");
// C/c4 was moved to A/a2 since we made the backup
fakeFolder.remoteModifier().rename("A/a2", "C/old_a2_location");
// The admin sets the data-fingerprint property
fakeFolder.remoteModifier().extraDavProperties = "<oc:data-fingerprint>new_finger_print</oc:data-fingerprint>";
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fingerprintRequests, 2);
auto currentState = fakeFolder.currentLocalState();
// Although the local file is kept as a conflict, the server file is downloaded
QCOMPARE(currentState.find("A/a1")->contentChar, 'O');
auto conflict = findConflict(currentState, "A/a1");
QVERIFY(conflict);
QCOMPARE(conflict->contentChar, 'W');
fakeFolder.localModifier().remove(conflict->path());
// b1 was restored (re-uploaded)
QVERIFY(currentState.find("B/b1"));
// b2 has the new content (was not restored), since its mode time goes forward in time
QCOMPARE(currentState.find("B/b2")->contentChar, 'N');
conflict = findConflict(currentState, "B/b2");
QVERIFY(conflict); // Just to be sure, we kept the old file in a conflict
QCOMPARE(conflict->contentChar, 'W');
fakeFolder.localModifier().remove(conflict->path());
// We actually do not remove files that technically should have been removed (we don't want data-loss)
QVERIFY(currentState.find("C/c3_removed"));
QVERIFY(currentState.find("C/old_a2_location"));
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
}
void testSingleFileRenamed() {
FakeFolder fakeFolder{FileInfo{}};
int aboutToRemoveAllFilesCalled = 0;
QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
[&](SyncFileItem::Direction , std::function<void(bool)> ) {
aboutToRemoveAllFilesCalled++;
QFAIL("should not be called");
});
// add a single file
fakeFolder.localModifier().insert("hello.txt");
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(aboutToRemoveAllFilesCalled, 0);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// rename it
fakeFolder.localModifier().rename("hello.txt", "goodbye.txt");
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(aboutToRemoveAllFilesCalled, 0);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
}
void testSelectiveSyncNoPopup() {
// Unselecting all folder should not cause the popup to be shown
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
int aboutToRemoveAllFilesCalled = 0;
QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
[&](SyncFileItem::Direction , std::function<void(bool)>) {
aboutToRemoveAllFilesCalled++;
QFAIL("should not be called");
});
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(aboutToRemoveAllFilesCalled, 0);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
fakeFolder.syncEngine().journal()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList,
QStringList() << "A/" << "B/" << "C/" << "S/");
QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), FileInfo{}); // all files should be one locally
QCOMPARE(fakeFolder.currentRemoteState(), FileInfo::A12_B12_C12_S12()); // Server not changed
QCOMPARE(aboutToRemoveAllFilesCalled, 0); // But we did not show the popup
}
};
QTEST_GUILESS_MAIN(TestAllFilesDeleted)
#include "testallfilesdeleted.moc"
|