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 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
|
/**
* @file sdk_test_backup_upload_operations_test.cpp
* @brief This file defines a test fixture that involves backup syncs in terms of files upload.
*/
#ifdef ENABLE_SYNC
#include "backup_test_utils.h"
using namespace sdk_test;
using namespace testing;
/**
* @class SdkTestBackupUploadsOperations
* @brief Test fixture for validating backup syncs in terms of files upload.
*/
class SdkTestBackupUploadsOperations: public SdkTestBackup
{
public:
static constexpr auto COMMON_TIMEOUT = 3min;
std::unique_ptr<FSACCESS_CLASS> mFsAccess;
/**
* @brief Sets the cleanup function to be executed during TearDown.
*
* If a custom cleanup function is provided, it will be used.
* Otherwise, a default one will be set.
*
* @example:
* - example1 (default cleanupFunction):
* auto cleanup = setCleanupFunction();
* - example2 (custom cleanupFunction):
* auto cleanup = setCleanupFunction([this](){
* // custom cleanup function code
* });
*
* @note: is mandatory calling this method at the beginning of each test of this file, otherwise
* test will fail at teardown. The reason behind is to enforce setting an appropriate cleanup
* function for each test.
*
* @param customCleanupFunction Optional custom cleanup function.
* @return The cleanup function that was set.
*/
std::unique_ptr<MrProper>
setCleanupFunction(std::function<void()> customCleanupFunction = nullptr)
{
mCleanupFunctionSet = true;
if (customCleanupFunction)
{
return std::make_unique<MrProper>(customCleanupFunction);
}
else
{
return std::make_unique<MrProper>(
[this]()
{
cleanDefaultListeners();
});
}
}
void cleanDefaultListeners()
{
removeBackupSync();
if (mMtl)
{
megaApi[0]->removeListener(mMtl.get());
mMtl.reset();
}
if (mMslStats)
{
megaApi[0]->removeListener(mMslStats.get());
mMslStats.reset();
}
if (mMslFiles)
{
megaApi[0]->removeListener(mMslFiles.get());
mMslFiles.reset();
}
}
/**
* @brief Creates a local file and waits until it is backed up.
* @param localFilePathAbs Absolute path to the local file to be created.
* @param contents The file contents to be written.
* @param customMtime Optional custom modification time.
* @param expectFullUpload If true, expects a transfer to occur (onTransferStart/Finish).
* If false, expects only sync state change (file may be cloned from existing node).
* Default is true for backward compatibility.
* @return Pair (success, shared pointer to the created LocalTempFile).
*/
std::pair<bool, shared_ptr<sdk_test::LocalTempFile>>
createLocalFileAndWaitForSync(const fs::path& localFilePathAbs,
const std::string_view contents,
std::optional<fs::file_time_type> customMtime,
bool expectFullUpload = true);
/**
* @brief Moves deconfigured backup nodes into a cloud folder.
*/
void moveDeconfiguredBackupNodesToCloud();
/**
* @brief Resets local variables that tracks when backup sync is up-to-date.
*/
void resetOnSyncStatsUpdated();
/**
* @brief Waits until the backup sync is up-to-date state.
* @return True if sync was up to date within the timeout, false otherwise.
*/
bool waitForBackupSyncUpToDate() const;
/**
* @brief Confirms that local and cloud models are identical.
*/
void confirmModels() const;
std::shared_ptr<SyncUploadOperationsTracker> addSyncListenerTracker(const std::string& s)
{
TestMutexGuard g(trackerMutex);
return mSyncListenerTrackers.add(s);
}
std::shared_ptr<SyncUploadOperationsTracker> getSyncListenerTrackerByPath(const std::string& s)
{
TestMutexGuard g(trackerMutex);
return mSyncListenerTrackers.getByPath(s);
}
std::shared_ptr<SyncUploadOperationsTransferTracker>
addTransferListenerTracker(const std::string& s)
{
TestMutexGuard g(trackerMutex);
return mTransferListenerTrackers.add(s);
}
std::shared_ptr<SyncUploadOperationsTransferTracker>
getTransferListenerTrackerByPath(const std::string& s)
{
TestMutexGuard g(trackerMutex);
return mTransferListenerTrackers.getByPath(s);
}
protected:
void SetUp() override;
void TearDown() override;
/**
* @brief Creates the `archive` destination directory in the cloud used to store deconfigured
* backup nodes.
*/
void createArchiveDestinationFolder();
/**
* @brief Returns the names of the first-level children in the local backup folder.
* @note Hidden files and DEBRISFOLDER are ignored
* @return Vector with the names of local files and directories.
*/
std::vector<std::string> getLocalFirstLevelChildrenNames() const;
/**
* @brief Gets the handle of the `archive` destination folder in the cloud.
* @return Handle of the `archive` destination folder.
*/
MegaHandle getArchiveDestinationFolderHandle() const;
/**
* @brief Gets the handle of the backup root folder in the cloud.
* @return Handle of the backup root folder.
*/
MegaHandle getBackupRootHandle() const;
/**
* @brief Recursively checks that the local and cloud models match.
* @param parentHandle handle of the cloud parent node.
* @param localPath Local relative path corresponding to the parent node.
* @return True if both models match, false otherwise.
*/
bool checkSyncRecursively(const MegaHandle parentHandle,
std::optional<std::string> localPath) const;
shared_ptr<sdk_test::LocalTempFile>
createLocalFile(const fs::path& filePath,
const std::string_view contents,
std::optional<fs::file_time_type> customMtime);
private:
mutable std::recursive_timed_mutex trackerMutex;
using TestMutexGuard = std::unique_lock<std::recursive_timed_mutex>;
std::unique_ptr<NiceMock<MockTransferListener>> mMtl;
std::unique_ptr<NiceMock<MockSyncListener>> mMslStats;
std::unique_ptr<NiceMock<MockSyncListener>> mMslFiles;
SyncItemTrackerManager<SyncUploadOperationsTracker> mSyncListenerTrackers;
SyncItemTrackerManager<SyncUploadOperationsTransferTracker> mTransferListenerTrackers;
MegaHandle mBackupRootHandle{INVALID_HANDLE};
MegaHandle mCloudArchiveBackupFolderHandle{INVALID_HANDLE};
const fs::path mCloudArchiveBackupFolderName{"BackupArchive"};
std::atomic<bool> mIsUpToDate{false};
std::shared_ptr<std::promise<void>> mSyncUpToDatePms;
std::unique_ptr<std::future<void>> mSyncFut;
bool mCleanupFunctionSet{false};
}; // class SdkTestBackupUploadsOperations
std::pair<bool, shared_ptr<sdk_test::LocalTempFile>>
SdkTestBackupUploadsOperations::createLocalFileAndWaitForSync(
const fs::path& localFilePathAbs,
const std::string_view contents,
std::optional<fs::file_time_type> customMtime,
bool expectFullUpload)
{
if (!mMtl)
{
LOG_err << "createLocalFileAndWaitForSync: invalid transfer listener";
return {false, nullptr};
}
if (!mMslFiles)
{
LOG_err << "createLocalFileAndWaitForSync: invalid sync listener";
return {false, nullptr};
}
// Always add transfer tracker to verify behavior (transfer vs clone)
auto tt = addTransferListenerTracker(localFilePathAbs.string());
if (!tt)
{
LOG_err << "Cannot add TransferListenerTracker for: " << localFilePathAbs.string();
return {false, nullptr};
}
auto st = addSyncListenerTracker(localFilePathAbs.string());
if (!st)
{
LOG_err << "Cannot add SyncListenerTracker for: " << localFilePathAbs.string();
return {false, nullptr};
}
auto localFile = createLocalFile(localFilePathAbs, contents, customMtime);
// Wait for sync state change (always expected)
auto [stFutStatus, stErrCode] = st->waitForCompletion(COMMON_TIMEOUT);
if (stFutStatus != std::future_status::ready)
{
LOG_err << "Sync state change not received for: " << localFilePathAbs.u8string();
return {false, nullptr};
}
if (stErrCode != API_OK)
{
LOG_err << "Sync failed for: " << localFilePathAbs.u8string();
return {false, nullptr};
}
// Wait for transfer with appropriate timeout:
// - Full upload: wait with COMMON_TIMEOUT for completion
// - Clone: wait briefly (30s) and expect timeout (no transfer should occur)
const auto transferTimeout = expectFullUpload ? COMMON_TIMEOUT : std::chrono::seconds(30);
auto [ttFutStatus, ttErrCode] = tt->waitForCompletion(transferTimeout);
const auto expectedTransferStatus =
expectFullUpload ? std::future_status::ready : std::future_status::timeout;
if (ttFutStatus != expectedTransferStatus)
{
LOG_err << "Unexpected transfer status for: " << localFilePathAbs.u8string()
<< " [expectFullUpload: " << expectFullUpload << "]";
return {false, nullptr};
}
// Verify transfer start count matches expectation
const int expectedTransferStartCount = expectFullUpload ? 1 : 0;
if (tt->transferStartCount.load() != expectedTransferStartCount)
{
LOG_err << "Transfer started count mismatch for: " << localFilePathAbs.u8string()
<< " [expected: " << expectedTransferStartCount
<< ", actual: " << tt->transferStartCount.load() << "]";
return {false, nullptr};
}
// Verify transfer succeeded if we expected a full upload
if (expectFullUpload && ttErrCode != API_OK)
{
LOG_err << "Transfer failed for: " << localFilePathAbs.u8string();
return {false, nullptr};
}
return {true, localFile};
}
void SdkTestBackupUploadsOperations::moveDeconfiguredBackupNodesToCloud()
{
NiceMock<MockRequestListener> reqListener{megaApi[0].get()};
reqListener.setErrorExpectations(API_OK);
megaApi[0]->moveOrRemoveDeconfiguredBackupNodes(getBackupRootHandle(),
getArchiveDestinationFolderHandle(),
&reqListener);
ASSERT_TRUE(reqListener.waitForFinishOrTimeout(MAX_TIMEOUT)) << "";
}
void SdkTestBackupUploadsOperations::resetOnSyncStatsUpdated()
{
mSyncUpToDatePms.reset(new std::promise<void>());
mSyncFut.reset(new std::future<void>(mSyncUpToDatePms->get_future()));
mIsUpToDate = false;
}
bool SdkTestBackupUploadsOperations::waitForBackupSyncUpToDate() const
{
if (!mMslStats)
return false;
return mSyncFut->wait_for(COMMON_TIMEOUT) == std::future_status::ready;
}
void SdkTestBackupUploadsOperations::confirmModels() const
{
const auto areLocalAndCloudSyncedExhaustive = [this]() -> bool
{
return checkSyncRecursively(getBackupRootHandle(), nullopt);
};
ASSERT_TRUE(waitFor(areLocalAndCloudSyncedExhaustive, COMMON_TIMEOUT, 10s));
}
void SdkTestBackupUploadsOperations::SetUp()
{
SdkTestBackup::SetUp();
mFsAccess = std::make_unique<FSACCESS_CLASS>();
ASSERT_NO_FATAL_FAILURE(createBackupSync());
ASSERT_NO_FATAL_FAILURE(createArchiveDestinationFolder());
const std::unique_ptr<MegaSync> sync{megaApi[0]->getSyncByBackupId(getBackupId())};
ASSERT_TRUE(sync);
mBackupRootHandle = sync->getMegaHandle();
// add transfer listener
mMtl.reset(new NiceMock<MockTransferListener>(megaApi[0].get()));
EXPECT_CALL(*mMtl, onTransferStart)
.WillRepeatedly(
[this](::mega::MegaApi*, ::mega::MegaTransfer* t)
{
if (!t || !t->getPath())
{
return;
}
auto element = getTransferListenerTrackerByPath(t->getPath());
if (!element)
return;
ASSERT_EQ(++element->transferStartCount, 1)
<< "Unexpected times onTransferStart has been called: " << t->getPath();
});
EXPECT_CALL(*mMtl, onTransferFinish)
.WillRepeatedly(
[this](::mega::MegaApi*, ::mega::MegaTransfer* t, ::mega::MegaError* e)
{
if (!t || !t->getPath())
{
return;
}
auto element = getTransferListenerTrackerByPath(t->getPath());
if (!element || !e)
return;
ASSERT_TRUE(!element->getActionCompleted())
<< "onTransferFinish has been previously received: " << t->getPath();
element->setActionCompleted();
element->setActionCompletedPms(e->getErrorCode());
});
megaApi[0]->addListener(mMtl.get());
// add sync listener and add EXPECT(S)
mMslStats.reset(new NiceMock<MockSyncListener>(megaApi[0].get()));
EXPECT_CALL(*mMslStats.get(), onSyncStatsUpdated(_, _))
.WillRepeatedly(
[this](MegaApi*, MegaSyncStats* stats)
{
if (stats->getBackupId() == getBackupId() && stats->getUploadCount() == 0 &&
!stats->isScanning() && !stats->isSyncing() && mSyncUpToDatePms && !mIsUpToDate)
{
mIsUpToDate = true;
mSyncUpToDatePms->set_value();
}
});
megaApi[0]->addListener(mMslStats.get());
mMslFiles.reset(new NiceMock<MockSyncListener>(megaApi[0].get()));
EXPECT_CALL(*mMslFiles.get(), onSyncFileStateChanged(_, _, _, _))
.WillRepeatedly(
[this](MegaApi*, MegaSync* sync, std::string* localPath, int newState)
{
if (sync && sync->getBackupId() == getBackupId() &&
newState == MegaApi::STATE_SYNCED && localPath)
{
auto element = getSyncListenerTrackerByPath(*localPath);
if (!element || element->getActionCompleted())
return;
element->setActionCompleted();
element->setActionCompletedPms(API_OK);
}
});
megaApi[0]->addListener(mMslFiles.get());
}
void SdkTestBackupUploadsOperations::TearDown()
{
ASSERT_TRUE(mCleanupFunctionSet) << getLogPrefix()
<< "(TearDown). cleanupfunction has not been properly set by "
"calling `setCleanupFunction()`.";
ASSERT_TRUE(!mMtl) << getLogPrefix()
<< "(TearDown). Transfer listener has not been unregistered yet";
ASSERT_TRUE(!mMslStats) << getLogPrefix()
<< "(TearDown). Sync listener has not been unregistered yet";
removeBackupSync();
SdkTestBackup::TearDown();
}
void SdkTestBackupUploadsOperations::createArchiveDestinationFolder()
{
unique_ptr<MegaNode> rootnode{megaApi[0]->getRootNode()};
ASSERT_TRUE(rootnode) << "setupDestinationDirectory: Account root node not available.";
mCloudArchiveBackupFolderHandle =
createFolder(0, path_u8string(mCloudArchiveBackupFolderName).c_str(), rootnode.get());
ASSERT_NE(mCloudArchiveBackupFolderHandle, INVALID_HANDLE)
<< "setupDestinationDirectory: Invalid destination folder handle";
}
std::vector<std::string> SdkTestBackupUploadsOperations::getLocalFirstLevelChildrenNames() const
{
fs::path localFolderPath = getLocalFolderPath();
return sdk_test::getLocalFirstChildrenNames_if(localFolderPath,
[](const std::string& name)
{
return name.front() != '.' &&
name != DEBRISFOLDER;
});
}
MegaHandle SdkTestBackupUploadsOperations::getArchiveDestinationFolderHandle() const
{
return mCloudArchiveBackupFolderHandle;
}
MegaHandle SdkTestBackupUploadsOperations::getBackupRootHandle() const
{
return mBackupRootHandle;
}
bool SdkTestBackupUploadsOperations::checkSyncRecursively(
const MegaHandle parentHandle,
std::optional<std::string> localPath) const
{
auto [childrenCloudNames, childrenNodeList] =
getCloudFirstChildren(megaApi[0].get(), parentHandle);
if (!childrenCloudNames.has_value() || !childrenNodeList)
{
return false;
}
const auto localChildrenNames = getLocalFirstLevelChildrenNames();
if (!Value(localChildrenNames, UnorderedElementsAreArray(childrenCloudNames.value())))
{
return false;
}
for (int i = 0; i < childrenNodeList->size(); ++i)
{
auto childNode = childrenNodeList->get(i);
if (!childNode)
{
return false;
}
const std::string childLocalPath = !localPath.has_value() ?
childNode->getName() :
localPath.value() + "/" + childNode->getName();
if (childNode->isFolder() && !checkSyncRecursively(childNode->getHandle(), childLocalPath))
{
return false;
}
}
return true;
}
shared_ptr<sdk_test::LocalTempFile>
SdkTestBackupUploadsOperations::createLocalFile(const fs::path& filePath,
const std::string_view contents,
std::optional<fs::file_time_type> customMtime)
{
return std::make_shared<sdk_test::LocalTempFile>(filePath, contents, customMtime);
}
/**
* @test SdkTestBackupUploadsOperations.BasicTest
*
* 1. Create multiple local file in the backup directory and ensure it's synced.
* 2. Suspend the backup sync and move backup nodes to the cloud.
* 3. Confirm that local and remote models match.
*/
TEST_F(SdkTestBackupUploadsOperations, BasicTest)
{
static const auto logPre{getLogPrefix()};
LOG_verbose << logPre << "#### Test body started ####";
// Add cleanup function to unregister listeners as soon as test fail/finish
const auto cleanup = setCleanupFunction();
// Reset MockSyncListener related promise/future
resetOnSyncStatsUpdated();
auto localBasePath{fs::absolute(getLocalFolderPath())};
LOG_debug << logPre << "#### TC1 Creating local file `file1` in Backup dir ####";
auto [res1, localFile1] = createLocalFileAndWaitForSync(localBasePath / "file1",
"abcde",
fs::file_time_type::clock::now());
ASSERT_TRUE(res1) << "Cannot create local file `file1`";
LOG_debug << "#### TC2 wait until all files (in Backup folder) have been synced ####";
ASSERT_TRUE(waitForBackupSyncUpToDate());
LOG_debug << logPre << "#### TC3 Ensure local and cloud drive structures matches ####";
ASSERT_NO_FATAL_FAILURE(confirmModels());
LOG_verbose << logPre << "#### Test finished ####";
}
/**
* @test SdkTestBackupUploadsOperations.NodesRemoteCopyUponResumingBackup
*
* 1. Create multiple local files in the backup directory and ensure they are synced.
* 2. Suspend the backup sync and move backup nodes to the cloud.
* 3. Remove the suspended sync, then set up the backup sync again.
* 4. Resume backup sync and ensure files are synced (remote copy must be done)
* 5. Confirm that local and remote models match.
*/
TEST_F(SdkTestBackupUploadsOperations, NodesRemoteCopyUponResumingBackup)
{
std::shared_ptr<NiceMock<MockTransferListener>> auxMtl;
static const auto logPre{getLogPrefix()};
LOG_verbose << logPre << "#### Test body started ####";
// Add cleanup function to unregister listeners as soon as test fail/finish
const auto cleanup = setCleanupFunction(
[this, auxMtl]()
{
cleanDefaultListeners();
if (auxMtl)
{
megaApi[0]->removeListener(auxMtl.get());
}
});
constexpr unsigned numFiles{3};
auto localBasePath{fs::absolute(getLocalFolderPath())};
std::vector<std::shared_ptr<sdk_test::LocalTempFile>> localFiles;
auto mtime = fs::file_time_type::clock::now();
for (unsigned i = 1; i <= numFiles; ++i)
{
auto auxMtime = mtime + std::chrono::seconds(MIN_ALLOW_MTIME_DIFFERENCE * i);
std::string filename = "file" + std::to_string(i);
LOG_debug << logPre << "#### TC " << std::to_string(i) << " Creating local file `"
<< filename << "` in Backup dir ####";
// First file triggers a full upload. Subsequent files with same content
// but different mtime will be cloned (no transfer expected).
const bool expectFullUpload = (i == 1);
auto [res, localFile] = createLocalFileAndWaitForSync(localBasePath / filename,
"abcde",
auxMtime,
expectFullUpload);
ASSERT_TRUE(res) << "Cannot create local file `" << filename << "`";
localFiles.push_back(localFile);
}
LOG_debug << logPre << "#### TC4 suspending sync ####";
ASSERT_NO_FATAL_FAILURE(suspendBackupSync());
LOG_debug << logPre << "#### TC5 moving backup nodes to Cloud ####";
ASSERT_NO_FATAL_FAILURE(moveDeconfiguredBackupNodesToCloud());
LOG_debug << logPre << "#### TC6 removing suspending ####";
removeBackupSync();
LOG_debug << logPre << "#### TC7 setup sync (again) ####";
createBackupSync();
resetOnSyncStatsUpdated();
// Clone Put nodes (no transfer is created)
auxMtl.reset(new NiceMock<MockTransferListener>(megaApi[0].get()));
EXPECT_CALL(*auxMtl.get(), onTransferStart).Times(0);
EXPECT_CALL(*auxMtl.get(), onTransferFinish).Times(0);
megaApi[0]->addListener(auxMtl.get());
LOG_debug << logPre << "#### TC8 resuming sync ####";
ASSERT_NO_FATAL_FAILURE(resumeBackupSync());
LOG_debug << logPre
<< "#### TC9 wait until all files (in Backup folder) have been synced ####";
ASSERT_TRUE(waitForBackupSyncUpToDate());
LOG_debug << logPre << "#### TC10 ensure local and cloud drive models match ####";
ASSERT_NO_FATAL_FAILURE(confirmModels());
LOG_verbose << logPre << "#### Test finished ####";
}
/**
* @test SdkTestBackupUploadsOperations.UpdateNodeMtime
*
* 1. Create a local file in the backup directory and ensure it is synced.
* 2. Wait until the backup sync is up to date.
* 3. Update mtime to local file and wait for notification that confirms change.
* 4. Confirm that local and remote (cloud) models match.
*/
TEST_F(SdkTestBackupUploadsOperations, UpdateNodeMtime)
{
static const auto logPre{getLogPrefix()};
LOG_verbose << logPre << "#### Test body started ####";
// Add cleanup function to unregister listeners as soon as test fail/finish
const auto cleanup = setCleanupFunction();
// Reset MockSyncListener related promise/future
resetOnSyncStatsUpdated();
auto localBasePath{fs::absolute(getLocalFolderPath())};
LOG_debug << logPre << "#### TC1 Creating local file `file1` in Backup dir ####";
auto [res1, localFile1] = createLocalFileAndWaitForSync(localBasePath / "file1",
"abcde",
fs::file_time_type::clock::now());
ASSERT_TRUE(res1) << "Cannot create local file `file1`";
LOG_debug << "#### TC2 wait until all files (in Backup folder) have been synced ####";
ASSERT_TRUE(waitForBackupSyncUpToDate());
resetOnSyncStatsUpdated();
std::unique_ptr<MegaSync> backupSync(megaApi[0]->getSyncByBackupId(getBackupId()));
ASSERT_TRUE(backupSync) << "Cannot get backup sync";
std::unique_ptr<MegaNode> backupNode(megaApi[0]->getNodeByHandle(backupSync->getMegaHandle()));
ASSERT_TRUE(backupNode) << "Cannot get backup sync";
std::unique_ptr<MegaNode> fileNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), "file1", FILENODE));
ASSERT_TRUE(fileNode) << "Cannot get file node";
bool mTimeChangeRecv{false};
mApi[0].mOnNodesUpdateCompletion =
[&mTimeChangeRecv,
oldMtime = fileNode->getModificationTime(),
targetNodeHandle = fileNode->getHandle()](size_t, MegaNodeList* nodes)
{
ASSERT_TRUE(nodes) << "Invalid meganode list received";
for (int i = 0; i < nodes->size(); ++i)
{
MegaNode* n = nodes->get(i);
if (n && n->getHandle() == targetNodeHandle &&
n->hasChanged(static_cast<uint64_t>(MegaNode::CHANGE_TYPE_ATTRIBUTES)) &&
oldMtime != n->getModificationTime())
{
mTimeChangeRecv = true;
}
}
};
LOG_debug << logPre << "#### TC3 Update mtime to local file ####";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(localBasePath / "file1")),
m_time(nullptr));
ASSERT_TRUE(waitForResponse(&mTimeChangeRecv))
<< "No mtime change received after " << maxTimeout << " seconds";
resetOnNodeUpdateCompletionCBs(); // important to reset
LOG_debug << logPre << "#### TC4 Ensure local and cloud drive structures matches ####";
ASSERT_NO_FATAL_FAILURE(confirmModels());
LOG_verbose << logPre << "#### Test finished ####";
}
/**
* @test SdkTestBackupUploadsOperations.getnodesByFingerprintNoMtime
*
* 1. Create '3' local files in the backup directory and ensure they are synced.
* 2. Validate `getNodesByFingerprint` and `getNodesByFingerprintIgnoringMtime` results
* 3. Modify the mtime of local file (idx_0) setting mtime of local file (idx_2) and wait for sync.
* 4. Modify the mtime of local file (idx_1) setting mtime of local file (idx_2) and wait for sync.
* 5. Validate `getNodesByFingerprint` and `getNodesByFingerprintIgnoringMtime` results
* 6. Confirm that local and remote (cloud) models match.
*/
TEST_F(SdkTestBackupUploadsOperations, getnodesByFingerprintNoMtime)
{
static const auto logPre{getLogPrefix()};
LOG_verbose << logPre << "#### Test body started ####";
// Add cleanup function to unregister listeners as soon as test fail/finish
const auto cleanup = setCleanupFunction();
std::unique_ptr<MegaSync> backupSync(megaApi[0]->getSyncByBackupId(getBackupId()));
ASSERT_TRUE(backupSync) << "Cannot get backup sync";
std::unique_ptr<MegaNode> backupNode(megaApi[0]->getNodeByHandle(backupSync->getMegaHandle()));
ASSERT_TRUE(backupNode) << "Cannot get backup sync node";
constexpr unsigned numFiles{3};
auto localBasePath{fs::absolute(getLocalFolderPath())};
std::vector<std::pair<std::shared_ptr<sdk_test::LocalTempFile>, fs::file_time_type>> localFiles;
std::vector<std::string> filenames;
LOG_debug << logPre << "#### TC1: create (" << numFiles
<< ") local files and wait until back up has been completed ####";
auto mtime{fs::file_time_type::clock::now()};
for (unsigned i = 1; i <= numFiles; ++i)
{
const auto auxMtime{mtime + std::chrono::seconds(MIN_ALLOW_MTIME_DIFFERENCE * i)};
std::string fn = "file" + std::to_string(i);
filenames.emplace_back(fn);
LOG_debug << logPre << "#### TC1." << std::to_string(i) << " Creating local file `" << fn
<< "` in Backup dir ####";
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// First file triggers a full upload. Subsequent files with same content
// but different mtime will be cloned (no transfer expected).
const bool expectFullUpload = (i == 1);
auto [res, localFile] =
createLocalFileAndWaitForSync(localBasePath / fn, "abcde", auxMtime, expectFullUpload);
ASSERT_TRUE(res) << "Cannot sync local file `" << fn << "`";
localFiles.push_back({localFile, auxMtime});
}
LOG_debug << logPre << "#### TC2: getNodesByFingerprint with and without mtime ####";
std::unique_ptr<MegaNodeList> nl;
std::vector<std::unique_ptr<MegaNode>> nodes;
nodes.emplace_back(
megaApi[0]->getChildNodeOfType(backupNode.get(), filenames[0].c_str(), FILENODE));
nodes.emplace_back(
megaApi[0]->getChildNodeOfType(backupNode.get(), filenames[1].c_str(), FILENODE));
nodes.emplace_back(
megaApi[0]->getChildNodeOfType(backupNode.get(), filenames[2].c_str(), FILENODE));
for (size_t i = 0; i < nodes.size(); ++i)
{
auto& n = nodes.at(i);
ASSERT_TRUE(n) << "Invalid node with index(" << std::to_string(i) << ")";
ASSERT_TRUE(n->getFingerprint())
<< "Invalid fingerprint for node(" << toNodeHandle(n->getHandle()) << ")";
auto auxfp = n->getFingerprint();
nl.reset(megaApi[0]->getNodesByFingerprint(auxfp));
ASSERT_EQ(nl->size(), 1) << "TC2.1(" << std::to_string(i) << "): getNodesByFingerprint("
<< auxfp << ") Unexpected node count";
nl.reset(megaApi[0]->getNodesByFingerprintIgnoringMtime(n->getFingerprint()));
ASSERT_EQ(nl->size(), nodes.size())
<< "TC2.2(" << std::to_string(i) << "): getNodesByFingerprintIgnoringMtime(" << auxfp
<< ") Unexpected node count";
}
auto updateNodeMtime =
[this](MegaHandle nodeHandle, const LocalPath& path, int64_t oldMtime, int64_t newMtime)
{
bool mTimeChangeRecv{false};
mApi[0].mOnNodesUpdateCompletion =
[&mTimeChangeRecv, oldMtime, nodeHandle](size_t, MegaNodeList* nodes)
{
ASSERT_TRUE(nodes) << "Invalid meganode list received";
for (int i = 0; i < nodes->size(); ++i)
{
MegaNode* n = nodes->get(i);
if (n && n->getHandle() == nodeHandle &&
n->hasChanged(static_cast<uint64_t>(MegaNode::CHANGE_TYPE_ATTRIBUTES)) &&
oldMtime != n->getModificationTime())
{
mTimeChangeRecv = true;
}
}
};
mFsAccess->setmtimelocal(path, newMtime);
ASSERT_TRUE(waitForResponse(&mTimeChangeRecv))
<< "No mtime change received after " << maxTimeout << " seconds";
resetOnNodeUpdateCompletionCBs(); // important to reset
};
LOG_debug
<< logPre
<< "#### TC3 update localNode (idx_0) mtime (with mtime of idx_2) and wait for sync ####";
auto h = nodes.at(0)->getHandle();
auto path = LocalPath::fromAbsolutePath(path_u8string(localFiles.at(0).first->getPath()));
auto oldMtime = nodes.at(0)->getModificationTime();
auto newMtime = nodes.at(2)->getModificationTime();
updateNodeMtime(h, path, oldMtime, newMtime);
LOG_debug
<< logPre
<< "#### TC4 update localNode (idx_1) mtime (with mtime of idx_2) and wait for sync ####";
h = nodes.at(1)->getHandle();
path = LocalPath::fromAbsolutePath(path_u8string(localFiles.at(1).first->getPath()));
oldMtime = nodes.at(1)->getModificationTime();
newMtime = nodes.at(2)->getModificationTime();
updateNodeMtime(h, path, oldMtime, newMtime);
LOG_debug << logPre
<< "#### TC5: getNodesByFingerprint with and without mtime (Now 3 nodes should have "
"same mtime) ####";
nodes.clear();
nodes.emplace_back(
megaApi[0]->getChildNodeOfType(backupNode.get(), filenames[0].c_str(), FILENODE));
nodes.emplace_back(
megaApi[0]->getChildNodeOfType(backupNode.get(), filenames[1].c_str(), FILENODE));
nodes.emplace_back(
megaApi[0]->getChildNodeOfType(backupNode.get(), filenames[2].c_str(), FILENODE));
for (auto& n: nodes)
{
nl.reset(megaApi[0]->getNodesByFingerprint(n->getFingerprint()));
ASSERT_EQ(nl->size(), nodes.size())
<< "(getNodesByFingerprint) Unexpected node count by FP1";
nl.reset(megaApi[0]->getNodesByFingerprintIgnoringMtime(n->getFingerprint()));
ASSERT_EQ(nl->size(), nodes.size())
<< "(getNodesByFingerprintIgnoringMtime) Unexpected node count by FP1";
}
LOG_debug << logPre << "#### TC4 Ensure local and cloud drive structures matches ####";
ASSERT_NO_FATAL_FAILURE(confirmModels());
LOG_verbose << logPre << "#### Test finished ####";
}
#endif
|