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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
|
/*
* 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 "testutils/syncenginetestutils.h"
#include "testutils/testutils.h"
#include "common/filesystembase.h"
#include "libsync/syncengine.h"
#include <QtTest>
using namespace std::chrono_literals;
using namespace OCC::FileSystem::SizeLiterals;
using namespace OCC;
namespace {
/* Upload a 1/3 of a file of given size.
* fakeFolder needs to be synchronized */
void partialUpload(FakeFolder &fakeFolder, const QString &name, quint64 size)
{
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.uploadState().children.count(), 0); // The state should be clean
fakeFolder.localModifier().insert(name, size);
QVERIFY(fakeFolder.applyLocalModificationsWithoutSync()); // non-vfs test, so force modifications to disk immediately
// Abort when the upload is at 1/3
std::optional<quint64> sizeWhenAbort;
auto con = QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::transmissionProgress,
[&](const ProgressInfo &progress) {
if (progress.completedSize() > (progress.totalSize() /3 )) {
sizeWhenAbort = progress.completedSize();
fakeFolder.syncEngine().abort({});
}
});
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // there should have been an error
QObject::disconnect(con);
QVERIFY(sizeWhenAbort.has_value());
QVERIFY(sizeWhenAbort < size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1); // the transfer was done with chunking
auto upStateChildren = fakeFolder.uploadState().children.first().children;
QCOMPARE(sizeWhenAbort, std::accumulate(upStateChildren.cbegin(), upStateChildren.cend(), 0, [](int s, const FileInfo &i) { return s + i.contentSize; }));
}
// Reduce max chunk size a bit so we get more chunks
void setChunkSize(SyncEngine &engine, qint64 size)
{
SyncOptions options = engine.syncOptions();
options._maxChunkSize = size;
options._initialChunkSize = size;
options._minChunkSize = size;
engine.setSyncOptions(options);
}
} // anonymous namespace
class TestChunkingNG : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testFileUpload() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
const auto size = 10_MiB; // 10 MB
fakeFolder.localModifier().insert(QStringLiteral("A/a0"), size);
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.uploadState().children.count(), 1); // the transfer was done with chunking
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size);
// Check that another upload of the same file also work.
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"));
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.uploadState().children.count(), 2); // the transfer was done with chunking
}
// Test resuming when there's a confusing chunk added
void testResume1() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const auto size = 10_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
auto chunkingId = fakeFolder.uploadState().children.first().name;
const auto &chunkMap = fakeFolder.uploadState().children.first().children;
uint64_t uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0_MiB, [](uint64_t s, const FileInfo &f) { return s + f.contentSize; });
QVERIFY(uploadedSize > 2_MiB); // at least 2 MB
// Add a fake chunk to make sure it gets deleted
fakeFolder.uploadState().children.first().insert(QStringLiteral("10000"), size);
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
if (op == QNetworkAccessManager::DeleteOperation) {
Q_ASSERT(request.url().path().endsWith(QStringLiteral("/10000")));
}
return nullptr;
});
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size);
// The same chunk id was re-used
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
}
// Test resuming when one of the uploaded chunks got removed
void testResume2() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
const int size = 150 * 1000 * 1000; // 30 MB
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
auto chunkingId = fakeFolder.uploadState().children.first().name;
const auto &chunkMap = fakeFolder.uploadState().children.first().children;
qint64 uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0LL, [](qint64 s, const FileInfo &f) { return s + f.contentSize; });
QVERIFY(uploadedSize > 2 * 1000 * 1000); // at least 50 MB
QVERIFY(chunkMap.size() >= 3); // at least three chunks
QStringList chunksToDelete;
// Remove the second chunk, so all further chunks will be deleted and resent
auto firstChunk = chunkMap.first();
auto secondChunk = *(++chunkMap.begin());
for (auto it = ++(++chunkMap.cbegin()); it != chunkMap.cend(); ++it) {
chunksToDelete.append(it.key());
}
fakeFolder.uploadState().children.first().remove(secondChunk.name);
QStringList deletedPaths;
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
if (op == QNetworkAccessManager::DeleteOperation) {
deletedPaths.append(request.url().path());
}
return nullptr;
});
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
for (const auto& toDelete : chunksToDelete) {
bool wasDeleted = false;
for (const auto& deleted : deletedPaths) {
if (deleted.mid(deleted.lastIndexOf(QLatin1Char('/')) + 1) == toDelete) {
wasDeleted = true;
break;
}
}
QVERIFY(wasDeleted);
}
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size);
// The same chunk id was re-used
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
}
// Test resuming when all chunks are already present
void testResume3() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const auto size = 30_MiB;
setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
auto chunkingId = fakeFolder.uploadState().children.first().name;
const auto &chunkMap = fakeFolder.uploadState().children.first().children;
uint64_t uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0LL, [](qint64 s, const FileInfo &f) { return s + f.contentSize; });
QVERIFY(uploadedSize > 5_MiB); // at least 5 MB
// Add a chunk that makes the file completely uploaded
fakeFolder.uploadState().children.first().insert(QString::number(uploadedSize).rightJustified(16, QLatin1Char('0')), size - uploadedSize);
bool sawPut = false;
bool sawDelete = false;
bool sawMove = false;
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
if (op == QNetworkAccessManager::PutOperation) {
sawPut = true;
} else if (op == QNetworkAccessManager::DeleteOperation) {
sawDelete = true;
} else if (request.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray() == "MOVE") {
sawMove = true;
}
return nullptr;
});
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QVERIFY(sawMove);
QVERIFY(!sawPut);
QVERIFY(!sawDelete);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size);
// The same chunk id was re-used
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
}
// Test resuming (or rather not resuming!) for the error case of the sum of
// chunk sizes being larger than the file size
void testResume4() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const auto size = 30_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
auto chunkingId = fakeFolder.uploadState().children.first().name;
const auto &chunkMap = fakeFolder.uploadState().children.first().children;
quint64 uploadedSize = std::accumulate(chunkMap.begin(), chunkMap.end(), 0LL, [](qint64 s, const FileInfo &f) { return s + f.contentSize; });
QVERIFY(uploadedSize > 5_MiB); // at least 5 MB
// Add a chunk that makes the file more than completely uploaded
fakeFolder.uploadState().children.first().insert(QString::number(uploadedSize).rightJustified(16, QLatin1Char('0')), size - uploadedSize + 100);
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
}
// Check what happens when we abort during the final MOVE and the
// the final MOVE takes longer than the abort-delay
void testLateAbortHard()
{
FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
const auto size = 15_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
// Make the MOVE never reply, but trigger a client-abort and apply the change remotely
QObject parent;
QByteArray moveChecksumHeader;
int nGET = 0;
const auto responseDelay = 24h; // bigger than abort-wait timeout
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
if (request.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray() == "MOVE") {
QTimer::singleShot(50ms, &parent, [&]() { fakeFolder.syncEngine().abort({}); });
moveChecksumHeader = request.rawHeader("OC-Checksum");
return new DelayedReply<FakeChunkMoveReply>(responseDelay, fakeFolder.uploadState(), fakeFolder.remoteModifier(), op, request, &parent);
} else if (op == QNetworkAccessManager::GetOperation) {
nGET++;
}
return nullptr;
});
// Test 1: NEW file aborted
fakeFolder.localModifier().insert(QStringLiteral("A/a0"), size);
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // error: abort!
// Now the next sync gets a NEW/NEW conflict and since there's no checksum
// it just becomes a UPDATE_METADATA
auto checkIsUpdateMetaData = [&](const SyncFileItemSet &items) {
QCOMPARE(items.size(), 2);
auto it = items.cbegin();
QCOMPARE(it->get()->_file, QLatin1String("A"));
QCOMPARE(it->get()->instruction(), CSYNC_INSTRUCTION_UPDATE_METADATA);
it++;
QCOMPARE(it->get()->_file, QLatin1String("A/a0"));
QCOMPARE(it->get()->instruction(), CSYNC_INSTRUCTION_UPDATE_METADATA);
QCOMPARE(it->get()->_etag, QString::fromUtf8(fakeFolder.remoteModifier().find(QStringLiteral("A/a0"))->etag));
};
auto checkEtagUpdated = [&](const SyncFileItemPtr &item) {
if (item->_file == QLatin1String("A/a0")) {
SyncJournalFileRecord record;
QVERIFY(fakeFolder.syncJournal().getFileRecord(QByteArray("A/a0"), &record));
QCOMPARE(record._etag, fakeFolder.remoteModifier().find(QStringLiteral("A/a0"))->etag);
QCOMPARE(item->_etag, QString::fromUtf8(fakeFolder.remoteModifier().find(QStringLiteral("A/a0"))->etag));
}
};
auto connection1 = connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToPropagate, checkIsUpdateMetaData);
auto connection2 = connect(&fakeFolder.syncEngine(), &SyncEngine::itemCompleted, checkEtagUpdated);
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
disconnect(connection1);
disconnect(connection2);
QCOMPARE(nGET, 0);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// Test 2: modified file upload aborted
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"));
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // error: abort!
// An EVAL/EVAL conflict is also UPDATE_METADATA when there's no checksums
connection1 = connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToPropagate, checkIsUpdateMetaData);
connection2 = connect(&fakeFolder.syncEngine(), &SyncEngine::itemCompleted, checkEtagUpdated);
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
disconnect(connection1);
disconnect(connection2);
QCOMPARE(nGET, 0);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// Test 3: modified file upload aborted, with good checksums
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"));
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // error: abort!
// Set the remote checksum -- the test setup doesn't do it automatically
QVERIFY(!moveChecksumHeader.isEmpty());
fakeFolder.remoteModifier().find(QStringLiteral("A/a0"))->checksums = moveChecksumHeader;
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(nGET, 0); // no new download, just a metadata update!
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// Test 4: New file, that gets deleted locally before the next sync
fakeFolder.localModifier().insert(QStringLiteral("A/a3"), size);
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // error: abort!
fakeFolder.localModifier().remove(QStringLiteral("A/a3"));
// bug: in this case we must expect a re-download of A/A3
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(nGET, 1);
QVERIFY(fakeFolder.currentLocalState().find(QStringLiteral("A/a3")));
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
}
// Check what happens when we abort during the final MOVE.
// The move succeeds on the server but as we abort it we won't realize in time.
// This means that the current sync fails, as it is aborted, but the MOVE was still performed on the server.
// Resulting in the local state being the same as the remote state.
void testLateAbortRecoverable()
{
FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
const auto size = 15_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
// Make the MOVE never reply, but trigger a client-abort and apply the change remotely
QObject parent;
const auto responseDelay = 200ms; // smaller than abort-wait timeout
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * {
if (request.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray() == "MOVE") {
QTimer::singleShot(50ms, &parent, [&]() { fakeFolder.syncEngine().abort({}); });
// while the response is delayed, the move is performed in the constructor, thus it happens immediately
return new DelayedReply<FakeChunkMoveReply>(responseDelay, fakeFolder.uploadState(), fakeFolder.remoteModifier(), op, request, &parent);
}
return nullptr;
});
// Test 1: NEW file aborted
fakeFolder.localModifier().insert(QStringLiteral("A/a0"), size);
// the sync will be aborted and thus fail
QVERIFY(!fakeFolder.applyLocalModificationsAndSync());
// the move was still performed
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// update the meta data after the aborted sync
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// Test 2: modified file upload aborted
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"));
// the sync will be aborted and thus fail
QVERIFY(!fakeFolder.applyLocalModificationsAndSync());
// the move was still performed
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// update the meta data after the aborted sync
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
}
// We modify the file locally after it has been partially uploaded
void testRemoveStale1() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const auto size = 10_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
auto chunkingId = fakeFolder.uploadState().children.first().name;
const auto a0size = fakeFolder.currentLocalState().find(QStringLiteral("A/a0"))->contentSize;
fakeFolder.localModifier().setContents(QStringLiteral("A/a0"), a0size, 'B');
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"), 'B');
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size + 1);
// A different chunk id was used, and the previous one is removed
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
QVERIFY(fakeFolder.uploadState().children.first().name != chunkingId);
}
// We remove the file locally after it has been partially uploaded
void testRemoveStale2() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const auto size = 10_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
fakeFolder.localModifier().remove(QStringLiteral("A/a0"));
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.uploadState().children.count(), 0);
}
void testCreateConflictWhileSyncing() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const quint64 size = 10_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
// Put a file on the server and download it.
fakeFolder.remoteModifier().insert(QStringLiteral("A/a0"), size);
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// Modify the file localy and start the upload
fakeFolder.localModifier().setContents(QStringLiteral("A/a0"), size, 'B');
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"), 'B');
// But in the middle of the sync, modify the file on the server
QMetaObject::Connection con = QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::transmissionProgress, [&](const ProgressInfo &progress) {
qDebug() << progress.completedSize() << progress.totalSize();
if (progress.completedSize() > (progress.totalSize() / 2)) {
auto a0size = fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize;
fakeFolder.remoteModifier().setContents(QStringLiteral("A/a0"), a0size, 'C');
QObject::disconnect(con);
}
});
QVERIFY(!fakeFolder.applyLocalModificationsAndSync());
// There was a precondition failed error, this means wen need to sync again
QCOMPARE(fakeFolder.syncEngine().isAnotherSyncNeeded(), true);
QCOMPARE(fakeFolder.uploadState().children.count(), 1); // We did not clean the chunks at this point
// Now we will download the server file and create a conflict
fakeFolder.syncJournal().wipeErrorBlacklist();
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
auto localState = fakeFolder.currentLocalState();
// A0 is the one from the server
QCOMPARE(localState.find(QStringLiteral("A/a0"))->contentSize, size);
QCOMPARE(localState.find(QStringLiteral("A/a0"))->contentChar, 'C');
// There is a conflict file with our version
auto &stateAChildren = localState.find(QStringLiteral("A"))->children;
auto it = std::find_if(stateAChildren.cbegin(), stateAChildren.cend(), [&](const FileInfo &fi) {
return fi.name.startsWith(QLatin1String("a0 (conflicted copy"));
});
QVERIFY(it != stateAChildren.cend());
QCOMPARE(it->contentChar, 'B');
QCOMPARE(it->contentSize, size + 1);
// Remove the conflict file so the comparison works!
fakeFolder.localModifier().remove(QStringLiteral("A/") + it->name);
QVERIFY(fakeFolder.applyLocalModificationsWithoutSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.uploadState().children.count(), 0); // The last sync cleaned the chunks
}
void testModifyLocalFileWhileUploading() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const quint64 size = 10_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
fakeFolder.localModifier().insert(QStringLiteral("A/a0"), size, 'A');
// middle of the sync, modify the file
QMetaObject::Connection con = QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::transmissionProgress,
[&](const ProgressInfo &progress) {
if (progress.completedSize() > (progress.totalSize() / 2 )) {
fakeFolder.localModifier().setContents(QStringLiteral("A/a0"), size, 'B');
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"), 'B');
QVERIFY(fakeFolder.applyLocalModificationsWithoutSync());
QObject::disconnect(con);
}
});
// we will get OCC::SyncFileItem::Message and error: "Local file changed during sync. It will be resumed."
// so a message but we report a successful sync
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QThread::sleep(1);
// There should be a followup sync
QCOMPARE(fakeFolder.syncEngine().isAnotherSyncNeeded(), true);
QCOMPARE(fakeFolder.uploadState().children.count(), 1); // We did not clean the chunks at this point
auto chunkingId = fakeFolder.uploadState().children.first().name;
// Now we make a new sync which should upload the file for good.
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size + 1);
// A different chunk id was used, and the previous one is removed
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
QVERIFY(fakeFolder.uploadState().children.first().name != chunkingId);
}
void testResumeServerDeletedChunks() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const int size = 30 * 1000 * 1000; // 30 MB
setChunkSize(fakeFolder.syncEngine(), 1 * 1000 * 1000);
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
auto chunkingId = fakeFolder.uploadState().children.first().name;
// Delete the chunks on the server
fakeFolder.uploadState().children.clear();
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size);
// A different chunk id was used
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
QVERIFY(fakeFolder.uploadState().children.first().name != chunkingId);
}
// Check what happens when the connection is dropped on the PUT (non-chunking) or MOVE (chunking)
// for on the issue #5106
void connectionDroppedBeforeEtagRecieved_data()
{
QTest::addColumn<bool>("chunking");
QTest::newRow("big file") << true;
QTest::newRow("small file") << false;
}
void connectionDroppedBeforeEtagRecieved()
{
QFETCH(bool, chunking);
FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
const auto size = chunking ? 1_MiB : 300_B;
setChunkSize(fakeFolder.syncEngine(), 300_KiB);
// Make the MOVE never reply, but trigger a client-abort and apply the change remotely
QByteArray checksumHeader;
int nGET = 0;
QScopedValueRollback<std::chrono::seconds> setHttpTimeout(AbstractNetworkJob::httpTimeout, 1s);
const auto responseDelay = 24h; // much bigger than http timeout (so a timeout will occur)
// This will perform the operation on the server, but the reply will not come to the client
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData) -> QNetworkReply * {
if (!chunking) {
Q_ASSERT(!request.url().path().contains(QStringLiteral("/uploads/")) && "Should not touch uploads endpoint when not chunking");
}
if (!chunking && op == QNetworkAccessManager::PutOperation) {
checksumHeader = request.rawHeader("OC-Checksum");
return new DelayedReply<FakePutReply>(responseDelay, fakeFolder.remoteModifier(), op, request, outgoingData->readAll(), &fakeFolder.syncEngine());
} else if (chunking && request.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray() == "MOVE") {
checksumHeader = request.rawHeader("OC-Checksum");
return new DelayedReply<FakeChunkMoveReply>(responseDelay, fakeFolder.uploadState(), fakeFolder.remoteModifier(), op, request, &fakeFolder.syncEngine());
} else if (op == QNetworkAccessManager::GetOperation) {
nGET++;
}
return nullptr;
});
// Test 1: a NEW file
fakeFolder.localModifier().insert(QStringLiteral("A/a0"), size);
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // timeout!
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState()); // but the upload succeeded
QVERIFY(!checksumHeader.isEmpty());
fakeFolder.remoteModifier().find(QStringLiteral("A/a0"))->checksums = checksumHeader; // The test system don't do that automatically
// Should be resolved properly
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(nGET, 0);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// Test 2: Modify the file further
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"));
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // timeout!
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState()); // but the upload succeeded
fakeFolder.remoteModifier().find(QStringLiteral("A/a0"))->checksums = checksumHeader;
// modify again, should not cause conflict
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"));
QVERIFY(!fakeFolder.applyLocalModificationsAndSync()); // now it's trying to upload the modified file
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
fakeFolder.remoteModifier().find(QStringLiteral("A/a0"))->checksums = checksumHeader;
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(nGET, 0);
}
void testPercentEncoding() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const auto size = 5_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
fakeFolder.localModifier().insert(QString::fromLatin1("A/file % \u20ac"), size);
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
// Only the second upload contains an "If" header
fakeFolder.localModifier().appendByte(QString::fromLatin1("A/file % \u20ac"));
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
}
// Test uploading large files (1 GiB)
void testVeryBigFiles() {
FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
const qint64 size = 1_GiB; // stay under the INT_MAX (2_gb) limit: QByteArray takes a signed int, so bigger sizes will give weird results.
// Partial upload of big files
partialUpload(fakeFolder, QStringLiteral("A/a0"), size);
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
auto chunkingId = fakeFolder.uploadState().children.first().name;
// Now resume
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size);
// The same chunk id was re-used
QCOMPARE(fakeFolder.uploadState().children.count(), 1);
QCOMPARE(fakeFolder.uploadState().children.first().name, chunkingId);
// Upload another file again, this time without interruption
fakeFolder.localModifier().appendByte(QStringLiteral("A/a0"));
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.currentRemoteState().find(QStringLiteral("A/a0"))->contentSize, size + 1);
}
// the final move fails but the succeeding sync recovers and finishes the upload
void testFinalMoveLocked()
{
FakeFolder fakeFolder { FileInfo::A12_B12_C12_S12() };
const auto size = 15_MiB;
setChunkSize(fakeFolder.syncEngine(), 1_MiB);
OperationCounter counter;
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *device) -> QNetworkReply * {
counter.serverOverride(op, request, device);
if (request.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray() == "MOVE") {
return new FakeErrorReply(op, request, this, 423, {});
}
return nullptr;
});
/* Insert a file.
* The file will be uploaded in chunks.
* in order to finish the upload the final file need to be moved to is destination.
* This move fails with a 423.
* Similar to the Folder class we will trigger a new sync but without the local discovery.
* The test ensures that we properly continue the download and only perform the final move.
*/
fakeFolder.localModifier().insert(QStringLiteral("A/a0"), size);
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QVERIFY(fakeFolder.currentLocalState() != fakeFolder.currentRemoteState());
QVERIFY(fakeFolder.syncEngine().isAnotherSyncNeeded());
QCOMPARE(counter.nMOVE, 1);
counter.reset();
fakeFolder.setServerOverride(counter.functor());
// do a partial discovery, don't actually list the local files
fakeFolder.syncEngine().setLocalDiscoveryOptions(OCC::LocalDiscoveryStyle::DatabaseAndFilesystem, {});
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
QCOMPARE(counter.nDELETE, 0);
QCOMPARE(counter.nPUT, 0);
QCOMPARE(counter.nMOVE, 1);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
QCOMPARE(fakeFolder.syncEngine().isAnotherSyncNeeded(), false);
}
};
QTEST_GUILESS_MAIN(TestChunkingNG)
#include "testchunkingng.moc"
|