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 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
|
/**
* @file
* @brief This file is expected to contain tests involving sync upload operations
* e.g., what happens when a file is duplicated inside a sync.
*/
#ifdef ENABLE_SYNC
#include "integration_test_utils.h"
#include "mega/testhooks.h"
#include "mega/utils.h"
#include "megautils.h"
#include "mock_listeners.h"
#include "sdk_test_utils.h"
#include "SdkTestSyncNodesOperations.h"
#include <gmock/gmock.h>
using namespace sdk_test;
using namespace testing;
namespace
{
struct ScopedLegacyBuggySparseCrcHook
{
#ifdef MEGASDK_DEBUG_TEST_HOOKS_ENABLED
std::function<void(bool&)> mPrev;
bool mEnabled = false;
explicit ScopedLegacyBuggySparseCrcHook(const bool enabled):
mPrev{::mega::globalMegaTestHooks.onHookFileFingerprintUseLegacyBuggySparseCrc},
mEnabled{enabled}
{
setEnabled(mEnabled);
}
~ScopedLegacyBuggySparseCrcHook()
{
::mega::globalMegaTestHooks.onHookFileFingerprintUseLegacyBuggySparseCrc = std::move(mPrev);
}
void setEnabled(const bool enabled)
{
mEnabled = enabled;
::mega::globalMegaTestHooks.onHookFileFingerprintUseLegacyBuggySparseCrc =
[enabled](bool& flag)
{
flag = enabled;
};
}
#else
explicit ScopedLegacyBuggySparseCrcHook(const bool) {}
void setEnabled(const bool) {}
#endif
};
} // namespace
/**
* @class SdkTestSyncUploadOperations
* @brief Test fixture designed to test operations involving sync uploads.
*/
class SdkTestSyncUploadsOperations: public SdkTestSyncNodesOperations
{
protected:
// [TODO] SDK-5629. Check Lifetime of listeners in this test suite once this ticket has been
// resolved
std::unique_ptr<NiceMock<MockTransferListener>> mMtl;
std::unique_ptr<NiceMock<MockSyncListener>> mMsl;
bool mCleanupFunctionSet{false};
const std::string SYNC_REMOTE_PATH{"localSyncedDir"};
std::vector<shared_ptr<sdk_test::LocalTempFile>> mLocalFiles;
std::unique_ptr<FSACCESS_CLASS> mFsAccess;
SyncItemTrackerManager<SyncUploadOperationsTracker> mSyncListenerTrackers;
SyncItemTrackerManager<SyncUploadOperationsTransferTracker> mTransferListenerTrackers;
mutable std::recursive_timed_mutex trackerMutex;
using TestMutexGuard = std::unique_lock<std::recursive_timed_mutex>;
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);
}
/**
* @brief Waits for sync completion and verifies transfer behavior for a file operation.
*
* This is the common logic for file creation and move operations.
* Call this AFTER setting up trackers and performing the file operation.
*
* @param localFilePathAbs Absolute path to the file being synced (for error messages).
* @param st The sync listener tracker for this file.
* @param tt The transfer listener tracker for this file.
* @param isFullUploadExpected If true, validates transfer completes with API_OK.
* If false, validates that NO transfer occurred (clone/setattr should be used instead).
* @param noTransferTimeout Timeout used when no transfer is expected.
*/
void waitForSyncAndVerifyTransfer(
const fs::path& localFilePathAbs,
std::shared_ptr<SyncUploadOperationsTracker> st,
std::shared_ptr<SyncUploadOperationsTransferTracker> tt,
const bool isFullUploadExpected,
const std::chrono::milliseconds noTransferTimeout = std::chrono::seconds(30))
{
auto [syncStatus, syncErrCode] = st->waitForCompletion(COMMON_TIMEOUT);
ASSERT_TRUE(syncStatus == std::future_status::ready)
<< "Sync state change not received for: " << localFilePathAbs;
ASSERT_EQ(syncErrCode, API_OK) << "Sync completed with error for: " << localFilePathAbs;
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
const auto transferTimeout = isFullUploadExpected ? COMMON_TIMEOUT : noTransferTimeout;
auto [transferStatus, transferErrCode] = tt->waitForCompletion(transferTimeout);
const auto expectedTransferStatus =
isFullUploadExpected ? std::future_status::ready : std::future_status::timeout;
ASSERT_EQ(transferStatus, expectedTransferStatus)
<< "Unexpected transfer status for: " << localFilePathAbs
<< " [isFullUploadExpected: " << isFullUploadExpected << "]";
const auto expectedTransferStartCount = isFullUploadExpected ? 1 : 0;
ASSERT_EQ(tt->transferStartCount.load(), expectedTransferStartCount)
<< "Transfer started count mismatch for: " << localFilePathAbs
<< " [isFullUploadExpected: " << isFullUploadExpected << "]";
if (isFullUploadExpected)
{
ASSERT_EQ(transferErrCode, API_OK) << "Transfer failed (" << localFilePathAbs << ")";
}
}
/**
* @brief Creates a local test file and verifies sync completion and transfer behavior.
*
* @param localFilePathAbs Absolute filesystem path where the test file will be created. Must be
* an absolute path to match correctly with sync state change events.
* @param fileContent The content to write to the test file.
* @param customMtime Custom modification time to apply to the created file
* @param isFullUploadExpected If true, validates that transfer completes with API_OK.
* If false, validates that NO transfer occurred (clone/setattr should be used instead).
*/
void createTestFileInternal(const fs::path& localFilePathAbs,
const std::string& fileContent,
std::chrono::time_point<fs::file_time_type::clock> customMtime,
const bool isFullUploadExpected)
{
static const std::string logPre{"createTestFileInternal: "};
ASSERT_TRUE(mMtl) << logPre << "Invalid transfer listener";
auto tt = addTransferListenerTracker(localFilePathAbs.string());
ASSERT_TRUE(tt) << logPre
<< "Cannot add TransferListenerTracker for: " << localFilePathAbs.string();
auto st = addSyncListenerTracker(localFilePathAbs.string());
ASSERT_TRUE(st) << logPre
<< "Cannot add SyncListenerTracker for: " << localFilePathAbs.string();
LOG_debug << logPre << "Creating local file: " << localFilePathAbs.string();
auto localFile =
std::make_shared<sdk_test::LocalTempFile>(localFilePathAbs, fileContent, customMtime);
mLocalFiles.emplace_back(localFile);
ASSERT_NO_FATAL_FAILURE(
waitForSyncAndVerifyTransfer(localFilePathAbs, st, tt, isFullUploadExpected));
}
/**
* @brief Moves a file into the sync and verifies sync completion and transfer behavior.
*
* @param sourcePath Absolute path to the source file (outside sync).
* @param targetPathInSync Absolute path where file will be moved (inside sync).
* @param isFullUploadExpected If true, validates transfer completes with API_OK.
* If false, validates that NO transfer occurred (clone/setattr should be used instead).
* @param expectedMtimeAfterMove Optional: if provided, verifies the moved file has this mtime.
*/
void moveFileIntoSyncAndVerify(const fs::path& sourcePath,
const fs::path& targetPathInSync,
const bool isFullUploadExpected,
std::optional<m_time_t> expectedMtimeAfterMove = std::nullopt)
{
moveIntoSyncAndVerifyImpl(targetPathInSync,
isFullUploadExpected,
expectedMtimeAfterMove,
[&]()
{
std::error_code ec;
fs::rename(sourcePath, targetPathInSync, ec);
if (ec)
{
LOG_err << "Failed to move file from "
<< path_u8string(sourcePath) << " to "
<< path_u8string(targetPathInSync)
<< ". Error: " << ec.message();
}
return ec;
});
}
void moveLocalTempFileIntoSyncAndVerify(
const std::shared_ptr<sdk_test::LocalTempFile>& file,
const fs::path& targetPathInSync,
const bool isFullUploadExpected,
std::optional<m_time_t> expectedMtimeAfterMove = std::nullopt)
{
ASSERT_TRUE(file);
moveIntoSyncAndVerifyImpl(targetPathInSync,
isFullUploadExpected,
expectedMtimeAfterMove,
[&]()
{
return file->move(targetPathInSync);
});
}
private:
template<typename MoveFn>
void moveIntoSyncAndVerifyImpl(const fs::path& targetPathInSync,
const bool isFullUploadExpected,
const std::optional<m_time_t>& expectedMtimeAfterMove,
MoveFn&& moveFn)
{
static const std::string logPre{"moveIntoSyncAndVerifyImpl: "};
ASSERT_TRUE(mMtl) << logPre << "Invalid transfer listener";
auto tt = addTransferListenerTracker(targetPathInSync.string());
ASSERT_TRUE(tt) << logPre
<< "Cannot add TransferListenerTracker for: " << targetPathInSync.string();
auto st = addSyncListenerTracker(targetPathInSync.string());
ASSERT_TRUE(st) << logPre
<< "Cannot add SyncListenerTracker for: " << targetPathInSync.string();
const auto ec = moveFn();
ASSERT_FALSE(ec) << logPre << "Move into sync failed for: " << targetPathInSync;
if (expectedMtimeAfterMove.has_value())
{
auto [getMtimeOk, movedFileMtime] = mFsAccess->getmtimelocal(
LocalPath::fromAbsolutePath(path_u8string(targetPathInSync)));
ASSERT_TRUE(getMtimeOk)
<< logPre << "Failed to get mtime of moved file: " << targetPathInSync;
ASSERT_EQ(movedFileMtime, expectedMtimeAfterMove.value())
<< logPre << "Move should preserve mtime for: " << targetPathInSync;
}
ASSERT_NO_FATAL_FAILURE(
waitForSyncAndVerifyTransfer(targetPathInSync, st, tt, isFullUploadExpected));
}
protected:
enum class CxfMtimeDirection
{
Increase,
Decrease
};
void runAsyncMacComputationForCxfCase(const std::string& logPre,
const CxfMtimeDirection direction)
{
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
static constexpr size_t FILE_SIZE =
(5 * 1024 * 1024) + (2 * 1024) + 3; // 5MB + 2KB + 3 bytes
const fs::path testFilePath = fs::absolute(getLocalTmpDir() / "test_file_cxf.dat");
LOG_debug << logPre << "1. Creating test file";
{
auto st = addSyncListenerTracker(testFilePath.string());
ASSERT_TRUE(st);
auto localFile = std::make_shared<sdk_test::LocalTempFile>(testFilePath, FILE_SIZE);
mLocalFiles.emplace_back(localFile);
auto [status, errCode] = st->waitForCompletion(COMMON_TIMEOUT);
ASSERT_EQ(status, std::future_status::ready) << "File sync timed out";
ASSERT_EQ(errCode, API_OK) << "File sync failed";
}
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
auto [getMtimeOk, originalMtime] =
mFsAccess->getmtimelocal(LocalPath::fromAbsolutePath(path_u8string(testFilePath)));
ASSERT_TRUE(getMtimeOk) << "Failed to get original mtime";
LOG_debug << logPre << "2. Removing sync (simulating logout without file deletion)";
removeTestSync();
LOG_debug << logPre << "3. Updating file mtime";
const m_time_t newMtime = direction == CxfMtimeDirection::Increase ?
originalMtime + MIN_ALLOW_MTIME_DIFFERENCE :
originalMtime - MIN_ALLOW_MTIME_DIFFERENCE;
ASSERT_TRUE(
mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(testFilePath)),
newMtime));
LOG_debug << logPre << "4. Re-adding sync (SRT_CXF case - no LocalNodes exist)";
ASSERT_NO_FATAL_FAILURE(
initiateSync(getLocalTmpDirU8string(), SYNC_REMOTE_PATH, mBackupId));
LOG_debug << logPre << "5. Waiting for sync to complete with async MAC recomputation";
{
auto st = addSyncListenerTracker(testFilePath.string());
ASSERT_TRUE(st);
auto [status, errCode] = st->waitForCompletion(COMMON_TIMEOUT);
ASSERT_EQ(status, std::future_status::ready) << "File sync timed out";
ASSERT_EQ(errCode, API_OK) << "File sync failed";
}
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode) << "Backup node not found";
std::unique_ptr<MegaNode> cloudNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), "test_file_cxf.dat", FILENODE));
ASSERT_TRUE(cloudNode) << "Cloud node not found after re-sync";
auto cloudNodeMtime = cloudNode->getModificationTime();
if (direction == CxfMtimeDirection::Increase)
{
ASSERT_EQ(cloudNodeMtime, newMtime)
<< "Cloud node mtime should match the updated local mtime after CXF sync";
}
else
{
auto [getMtimeLocalOk, localMtime] =
mFsAccess->getmtimelocal(LocalPath::fromAbsolutePath(path_u8string(testFilePath)));
ASSERT_TRUE(getMtimeLocalOk) << "Failed to get local mtime after CXF resync";
ASSERT_GT(cloudNodeMtime, newMtime) << "Cloud node mtime should be newer than the last "
"local changed mtime after CXF sync";
ASSERT_EQ(localMtime, cloudNodeMtime)
<< "Local file mtime should match cloud after CXF resync when cloud is newer";
}
LOG_debug << logPre << "Test completed successfully";
}
void runAsyncMacNonBlockingScenario(const std::string& logPre,
const std::string& namePrefix,
const bool startFromCxf)
{
// File sizes shared by CSF and CXF variants.
static constexpr size_t SMALL_FILE_SIZE = (5 * 1024 * 1024) + (126 * 1024) + 17; // ~5MB
static constexpr size_t LARGE_FILE_SIZE = (100 * 1024 * 1024) + (212 * 1024) + 2; // ~100MB
auto absPath = [&](const std::string& filename)
{
return fs::absolute(getLocalTmpDir() / filename);
};
const fs::path smallFile1Path = absPath(namePrefix + "small_file1.dat");
const fs::path smallFile2Path = absPath(namePrefix + "small_file2.dat");
const fs::path largeFilePath = absPath(namePrefix + "large_file.dat");
LOG_debug << logPre << "1. Creating test files (2 small, 1 large)";
auto createFileAndSync =
[&](const fs::path& path, const size_t size, const std::string& label)
{
auto st = mSyncListenerTrackers.add(path.string());
ASSERT_TRUE(st) << logPre << "Cannot add SyncListenerTracker for " << label;
auto localFile = std::make_shared<sdk_test::LocalTempFile>(path, size);
mLocalFiles.emplace_back(localFile);
auto [status, errCode] = st->waitForCompletion(COMMON_TIMEOUT);
ASSERT_EQ(status, std::future_status::ready) << logPre << label << " sync timed out";
ASSERT_EQ(errCode, API_OK) << logPre << label << " sync failed";
};
ASSERT_NO_FATAL_FAILURE(createFileAndSync(smallFile1Path, SMALL_FILE_SIZE, "Small file 1"));
ASSERT_NO_FATAL_FAILURE(createFileAndSync(smallFile2Path, SMALL_FILE_SIZE, "Small file 2"));
ASSERT_NO_FATAL_FAILURE(createFileAndSync(largeFilePath, LARGE_FILE_SIZE, "Large file"));
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
LOG_debug << logPre << "2. All files synced, now updating mtimes";
const m_time_t newMtime = m_time(nullptr) + MIN_ALLOW_MTIME_DIFFERENCE;
std::shared_ptr<SyncUploadOperationsTracker> stSmall1;
std::shared_ptr<SyncUploadOperationsTracker> stSmall2;
std::shared_ptr<SyncUploadOperationsTracker> stLarge;
auto addSyncListeners = [&]()
{
stSmall1 = mSyncListenerTrackers.add(smallFile1Path.string());
stSmall2 = mSyncListenerTrackers.add(smallFile2Path.string());
stLarge = mSyncListenerTrackers.add(largeFilePath.string());
ASSERT_TRUE(stSmall1 && stSmall2 && stLarge);
};
if (!startFromCxf)
{
ASSERT_NO_FATAL_FAILURE(addSyncListeners());
}
else
{
LOG_debug << logPre << "2b. Removing sync (CXF) before updating mtimes";
removeTestSync();
}
LOG_debug << logPre << "3. Updating mtimes for all 3 files";
ASSERT_TRUE(
mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(smallFile1Path)),
newMtime));
ASSERT_TRUE(
mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(smallFile2Path)),
newMtime));
ASSERT_TRUE(
mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(largeFilePath)),
newMtime));
if (startFromCxf)
{
LOG_debug << logPre << "3b. Re-adding sync (CXF path upon sync re-addition)";
ASSERT_NO_FATAL_FAILURE(
initiateSync(getLocalTmpDirU8string(), SYNC_REMOTE_PATH, mBackupId));
ASSERT_NO_FATAL_FAILURE(addSyncListeners());
}
LOG_debug << logPre << "4. Waiting for all files to sync and tracking completion order";
using clock = std::chrono::steady_clock;
std::atomic<clock::time_point> small1CompleteTime{clock::time_point::max()};
std::atomic<clock::time_point> small2CompleteTime{clock::time_point::max()};
std::atomic<clock::time_point> largeCompleteTime{clock::time_point::max()};
std::atomic<bool> small1Done{false};
std::atomic<bool> small2Done{false};
std::atomic<bool> largeDone{false};
auto waitAndStamp = [&](std::shared_ptr<SyncUploadOperationsTracker> st,
std::atomic<clock::time_point>& ts,
std::atomic<bool>& done,
std::chrono::seconds timeout)
{
return std::thread(
[&, st = std::move(st), timeout]()
{
auto [status, err] = st->waitForCompletion(timeout);
if (status == std::future_status::ready && err == API_OK)
{
ts = clock::now();
done = true;
}
});
};
auto waitLarge =
waitAndStamp(stLarge, largeCompleteTime, largeDone, std::chrono::seconds(180));
auto waitSmall1 =
waitAndStamp(stSmall1, small1CompleteTime, small1Done, std::chrono::seconds(60));
auto waitSmall2 =
waitAndStamp(stSmall2, small2CompleteTime, small2Done, std::chrono::seconds(60));
waitSmall1.join();
waitSmall2.join();
// By the time small files are done, the large one should not have finished yet in the
// non-blocking scenario. If it has, we'll catch it in the ordering assertions below.
EXPECT_FALSE(largeDone.load()) << logPre << "Large file completed before small files";
waitLarge.join();
ASSERT_TRUE(small1Done) << logPre << "Small file 1 mtime sync failed or timed out";
ASSERT_TRUE(small2Done) << logPre << "Small file 2 mtime sync failed or timed out";
ASSERT_TRUE(largeDone) << logPre << "Large file mtime sync failed or timed out";
LOG_debug << logPre << "6. Verifying completion order";
auto small1Time = small1CompleteTime.load();
auto small2Time = small2CompleteTime.load();
auto largeTime = largeCompleteTime.load();
EXPECT_LT(small1Time, largeTime)
<< logPre << "Small file 1 should complete before large file";
EXPECT_LT(small2Time, largeTime)
<< logPre << "Small file 2 should complete before large file";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
LOG_debug << logPre << "Test completed successfully";
}
public:
void SetUp() override
{
SdkTestNodesSetUp::SetUp();
mFsAccess = std::make_unique<FSACCESS_CLASS>();
if (createSyncOnSetup())
{
ASSERT_NO_FATAL_FAILURE(
initiateSync(getLocalTmpDirU8string(), SYNC_REMOTE_PATH, mBackupId));
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
}
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());
mMsl.reset(new NiceMock<MockSyncListener>(megaApi[0].get()));
EXPECT_CALL(*mMsl.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(mMsl.get());
}
void TearDown() override
{
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(!mMsl) << getLogPrefix()
<< "(TearDown). Sync listener has not been unregistered yet";
SdkTestSyncNodesOperations::TearDown();
}
/**
* @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 removeTestSync()
{
if (mBackupId != UNDEF)
{
auto succeeded = removeSync(megaApi[0].get(), mBackupId);
if (succeeded)
{
mBackupId = UNDEF;
}
ASSERT_TRUE(succeeded);
}
}
void cleanDefaultListeners()
{
removeTestSync();
if (mMtl)
{
megaApi[0]->removeListener(mMtl.get());
mMtl.reset();
}
if (mMsl)
{
megaApi[0]->removeListener(mMsl.get());
mMsl.reset();
}
}
/**
* @brief Build a file tree with two empty sync folders
*/
const std::vector<NodeInfo>& getElements() const override
{
static const std::vector<NodeInfo> ELEMENTS{DirNodeInfo(SYNC_REMOTE_PATH)
.addChild(DirNodeInfo("dir1"))
.addChild(DirNodeInfo("dir2"))};
return ELEMENTS;
}
/**
* @brief Updates local node mtime. See MIN_ALLOW_MTIME_DIFFERENCE
*/
void updateLocalNodeMtime(MegaHandle nodeHandle,
const LocalPath& path,
int64_t oldMtime,
int64_t newMtime,
const string& msg)
{
LOG_debug << "#### updateNodeMtime (" << msg << ")####";
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
}
/**
* @brief Creates a new local file for test. See createTestFileInternal
*/
void createTestFile(const string folderName,
const std::string commonFileName,
const std::string content,
std::chrono::time_point<fs::file_time_type::clock> customMtime,
const std::string& msg,
bool isFullUploadExpected)
{
LOG_debug << "#### createTestFile ( " << msg << ") `" << commonFileName << "` into `"
<< folderName << "` with content(" << content
<< ") and customMtime (full upload expected) ####";
ASSERT_NO_FATAL_FAILURE(
createTestFileInternal(fs::absolute(getLocalTmpDir() / folderName / commonFileName),
content,
customMtime,
isFullUploadExpected));
};
/**
* @brief Search nodes by fingerprint and validates the result.
*
* @param n The source node whose fingerprint will be used for the search. Must be a
* valid node with a non-null fingerprint.
* @param excludeMtime If true, uses getNodesByFingerprintIgnoringMtime() which compares
* only CRC + size + isValid, ignoring the modification time. If false,
* uses getNodesByFingerprint() which compares the entire fingerprint
* including modification time
*
* @param expNodeCount The expected number of nodes that should be found with the given
* fingerprint.
* @param msg A descriptive message used for logging purposes
* @see FS_MTIME_TOLERANCE_SECS tolerance
*/
void getNodesByFingerprint(MegaNode* n,
bool excludeMtime,
size_t expNodeCount,
const string& msg)
{
LOG_debug << "#### getNodesByFingerprint (" << msg << ") ####";
ASSERT_TRUE(n) << "getNodesByFingerprint: Invalid node";
ASSERT_TRUE(n->getFingerprint())
<< "Invalid fingerprint for node(" << toNodeHandle(n->getHandle()) << ")";
auto auxfp = n->getFingerprint();
std::unique_ptr<MegaNodeList> nl;
if (excludeMtime)
nl.reset(megaApi[0]->getNodesByFingerprintIgnoringMtime(auxfp));
else
nl.reset(megaApi[0]->getNodesByFingerprint(auxfp));
ASSERT_EQ(nl->size(), expNodeCount)
<< "getNodesByFingerprint. " << msg << " Unexpected node count";
}
/**
* @brief Returns the backup MegaNode
*/
std::shared_ptr<MegaNode> getBackupNode()
{
std::unique_ptr<MegaSync> backupSync(megaApi[0]->getSyncByBackupId(getBackupId()));
if (!backupSync)
{
LOG_err << "Cannot get backup sync";
return nullptr;
}
std::unique_ptr<MegaNode> backupNode(
megaApi[0]->getNodeByHandle(backupSync->getMegaHandle()));
if (!backupNode)
{
LOG_err << "Cannot get backup sync node";
return nullptr;
}
return backupNode;
}
/**
* @brief Retrieves test folder nodes and their first-level child file nodes.
*
* This function locates a set of folders under a given backup node (`backupNode`),
* as specified by the list of folder names in `folderNames`. For each folder found,
* it retrieves a file node with the common name `commonFileName` contained directly
* within that folder. The resulting folder and file nodes are stored in the
* `folderNodes` and `fileNodes` vectors, respectively.
*
* This function assumes a single-level hierarchy: folders exist directly under
* the backup node, and files exist directly inside those folders.
*
* @param backupNode The backup node under which the folders are located.
* @param folderNames A list of the folder names.
* @param folderNodes Vector to store the retrieved folder nodes.
* @param fileNodes Vector to store the retrieved file nodes.
* @param commonFileName The name of the common file expected inside each folder.
* @param msg A descriptive message used for logging purposes.
*/
void
getTestFolderNodesAndFirstLevelChildren(std::shared_ptr<MegaNode> backupNode,
const std::vector<string>& folderNames,
std::vector<std::unique_ptr<MegaNode>>& folderNodes,
std::vector<std::unique_ptr<MegaNode>>& fileNodes,
const std::string& commonFileName,
const std::string& msg)
{
LOG_debug << "#### getTestFolderNodesAndFirstLevelChildren (" << msg << ") ####";
folderNodes.clear();
fileNodes.clear();
for (size_t i = 0; i < folderNames.size(); ++i)
{
std::unique_ptr<MegaNode> folderNode(
megaApi[0]->getChildNodeOfType(backupNode.get(),
folderNames.at(i).c_str(),
FOLDERNODE));
ASSERT_TRUE(folderNode) << msg << "Cannot get folderNode(" << folderNames.at(i) << ")";
std::unique_ptr<MegaNode> fileNode(
megaApi[0]->getChildNodeOfType(folderNode.get(), commonFileName.c_str(), FILENODE));
ASSERT_TRUE(fileNode) << msg << "Can not get fileNode(" << commonFileName
<< ") which is inside " << folderNames.at(i);
folderNodes.emplace_back(std::move(folderNode));
fileNodes.emplace_back(std::move(fileNode));
}
}
/**
* @brief Gets the absolute LocalPath of a localtest file.
*
* @param folderName The name of the parent folder of file.
* @param fileName The name of the file for which to construct the absolute path.
* @return A LocalPath object representing the absolute path to the specified test file.
*/
LocalPath getTestFileAbsolutePath(const std::string& folderName, const std::string& fileName)
{
return LocalPath::fromAbsolutePath(
path_u8string(fs::absolute(getLocalTmpDir() / folderName / fileName)));
}
};
/**
* @test
* SdkTestSyncUploadsOperations.CrcOnlyMismatchBugFixUpdatesRemoteFingerprintWithoutTransfer_CSF
*
* 1. Enable legacy (buggy) sparse CRC sampling via debug hook.
* 2. Create a couple of large random files outside the sync and move them in -> full upload
* expected.
* 3. Disable legacy hook, trigger a sync rescan to recompute fingerprint.
* 4. Verify cloud nodes get their fingerprint updated (CRC corrected) without transfers.
*/
TEST_F(SdkTestSyncUploadsOperations,
CrcOnlyMismatchBugFixUpdatesRemoteFingerprintWithoutTransfer_CSF)
{
#ifndef MEGASDK_DEBUG_TEST_HOOKS_ENABLED
GTEST_SKIP() << "Requires MEGASDK_DEBUG_TEST_HOOKS_ENABLED";
#else
const auto cleanup = setCleanupFunction();
static const std::string logPre{
"CrcOnlyMismatchBugFixUpdatesRemoteFingerprintWithoutTransfer_CSF: "};
const auto threadSuffix = "_" + getThisThreadIdStr();
ScopedLegacyBuggySparseCrcHook legacyHook{true};
// Sizes chosen to exceed the historical 32-bit sparse CRC offset overflow threshold (~33MB),
// while keeping uploads fast enough for integration environments.
static constexpr size_t FILE1_SIZE = 40 * 1024 * 1024; // 40MB
static constexpr size_t FILE2_SIZE = 90 * 1024 * 1024; // 90MB
const fs::path file1Path =
fs::absolute(getLocalTmpDir() / ("crc_bug_csf_1" + threadSuffix + ".dat"));
const fs::path file2Path =
fs::absolute(getLocalTmpDir() / ("crc_bug_csf_2" + threadSuffix + ".dat"));
const fs::path outsideLocalDir =
getLocalTmpDir().parent_path() / ("crc_bug_csf_tmp_dir" + threadSuffix);
LocalTempDir outsideDirCleanup(outsideLocalDir);
const fs::path outsideFile1 = outsideLocalDir / file1Path.filename();
const fs::path outsideFile2 = outsideLocalDir / file2Path.filename();
LOG_debug << logPre << "1. Creating two large files outside sync (avoid partial-file uploads)";
auto localFile1 = std::make_shared<sdk_test::LocalTempFile>(outsideFile1, FILE1_SIZE);
auto localFile2 = std::make_shared<sdk_test::LocalTempFile>(outsideFile2, FILE2_SIZE);
mLocalFiles.emplace_back(localFile1);
mLocalFiles.emplace_back(localFile2);
auto moveFileIntoSyncWithLocalTempFileAndVerify =
[&](const fs::path& targetPathInSync, const std::shared_ptr<sdk_test::LocalTempFile>& f)
{
ASSERT_NO_FATAL_FAILURE(moveLocalTempFileIntoSyncAndVerify(f, targetPathInSync, true));
};
LOG_debug << logPre << "2. Moving files into sync with legacy buggy sparse CRC enabled";
ASSERT_NO_FATAL_FAILURE(moveFileIntoSyncWithLocalTempFileAndVerify(file1Path, localFile1));
ASSERT_NO_FATAL_FAILURE(moveFileIntoSyncWithLocalTempFileAndVerify(file2Path, localFile2));
LOG_debug << logPre << "3. Disabling legacy hook and rescanning sync to recompute fingerprints";
legacyHook.setEnabled(false);
megaApi[0]->rescanSync(getBackupId(), true);
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode) << logPre << "Cannot get backup node";
auto waitForRemoteFingerprint = [&](const fs::path& localPath,
const std::string& filename,
std::shared_ptr<SyncUploadOperationsTransferTracker> tt)
{
const std::unique_ptr<char[]> expectedFp{
megaApi[0]->getFingerprint(path_u8string(localPath).c_str())};
ASSERT_TRUE(expectedFp) << logPre << "Cannot compute local fingerprint for: " << localPath;
const auto deadline = std::chrono::steady_clock::now() + COMMON_TIMEOUT;
for (;;)
{
std::unique_ptr<MegaNode> cloudNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), filename.c_str(), FILENODE));
if (cloudNode && cloudNode->getFingerprint() &&
std::string_view{cloudNode->getFingerprint()} == expectedFp.get())
{
break;
}
if (std::chrono::steady_clock::now() >= deadline)
{
FAIL() << logPre
<< "Timed out waiting for remote fingerprint update for: " << localPath;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
ASSERT_EQ(tt->transferStartCount.load(), 0)
<< logPre << "Transfer must not start for: " << localPath;
};
auto tt1 = addTransferListenerTracker(file1Path.string());
auto tt2 = addTransferListenerTracker(file2Path.string());
ASSERT_TRUE(tt1 && tt2);
LOG_debug << logPre << "4. Verifying remote fingerprints updated without transfers";
ASSERT_NO_FATAL_FAILURE(
waitForRemoteFingerprint(file1Path, path_u8string(file1Path.filename()), tt1));
ASSERT_NO_FATAL_FAILURE(
waitForRemoteFingerprint(file2Path, path_u8string(file2Path.filename()), tt2));
#endif
}
/**
* @test
* SdkTestSyncUploadsOperations.CrcOnlyMismatchBugFixUpdatesRemoteFingerprintWithoutTransfer_CXF
*
* 1. Remove sync to start from CXF (no LocalNodes).
* 2. Manually upload a large file with legacy (buggy) sparse CRC enabled.
* 3. Disable legacy hook, place the same file into the sync local root, and re-add the sync.
* 4. Verify cloud node fingerprint gets corrected without transfers.
*/
TEST_F(SdkTestSyncUploadsOperations,
CrcOnlyMismatchBugFixUpdatesRemoteFingerprintWithoutTransfer_CXF)
{
#ifndef MEGASDK_DEBUG_TEST_HOOKS_ENABLED
GTEST_SKIP() << "Requires MEGASDK_DEBUG_TEST_HOOKS_ENABLED";
#else
const auto cleanup = setCleanupFunction();
static const std::string logPre{
"CrcOnlyMismatchBugFixUpdatesRemoteFingerprintWithoutTransfer_CXF: "};
const auto threadSuffix = "_" + getThisThreadIdStr();
static constexpr size_t FILE_SIZE = 40 * 1024 * 1024; // 40MB
const std::string fileName = "crc_bug_cxf" + threadSuffix + ".dat";
auto backupNodeBefore = getBackupNode();
ASSERT_TRUE(backupNodeBefore) << logPre << "Cannot get backup node";
const MegaHandle backupHandle = backupNodeBefore->getHandle();
LOG_debug << logPre << "1. Removing sync (CXF path upon re-addition)";
removeTestSync();
std::unique_ptr<MegaNode> backupNode(megaApi[0]->getNodeByHandle(backupHandle));
ASSERT_TRUE(backupNode) << logPre << "Cannot re-acquire backup node by handle";
const fs::path outsideLocalDir =
getLocalTmpDir().parent_path() / ("crc_bug_cxf_tmp_dir" + threadSuffix);
LocalTempDir outsideDirCleanup(outsideLocalDir);
const fs::path outsideLocalPath = outsideLocalDir / fileName;
LOG_debug << logPre << "2. Creating random file outside sync";
createRandomFile(outsideLocalPath, FILE_SIZE);
const m_time_t fixedMtime = m_time(nullptr) - 60;
ASSERT_TRUE(
mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(outsideLocalPath)),
fixedMtime))
<< logPre << "Failed to set mtime on temp file";
LOG_debug << logPre << "3. Manual upload with legacy buggy sparse CRC enabled";
ScopedLegacyBuggySparseCrcHook legacyHook{true};
auto uploadedNode = uploadFile(megaApi[0].get(), outsideLocalPath, backupNode.get());
ASSERT_TRUE(uploadedNode) << logPre << "Manual upload failed";
LOG_debug << logPre << "4. Disabling legacy hook and moving file into sync local root";
legacyHook.setEnabled(false);
const fs::path insideSyncPath = fs::absolute(getLocalTmpDir() / fileName);
std::error_code renameError;
fs::rename(outsideLocalPath, insideSyncPath, renameError);
ASSERT_FALSE(renameError) << logPre
<< "Failed to move file into sync: " << renameError.message();
auto [getMtimeOk, movedMtime] =
mFsAccess->getmtimelocal(LocalPath::fromAbsolutePath(path_u8string(insideSyncPath)));
ASSERT_TRUE(getMtimeOk) << logPre << "Failed to get mtime of moved file";
ASSERT_EQ(movedMtime, fixedMtime) << logPre << "fs::rename should preserve mtime";
auto tt = addTransferListenerTracker(insideSyncPath.string());
ASSERT_TRUE(tt);
LOG_debug << logPre << "5. Re-adding sync and waiting for remote fingerprint correction";
ASSERT_NO_FATAL_FAILURE(initiateSync(getLocalTmpDirU8string(), SYNC_REMOTE_PATH, mBackupId));
const std::unique_ptr<char[]> expectedFp{
megaApi[0]->getFingerprint(path_u8string(insideSyncPath).c_str())};
ASSERT_TRUE(expectedFp) << logPre << "Cannot compute local fingerprint";
const auto deadline = std::chrono::steady_clock::now() + COMMON_TIMEOUT;
for (;;)
{
std::unique_ptr<MegaNode> cloudNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), fileName.c_str(), FILENODE));
if (cloudNode && cloudNode->getFingerprint() &&
std::string_view{cloudNode->getFingerprint()} == expectedFp.get())
{
break;
}
if (std::chrono::steady_clock::now() >= deadline)
{
FAIL() << logPre << "Timed out waiting for remote fingerprint update";
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
ASSERT_EQ(tt->transferStartCount.load(), 0) << logPre << "Transfer must not start";
#endif
}
/**
* @test SdkTestSyncUploadsOperations.BasicFileUpload
*
* 1. Create a new local file inside sync directory `dir2`.
* 2. Wait for sync (sync engine must upload file to the cloud).
* 3. Verify that local and remote models match
*/
TEST_F(SdkTestSyncUploadsOperations, BasicFileUpload)
{
const auto cleanup = setCleanupFunction();
auto mtime = fs::file_time_type::clock::now();
LOG_err << "BasicFileUpload (TC1) create `file1`";
ASSERT_NO_FATAL_FAILURE(createTestFile("dir1", "file1", "abcde", mtime, "CF1", true));
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
}
/**
* @test SdkTestSyncUploadsOperations.DuplicatedFilesUpload
*
* 1. Create a new local file `file1` in `dir1` with given content and mtime.
* - Expect a full upload (transfer started and finished).
* 2. Create a new local file `file1` in `dir2` with the same content and mtime.
* - Expect a remote copy (no transfer started).
* 3. Verify that local and remote models match
*/
TEST_F(SdkTestSyncUploadsOperations, DuplicatedFilesUpload)
{
const auto cleanup = setCleanupFunction();
auto mtime = fs::file_time_type::clock::now();
ASSERT_NO_FATAL_FAILURE(createTestFile("dir1", "file1", "abcde", mtime, "CF1", true));
ASSERT_NO_FATAL_FAILURE(createTestFile("dir2", "file1", "abcde", mtime, "CF2", false));
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
}
/**
* @test SdkTestSyncUploadsOperations.DuplicatedFilesUploadDifferentMtime
*
* 1. Create a new local file `file1` in `dir1` with given content and mtime `mt1`.
* - Expect a full upload.
* 2. Create the same file `file1` in `dir2` with same content but different mtime `mt2`.
* - Expect a remote copy since fingerprints differs in mtime only, and MAC matches.
* 3. Verify that local and remote models match
*/
TEST_F(SdkTestSyncUploadsOperations, DuplicatedFilesUploadDifferentMtime)
{
const auto cleanup = setCleanupFunction();
auto mtime1 = fs::file_time_type::clock::now();
auto mtime2 =
mtime1 + std::chrono::seconds(
MIN_ALLOW_MTIME_DIFFERENCE); // See MIN_ALLOW_MTIME_DIFFERENCE definition
ASSERT_NO_FATAL_FAILURE(createTestFile("dir1", "file1", "abcde", mtime1, "CF1", true));
ASSERT_NO_FATAL_FAILURE(createTestFile("dir2", "file1", "abcde", mtime2, "CF2", false));
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
}
/**
* @brief SdkTestSyncUploadsOperations.MultimediaFileUpload
*
* Test the metadata and thumbnails from a synced video.
*
*/
#if !defined(USE_FREEIMAGE) && !defined(USE_MEDIAINFO)
TEST_F(SdkTestSyncUploadsOperations, DISABLED_MultimediaFileUpload)
#else
TEST_F(SdkTestSyncUploadsOperations, MultimediaFileUpload)
#endif
{
const auto cleanup = setCleanupFunction();
static const string VIDEO_FILE = "sample_video.mp4";
static const std::string logPre = getLogPrefix();
LOG_verbose << logPre << "Upload a multimedia file in a sync";
// Get the file first and move it later to ensure that it is fully uploaded at once.
ASSERT_TRUE(getFileFromArtifactory("test-data/" + VIDEO_FILE, VIDEO_FILE));
fs::rename(VIDEO_FILE, getLocalTmpDir() / VIDEO_FILE);
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
auto uploadedNode = getNodeByPath(SYNC_REMOTE_PATH + "/" + VIDEO_FILE);
ASSERT_TRUE(uploadedNode);
#ifdef USE_MEDIAINFO
static constexpr int VIDEO_FILE_DURATION_SECS{5};
static constexpr int VIDEO_FILE_HEIGHT_PX{360};
static constexpr int VIDEO_FILE_WIDTH_PX{640};
static constexpr int AVC1_FORMAT{887}; // ID from MediaInfo
ASSERT_EQ(uploadedNode->getDuration(), VIDEO_FILE_DURATION_SECS)
<< "Duration is not correct or unavailable.";
ASSERT_EQ(uploadedNode->getHeight(), VIDEO_FILE_HEIGHT_PX)
<< "Height is not correct or unavailable.";
ASSERT_EQ(uploadedNode->getWidth(), VIDEO_FILE_WIDTH_PX)
<< "Width ID is not correct or unavailable.";
ASSERT_EQ(uploadedNode->getVideocodecid(), AVC1_FORMAT)
<< "Codec ID is not correct or unavailable.";
#endif
#ifdef USE_FREEIMAGE
ASSERT_TRUE(uploadedNode->hasThumbnail())
<< "Thumbnail is not available for the uploaded node.";
#endif
}
/**
* @test SdkTestSyncUploadsOperations.getnodesByFingerprintNoMtime
*
* 1. Create two files with identical content but different mtimes in separate directories.
* - File `file1` in `dir1` with mtime `mt1`
* - File `file1` in `dir2` with mtime `mt2` (differs by MIN_ALLOW_MTIME_DIFFERENCE)
* 2. Get nodes by fingerprint with and without mtime
* 3. Update the mtime of `file1` to match mtime of `file2`.
* 4. Get nodes by fingerprint with and without mtime
* 5. Verify that local and remote models match
*/
TEST_F(SdkTestSyncUploadsOperations, getnodesByFingerprintNoMtime)
{
const auto cleanup = setCleanupFunction();
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode) << "Cannot get backup sync node";
const std::vector<string> folderNames{"dir1", "dir2"};
const std::string commonContent{"abcde"};
const std::string commonFileName{"file1"};
std::vector<std::unique_ptr<MegaNode>> folderNodes;
std::vector<std::unique_ptr<MegaNode>> fileNodes;
const auto mtime1 = fs::file_time_type::clock::now();
const auto mtime2 =
mtime1 + std::chrono::seconds(
MIN_ALLOW_MTIME_DIFFERENCE); // See MIN_ALLOW_MTIME_DIFFERENCE definition
const std::vector<std::chrono::time_point<fs::file_time_type::clock>> mtimes{mtime1, mtime2};
ASSERT_NO_FATAL_FAILURE(createTestFile(folderNames.at(0),
commonFileName,
commonContent,
mtimes.at(0),
"CF1",
true));
ASSERT_NO_FATAL_FAILURE(createTestFile(folderNames.at(1),
commonFileName,
commonContent,
mtimes.at(1),
"CF2",
false));
ASSERT_NO_FATAL_FAILURE(getTestFolderNodesAndFirstLevelChildren(backupNode,
folderNames,
folderNodes,
fileNodes,
commonFileName,
"(GN1)"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(0).get(),
false /*excludeMtime*/,
1 /*expNumNodes*/,
"FP1"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(0).get(),
true /*excludeMtime*/,
fileNodes.size() /*expNumNodes*/,
"FP2"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(1).get(),
false /*excludeMtime*/,
1 /*expNumNodes*/,
"FP3"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(1).get(),
true /*excludeMtime*/,
fileNodes.size() /*expNumNodes*/,
"FP4"));
updateLocalNodeMtime(fileNodes.at(0)->getHandle(),
getTestFileAbsolutePath(folderNames.at(0), commonFileName),
fileNodes.at(0)->getModificationTime(), /*oldMtime*/
fileNodes.at(1)->getModificationTime(), /*newMtime*/
"MT1");
ASSERT_NO_FATAL_FAILURE(getTestFolderNodesAndFirstLevelChildren(backupNode,
folderNames,
folderNodes,
fileNodes,
commonFileName,
"(GN2)"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(0).get(),
false /*excludeMtime*/,
fileNodes.size() /*expNumNodes*/,
"FP5"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(0).get(),
true /*excludeMtime*/,
fileNodes.size() /*expNumNodes*/,
"FP6"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(1).get(),
false /*excludeMtime*/,
fileNodes.size() /*expNumNodes*/,
"FP7"));
ASSERT_NO_FATAL_FAILURE(getNodesByFingerprint(fileNodes.at(1).get(),
true /*excludeMtime*/,
fileNodes.size() /*expNumNodes*/,
"FP8"));
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
}
/**
* @test SdkTestSyncUploadsOperations.updateLocalNodeMtime
*
* 1. Create a new local file `file1` in `dir1` with given content and mtime (now).
* - Expect a full upload.
* 2. Update the mtime of `file1`.
* 3. Verify that local and remote models match
*/
TEST_F(SdkTestSyncUploadsOperations, updateLocalNodeMtime)
{
const auto cleanup = setCleanupFunction();
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode) << "Cannot get backup sync node";
const std::vector<string> folderNames{"dir1"};
const std::string commonFileName{"file1"};
std::vector<std::unique_ptr<MegaNode>> folderNodes;
std::vector<std::unique_ptr<MegaNode>> fileNodes;
ASSERT_NO_FATAL_FAILURE(createTestFile(folderNames.at(0),
commonFileName,
"abcde",
fs::file_time_type::clock::now(),
"CF1",
true));
ASSERT_NO_FATAL_FAILURE(getTestFolderNodesAndFirstLevelChildren(backupNode,
folderNames,
folderNodes,
fileNodes,
commonFileName,
"(GN1)"));
updateLocalNodeMtime(fileNodes.at(0)->getHandle(),
getTestFileAbsolutePath(folderNames.at(0), commonFileName),
fileNodes.at(0)->getModificationTime(), /*oldMtime*/
fileNodes.at(0)->getModificationTime() + 100, /*newMtime*/
"MT1");
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
}
/**
* @test SdkTestSyncUploadsOperations.CloneNodeWithDifferentMtime
*
* This test validates the clone node mechanism when a file with different mtime
* is moved into the sync. The clone should be found via NODE_COMP_DIFFERS_MTIME.
*
* 1. Create a random file (50MB) outside the sync local root (thread-safe path).
* 2. Upload it manually (not through sync) to a unique remote folder outside sync.
* 3. Change the mtime of the local file.
* 4. Set up expectations that no full transfer will occur.
* 5. Move/rename the file into the sync local root with a different name (preserving mtime).
* 6. Wait for sync - a clone node operation should occur (NODE_COMP_DIFFERS_MTIME).
* 7. Verify both local and remote files have the same mtime (the updated one from step 3).
*/
TEST_F(SdkTestSyncUploadsOperations, CloneNodeWithDifferentMtime)
{
static const auto logPre = getLogPrefix();
const auto cleanup = setCleanupFunction();
const std::string originalFileName = "original_file_outside_sync.dat";
const std::string clonedFileName = "cloned_file_inside_sync.dat";
const size_t fileSize = 50 * 1024 * 1024; // 50MB
LOG_debug << logPre << "1. Prepare unique remote folder outside sync";
const std::string threadSuffix = "_" + getThisThreadIdStr();
const std::string uniqueRemoteFolderName = "clone_mtime_test_folder" + threadSuffix;
const fs::path outsideLocalDir =
getLocalTmpDir().parent_path() / ("clone_mtime_test_dir" + threadSuffix);
LocalTempDir outsideDirCleanup(outsideLocalDir);
const fs::path outsideLocalPath = outsideLocalDir / originalFileName;
LOG_debug << logPre
<< "1b. Creating random file outside sync at: " << path_u8string(outsideLocalPath);
createRandomFile(outsideLocalPath, fileSize);
LOG_debug << logPre << "2. Creating unique remote folder to manually upload the file: "
<< uniqueRemoteFolderName;
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode) << "Cannot get backup sync node";
std::unique_ptr<MegaNode> rootTestNode(
megaApi[0]->getNodeByHandle(backupNode->getParentHandle()));
ASSERT_TRUE(rootTestNode) << "Cannot get parent node of sync remote root";
auto uploadTargetHandle = createFolder(0, uniqueRemoteFolderName.c_str(), rootTestNode.get());
ASSERT_NE(uploadTargetHandle, UNDEF) << "Failed to create unique remote folder";
std::unique_ptr<MegaNode> uploadTargetNode(megaApi[0]->getNodeByHandle(uploadTargetHandle));
ASSERT_TRUE(uploadTargetNode) << "Cannot get created remote folder node";
LOG_debug << logPre << "2b. Uploading file manually to cloud at the unique remote folder: "
<< uniqueRemoteFolderName;
auto uploadedNode = uploadFile(megaApi[0].get(), outsideLocalPath, uploadTargetNode.get());
ASSERT_TRUE(uploadedNode) << "Manual upload failed";
const int64_t originalMtime = uploadedNode->getModificationTime();
LOG_debug << logPre << "2c. Original uploaded file mtime: " << originalMtime;
const m_time_t newMtimeTimeT = m_time(nullptr) + MIN_ALLOW_MTIME_DIFFERENCE;
LOG_debug << logPre << "3. Changing local file mtime to: " << newMtimeTimeT << " seconds";
ASSERT_TRUE(
mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(outsideLocalPath)),
newMtimeTimeT))
<< "Failed to set mtime on file outside sync";
const fs::path insideSyncPath = fs::absolute(getLocalTmpDir() / clonedFileName);
LOG_debug << logPre << "4. Moving file into sync and waiting for sync (no transfer expected): "
<< path_u8string(insideSyncPath);
ASSERT_NO_FATAL_FAILURE(moveFileIntoSyncAndVerify(outsideLocalPath,
insideSyncPath,
false /*isFullUploadExpected*/,
newMtimeTimeT /*expectedMtimeAfterMove*/));
LOG_debug << logPre << "5. Verifying mtime of cloned node";
std::unique_ptr<MegaNode> clonedNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), clonedFileName.c_str(), FILENODE));
ASSERT_TRUE(clonedNode) << "Cloned node not found in cloud";
ASSERT_EQ(clonedNode->getModificationTime(), newMtimeTimeT)
<< "Cloned remote node mtime should match the updated local mtime";
LOG_debug << logPre << "6. Verifying mtime of local file";
{
auto [getMtimeSucceeded, currentLocalMtimeTimeT] =
mFsAccess->getmtimelocal(LocalPath::fromAbsolutePath(path_u8string(insideSyncPath)));
ASSERT_TRUE(getMtimeSucceeded) << "Failed to get local file mtime";
ASSERT_EQ(currentLocalMtimeTimeT, newMtimeTimeT)
<< "Local file mtime should still be the updated value";
}
LOG_debug << logPre << "7. Verifying mtime of original uploaded node";
{
std::unique_ptr<MegaNode> refreshedUploadedNode(
megaApi[0]->getNodeByHandle(uploadedNode->getHandle()));
ASSERT_TRUE(refreshedUploadedNode) << "Cannot get refreshed original uploaded node";
ASSERT_EQ(refreshedUploadedNode->getModificationTime(), originalMtime)
<< "Original uploaded node mtime should remain unchanged";
}
LOG_debug << logPre << "8. Cleanup: removing remote test folder";
ASSERT_EQ(API_OK, doDeleteNode(0, uploadTargetNode.get()))
<< "Failed to cleanup remote test folder";
LOG_debug << logPre << "Test completed successfully";
}
/**
* @brief Tests that async MAC computation doesn't block small files.
*
* Creates 3 files (2 small, 1 large ~10MB), syncs them, then updates mtimes for all 3.
* Verifies that the small files complete their mtime-only sync quickly, even while
* the large file's MAC computation may be in progress.
*
* This validates the non-blocking MAC computation implementation for SRT_CSF cases.
*/
TEST_F(SdkTestSyncUploadsOperations, AsyncMacComputationDoesNotBlockSmallFiles)
{
static const std::string logPre{"AsyncMacComputationDoesNotBlockSmallFiles: "};
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
ASSERT_NO_FATAL_FAILURE(runAsyncMacNonBlockingScenario(logPre, "csf_", false));
}
/**
* @brief Tests async MAC computation does not block small files when resyncing from CXF state.
*
* Creates two small files and one large file, updates mtimes, removes and re-adds the sync (CXF),
* and verifies small files complete before the large file while using the shared async MAC path.
*/
TEST_F(SdkTestSyncUploadsOperations, AsyncMacComputationDoesNotBlockSmallFilesFromCxf)
{
static const std::string logPre{"AsyncMacComputationDoesNotBlockSmallFilesFromCxf: "};
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
ASSERT_NO_FATAL_FAILURE(runAsyncMacNonBlockingScenario(logPre, "cxf_", true));
}
/**
* @brief Tests async MAC computation for SRT_CXF case (local logout/relog).
*
* This exercises the scenario where LocalNodes are lost (logout/relog) but files
* remain in sync folder. Upon re-sync, files with mtime-only differences use
* async MAC computation as unsynced nodes are re-processed.
*
* The test verifies that the sync eventually completes correctly.
*/
TEST_F(SdkTestSyncUploadsOperations, AsyncMacComputationForCxfCase_LocalNewer)
{
ASSERT_NO_FATAL_FAILURE(
runAsyncMacComputationForCxfCase("AsyncMacComputationForCxfCase_LocalNewer: ",
CxfMtimeDirection::Increase));
}
TEST_F(SdkTestSyncUploadsOperations, AsyncMacComputationForCxfCase_CloudNewer)
{
ASSERT_NO_FATAL_FAILURE(
runAsyncMacComputationForCxfCase("AsyncMacComputationForCxfCase_CloudNewer: ",
CxfMtimeDirection::Decrease));
}
/**
* @brief Tests MAC computation obsolescence when file is deleted during computation.
*
* Creates a large file, triggers mtime update to start MAC computation, then
* deletes the file while computation may be pending. The sync should handle
* this gracefully without errors.
*/
TEST_F(SdkTestSyncUploadsOperations, MacComputationObsolescenceOnDelete)
{
static const std::string logPre{"MacComputationObsolescenceOnDelete: "};
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
// Use a large file to increase chance of pending MAC computation
static constexpr size_t LARGE_FILE_SIZE = 100 * 1024 * 1024; // 100MB
const fs::path largeFilePath = fs::absolute(getLocalTmpDir() / "large_file_delete_test.dat");
LOG_debug << logPre << "1. Creating large test file";
{
auto st = addSyncListenerTracker(largeFilePath.string());
ASSERT_TRUE(st);
auto localFile = std::make_shared<sdk_test::LocalTempFile>(largeFilePath, LARGE_FILE_SIZE);
mLocalFiles.emplace_back(localFile);
auto [status, errCode] = st->waitForCompletion(COMMON_TIMEOUT);
ASSERT_EQ(status, std::future_status::ready) << "Large file sync timed out";
ASSERT_EQ(errCode, API_OK) << "Large file sync failed";
}
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
LOG_debug << logPre << "2. Updating mtime to trigger MAC computation";
const m_time_t newMtime = m_time(nullptr) + MIN_ALLOW_MTIME_DIFFERENCE;
ASSERT_TRUE(mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(largeFilePath)),
newMtime));
// Brief delay to let sync start processing
std::this_thread::sleep_for(std::chrono::seconds(3));
LOG_debug << logPre << "3. Deleting the file while MAC computation may be pending";
mLocalFiles.clear();
LOG_debug << logPre << "4. Waiting for sync to stabilize (file should be removed from cloud)";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
// Verify the cloud node is gone
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode) << "Backup node not found";
std::unique_ptr<MegaNode> cloudNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), "large_file_delete_test.dat", FILENODE));
ASSERT_FALSE(cloudNode) << "Cloud node should have been deleted";
LOG_debug << logPre << "Test completed successfully";
}
/**
* @brief Tests MAC computation obsolescence when file is moved during computation.
*
* Creates a large file, triggers mtime update to start MAC computation, then
* moves the file to another directory while computation may be pending.
* The sync should handle this gracefully, completing the move correctly.
*/
TEST_F(SdkTestSyncUploadsOperations, MacComputationObsolescenceOnMove)
{
static const std::string logPre{"MacComputationObsolescenceOnMove: "};
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
// Sync is already created during SetUp()
// Use a large file to increase chance of pending MAC computation
static constexpr size_t LARGE_FILE_SIZE = 100 * 1024 * 1024; // 100MB
const fs::path largeFilePath = fs::absolute(getLocalTmpDir() / "large_file_move_test.dat");
const fs::path destDir = fs::absolute(getLocalTmpDir() / "dir1");
const fs::path destFilePath = destDir / "large_file_move_test.dat";
LOG_debug << logPre << "1. Creating large test file";
{
auto st = addSyncListenerTracker(largeFilePath.string());
ASSERT_TRUE(st);
auto localFile = std::make_shared<sdk_test::LocalTempFile>(largeFilePath, LARGE_FILE_SIZE);
mLocalFiles.emplace_back(localFile);
auto [status, errCode] = st->waitForCompletion(COMMON_TIMEOUT);
ASSERT_EQ(status, std::future_status::ready) << "Large file sync timed out";
ASSERT_EQ(errCode, API_OK) << "Large file sync failed";
}
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
LOG_debug << logPre << "2. Updating mtime to trigger MAC computation";
const m_time_t newMtime = m_time(nullptr) + MIN_ALLOW_MTIME_DIFFERENCE;
ASSERT_TRUE(mFsAccess->setmtimelocal(LocalPath::fromAbsolutePath(path_u8string(largeFilePath)),
newMtime));
// Brief delay to let sync start processing
std::this_thread::sleep_for(std::chrono::seconds(3));
LOG_debug << logPre << "3. Moving file to dir1 while MAC computation may be pending";
{
auto st = addSyncListenerTracker(destFilePath.string());
ASSERT_TRUE(st);
// On Windows, if MAC computation has the file open, the move will fail.
// Retry a few times with delays to handle this platform limitation.
std::error_code ec;
static constexpr int MAX_MOVE_RETRIES = 10;
static constexpr auto RETRY_DELAY = std::chrono::seconds(3);
for (int attempt = 0; attempt < MAX_MOVE_RETRIES; ++attempt)
{
ec = mLocalFiles.back()->move(destFilePath);
if (!ec)
{
LOG_debug << logPre << "File moved successfully on attempt " << (attempt + 1);
break;
}
LOG_debug << logPre << "Move attempt " << (attempt + 1) << " failed: " << ec.message()
<< ". Retrying...";
std::this_thread::sleep_for(RETRY_DELAY);
}
ASSERT_FALSE(ec) << "Failed to move large file after " << MAX_MOVE_RETRIES
<< " attempts: " << ec.message();
auto [status, errCode] = st->waitForCompletion(COMMON_TIMEOUT);
ASSERT_EQ(status, std::future_status::ready) << "Large file sync timed out";
ASSERT_EQ(errCode, API_OK) << "Large file sync failed";
}
LOG_debug << logPre << "4. Waiting for sync to complete the move";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
// Verify the cloud node is in the new location
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode) << "Backup node not found";
std::unique_ptr<MegaNode> dir1Node(
megaApi[0]->getChildNodeOfType(backupNode.get(), "dir1", FOLDERNODE));
ASSERT_TRUE(dir1Node) << "dir1 not found in cloud";
std::unique_ptr<MegaNode> movedNode(
megaApi[0]->getChildNodeOfType(dir1Node.get(), "large_file_move_test.dat", FILENODE));
ASSERT_TRUE(movedNode) << "Moved file not found in dir1";
// Original location should be empty
std::unique_ptr<MegaNode> oldLocationNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), "large_file_move_test.dat", FILENODE));
ASSERT_FALSE(oldLocationNode) << "File should not exist at original location";
// Verify the mtime of the moved file
ASSERT_EQ(movedNode->getModificationTime(), newMtime)
<< "Moved file should have the updated mtime";
LOG_debug << logPre << "Test completed successfully";
}
/**
* @brief Tests pre-computed MAC for clone candidates (SRT_XSF case).
*
* This test validates the non-blocking MAC pre-computation for files that
* have potential clone candidates in the cloud (outside the sync root).
*
* Scenario:
* 1. Upload several files to cloud (outside sync root) - various sizes
* 2. Create sync with local files having same content but different mtime
* 3. Verify that small files sync quickly (cloned) while large file MAC computes
*/
TEST_F(SdkTestSyncUploadsOperations, PreComputedMacForCloneCandidatesNonBlocking)
{
static const std::string logPre{"PreComputedMacForCloneCandidatesNonBlocking: "};
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
// File sizes - names chosen so large file is processed first alphabetically
// ("a_large" < "b_small1" < "c_small2")
static constexpr size_t SMALL_FILE_SIZE = (5 * 1024 * 1024) + (100 * 1024) + 7; // ~5MB
static constexpr size_t LARGE_FILE_SIZE = (400 * 1024 * 1024) + (500 * 1024) + 3; // ~400MB
LOG_debug << logPre << "1. Creating cloud folder for clone candidates (outside sync root)";
std::unique_ptr<MegaNode> rootNode(megaApi[0]->getRootNode());
ASSERT_TRUE(rootNode);
const std::string candidatesFolderName = "clone_candidates_" + std::to_string(m_time(nullptr));
{
RequestTracker createFolderTracker(megaApi[0].get());
megaApi[0]->createFolder(candidatesFolderName.c_str(),
rootNode.get(),
&createFolderTracker);
ASSERT_EQ(API_OK, createFolderTracker.waitForResult());
}
std::unique_ptr<MegaNode> candidatesFolder(
megaApi[0]->getChildNode(rootNode.get(), candidatesFolderName.c_str()));
ASSERT_TRUE(candidatesFolder);
LOG_debug << logPre << "2. Creating temp files for upload to cloud";
// Create temp files outside sync folder using LocalTempFile
const fs::path tempDir = makeProcessTempDir("clone_test");
auto largeTempFile =
std::make_shared<sdk_test::LocalTempFile>(tempDir / "a_large_file.dat", LARGE_FILE_SIZE);
auto small1TempFile =
std::make_shared<sdk_test::LocalTempFile>(tempDir / "b_small_file1.dat", SMALL_FILE_SIZE);
auto small2TempFile =
std::make_shared<sdk_test::LocalTempFile>(tempDir / "c_small_file2.dat", SMALL_FILE_SIZE);
LOG_debug << logPre << "3. Uploading clone candidate files to cloud (outside sync)";
// Helper to upload a file to cloud with older mtime
auto uploadToCloud = [&](const fs::path& filePath, const int timeoutSeconds = 60)
{
TransferTracker uploadTracker(megaApi[0].get());
MegaUploadOptions uploadOptions;
uploadOptions.mtime = m_time(nullptr) - 86400;
const auto localPath = path_u8string(filePath);
megaApi[0]->startUpload(localPath,
candidatesFolder.get(),
nullptr,
&uploadOptions,
&uploadTracker);
return uploadTracker.waitForResult(timeoutSeconds) == API_OK;
};
ASSERT_TRUE(uploadToCloud(largeTempFile->getPath(), 600)) << "Failed to upload large file";
ASSERT_TRUE(uploadToCloud(small1TempFile->getPath())) << "Failed to upload small file 1";
ASSERT_TRUE(uploadToCloud(small2TempFile->getPath())) << "Failed to upload small file 2";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
LOG_debug << logPre << "4. Moving files into sync folder (triggers clone with MAC computation)";
const fs::path largeSyncPath = fs::absolute(getLocalTmpDir() / "a_large_file.dat");
const fs::path small1SyncPath = fs::absolute(getLocalTmpDir() / "b_small_file1.dat");
const fs::path small2SyncPath = fs::absolute(getLocalTmpDir() / "c_small_file2.dat");
// Track completion times
using clock = std::chrono::steady_clock;
std::atomic<clock::time_point> largeCompleteTime{clock::time_point::max()};
std::atomic<clock::time_point> small1CompleteTime{clock::time_point::max()};
std::atomic<clock::time_point> small2CompleteTime{clock::time_point::max()};
std::atomic<bool> largeDone{false}, small1Done{false}, small2Done{false};
auto stLarge = mSyncListenerTrackers.add(largeSyncPath.string());
auto stSmall1 = mSyncListenerTrackers.add(small1SyncPath.string());
auto stSmall2 = mSyncListenerTrackers.add(small2SyncPath.string());
ASSERT_TRUE(stLarge && stSmall1 && stSmall2);
// Move files into sync folder
ASSERT_FALSE(largeTempFile->move(largeSyncPath)) << "Failed to move large file";
ASSERT_FALSE(small1TempFile->move(small1SyncPath)) << "Failed to move small file 1";
ASSERT_FALSE(small2TempFile->move(small2SyncPath)) << "Failed to move small file 2";
// Track these for cleanup
mLocalFiles.emplace_back(largeTempFile);
mLocalFiles.emplace_back(small1TempFile);
mLocalFiles.emplace_back(small2TempFile);
LOG_debug << logPre << "5. Waiting for sync completions and tracking order";
// Wait in parallel threads to capture accurate timestamps
std::thread waitLarge(
[&]()
{
auto [status, err] = stLarge->waitForCompletion(std::chrono::seconds(600));
if (status == std::future_status::ready && err == API_OK)
{
largeCompleteTime = clock::now();
largeDone = true;
LOG_debug << logPre << "Large file sync completed";
}
});
std::thread waitSmall1(
[&]()
{
auto [status, err] = stSmall1->waitForCompletion(std::chrono::seconds(300));
if (status == std::future_status::ready && err == API_OK)
{
small1CompleteTime = clock::now();
small1Done = true;
LOG_debug << logPre << "Small file 1 sync completed";
}
});
std::thread waitSmall2(
[&]()
{
auto [status, err] = stSmall2->waitForCompletion(std::chrono::seconds(300));
if (status == std::future_status::ready && err == API_OK)
{
small2CompleteTime = clock::now();
small2Done = true;
LOG_debug << logPre << "Small file 2 sync completed";
}
});
waitLarge.join();
waitSmall1.join();
waitSmall2.join();
// Verify all completed
ASSERT_TRUE(largeDone) << "Large file sync failed or timed out";
ASSERT_TRUE(small1Done) << "Small file 1 sync failed or timed out";
ASSERT_TRUE(small2Done) << "Small file 2 sync failed or timed out";
LOG_debug << logPre
<< "6. Verifying completion order (small files should complete before large)";
auto largeTime = largeCompleteTime.load();
auto small1Time = small1CompleteTime.load();
auto small2Time = small2CompleteTime.load();
LOG_debug
<< logPre << "Completion times relative to large file:"
<< " small1="
<< std::chrono::duration_cast<std::chrono::milliseconds>(small1Time - largeTime).count()
<< "ms"
<< " small2="
<< std::chrono::duration_cast<std::chrono::milliseconds>(small2Time - largeTime).count()
<< "ms";
// The key assertion: small files should complete BEFORE large file
EXPECT_LT(small1Time, largeTime)
<< "Small file 1 should complete before large file - async MAC may be blocking";
EXPECT_LT(small2Time, largeTime)
<< "Small file 2 should complete before large file - async MAC may be blocking";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
// Verify files exist in sync
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode);
std::unique_ptr<MegaNode> largeNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), "a_large_file.dat", FILENODE));
std::unique_ptr<MegaNode> small1Node(
megaApi[0]->getChildNodeOfType(backupNode.get(), "b_small_file1.dat", FILENODE));
std::unique_ptr<MegaNode> small2Node(
megaApi[0]->getChildNodeOfType(backupNode.get(), "c_small_file2.dat", FILENODE));
ASSERT_TRUE(largeNode) << "Large file not found in sync";
ASSERT_TRUE(small1Node) << "Small file 1 not found in sync";
ASSERT_TRUE(small2Node) << "Small file 2 not found in sync";
// Cleanup
fs::remove_all(tempDir);
{
RequestTracker removeTracker(megaApi[0].get());
megaApi[0]->remove(candidatesFolder.get(), &removeTracker);
removeTracker.waitForResult();
}
LOG_debug << logPre << "Test completed successfully";
}
/**
* @brief Tests clone candidate MAC computation when local file is deleted mid-computation.
*/
TEST_F(SdkTestSyncUploadsOperations, CloneCandidateMacObsolescenceOnLocalDelete)
{
static const std::string logPre{"CloneCandidateMacObsolescenceOnLocalDelete: "};
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
static constexpr size_t LARGE_FILE_SIZE = 300 * 1024 * 1024; // 300MB for reliable MAC delay
LOG_debug << logPre << "1. Creating cloud candidate file (outside sync)";
std::unique_ptr<MegaNode> rootNode(megaApi[0]->getRootNode());
ASSERT_TRUE(rootNode);
const std::string candidatesFolderName = "clone_del_test_" + std::to_string(m_time(nullptr));
{
RequestTracker createFolderTracker(megaApi[0].get());
megaApi[0]->createFolder(candidatesFolderName.c_str(),
rootNode.get(),
&createFolderTracker);
ASSERT_EQ(API_OK, createFolderTracker.waitForResult());
}
std::unique_ptr<MegaNode> candidatesFolder(
megaApi[0]->getChildNode(rootNode.get(), candidatesFolderName.c_str()));
ASSERT_TRUE(candidatesFolder);
// Create temp file for upload using LocalTempFile
const fs::path tempDir = makeProcessTempDir("clone_del");
auto tempFile =
std::make_shared<sdk_test::LocalTempFile>(tempDir / "large_clone_del.dat", LARGE_FILE_SIZE);
// Upload to cloud with old mtime
{
TransferTracker uploadTracker(megaApi[0].get());
MegaUploadOptions uploadOptions;
uploadOptions.mtime = m_time(nullptr) - 86400;
const auto tempLocalPath = path_u8string(tempFile->getPath());
megaApi[0]->startUpload(tempLocalPath,
candidatesFolder.get(),
nullptr,
&uploadOptions,
&uploadTracker);
ASSERT_EQ(API_OK, uploadTracker.waitForResult(600));
}
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
LOG_debug << logPre << "2. Moving file into sync folder (triggers clone MAC computation)";
const fs::path localPath = fs::absolute(getLocalTmpDir() / "large_clone_del.dat");
ASSERT_FALSE(tempFile->move(localPath)) << "Failed to move file into sync";
mLocalFiles.emplace_back(std::move(tempFile));
// Brief delay to let MAC computation start
std::this_thread::sleep_for(std::chrono::seconds(10));
LOG_debug << logPre << "3. Deleting local file while MAC computation may be pending";
mLocalFiles.clear();
LOG_debug << logPre << "4. Waiting for sync to stabilize";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
// File should not exist in sync since we deleted it
LOG_debug << logPre << "5. Verifying file does not exist in sync";
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode);
std::unique_ptr<MegaNode> syncNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), "large_clone_del.dat", FILENODE));
ASSERT_FALSE(syncNode) << "File should not exist in sync after deletion";
// Cleanup
fs::remove_all(tempDir);
{
RequestTracker removeTracker(megaApi[0].get());
megaApi[0]->remove(candidatesFolder.get(), &removeTracker);
removeTracker.waitForResult();
}
LOG_debug << logPre << "Test completed successfully";
}
/**
* @brief Tests clone candidate MAC computation when cloud candidate is deleted mid-computation.
*/
TEST_F(SdkTestSyncUploadsOperations, CloneCandidateMacObsolescenceOnCloudDelete)
{
static const std::string logPre{"CloneCandidateMacObsolescenceOnCloudDelete: "};
auto cleanup = setCleanupFunction();
LOG_debug << logPre << "Test started";
static constexpr size_t LARGE_FILE_SIZE = 300 * 1024 * 1024; // 300MB for reliable MAC delay
LOG_debug << logPre << "1. Creating cloud candidate file (outside sync)";
std::unique_ptr<MegaNode> rootNode(megaApi[0]->getRootNode());
ASSERT_TRUE(rootNode);
const std::string candidatesFolderName = "clone_cloud_del_" + std::to_string(m_time(nullptr));
{
RequestTracker createFolderTracker(megaApi[0].get());
megaApi[0]->createFolder(candidatesFolderName.c_str(),
rootNode.get(),
&createFolderTracker);
ASSERT_EQ(API_OK, createFolderTracker.waitForResult());
}
std::unique_ptr<MegaNode> candidatesFolder(
megaApi[0]->getChildNode(rootNode.get(), candidatesFolderName.c_str()));
ASSERT_TRUE(candidatesFolder);
// Create temp file for upload using LocalTempFile
const fs::path tempDir = makeProcessTempDir("clone_cloud_del");
auto tempFile =
std::make_shared<sdk_test::LocalTempFile>(tempDir / "large_cloud_del.dat", LARGE_FILE_SIZE);
// Upload to cloud with old mtime
{
TransferTracker uploadTracker(megaApi[0].get());
MegaUploadOptions uploadOptions;
uploadOptions.mtime = m_time(nullptr) - 86400;
const auto tempLocalPath = path_u8string(tempFile->getPath());
megaApi[0]->startUpload(tempLocalPath,
candidatesFolder.get(),
nullptr,
&uploadOptions,
&uploadTracker);
ASSERT_EQ(API_OK, uploadTracker.waitForResult(600));
}
std::unique_ptr<MegaNode> candidateNode(
megaApi[0]->getChildNode(candidatesFolder.get(), "large_cloud_del.dat"));
ASSERT_TRUE(candidateNode);
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
LOG_debug << logPre << "2. Moving file into sync folder (triggers clone MAC computation)";
const fs::path localPath = fs::absolute(getLocalTmpDir() / "large_cloud_del.dat");
// Set up tracker BEFORE moving file
auto st = mSyncListenerTrackers.add(localPath.string());
ASSERT_TRUE(st);
ASSERT_FALSE(tempFile->move(localPath)) << "Failed to move file into sync";
mLocalFiles.emplace_back(std::move(tempFile));
// Brief delay to let MAC computation start
std::this_thread::sleep_for(std::chrono::seconds(10));
LOG_debug << logPre << "3. Deleting cloud candidate while MAC computation may be pending";
{
RequestTracker removeTracker(megaApi[0].get());
megaApi[0]->remove(candidateNode.get(), &removeTracker);
ASSERT_EQ(API_OK, removeTracker.waitForResult());
}
LOG_debug << logPre << "4. Waiting for sync to complete (should fall back to full upload)";
auto [status, err] = st->waitForCompletion(std::chrono::seconds(600));
ASSERT_EQ(status, std::future_status::ready) << "Sync timed out after cloud candidate deletion";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocalExhaustive());
// File should exist in sync (uploaded, not cloned since candidate was deleted)
LOG_debug << logPre << "5. Verifying file exists in sync";
auto backupNode = getBackupNode();
ASSERT_TRUE(backupNode);
std::unique_ptr<MegaNode> syncNode(
megaApi[0]->getChildNodeOfType(backupNode.get(), "large_cloud_del.dat", FILENODE));
ASSERT_TRUE(syncNode) << "File should exist in sync after full upload";
// Cleanup
fs::remove_all(tempDir);
{
RequestTracker removeTracker(megaApi[0].get());
megaApi[0]->remove(candidatesFolder.get(), &removeTracker);
removeTracker.waitForResult();
}
LOG_debug << logPre << "Test completed successfully";
}
#endif // ENABLE_SYNC
|