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 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
|
/**
* @file
* @brief This file contains tests for the public interfaces available to modify the local root of a
* sync.
*/
#ifdef ENABLE_SYNC
#include "integration_test_utils.h"
#include "megautils.h"
#include "mock_listeners.h"
#include "sdk_test_utils.h"
#include "SdkTestNodesSetUp.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace sdk_test;
using namespace testing;
/**
* @class SdkTestSyncLocalRootChange
* @brief Test fixture designed to test the feature that allows changing the local root of a sync.
*/
class SdkTestSyncLocalRootChange: public SdkTestNodesSetUp
{
public:
static constexpr auto MAX_TIMEOUT = 3min; // Timeout for operations in this tests suite
void SetUp() override
{
SdkTestNodesSetUp::SetUp();
mBackupId = syncFolder(megaApi[0].get(),
path_u8string(getLocalTmpDir()),
getNodeByPath("dir1/")->getHandle());
ASSERT_NE(mBackupId, UNDEF) << "API Error adding a new sync";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
}
void TearDown() override
{
if (mBackupId != UNDEF)
{
ASSERT_NO_FATAL_FAILURE(removeSync(megaApi[0].get(), mBackupId));
}
SdkTestNodesSetUp::TearDown();
}
/**
* @brief Build a simple file tree. dir1 for sync and dir2 as auxiliary node
*/
const std::vector<NodeInfo>& getElements() const override
{
static const std::vector<NodeInfo> ELEMENTS{
DirNodeInfo("dir1")
.addChild(FileNodeInfo("testFile").setSize(1))
.addChild(FileNodeInfo("testCommonFile"))
.addChild(FileNodeInfo("testFile1")),
DirNodeInfo("dir2")};
return ELEMENTS;
}
const std::string& getRootTestDir() const override
{
static const std::string dirName{"SDK_TEST_SYNC_LOCAL_ROOT_CHANGE_AUX_DIR"};
return dirName;
}
/**
* @brief We don't want different creation times
*/
bool keepDifferentCreationTimes() override
{
return false;
}
/**
* @brief Waits until all direct successors from both remote and local roots of the sync match.
*
* Asserts false if a timeout is exceeded.
*/
void waitForSyncToMatchCloudAndLocal() const
{
const auto areLocalAndCloudSynched = [this]() -> bool
{
const auto childrenCloudName =
getCloudFirstChildrenNames(megaApi[0].get(), getSync()->getMegaHandle());
return childrenCloudName && Value(getLocalFirstChildrenNames(),
UnorderedElementsAreArray(*childrenCloudName));
};
ASSERT_TRUE(waitFor(areLocalAndCloudSynched, MAX_TIMEOUT, 10s));
}
/**
* @brief Returns a vector with the names of the first successor files/directories inside the
* local root.
*
* Hidden files (starting with .) and the debris folder are excluded
*/
std::vector<std::string> getLocalFirstChildrenNames() const
{
return getLocalFirstChildrenNames_if(getLocalSyncRoot().value_or(getLocalTmpDir()),
[](const std::string& name)
{
return name.front() != '.' && name != DEBRISFOLDER;
});
}
/**
* @brief Returns the identifier to get the sync from megaApi
*/
handle getBackupId() const
{
return mBackupId;
}
/**
* @brief Returns the initiated sync object
*/
std::unique_ptr<MegaSync> getSync() const
{
return std::unique_ptr<MegaSync>(megaApi[0]->getSyncByBackupId(mBackupId));
}
/**
* @brief Returns the current sync state if initiated
*/
std::optional<int> getSyncRunState() const
{
const auto sync = getSync();
if (!sync)
return {};
return sync->getRunState();
}
/**
* @brief Returns the current path the sync is using as root. If there is no sync, nullopt is
* returned
*/
std::optional<std::filesystem::path> getLocalSyncRoot() const
{
const auto sync = getSync();
if (!sync)
return {};
return sync->getLocalFolder();
}
/**
* @brief Where should we put our sync locally?
*/
static const fs::path& getLocalTmpDir()
{
// Prevent parallel test from the same suite writing to the same dir
thread_local const fs::path localTmpDir{"./SDK_TEST_SYNC_LOCAL_ROOT_CHANGE_AUX_LOCAL_DIR_" +
getThisThreadIdStr()};
return localTmpDir;
}
/**
* @brief Removes the node located at the give relative path
*/
void removeRemoteNode(const std::string& path)
{
const auto node = getNodeByPath(path);
ASSERT_EQ(API_OK, doDeleteNode(0, node.get()));
}
/**
* @brief Changes the local root of the sync and expects the operation to success.
*/
void changeLocalSyncRootNoErrors(const std::filesystem::path& newRootPath) const
{
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_OK);
const auto rootPath = path_u8string(newRootPath);
megaApi[0]->changeSyncLocalRoot(getBackupId(), rootPath.c_str(), &mockListener);
ASSERT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
/**
* @brief Sets up three files inside the given directory. These are:
* - testCommonFile: An exact copy of the file created originally in the cloud
* - testFile: A file with the same name as the one in the cloud originally but different
* contents (2 bytes of data)
* - testFile2: Complete new file
*/
void prepareSimilarRoot(const std::filesystem::path& newRootPath) const
{
const auto currentRoot = getLocalSyncRoot();
ASSERT_TRUE(currentRoot);
ASSERT_THAT(getLocalFirstChildrenNames(),
IsSupersetOf({"testCommonFile", "testFile", "testFile1"}));
// Exact copy (including the mtime)
const auto source = *currentRoot / "testCommonFile";
const auto destination = newRootPath / "testCommonFile";
std::filesystem::copy(source,
destination,
std::filesystem::copy_options::overwrite_existing);
const auto mod_time = fs::last_write_time(source);
fs::last_write_time(destination, mod_time);
// Empty different file
std::ofstream test2(newRootPath / "testFile2", std::ios::binary);
// Same name different content
std::ofstream test(newRootPath / "testFile", std::ios::binary);
std::vector<char> buffer(2, 0);
test.write(buffer.data(), static_cast<std::streamsize>(buffer.size()));
}
/**
* @brief Check that the current local root of the sync has the contents specified by the
* prepareSimilarRoot method
*/
void checkCurrentLocalMatchesSimilar() const
{
const auto currentRoot = getLocalSyncRoot();
ASSERT_TRUE(currentRoot);
ASSERT_THAT(getLocalFirstChildrenNames(),
UnorderedElementsAre("testCommonFile", "testFile2", "testFile"));
ASSERT_EQ(std::filesystem::file_size(*currentRoot / "testFile"), 2);
}
/**
* @brief Ensures the current local root of the sync matches the state expected after mirroring
* original contests + the ones specified by prepareSimilarRoot. This includes a stall issue
* with "testFile"
*/
void checkCurrentLocalMatchesMirror() const
{
ASSERT_THAT(getLocalFirstChildrenNames(),
UnorderedElementsAre("testFile", "testCommonFile", "testFile1", "testFile2"));
ASSERT_TRUE(sdk_test::waitForSyncStallState(megaApi[0].get()));
ASSERT_NO_FATAL_FAILURE(thereIsAStall("testFile"));
}
/**
* @brief Ensures there is a stall issue involving the file with the given name.
*
* The expected reason for the stall is: LocalAndRemotePreviouslyUnsyncedDiffer_userMustChoose
*/
void thereIsAStall(const std::string_view fileName) const
{
const auto stalls = sdk_test::getStalls(megaApi[0].get());
ASSERT_EQ(stalls.size(), 1);
ASSERT_TRUE(stalls[0]);
const auto& stall = *stalls[0];
EXPECT_THAT(stall.path(false, 0), EndsWith(fileName));
EXPECT_THAT(
stall.reason(),
MegaSyncStall::SyncStallReason::LocalAndRemotePreviouslyUnsyncedDiffer_userMustChoose);
ASSERT_FALSE(HasNonfatalFailure());
}
void moveLocalTmpDir(const std::filesystem::path& newLocation)
{
ASSERT_TRUE(mTempLocalDir.move(newLocation)) << "Error moving local tmp dir";
}
protected:
handle mBackupId{UNDEF};
LocalTempDir mTempLocalDir{getLocalTmpDir()};
};
/**
* @brief SdkTestSyncLocalRootChange.ArgumentErrors : Validate the input error code paths.
*/
TEST_F(SdkTestSyncLocalRootChange, ArgumentErrors)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.ArgumentErrors : "};
{
LOG_verbose << logPre << "Giving undef backupId and undef remote handle";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, NO_SYNC_ERROR);
megaApi[0]->changeSyncLocalRoot(UNDEF, nullptr, &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
const std::filesystem::path newRootPath{"./newLocaRootPathForTests/"};
const LocalTempDir newRootDir(newRootPath);
const auto newRootAbsPath = path_u8string(std::filesystem::absolute(newRootPath));
{
LOG_verbose << logPre << "Giving undef backupId and good new root path";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, _);
megaApi[0]->changeSyncLocalRoot(UNDEF, newRootAbsPath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
{
LOG_verbose << logPre << "Giving non existent backupId and good remote handle";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, UNKNOWN_ERROR);
megaApi[0]->changeSyncLocalRoot(getNodeHandleByPath("dir1"),
newRootAbsPath.c_str(),
&mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
{
LOG_verbose << logPre << "Giving good backupId and a path to a file";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EACCESS, INVALID_LOCAL_TYPE);
const auto filePath =
path_u8string(std::filesystem::absolute(getLocalTmpDir() / "testFile"));
megaApi[0]->changeSyncLocalRoot(getBackupId(), filePath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
{
LOG_verbose << logPre << "Giving good backupId and a path to non existent dir";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_ENOENT, LOCAL_PATH_UNAVAILABLE);
const auto nonExsitsPath = path_u8string(std::filesystem::absolute("./NoExistsDir/"));
megaApi[0]->changeSyncLocalRoot(getBackupId(), nonExsitsPath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
{
LOG_verbose << logPre << "Giving good backupId and path to the already synced root";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, LOCAL_PATH_SYNC_COLLISION);
const auto rootPath = path_u8string(std::filesystem::absolute(getLocalTmpDir()));
megaApi[0]->changeSyncLocalRoot(getBackupId(), rootPath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
// Just make sure that after all the attempts the sync is still running fine
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
}
/**
* @brief SdkTestSyncLocalRootChange.ErrorNestedSyncs : Validate error code paths triggered when
* trying to set the new root to a directory that is part of an existing sync
*/
TEST_F(SdkTestSyncLocalRootChange, ErrorNestedSyncs)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.ErrorNestedSyncs : "};
LOG_verbose << logPre << "Creating a new sync between auxTmpDirForNewSync/ and dir2/";
const LocalTempDir tmpDir{"./auxTmpDirForNewSync/"};
const LocalTempDir tmpSubDir{"./auxTmpDirForNewSync/subdir"};
const auto dir2BackupId = syncFolder(megaApi[0].get(),
path_u8string(tmpDir.getPath()),
getNodeByPath("dir2/")->getHandle());
ASSERT_NE(dir2BackupId, UNDEF) << "API Error adding a new sync";
{
LOG_verbose << logPre << "Moving local root to another sync root";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, LOCAL_PATH_SYNC_COLLISION);
const auto rootPath = path_u8string(tmpDir.getPath());
megaApi[0]->changeSyncLocalRoot(getBackupId(), rootPath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
{
LOG_verbose << logPre << "Moving local root to a subdir inside another sync";
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, LOCAL_PATH_SYNC_COLLISION);
const auto rootPath = path_u8string(tmpSubDir.getPath());
megaApi[0]->changeSyncLocalRoot(getBackupId(), rootPath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
}
}
#ifndef WIN32
/**
* @brief SdkTestSyncLocalRootChange.ErrorNestedSyncSymLink :
* 1. Change the root of the sync to a symlink pointing to the original root
* 2. Change the root of the sync to a symlink pointing to a root of another sync
* NOTE: This test does not make sense on windows due to how symlinks are handled there.
*/
TEST_F(SdkTestSyncLocalRootChange, ErrorNestedSyncSymLink)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.ErrorNestedSyncSymLink : "};
LOG_verbose << logPre
<< "Creating a new sync between auxTmpDirErrorNestedSyncSymLink/ and dir2/";
const LocalTempDir tmpDir{"./auxTmpDirErrorNestedSyncSymLink/"};
const auto dir2BackupId = syncFolder(megaApi[0].get(),
path_u8string(tmpDir.getPath()),
getNodeByPath("dir2/")->getHandle());
ASSERT_NE(dir2BackupId, UNDEF) << "API Error adding a new sync";
{
LOG_verbose << logPre << "Changing the root to a symlink pointing to the original root";
std::filesystem::path linkName{"./symLinkToOriginal"};
std::filesystem::create_directory_symlink(getLocalTmpDir(), linkName);
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, LOCAL_PATH_SYNC_COLLISION);
const auto rootPath = path_u8string(linkName);
megaApi[0]->changeSyncLocalRoot(getBackupId(), rootPath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
std::filesystem::remove(linkName);
}
{
LOG_verbose << logPre
<< "Changing the root to a symlink pointing to the root of another sync";
std::filesystem::path linkName{"./symLinkToSecondSync"};
std::filesystem::create_directory_symlink(tmpDir.getPath(), linkName);
NiceMock<MockRequestListener> mockListener{megaApi[0].get()};
mockListener.setErrorExpectations(API_EARGS, LOCAL_PATH_SYNC_COLLISION);
const auto rootPath = path_u8string(linkName);
megaApi[0]->changeSyncLocalRoot(getBackupId(), rootPath.c_str(), &mockListener);
EXPECT_TRUE(mockListener.waitForFinishOrTimeout(MAX_TIMEOUT));
std::filesystem::remove(linkName);
}
}
#endif
/**
* @brief SdkTestSyncLocalRootChange.OKSyncRunningToEmptyRoot: Change the root of a running sync to
* an empty directory. Ensure the new .debris is properly created.
*/
TEST_F(SdkTestSyncLocalRootChange, OKSyncRunningToEmptyRoot)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.OKSyncRunningToEmptyRoot : "};
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Moving local root to an empty new root";
const LocalTempDir tmpDir{"./auxTmpDirOKSyncRunningToEmptyRoot/"};
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Waiting for local to match cloud";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations: Empty dir (local has preference)";
EXPECT_THAT(getLocalFirstChildrenNames_if(tmpDir.getPath()),
testing::UnorderedElementsAre(".megaignore"));
// Create a file and remove it in the cloud to force debris creation
LOG_verbose << logPre << "Creating new file and removing from cloud to force .debris";
const std::string testFileName{"testTempFile.txt"};
LocalTempFile f{tmpDir.getPath() / testFileName, 0};
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
removeRemoteNode("dir1/" + testFileName);
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations: Empty + .debris";
EXPECT_THAT(getLocalFirstChildrenNames_if(tmpDir.getPath()),
testing::UnorderedElementsAre(".megaignore", DEBRISFOLDER));
}
/**
* @brief SdkTestSyncLocalRootChange.OKSyncRunningPauseAndResume: Change the root of a running sync
* and ensure everything works as expected after pausing and resuming.
*/
TEST_F(SdkTestSyncLocalRootChange, OKSyncRunningPauseAndResume)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.OKSyncRunningPauseAndResume : "};
LOG_verbose << logPre << "Moving local root to an empty new root";
const LocalTempDir tmpDir{"./auxTmpDirOKSyncRunningToEmptyRoot/"};
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Suspending the sync";
ASSERT_TRUE(sdk_test::suspendSync(megaApi[0].get(), getBackupId()))
<< "Error suspending the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
LOG_verbose << logPre << "Creating a new file locally";
const std::string testFileName{"testTempFile.txt"};
LocalTempFile f{tmpDir.getPath() / testFileName, 0};
LOG_verbose << logPre << "Resuming the sync";
ASSERT_TRUE(sdk_test::resumeSync(megaApi[0].get(), getBackupId())) << "Error resuming the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Checking the new file uploads";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
EXPECT_THAT(getLocalFirstChildrenNames_if(tmpDir.getPath()),
testing::UnorderedElementsAre(".megaignore", testFileName));
}
/**
* @brief SdkTestSyncLocalRootChange.OKSyncRunningToSimilarRoot: Change the root of a running sync
* to a directory that contains different files:
* - One exactly the same as in the previous root
* - One different
* - One with same name and different contents
* - It misses one that was in previous root
*
* The final state prioritize local new root
*
*/
TEST_F(SdkTestSyncLocalRootChange, OKSyncRunningToSimilarRoot)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.OKSyncRunningToSimilarRoot : "};
LOG_verbose << logPre << "Preparing new root with similar contents";
const LocalTempDir tmpDir{"./auxTmpOKSyncRunningToSimilarRoot/"};
prepareSimilarRoot(tmpDir.getPath());
LOG_verbose << logPre << "Changing the root";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations";
ASSERT_NO_FATAL_FAILURE(checkCurrentLocalMatchesSimilar());
}
/**
* @brief SdkTestSyncLocalRootChange.OKSyncSuspendedToSimilarRoot: Same as
* OKSyncRunningToSimilarRoot but changing the root while the sync is suspended, then it is resumed
* and wait to validate expectations.
*/
TEST_F(SdkTestSyncLocalRootChange, OKSyncSuspendedToSimilarRoot)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.OKSyncSuspendedToSimilarRoot : "};
LOG_verbose << logPre << "Preparing new root with similar contents";
const LocalTempDir tmpDir{"./auxTmpOKSyncSuspendedToSimilarRoot/"};
prepareSimilarRoot(tmpDir.getPath());
LOG_verbose << logPre << "Suspending the sync";
ASSERT_TRUE(sdk_test::suspendSync(megaApi[0].get(), getBackupId()))
<< "Error suspending the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
LOG_verbose << logPre << "Changing the root";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
LOG_verbose << logPre << "Resuming the sync";
ASSERT_TRUE(sdk_test::resumeSync(megaApi[0].get(), getBackupId())) << "Error resuming the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations";
ASSERT_NO_FATAL_FAILURE(checkCurrentLocalMatchesSimilar());
}
/**
* @brief SdkTestSyncLocalRootChange.OKSyncDisabledToSimilarRoot: Same as
* OKSyncRunningToSimilarRoot but changing the root while the sync is disabled, then it is enabled
* and wait to validate expectations.
*
* NOTE: In this case, the final state must be a mirror between cloud and local.
*/
TEST_F(SdkTestSyncLocalRootChange, OKSyncDisabledToSimilarRoot)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.OKSyncDisabledToSimilarRoot : "};
LOG_verbose << logPre << "Preparing new root with similar contents";
const LocalTempDir tmpDir{"./auxTmpOKSyncDisabledToSimilarRoot/"};
prepareSimilarRoot(tmpDir.getPath());
LOG_verbose << logPre << "Disable the sync";
ASSERT_TRUE(sdk_test::disableSync(megaApi[0].get(), getBackupId()))
<< "Error suspending the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_DISABLED});
LOG_verbose << logPre << "Changing the root";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_DISABLED});
LOG_verbose << logPre << "Resuming the sync";
ASSERT_TRUE(sdk_test::resumeSync(megaApi[0].get(), getBackupId())) << "Error resuming the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations";
ASSERT_NO_FATAL_FAILURE(checkCurrentLocalMatchesMirror());
}
#ifndef WIN32
/**
* @brief SdkTestSyncLocalRootChange.OKSyncRunningMoveRootAndReassing:
* 1. Move the root directory of a running sync to a different location
* 2. Check that it gets suspended
* 3. Reassign the root to the new location
* 4. Sync can be resumed and everything stays as it was
* NOTE: This test does not apply to windows because windows will block the rename operation on the
* root while the sync is running (the directory is opened by the sync engine). We should pause the
* sync before the rename but that scenario falls into the domain of other tests.
*/
TEST_F(SdkTestSyncLocalRootChange, OKSyncRunningMoveRootAndReassing)
{
static const std::string logPre{
"SdkTestSyncLocalRootChange.OKSyncRunningMoveRootAndReassing : "};
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Renaming local root";
const auto newRoot = getLocalTmpDir().parent_path() / "TestDirOKSyncRunningMoveRootAndReassing";
ASSERT_NO_FATAL_FAILURE(moveLocalTmpDir(newRoot));
LOG_verbose << logPre << "Waiting for the sync to be disabled";
ASSERT_TRUE(waitFor(
[this]()
{
return getSyncRunState() == std::optional{MegaSync::RUNSTATE_SUSPENDED};
},
MAX_TIMEOUT,
10s));
LOG_verbose << logPre << "Change sync root to new location";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(newRoot));
LOG_verbose << logPre << "Enabling the sync";
ASSERT_TRUE(sdk_test::resumeSync(megaApi[0].get(), getBackupId())) << "Error resuming the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
// Move the directory back to where it was
moveLocalTmpDir(getLocalTmpDir());
}
#endif
/**
* @brief SdkTestSyncLocalRootChange.SymLinkAsRootChangeWhereItPointsTo:
*
* Use case: we have a sync that uses a symlink as local root and that symlink changes where it is
* pointing to. That should invalidate the sync and with the MegaApi::changeSyncLocalRoot we should
* be able to solve the issue.
*
* 1. Create the sync with a symlink as root. symlink pointing to -> "originalRoot" that has some
* files inside.
* 2. Suspend the sync (we need this to run the test on windows)
* 3. Create a new empty directory
* 4. Move the symlink to point to the new directory. symlink pointing to -> "tempEmtpyTestDir"
* 5. Try to resume the Sync. An error (MISMATCH_OF_ROOT_FSID) is expected.
* 6. Change the root to "./symlink". Although it is the same path stored in the sync, it points to
* a different directory so this should fix the issue.
* 7. Enable the sync. It should work now.
* 8. Validate final state (empty, local has preference).
*/
#ifdef __APPLE__
// Disabled due to SDK-4472
TEST_F(SdkTestSyncLocalRootChange, DISABLED_SymLinkAsRootChangeWhereItPointsTo)
#else
TEST_F(SdkTestSyncLocalRootChange, SymLinkAsRootChangeWhereItPointsTo)
#endif
{
static const std::string logPre{
"SdkTestSyncLocalRootChange.SymLinkAsRootChangeWhereItPointsTo : "};
// We need to remove the original sync because we cannot create a new one pointing to a symlink
// that points to the root of an existing sync
LOG_verbose << logPre << "Removing the original sync";
ASSERT_NO_FATAL_FAILURE(removeSync(megaApi[0].get(), mBackupId));
LOG_verbose << logPre << "Creating a symlink to the original root";
std::filesystem::path linkName{"./symLinkToOriginal"};
const MrProper defer{[&linkName]()
{
std::filesystem::remove(linkName);
}};
std::filesystem::create_directory_symlink(getLocalTmpDir(), linkName);
LOG_verbose << logPre << "Creating a new sync with the symlink as root";
mBackupId =
syncFolder(megaApi[0].get(), path_u8string(linkName), getNodeByPath("dir1/")->getHandle());
ASSERT_NE(mBackupId, UNDEF) << "API Error adding a new sync";
LOG_verbose << logPre << "Waiting for cloud/local content to match and local nodes are created";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
static constexpr auto WAIT_TIME_TO_CREATE_LOCAL_NODES{20s};
std::this_thread::sleep_for(WAIT_TIME_TO_CREATE_LOCAL_NODES);
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Suspending the sync";
ASSERT_TRUE(sdk_test::suspendSync(megaApi[0].get(), getBackupId()))
<< "Error suspending the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
LOG_verbose << logPre << "Creating an empty dir and moving the symlink to point to it";
static const std::string tmpEmptyDirName{"./tempEmtpyTestDir"};
const LocalTempDir tmp{tmpEmptyDirName};
std::filesystem::remove(linkName);
std::filesystem::create_directory_symlink(tmp.getPath(), linkName);
LOG_verbose << logPre << "Trying to resume the sync expecting an error (MISMATCH_OF_ROOT_FSID)";
NiceMock<MockRequestListener> reqListener{megaApi[0].get()};
reqListener.setErrorExpectations(API_EFAILED, MISMATCH_OF_ROOT_FSID);
megaApi[0]->setSyncRunState(getBackupId(),
MegaSync::SyncRunningState::RUNSTATE_RUNNING,
&reqListener);
ASSERT_TRUE(reqListener.waitForFinishOrTimeout(MAX_TIMEOUT));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
LOG_verbose << logPre << "Change local root to the symlink again";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(linkName));
LOG_verbose << logPre << "We should be able to resume the sync now";
ASSERT_TRUE(sdk_test::resumeSync(megaApi[0].get(), getBackupId())) << "Error resuming the sync";
LOG_verbose << logPre << "Waiting for local to match cloud";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
EXPECT_THAT(getLocalFirstChildrenNames_if(tmp.getPath()),
testing::UnorderedElementsAre(".megaignore"));
}
/**
* @brief SdkTestSyncLocalRootChange.ChangeRootToSameNameDifferentFsid:
*
* Use case: The path used as local root sill exists but it is a different directory (different
* fsid). The MegaApi::changeSyncLocalRoot method allows to refresh internally the fsid of the local
* root.
*
* 1. Suspend the sync (we need this to run the test on windows)
* 2. Rename the original root to "TestChangeRoot"
* 3. Create a new empty dir in the same path as the original root
* 4. Try to resume the Sync. An error (MISMATCH_OF_ROOT_FSID) is expected.
* 5. Change the root to "originalPath". This should fix the mismatch fsid issue
* 6. Enable the sync. It should work now.
* 7. Validate final state (empty, local has preference).
*/
#ifdef __APPLE__
// Disabled due to SDK-4472
TEST_F(SdkTestSyncLocalRootChange, DISABLED_ChangeRootToSameNameDifferentFsid)
#else
TEST_F(SdkTestSyncLocalRootChange, ChangeRootToSameNameDifferentFsid)
#endif
{
static const std::string logPre{
"SdkTestSyncLocalRootChange.ChangeRootToSameNameDifferentFsid : "};
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
ASSERT_TRUE(sdk_test::suspendSync(megaApi[0].get(), getBackupId()))
<< "Error suspending the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
LOG_verbose << logPre << "Renaming local root";
const auto originalRoot = getLocalTmpDir();
const auto newRoot = getLocalTmpDir().parent_path() / "TestChangeRootToSameNameDifferentFsid";
// On windows we need to wait until the filesystem notify thread stops once the sync is
// suspended. That's why we need multiple attempts to move the directory
ASSERT_TRUE(sdk_test::waitFor(
[this, &newRoot]() -> bool
{
return mTempLocalDir.move(newRoot);
},
MAX_TIMEOUT))
<< "Error moving root directory";
LOG_verbose << logPre << "Try to resume the sync in the same path but different dir";
const LocalTempDir tmpNewRootSameName{originalRoot};
NiceMock<MockRequestListener> reqListener{megaApi[0].get()};
reqListener.setErrorExpectations(API_EFAILED, MISMATCH_OF_ROOT_FSID);
megaApi[0]->setSyncRunState(getBackupId(),
MegaSync::SyncRunningState::RUNSTATE_RUNNING,
&reqListener);
ASSERT_TRUE(reqListener.waitForFinishOrTimeout(MAX_TIMEOUT));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
LOG_verbose << logPre << "Change the root to the same path and try to resume again";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(originalRoot));
ASSERT_TRUE(sdk_test::resumeSync(megaApi[0].get(), getBackupId())) << "Error resuming the sync";
LOG_verbose << logPre << "Waiting for local to match cloud";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
EXPECT_THAT(getLocalFirstChildrenNames_if(tmpNewRootSameName.getPath()),
testing::UnorderedElementsAre(".megaignore"));
LOG_verbose << logPre << "Stopping the sync and moving tmp dir to its original path";
ASSERT_TRUE(sdk_test::suspendSync(megaApi[0].get(), getBackupId()))
<< "Error suspending the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_SUSPENDED});
}
/**
* @brief SdkTestSyncLocalRootChange.OKChangRootToASymLink: Change root to a symlink to an empty
* directory. Validate final state.
*/
TEST_F(SdkTestSyncLocalRootChange, OKChangRootToASymLink)
{
static const std::string logPre{"SdkTestSyncLocalRootChange.OKChangRootToASymLink : "};
const LocalTempDir tmpDir{"./auxTmpDirOKChangRootToASymLink/"};
std::filesystem::path linkName{"./symLinkToEmpty"};
const MrProper defer{[&linkName]()
{
std::filesystem::remove(linkName);
}};
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
std::filesystem::create_directory_symlink(tmpDir.getPath(), linkName);
LOG_verbose << logPre << "Moving local root to an empty new root";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(linkName));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Waiting for local to match cloud";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations: Empty dir (local has preference)";
EXPECT_THAT(getLocalFirstChildrenNames_if(tmpDir.getPath()),
testing::UnorderedElementsAre(".megaignore"));
}
class SdkTestBackupSyncLocalRootChange: public SdkTestSyncLocalRootChange
{
public:
void SetUp() override
{
SdkTestNodesSetUp::SetUp();
createAuxFiles();
mBackupId = backupFolder(megaApi[0].get(), path_u8string(getLocalTmpDir()), "myBackup");
ASSERT_NE(mBackupId, UNDEF) << "API Error adding a new backup sync";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
}
/**
* @brief We don't need nodes on the cloud for backups
*/
const std::vector<NodeInfo>& getElements() const override
{
static const std::vector<NodeInfo> ELEMENTS{};
return ELEMENTS;
}
void createAuxFiles()
{
auxFiles.emplace_back(getLocalTmpDir() / "testFile", 1);
auxFiles.emplace_back(getLocalTmpDir() / "testCommonFile", 0);
auxFiles.emplace_back(getLocalTmpDir() / "testFile1", 0);
}
enum class StopAction
{
disable,
pause
};
/**
* @brief Disables or pauses the current backup, then changes the local root to a new directory
* with similar contents. The backup is resumed and the final state is validated.
*
* @param action Specifies how the backup has to be stopped
*/
void changeRootToSimilarWhileStop(const StopAction action)
{
const auto* testInfo = ::testing::UnitTest::GetInstance()->current_test_info();
const std::string suiteName = testInfo->test_suite_name();
const std::string testName = testInfo->name();
const std::string logPrefix = suiteName + "." + testName + " : ";
LOG_verbose << logPrefix << "Preparing new root with similar contents";
const LocalTempDir tmpDir{"./auxTmp" + testName};
prepareSimilarRoot(tmpDir.getPath());
MegaSync::SyncRunningState expectedRunState;
switch (action)
{
case StopAction::pause:
{
LOG_verbose << logPrefix << "Suspending the backup sync";
ASSERT_TRUE(sdk_test::suspendSync(megaApi[0].get(), getBackupId()))
<< "Error suspending the sync";
expectedRunState = MegaSync::RUNSTATE_SUSPENDED;
break;
}
case StopAction::disable:
{
LOG_verbose << logPrefix << "Disable the backup sync";
ASSERT_TRUE(sdk_test::disableSync(megaApi[0].get(), getBackupId()))
<< "Error disabling the sync";
expectedRunState = MegaSync::RUNSTATE_DISABLED;
break;
}
}
ASSERT_EQ(getSyncRunState(), std::optional{expectedRunState});
LOG_verbose << logPrefix << "Changing the root";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{expectedRunState});
LOG_verbose << logPrefix << "Resuming the backup sync";
ASSERT_TRUE(sdk_test::resumeSync(megaApi[0].get(), getBackupId()))
<< "Error resuming the sync";
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPrefix << "Validating expectations";
ASSERT_NO_FATAL_FAILURE(checkCurrentLocalMatchesSimilar());
}
private:
std::vector<LocalTempFile> auxFiles;
};
/**
* @brief Change the root of the backup to an empty local dir.
* Expectations -> final state = empty
* The name of the backup and the remote root node do not change
*/
TEST_F(SdkTestBackupSyncLocalRootChange, OKChangeRootToEmpty)
{
static const std::string logPre{"SdkTestBackupSyncLocalRootChange.OKChangeRootToEmpty : "};
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Moving local root to an empty new root";
const LocalTempDir tmpDir{"./auxTmpDirOKChangeRootToEmpty/"};
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
LOG_verbose << logPre << "Waiting for local to match cloud";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations: Empty dir (local has preference)";
EXPECT_THAT(getLocalFirstChildrenNames_if(tmpDir.getPath()),
testing::UnorderedElementsAre(".megaignore"));
const auto backup = getSync();
ASSERT_TRUE(backup);
EXPECT_STREQ(backup->getName(), "myBackup");
EXPECT_THAT(backup->getLastKnownMegaFolder(), EndsWith("myBackup"));
}
/**
* @brief SdkTestBackupSyncLocalRootChange.OKBackupRunningToSimilarRoot: Change the root of a
* running backup sync to a directory that contains different files:
* - One exactly the same as in the previous root
* - One different
* - One with same name and different contents
* - It misses one that was in previous root
*
* The final state prioritize the new local root
*/
TEST_F(SdkTestBackupSyncLocalRootChange, OKBackupRunningToSimilarRoot)
{
static const std::string logPre{
"SdkTestBackupSyncLocalRootChange.OKBackupRunningToSimilarRoot : "};
LOG_verbose << logPre << "Preparing new root with similar contents";
const LocalTempDir tmpDir{"./auxTmpOKBackupRunningToSimilarRoot/"};
prepareSimilarRoot(tmpDir.getPath());
LOG_verbose << logPre << "Changing the root";
ASSERT_NO_FATAL_FAILURE(changeLocalSyncRootNoErrors(tmpDir.getPath()));
ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Validating expectations";
ASSERT_NO_FATAL_FAILURE(checkCurrentLocalMatchesSimilar());
}
/**
* @brief SdkTestBackupSyncLocalRootChange.OKBackupSuspendedToSimilarRoot: Same as
* OKBackupRunningToSimilarRoot but changing the root while the backup is suspended, then it is
* resumed and waits to validate expectations.
*/
TEST_F(SdkTestBackupSyncLocalRootChange, OKBackupSuspendedToSimilarRoot)
{
ASSERT_NO_FATAL_FAILURE(changeRootToSimilarWhileStop(StopAction::pause));
}
/**
* @brief SdkTestBackupSyncLocalRootChange.OKBackupDisabledToSimilarRoot: Same as
* OKBackupRunningToSimilarRoot but changing the root while the backup is disabled, then it is
* enabled and waits to validate expectations.
*/
TEST_F(SdkTestBackupSyncLocalRootChange, OKBackupDisabledToSimilarRoot)
{
ASSERT_NO_FATAL_FAILURE(changeRootToSimilarWhileStop(StopAction::disable));
}
#endif // ENABLE_SYNC
|